You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 8.3 KiB

3 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <div align="center">
  2. <!-- replace with accurate logo e.g from https://worldvectorlogo.com/ -->
  3. <img width="200" height="200" src="https://cdn.worldvectorlogo.com/logos/javascript.svg">
  4. <a href="https://webpack.js.org/">
  5. <img width="200" height="200" vspace="" hspace="25" src="https://cdn.rawgit.com/webpack/media/e7485eb2/logo/icon-square-big.svg">
  6. </a>
  7. <h1>mini-css-extract-plugin</h1>
  8. </div>
  9. [![npm][npm]][npm-url]
  10. [![deps][deps]][deps-url]
  11. [![tests][tests]][tests-url]
  12. [![coverage][cover]][cover-url]
  13. [![chat][chat]][chat-url]
  14. 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.
  15. It builds on top of a new webpack v4 feature (module types) and requires webpack 4 to work.
  16. Compared to the extract-text-webpack-plugin:
  17. * Async loading
  18. * No duplicate compilation (performance)
  19. * Easier to use
  20. * Specific to CSS
  21. TODO:
  22. * HMR support
  23. <h2 align="center">Install</h2>
  24. ```bash
  25. npm install --save-dev mini-css-extract-plugin
  26. ```
  27. <h2 align="center">Usage</h2>
  28. ### Configuration
  29. #### Minimal example
  30. **webpack.config.js**
  31. ```js
  32. const MiniCssExtractPlugin = require("mini-css-extract-plugin");
  33. module.exports = {
  34. plugins: [
  35. new MiniCssExtractPlugin({
  36. // Options similar to the same options in webpackOptions.output
  37. // both options are optional
  38. filename: "[name].css",
  39. chunkFilename: "[id].css"
  40. })
  41. ],
  42. module: {
  43. rules: [
  44. {
  45. test: /\.css$/,
  46. use: [
  47. {
  48. loader: MiniCssExtractPlugin.loader,
  49. options: {
  50. // you can specify a publicPath here
  51. // by default it use publicPath in webpackOptions.output
  52. publicPath: '../'
  53. }
  54. },
  55. "css-loader"
  56. ]
  57. }
  58. ]
  59. }
  60. }
  61. ```
  62. #### Advanced configuration example
  63. 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`.
  64. Here is an example to have both HMR in `development` and your styles extracted in a file for `production` builds.
  65. (Loaders options left out for clarity, adapt accordingly to your needs.)
  66. **webpack.config.js**
  67. ```js
  68. const MiniCssExtractPlugin = require("mini-css-extract-plugin");
  69. const devMode = process.env.NODE_ENV !== 'production'
  70. module.exports = {
  71. plugins: [
  72. new MiniCssExtractPlugin({
  73. // Options similar to the same options in webpackOptions.output
  74. // both options are optional
  75. filename: devMode ? '[name].css' : '[name].[hash].css',
  76. chunkFilename: devMode ? '[id].css' : '[id].[hash].css',
  77. })
  78. ],
  79. module: {
  80. rules: [
  81. {
  82. test: /\.(sa|sc|c)ss$/,
  83. use: [
  84. devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
  85. 'css-loader',
  86. 'postcss-loader',
  87. 'sass-loader',
  88. ],
  89. }
  90. ]
  91. }
  92. }
  93. ```
  94. ### Minimizing For Production
  95. While webpack 5 is likely to come with a CSS minimizer built-in, with webpack 4 you need to bring your own. 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:
  96. **webpack.config.js**
  97. ```js
  98. const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
  99. const MiniCssExtractPlugin = require("mini-css-extract-plugin");
  100. const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
  101. module.exports = {
  102. optimization: {
  103. minimizer: [
  104. new UglifyJsPlugin({
  105. cache: true,
  106. parallel: true,
  107. sourceMap: true // set to true if you want JS source maps
  108. }),
  109. new OptimizeCSSAssetsPlugin({})
  110. ]
  111. },
  112. plugins: [
  113. new MiniCssExtractPlugin({
  114. filename: "[name].css",
  115. chunkFilename: "[id].css"
  116. })
  117. ],
  118. module: {
  119. rules: [
  120. {
  121. test: /\.css$/,
  122. use: [
  123. MiniCssExtractPlugin.loader,
  124. "css-loader"
  125. ]
  126. }
  127. ]
  128. }
  129. }
  130. ```
  131. ### Features
  132. #### Using preloaded or inlined CSS
  133. The runtime code detects already added CSS via `<link>` or `<style>` tag.
  134. This can be useful when injecting CSS on server-side for Server-Side-Rendering.
  135. The `href` of the `<link>` tag has to match the URL that will be used for loading the CSS chunk.
  136. The `data-href` attribute can be used for `<link>` and `<style>` too.
  137. When inlining CSS `data-href` must be used.
  138. #### Extracting all CSS in a single file
  139. Similar to what [extract-text-webpack-plugin](https://github.com/webpack-contrib/extract-text-webpack-plugin) does, the CSS
  140. can be extracted in one CSS file using `optimization.splitChunks.cacheGroups`.
  141. **webpack.config.js**
  142. ```js
  143. const MiniCssExtractPlugin = require("mini-css-extract-plugin");
  144. module.exports = {
  145. optimization: {
  146. splitChunks: {
  147. cacheGroups: {
  148. styles: {
  149. name: 'styles',
  150. test: /\.css$/,
  151. chunks: 'all',
  152. enforce: true
  153. }
  154. }
  155. }
  156. },
  157. plugins: [
  158. new MiniCssExtractPlugin({
  159. filename: "[name].css",
  160. })
  161. ],
  162. module: {
  163. rules: [
  164. {
  165. test: /\.css$/,
  166. use: [
  167. MiniCssExtractPlugin.loader,
  168. "css-loader"
  169. ]
  170. }
  171. ]
  172. }
  173. }
  174. ```
  175. #### Extracting CSS based on entry
  176. You may also extract the CSS based on the webpack entry name. This is especially useful if you import routes dynamically
  177. but want to keep your CSS bundled according to entry. This also prevents the CSS duplication issue one had with the
  178. ExtractTextPlugin.
  179. ```javascript
  180. const path = require('path');
  181. const MiniCssExtractPlugin = require("mini-css-extract-plugin");
  182. function recursiveIssuer(m) {
  183. if (m.issuer) {
  184. return recursiveIssuer(m.issuer);
  185. } else if (m.name) {
  186. return m.name;
  187. } else {
  188. return false;
  189. }
  190. }
  191. module.exports = {
  192. entry: {
  193. foo: path.resolve(__dirname, 'src/foo'),
  194. bar: path.resolve(__dirname, 'src/bar')
  195. },
  196. optimization: {
  197. splitChunks: {
  198. cacheGroups: {
  199. fooStyles: {
  200. name: 'foo',
  201. test: (m,c,entry = 'foo') => m.constructor.name === 'CssModule' && recursiveIssuer(m) === entry,
  202. chunks: 'all',
  203. enforce: true
  204. },
  205. barStyles: {
  206. name: 'bar',
  207. test: (m,c,entry = 'bar') => m.constructor.name === 'CssModule' && recursiveIssuer(m) === entry,
  208. chunks: 'all',
  209. enforce: true
  210. }
  211. }
  212. }
  213. },
  214. plugins: [
  215. new MiniCssExtractPlugin({
  216. filename: "[name].css",
  217. })
  218. ],
  219. module: {
  220. rules: [
  221. {
  222. test: /\.css$/,
  223. use: [
  224. MiniCssExtractPlugin.loader,
  225. "css-loader"
  226. ]
  227. }
  228. ]
  229. }
  230. }
  231. ```
  232. #### Long Term Caching
  233. For long term caching use `filename: "[contenthash].css"`. Optionally add `[name]`.
  234. ### Media Query Plugin
  235. If you'd like to extract the media queries from the extracted CSS (so mobile users don't need to load desktop or tablet specific CSS anymore) you should use one of the following plugins:
  236. - [Media Query Plugin](https://github.com/SassNinja/media-query-plugin)
  237. - [Media Query Splitting Plugin](https://github.com/mike-diamond/media-query-splitting-plugin)
  238. <h2 align="center">Maintainers</h2>
  239. <table>
  240. <tbody>
  241. <tr>
  242. <td align="center">
  243. <a href="https://github.com/sokra">
  244. <img width="150" height="150" src="https://github.com/sokra.png?size=150">
  245. </br>
  246. Tobias Koppers
  247. </a>
  248. </td>
  249. </tr>
  250. <tbody>
  251. </table>
  252. ## License
  253. #### [MIT](./LICENSE)
  254. [npm]: https://img.shields.io/npm/v/mini-css-extract-plugin.svg
  255. [npm-url]: https://npmjs.com/package/mini-css-extract-plugin
  256. [node]: https://img.shields.io/node/v/mini-css-extract-plugin.svg
  257. [node-url]: https://nodejs.org
  258. [deps]: https://david-dm.org/webpack-contrib/mini-css-extract-plugin.svg
  259. [deps-url]: https://david-dm.org/webpack-contrib/mini-css-extract-plugin
  260. [tests]: https://img.shields.io/circleci/project/github/webpack-contrib/mini-css-extract-plugin.svg
  261. [tests-url]: https://circleci.com/gh/webpack-contrib/mini-css-extract-plugin
  262. [cover]: https://codecov.io/gh/webpack-contrib/mini-css-extract-plugin/branch/master/graph/badge.svg
  263. [cover-url]: https://codecov.io/gh/webpack-contrib/mini-css-extract-plugin
  264. [chat]: https://img.shields.io/badge/gitter-webpack%2Fwebpack-brightgreen.svg
  265. [chat-url]: https://gitter.im/webpack/webpack