Giter Site home page Giter Site logo

css-split-example's Introduction

CSS Split Example Project

The goal of this repository is to demonstrate CSS split with Webpack 4, mini-css-extract-plugin and dynamic imports.

The idea is to discriminate the critical CSS by creating SASS files nameds *.critical.scss.

Critical CSS is typically used to support Server Side Rendering (SSR) version of the DOM generated by React.

Both critical and on-demand CSS are extracted using one instance of mini-css-extract-plugin

Getting Started

npm install
npm run build

Webpack Config

The modules are loaded like this (see webpack.config.babel.js);

{
  test: /\.s?css$/,
  use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader', 'sass-loader'],
},

Then the assets are generated by one instance of the plugins:

new MiniCssExtractPlugin({
  filename: '[name].css',
  chunkFilename: '[name].css',
}),

The distinction for critical and non-critical is done in splitChunks.

// Extract the critical CSS into a dedicated file
styles: {
  name: 'critical',
  test: /(\.critical)\.s?css$/,
  chunks: 'all',
  minChunks: 1,
  reuseExistingChunk: true,
  enforce: true,
},

This outputs the following assets:

          Asset       Size  Chunks             Chunk Names
   critical.css   68 bytes       0  [emitted]  critical
    critical.js  101 bytes       0  [emitted]  critical
MyComponent.css   50 bytes       1  [emitted]  MyComponent
 MyComponent.js  436 bytes       1  [emitted]  MyComponent
     vendors.js   96.8 KiB       2  [emitted]  vendors
       main.css   31 bytes       3  [emitted]  main
        main.js   3.74 KiB       3  [emitted]  main
     index.html  399 bytes          [emitted]  
Entrypoint main = vendors.js critical.css critical.js main.css main.js

Both main.css and critical.css should be loaded as link in the page. main.css could however be deferred, as illustrated below.

<link href="critical.css" rel="stylesheet">
<link
    rel="stylesheet"
    href="main.css"
    media="none"
    onload="if(media!='screen')media='screen'">
<noscript>
    <link rel="stylesheet" href="main.css">
</noscript>

css-split-example's People

Contributors

pierre-do avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.