The principle and development experience of the POSTCSS framework in the Java class library

PostCSS is a framework used in the Java library, which allows developers to use syntax similar to CSS to write styles.Its principle is to parse the CSS style file into ABSTRACT SYNTAX Tree (AST), and then process and convert AST through the plug -in to eventually generate the target style file. The development experience of postcss is as follows: 1. Installation and configuration: First of all, you need to introduce the POSTCSS dependency library in the project, which can be managed by Maven or Gradle.Then configure the POSTCSS plug -in, you can choose different plug -in according to the requirements of the project to complete the corresponding style conversion operation. 2. Create plug -in: In order to meet specific style conversion requirements, developers can write custom PostCSS plug -in according to their needs.One plug -in usually contains three main parts: parsing, conversion, and generation.The parsing phase analyzes the CSS style file into AST, and the AST is processed and converted to AST during the conversion phase. The following is a simple sample plug -in, which is used to convert all style units into REM units: import org.postcss.*; import org.postcss.nodes.*; import org.postcss.parser.*; public class RemUnitPlugin implements Plugin { // Implement the PARSE method analysis AST @Override public RootNode parse(String css, ParserOptions options) { // Use PostCSS built -in CSS parser to resolve CSS style file Parser parser = new Parser(options); return parser.parse(css); } // Implement the transform method to convert AST @Override public RootNode transform(RootNode root) { // Traversing all the rules nodes of AST root.walkRules(rule -> { // All declaration nodes of the rules nodes rule.walkDecls(decl -> { // Convert pixel units (PX) to REM unit if (decl.value().endsWith("px")) { decl.setValue(decl.value().replace("px", "rem")); } }); }); return root; } // Implement the genetic method of the generate method @Override public String generate(RootNode root, GeneratorOptions options) { // Use postcss built -in CSS generator to generate target style file Generator generator = new Generator(options); return generator.generate(root); } } 3. Use plug -in: The useCSS plug -in in the project is very simple. You only need to create a postcss instance and use the plug -in to process the style file. The following is a simple example. Demonstrate how to use the above Remunitplugin plug -in: import org.postcss.*; import org.postcss.loader.*; import org.postcss.processor.*; public class PostCSSExample { public static void main(String[] args) { // Create postcss instance PostCSS postcss = new PostCSS(); try { // Load style files String css = new String(Files.readAllBytes(Paths.get("styles.css"))); // Use the plug -in processing style file ProcessorResult result = postcss.process(css, new RemUnitPlugin()); // The style file after the output processing System.out.println(result.getCss()); } catch (IOException e) { e.printStackTrace(); } } }