diff --git a/.babelrc b/.babelrc deleted file mode 100644 index 5f1933bf..00000000 --- a/.babelrc +++ /dev/null @@ -1,27 +0,0 @@ -{ - "presets": [ - [ - "@babel/preset-env", - { - "targets": { - "node": "6.9.0" - } - } - ] - ], - "overrides": [ - { - "test": "**/hmr/**/*.js", - "presets": [ - [ - "@babel/preset-env", - { - "targets": { - "node": "0.12" - } - } - ] - ] - } - ] -} diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 00000000..12eedf0c --- /dev/null +++ b/.prettierignore @@ -0,0 +1,8 @@ +/coverage +/dist +/node_modules +/test/fixtures +/test/cases/*/expected +/test/js +CHANGELOG.md + diff --git a/.travis.yml b/.travis.yml index 0fef0925..a2cd3090 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,6 +14,6 @@ notifications: webhooks: urls: - https://webhooks.gitter.im/e/5156be73e058008e1ed2 - on_success: always # options: [always|never|change] default: always - on_failure: always # options: [always|never|change] default: always - on_start: never # options: [always|never|change] default: always + on_success: always # options: [always|never|change] default: always + on_failure: always # options: [always|never|change] default: always + on_start: never # options: [always|never|change] default: always diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 00000000..7c815d85 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,138 @@ +# Changelog + +All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. + +### [0.9.0](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/compare/v0.8.2...v0.9.0) (2019-12-20) + + +### Features + +* new `esModule` option ([#475](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/475)) ([596e47a](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/596e47a8aead53f9cc0e2b1e09a2c20e455e45c1)) + +### [0.8.2](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/compare/v0.8.1...v0.8.2) (2019-12-17) + + +### Bug Fixes + +* context for dependencies ([#474](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/474)) ([0269860](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/0269860adb0eaad477901188eea66693fedf7769)) + +### [0.8.1](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/compare/v0.8.0...v0.8.1) (2019-12-17) + + +### Bug Fixes + +* use filename mutated after instantiation ([#430](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/430)) ([0bacfac](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/0bacfac7ef4a06b4810fbc140875f7a038caa5bc)) +* improve warning of conflict order ([#465](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/465)) ([357d073](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/357d073bf0259f2c44e613ad4dfcbcc8354e4be3)) +* support ES module syntax ([#472](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/472)) ([2f72e1a](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/2f72e1aa267de23f121441714e88406f579e77b2)) + +## [0.8.0](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/compare/v0.7.0...v0.8.0) (2019-07-16) + + +### Features + +* Add ignoreOrder option ([#422](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/422)) ([4ad3373](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/4ad3373)) + + + +## [0.7.0](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/compare/v0.6.0...v0.7.0) (2019-05-27) + + +### Bug Fixes + +* do not attempt to reload unrequestable urls ([#378](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/378)) ([44d00ea](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/44d00ea)) +* fix `publicPath` regression ([#384](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/384)) ([582ebfe](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/582ebfe)) +* enable using plugin without defining options ([#393](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/393)) ([a7dee8c](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/a7dee8c)) +* downgrading normalize-url ([#399](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/399)) ([0dafaf6](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/0dafaf6)) +* hmr do not crash on link without href ([#400](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/400)) ([aa9b541](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/aa9b541)) +* hmr reload with invalid link url ([#402](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/402)) ([30a19b0](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/30a19b0)) + + +### Features + +* add `moduleFilename` option ([#381](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/381)) ([13e9cbf](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/13e9cbf)) + + + + +# [0.6.0](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/compare/v0.5.0...v0.6.0) (2019-04-10) + + +### Features + +* added error code to chunk load Error ([#347](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/347)) ([b653641](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/b653641)) +* adding hot module reloading ([#334](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/334)) ([4ed9c5a](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/4ed9c5a)) +* publicPath can be a function ([#373](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/373)) ([7b1425a](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/7b1425a)) + + + + +# [0.5.0](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/compare/v0.4.5...v0.5.0) (2018-12-07) + + +### Features + +* add crossOriginLoading option support ([#313](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/313)) ([ffb0d87](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/ffb0d87)) + + + + +## [0.4.5](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/compare/v0.4.4...v0.4.5) (2018-11-21) + + +### Bug Fixes + +* **index:** allow requesting failed async css files ([#292](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/292)) ([2eb0af5](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/2eb0af5)) + + + + +## [0.4.4](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/compare/v0.4.3...v0.4.4) (2018-10-10) + + +### Bug Fixes + +* **index:** assign empty `module.id` to prevent `contenthash` from changing unnecessarily ([#284](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/284)) ([d7946d0](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/d7946d0)) + + + + +## [0.4.3](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/compare/v0.4.2...v0.4.3) (2018-09-18) + + +### Bug Fixes + +* **loader:** pass `emitFile` to the child compilation (`loaderContext.emitFile`) ([#177](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/177)) ([18c066e](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/18c066e)) + + + + +## [0.4.2](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/compare/v0.4.0...v0.4.2) (2018-08-21) + + +### Bug Fixes + +* use correct order when multiple chunk groups are merged ([#246](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/246)) ([c3b363d](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/c3b363d)) + + + + +## [0.4.1](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/compare/v0.4.0...v0.4.1) (2018-06-29) + + +### Bug Fixes + +* CSS ordering with multiple entry points ([#130](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/issues/130)) ([79373eb](https://github.com/webpack-contrib/extract-css-chunks-webpack-plugin/commit/79373eb)) + + + +# Change Log + +All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. + +x.x.x / -- +================== + + * Bug fix - + * Feature - + * Chore - + * Docs - diff --git a/LICENSE b/LICENSE index 8c11fc72..dfc572fb 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,5 @@ Copyright JS Foundation and other contributors +Copyright Zack Jackson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/README.md b/README.md index b3512e75..ca3a5517 100644 --- a/README.md +++ b/README.md @@ -1,76 +1,205 @@ -If you like our work, check out our Redux-based router redux-first-router or its sucessor which, Rudy +
+ + + + +

extract-css-chunks-webpack-plugin

+
+[![npm][npm]][npm-url] +[![node][node]][node-url] +[![deps][deps]][deps-url] +[![tests][tests]][tests-url] +[![coverage][cover]][cover-url] +[![chat][chat]][chat-url] +[![size][size]][size-url] # extract-css-chunks-webpack-plugin -

- - Version - +This plugin extracts CSS into separate files. It creates a CSS file per JS file which contains CSS. It supports On-Demand-Loading of CSS and SourceMaps. - - Build Status - +It builds on top of a new webpack v4 feature (module types) and requires webpack 4 to work. - - Downloads - +Compared to the extract-text-webpack-plugin: - - License - - - - License - -

+- Async loading +- No duplicate compilation (performance) +- Easier to use +- Specific to CSS + +## Getting Started + +To begin, you'll need to install `extract-css-chunks-webpack-plugin`: + +```bash +npm install --save-dev extract-css-chunks-webpack-plugin +``` + +It's recommended to combine `extract-css-chunks-webpack-plugin` with the [`css-loader`](https://github.com/webpack-contrib/css-loader) + +Then add the loader and the plugin to your `webpack` config. For example: + +**style.css** + +```css +body { + background: green; +} +``` + +**component.js** -

🍾🍾🍾It's our absolute pleasure to announce Webpack 4 Support πŸš€πŸš€πŸš€

+```js +import './style.css'; +``` -> **HEADLINES (May 2018): Now Independently supports Webpack 4:** -Yep that's right. The universal family is now fully Webpack 4. Thank you to all our users for your loyalty and patience! If you love Universal, then you are gonna fall head over heels when we bring out the main course! +**webpack.config.js** -So... why did we rebuild `extract-css-chunks`? What does it offer? +```js +const ExtractCssChunks = require('extract-css-chunks-webpack-plugin'); -It's got all the goodness of `mini-css-extract-plugin` but with 2 gleaming, sought after benefits. +module.exports = { + plugins: [new ExtractCssChunks()], + module: { + rules: [ + { + test: /\.css$/i, + use: [ExtractCssChunks.loader, 'css-loader'], + }, + ], + }, +}; +``` -Compared to the existing loaders, we are offering a single solution as opposed to needing to depend on multiple loaders to cater for different features: +## Options -## Perks -* **HMR:** It also has first-class support for **Hot Module Replacement** across ALL those css files/chunks!!! -* cacheable stylesheets -* smallest total bytes sent compared to "render-path" css-in-js solutions that include your CSS definitions in JS -* Faster than the V2! -* Async loading -* No duplicate compilation (performance) -* Easier to use -* Specific to CSS -* SSR Friendly development build, focused on frontend DX -* Works seamlessly with the Universal family -* Works fantastically as a standalone style loader (You can use it for any webpack project! with no extra dependencies!) +### `publicPath` -Additionally, if you are already a user of the universal family -- we will be waving goodbye to the mandatory ```window.__CSS_CHUNKS__```. +Type: `String|Function` +Default: the `publicPath` in `webpackOptions.output` + +Specifies a custom public path for the target file(s). + +#### `String` + +**webpack.config.js** -The functionality is still available to you via chunk flushing, and it can come in super handy when needing to easily resolve style assets as urls that might need to be passed to a third party. +```js +const ExtractCssChunks = require('extract-css-chunks-webpack-plugin'); -## Webpack 4 Standalone Installation: +module.exports = { + plugins: [ + new ExtractCssChunks({ + // Options similar to the same options in webpackOptions.output + // both options are optional + filename: '[name].css', + chunkFilename: '[id].css', + }), + ], + module: { + rules: [ + { + test: /\.css$/, + use: [ + { + loader: ExtractCssChunks.loader, + options: { + publicPath: '/public/path/to/', + }, + }, + 'css-loader', + ], + }, + ], + }, +}; +``` -If you are just looking for something that works like `mini-css-extract-plugin` but with HMR, then look no further. +#### `Function` -NOTE: We have aligned out loader implementation to be the same as `mini-css-extract-plugin` +**webpack.config.js** -**If you already use `mini-css-extract-plugin`, then you can just change the `require` statement - it's that easy** +```js +const ExtractCssChunks = require('extract-css-chunks-webpack-plugin'); +module.exports = { + plugins: [ + new ExtractCssChunks({ + // Options similar to the same options in webpackOptions.output + // both options are optional + filename: '[name].css', + chunkFilename: '[id].css', + }), + ], + module: { + rules: [ + { + test: /\.css$/, + use: [ + { + loader: ExtractCssChunks.loader, + options: { + publicPath: (resourcePath, context) => { + return path.relative(path.dirname(resourcePath), context) + '/'; + }, + }, + }, + 'css-loader', + ], + }, + ], + }, +}; ``` -yarn add --dev extract-css-chunks-webpack-plugin + +### `esModule` + +Type: `Boolean` +Default: `false` + +By default, `extract-css-chunks-webpack-plugin` generates JS modules that use the CommonJS modules syntax. +There are some cases in which using ES modules is beneficial, like in the case of [module concatenation](https://webpack.js.org/plugins/module-concatenation-plugin/) and [tree shaking](https://webpack.js.org/guides/tree-shaking/). + +You can enable a ES module syntax using: + +**webpack.config.js** + +```js +const ExtractCssChunks = require('extract-css-chunks-webpack-plugin'); + +module.exports = { + plugins: [new ExtractCssChunks()], + module: { + rules: [ + { + test: /\.css$/i, + use: [ + { + loader: ExtractCssChunks.loader, + options: { + esModule: true, + }, + }, + 'css-loader', + ], + }, + ], + }, +}; ``` -*webpack.config.js:* +## Examples + +### Minimal example + +**webpack.config.js** + ```js -const MiniCssExtractPlugin = require('mini-css-extract-plugin'); +const ExtractCssChunks = require('extract-css-chunks-webpack-plugin'); + module.exports = { plugins: [ - new MiniCssExtractPlugin({ + new ExtractCssChunks({ // Options similar to the same options in webpackOptions.output // all options are optional filename: '[name].css', @@ -84,12 +213,12 @@ module.exports = { test: /\.css$/, use: [ { - loader: MiniCssExtractPlugin.loader, + loader: ExtractCssChunks.loader, options: { // you can specify a publicPath here // by default it uses publicPath in webpackOptions.output publicPath: '../', - hot: process.env.NODE_ENV === 'development', + hmr: process.env.NODE_ENV === 'development', }, }, 'css-loader', @@ -100,25 +229,16 @@ module.exports = { }; ``` -*webpack.server.config.js* - -The server needs to be handled differently, we still want one chunk. Luckily webpack 4 supports **LimitChunkCountPlugin** - -```js -new webpack.optimize.LimitChunkCountPlugin({ - maxChunks: 1 -}) -``` - -#### `publicPath` function example +### The `publicPath` option as function **webpack.config.js** ```js -const MiniCssExtractPlugin = require('mini-css-extract-plugin'); +const ExtractCssChunks = require('extract-css-chunks-webpack-plugin'); + module.exports = { plugins: [ - new MiniCssExtractPlugin({ + new ExtractCssChunks({ // Options similar to the same options in webpackOptions.output // both options are optional filename: '[name].css', @@ -131,7 +251,7 @@ module.exports = { test: /\.css$/, use: [ { - loader: MiniCssExtractPlugin.loader, + loader: ExtractCssChunks.loader, options: { publicPath: (resourcePath, context) => { // publicPath is the relative path of the resource to the context @@ -149,7 +269,7 @@ module.exports = { }; ``` -#### Advanced configuration example +### Advanced configuration example This plugin should be used only on `production` builds without `style-loader` in the loaders chain, especially if you want to have HMR in `development`. @@ -160,12 +280,12 @@ Here is an example to have both HMR in `development` and your styles extracted i **webpack.config.js** ```js -const MiniCssExtractPlugin = require('mini-css-extract-plugin'); +const ExtractCssChunks = require('extract-css-chunks-webpack-plugin'); const devMode = process.env.NODE_ENV !== 'production'; module.exports = { plugins: [ - new MiniCssExtractPlugin({ + new ExtractCssChunks({ // Options similar to the same options in webpackOptions.output // both options are optional filename: devMode ? '[name].css' : '[name].[hash].css', @@ -178,9 +298,9 @@ module.exports = { test: /\.(sa|sc|c)ss$/, use: [ { - loader: MiniCssExtractPlugin.loader, + loader: ExtractCssChunks.loader, options: { - hot: process.env.NODE_ENV === 'development', + hmr: process.env.NODE_ENV === 'development', }, }, 'css-loader', @@ -193,19 +313,24 @@ module.exports = { }; ``` -#### Hot Module Reloading (HMR) +### Hot Module Reloading (HMR) -extract-mini-css-plugin supports hot reloading of actual css files in development. Some options are provided to enable HMR of both standard stylesheets and locally scoped CSS or CSS modules. Below is an example configuration of mini-css for HMR use with CSS modules. +The `extract-css-chunks-webpack-plugin` supports hot reloading of actual css files in development. +Some options are provided to enable HMR of both standard stylesheets and locally scoped CSS or CSS modules. +Below is an example configuration of mini-css for HMR use with CSS modules. -While we attempt to hmr css-modules. It is not easy to perform when code-splitting with custom chunk names. `reloadAll` is an option that should only be enabled if HMR isn't working correctly. The core challenge with css-modules is that when code-split, the chunk ids can and do end up different compared to the filename. +While we attempt to hmr css-modules. It is not easy to perform when code-splitting with custom chunk names. +`reloadAll` is an option that should only be enabled if HMR isn't working correctly. +The core challenge with css-modules is that when code-split, the chunk ids can and do end up different compared to the filename. **webpack.config.js** ```js -const MiniCssExtractPlugin = require('mini-css-extract-plugin'); +const ExtractCssChunks = require('extract-css-chunks-webpack-plugin'); + module.exports = { plugins: [ - new MiniCssExtractPlugin({ + new ExtractCssChunks({ // Options similar to the same options in webpackOptions.output // both options are optional filename: '[name].css', @@ -218,10 +343,10 @@ module.exports = { test: /\.css$/, use: [ { - loader: MiniCssExtractPlugin.loader, + loader: ExtractCssChunks.loader, options: { // only enable hot in development - hot: process.env.NODE_ENV === 'development', + hmr: process.env.NODE_ENV === 'development', // if hmr does not work, this is a forceful method. reloadAll: true, }, @@ -236,20 +361,22 @@ module.exports = { ### Minimizing For Production -To minify the output, use a plugin like [optimize-css-assets-webpack-plugin](https://github.com/NMFR/optimize-css-assets-webpack-plugin). Setting `optimization.minimizer` overrides the defaults provided by webpack, so make sure to also specify a JS minimizer: +To minify the output, use a plugin like [optimize-css-assets-webpack-plugin](https://github.com/NMFR/optimize-css-assets-webpack-plugin). +Setting `optimization.minimizer` overrides the defaults provided by webpack, so make sure to also specify a JS minimizer: **webpack.config.js** ```js const TerserJSPlugin = require('terser-webpack-plugin'); -const MiniCssExtractPlugin = require('mini-css-extract-plugin'); +const ExtractCssChunks = require('extract-css-chunks-webpack-plugin'); const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin'); + module.exports = { optimization: { minimizer: [new TerserJSPlugin({}), new OptimizeCSSAssetsPlugin({})], }, plugins: [ - new MiniCssExtractPlugin({ + new ExtractCssChunks({ filename: '[name].css', chunkFilename: '[id].css', }), @@ -258,338 +385,246 @@ module.exports = { rules: [ { test: /\.css$/, - use: [MiniCssExtractPlugin.loader, 'css-loader'], + use: [ExtractCssChunks.loader, 'css-loader'], }, ], }, }; ``` +### Using preloaded or inlined CSS -### What about Webpack 3? -This is a breaking change. The entire loader has been fundamentally rewritten specifically for Webpack 4. Aiming to support our existing user base, allowing them to upgrade their infrastructure to support Webpack 4 based universally code-split server-side rendered react applications. - -There have been some challenges along the way since the release of webpack 4. Ultimately the only remaining hurdle is code split, async style loading. - -If you do need Webpack 3, make sure to stick with the latest `v2.x.x` release. `> v3.x.x` is only intended for users with Webpack 4 - - - +The runtime code detects already added CSS via `` or `