question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Webpack CSS extraction discussion

See original GitHub issue

I really like the idea of compiled as you move as much css processing to the build step and shrink the runtime code to almost 0.

Only by a coincidence I noticed that you are trying to build a webpack plugin and would love to jump in…

My knowledge of compiled is quite limited so maybe my solution could be over simplified and might not cover all your requirements.
But so far the first POC is working with the following features:

  • Entry Based Chunk Splitting (e.g. main.css and about-us.css)
  • Lazy Chunk Splitting (e.g. load styles for a modal only once the modal opens)
  • HMR

It works together with the following third party packages from the webpack ecosystem:

  • MiniCssExtractPlugin (optional)
  • HtmlWebpackPlugin (optional)
  • PostCssLoader (optional)
  • StyleLoader (optional)
  • CssLoader

For the atomic loader flow I stole an idea from Tobias Koppers (the author of webpack)…
In case this idea works out a lot of credits go to him 😃

<u>👉 Webpack Config Demos (click to expand)</u>

Webpack config (dev):

const HtmlWebpackPlugin = require("html-webpack-plugin");

module.exports = {
  devtool: false,
  mode: 'development',
  module: {
    rules: [
      
      {
        test: /\.js$/i,
        use: '@compiled/loader',
        exclude: /node_modules/
      },

      {
        test: /\.css$/i,
        use: ["style-loader", "css-loader"],
      },

    ],
  },
  plugins: [ new HtmlWebpackPlugin() ]
}

Webpack config (prod):

const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = {
  mode: 'production',
  module: {
    rules: [
      
      {
        test: /\.js$/i,
        use: '@compiled/loader',
        exclude: /node_modules/
      },

      {
        test: /\.css$/i,
        use: [MiniCssExtractPlugin.loader, 'css-loader'],
      },
    ],
  },
  plugins: [new MiniCssExtractPlugin(), new HtmlWebpackPlugin()],
};
<u>👉 the POC of @compiled/loader (click to expand)</u>

I created a very very naive and simplified loader (~80 LOC) which is only able to extract from a styled components:


export const Button = styled.button`
  border: none;
  border-radius: 3px;
  padding: 8px 10px;
  background-color: #6554c0;
  color: #fff;
  font-weight: 400;
  font-family: inherit;
  font-size: 14px;
`

The compiled result:

Preview of the Button

The demo is also lazy loading a second component:

// After 2s load the header
setTimeout(() => {
  import('./Header');
}, 2000);

The code for the header is also using the styled API:

export const Header = styled.header`
  border: none;
  background: purple;
  color: #fff;
`

As you can see Header shares 2 atomic styles with Button.
Webpack will notices that and will create a second CSS file which will contain only the purple background color.

<u>👉 run the POC locally (click to expand)</u>

download the demo (zip)

Run (dev server):

npm i
npm start

Build:

npm i
npm start

Although it is only a first draft I would love to know your feedback 😃

If you need any help to understand how exactly this draft works we could probably save some time with a screen sharing session - just ping me on twitter @jantimon - my DM is always open

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:8
  • Comments:21

github_iconTop GitHub Comments

1reaction
jantimoncommented, Feb 26, 2021

@TxHawks yes that’s true - probably the only good way to find out is to use compiled on some larger projects…

1reaction
TxHawkscommented, Feb 26, 2021

Fela is all runtime, so it doesn’t grow beyond exactly what’s on the page at a given moment and therefore probably not a good comparison for this aspect of Complied.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Handle CSS in webpack | Extract CSS - DEV Community ‍ ‍
We implement the extraction of CSS in a separate file that is executed at the same time as the rest. Flow of Thought....
Read more >
Separating CSS - SurviveJS
Webpack provides a means to generate a separate CSS bundles using mini-css-extract-plugin (MCEP). It can aggregate multiple CSS files into one.
Read more >
Webpack CSS Extraction
Extracting to a static style sheet can be enabled with some extra configuration. We recommend only extracting when building an application for production....
Read more >
Webpack: Part-3 - Suprabha Supi - Medium
Extract CSS & Minify HTML/CSS/JS. HTML Loader: For images, when we use same img file structure for dev and prod. We will get...
Read more >
Learn Webpack Pt. 10: Extract CSS & Minify HTML/CSS/JS
This 10-part course teaches Webpack from scratch. It begins with a simple question: "What is webpack ?" Quickly we move on to installing ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found