Skip to content

Instantly share code, notes, and snippets.

@itproto
Created August 9, 2024 15:45
Show Gist options
  • Save itproto/b7cb1b19c69bf6c80525bdee35bc3442 to your computer and use it in GitHub Desktop.
Save itproto/b7cb1b19c69bf6c80525bdee35bc3442 to your computer and use it in GitHub Desktop.

Revisions

  1. itproto created this gist Aug 9, 2024.
    139 changes: 139 additions & 0 deletions LESS.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,139 @@
    To achieve the parallel processing of PostCSS files and load them as a separate CSS bundle in your Webpack setup with React, TypeScript, and CSS/LESS, you can follow these steps:

    ### 1. Install Necessary Dependencies
    First, make sure you have the necessary PostCSS and Webpack loaders installed:

    ```bash
    npm install postcss-loader postcss-preset-env css-loader style-loader mini-css-extract-plugin --save-dev
    ```

    ### 2. Configure PostCSS
    Create a `postcss.config.js` file in the root of your project to configure PostCSS with popular plugins. Here’s an example configuration:

    ```javascript
    module.exports = {
    plugins: [
    require('postcss-preset-env')({
    stage: 1,
    features: {
    'nesting-rules': true,
    },
    }),
    // Add more PostCSS plugins as needed
    ],
    };
    ```

    ### 3. Webpack Configuration
    Next, update your Webpack configuration to handle both your regular CSS/LESS files and the PostCSS files separately.

    Here’s an example `webpack.config.js`:

    ```javascript
    const path = require('path');
    const MiniCssExtractPlugin = require('mini-css-extract-plugin');

    module.exports = {
    entry: {
    main: './src/index.tsx',
    styles: './src/styles/main.postcss' // Entry point for PostCSS
    },
    output: {
    filename: '[name].js',
    path: path.resolve(__dirname, 'dist'),
    },
    module: {
    rules: [
    // Rule for regular CSS/LESS
    {
    test: /\.(css|less)$/,
    use: [
    MiniCssExtractPlugin.loader,
    'css-loader',
    'less-loader',
    ],
    },
    // Rule for PostCSS files
    {
    test: /\.postcss$/,
    use: [
    MiniCssExtractPlugin.loader,
    'css-loader',
    'postcss-loader',
    ],
    },
    // Rule for TypeScript
    {
    test: /\.tsx?$/,
    use: 'ts-loader',
    exclude: /node_modules/,
    },
    ],
    },
    resolve: {
    extensions: ['.tsx', '.ts', '.js'],
    },
    plugins: [
    new MiniCssExtractPlugin({
    filename: '[name].css', // This will generate separate CSS files for each entry point
    }),
    ],
    };
    ```

    ### 4. Create PostCSS Files
    Now, create a `.postcss` file in your source directory:

    ```css
    /* src/styles/main.postcss */
    :root {
    --main-color: #3498db;
    }

    body {
    font-family: 'Arial', sans-serif;
    color: var(--main-color);
    }

    /* More PostCSS code */
    ```

    ### 5. Import CSS/LESS and PostCSS in Your React Components
    In your React components, you can import the regular CSS/LESS files as usual. The PostCSS files will be bundled separately and loaded via the entry point you defined.

    Example:

    ```javascript
    import React from 'react';
    import './styles/app.less'; // Regular CSS/LESS

    const App = () => {
    return <div>Hello, World!</div>;
    };

    export default App;
    ```

    ### 6. Build Your Project
    Run your Webpack build to generate the separate CSS bundles:

    ```bash
    npm run build
    ```

    This setup will generate two CSS files in the `dist` directory:

    - `main.css` for your standard CSS/LESS styles.
    - `styles.css` for the PostCSS processed styles.

    These will be loaded separately when you include them in your HTML or within your React application.

    ### 7. Include the Bundles in HTML (if necessary)
    If you're manually including the CSS bundles in your HTML, it would look something like this:

    ```html
    <link rel="stylesheet" href="dist/main.css">
    <link rel="stylesheet" href="dist/styles.css">
    ```

    This configuration ensures that your PostCSS files are processed separately with the desired plugins and are output as a separate CSS bundle.