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.

272 lines
8.7 KiB

4 years ago
  1. [![npm][npm]][npm-url]
  2. [![node][node]][node-url]
  3. [![deps][deps]][deps-url]
  4. [![tests][tests]][tests-url]
  5. [![coverage][cover]][cover-url]
  6. [![chat][chat]][chat-url]
  7. <div align="center">
  8. <img width="200" height="200"
  9. src="https://cdn.rawgit.com/webpack-contrib/extract-text-webpack-plugin/574e3200/logo.svg">
  10. <a href="https://github.com/webpack/webpack">
  11. <img width="200" height="200"
  12. src="https://webpack.js.org/assets/icon-square-big.svg">
  13. </a>
  14. <h1>Extract Text Plugin</h1>
  15. <p>Extract text from a bundle, or bundles, into a separate file.</p>
  16. </div>
  17. <h2 align="center">Install</h2>
  18. ```bash
  19. # for webpack 3
  20. npm install --save-dev extract-text-webpack-plugin
  21. # for webpack 2
  22. npm install --save-dev extract-text-webpack-plugin@2.1.2
  23. # for webpack 1
  24. npm install --save-dev extract-text-webpack-plugin@1.0.1
  25. ```
  26. <h2 align="center">Usage</h2>
  27. > :warning: For webpack v1, see [the README in the webpack-1 branch](https://github.com/webpack/extract-text-webpack-plugin/blob/webpack-1/README.md).
  28. ```js
  29. const ExtractTextPlugin = require("extract-text-webpack-plugin");
  30. module.exports = {
  31. module: {
  32. rules: [
  33. {
  34. test: /\.css$/,
  35. use: ExtractTextPlugin.extract({
  36. fallback: "style-loader",
  37. use: "css-loader"
  38. })
  39. }
  40. ]
  41. },
  42. plugins: [
  43. new ExtractTextPlugin("styles.css"),
  44. ]
  45. }
  46. ```
  47. It moves all the required `*.css` modules in entry chunks into a separate CSS file. So your styles are no longer inlined into the JS bundle, but in a separate CSS file (`styles.css`). If your total stylesheet volume is big, it will be faster because the CSS bundle is loaded in parallel to the JS bundle.
  48. |Advantages|Caveats|
  49. |:---------|:------|
  50. | Fewer style tags (older IE has a limit) | Additional HTTP request |
  51. | CSS SourceMap (with `devtool: "source-map"` and `extract-text-webpack-plugin?sourceMap`) | Longer compilation time |
  52. | CSS requested in parallel | No runtime public path modification |
  53. | CSS cached separate | No Hot Module Replacement |
  54. | Faster runtime (less code and DOM operations) | ... |
  55. <h2 align="center">Options</h2>
  56. ```js
  57. new ExtractTextPlugin(options: filename | object)
  58. ```
  59. |Name|Type|Description|
  60. |:--:|:--:|:----------|
  61. |**`id`**|`{String}`|Unique ident for this plugin instance. (For advanced usage only, by default automatically generated)|
  62. |**`filename`**|`{String\|Function}`|Name of the result file. May contain `[name]`, `[id]` and `[contenthash]`|
  63. |**`allChunks`**|`{Boolean}`|Extract from all additional chunks too (by default it extracts only from the initial chunk(s))<br />When using `optimization.splitChunks` and there are extracted chunks (from `ExtractTextPlugin.extract`) in the commons chunk, `allChunks` **must** be set to `true`|
  64. |**`disable`**|`{Boolean}`|Disables the plugin|
  65. |**`ignoreOrder`**|`{Boolean}`|Disables order check (useful for CSS Modules!), `false` by default|
  66. * `[name]` name of the chunk
  67. * `[id]` number of the chunk
  68. * `[contenthash]` hash of the content of the extracted file
  69. * `[<hashType>:contenthash:<digestType>:<length>]` optionally you can configure
  70. * other `hashType`s, e.g. `sha1`, `md5`, `sha256`, `sha512`
  71. * other `digestType`s, e.g. `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`
  72. * and `length`, the length of the hash in chars
  73. > :warning: `ExtractTextPlugin` generates a file **per entry**, so you must use `[name]`, `[id]` or `[contenthash]` when using multiple entries.
  74. #### `#extract`
  75. ```js
  76. ExtractTextPlugin.extract(options: loader | object)
  77. ```
  78. Creates an extracting loader from an existing loader. Supports loaders of type `{ loader: [name]-loader -> {String}, options: {} -> {Object} }`.
  79. |Name|Type|Description|
  80. |:--:|:--:|:----------|
  81. |**`options.use`**|`{String}`/`{Array}`/`{Object}`|Loader(s) that should be used for converting the resource to a CSS exporting module _(required)_|
  82. |**`options.fallback`**|`{String}`/`{Array}`/`{Object}`|loader(e.g `'style-loader'`) that should be used when the CSS is not extracted (i.e. in an additional chunk when `allChunks: false`)|
  83. |**`options.publicPath`**|`{String}`|Override the `publicPath` setting for this loader|
  84. #### Multiple Instances
  85. There is also an `extract` function on the instance. You should use this if you have more than one instance of `ExtractTextPlugin`.
  86. ```js
  87. const ExtractTextPlugin = require('extract-text-webpack-plugin');
  88. // Create multiple instances
  89. const extractCSS = new ExtractTextPlugin('stylesheets/[name]-one.css');
  90. const extractLESS = new ExtractTextPlugin('stylesheets/[name]-two.css');
  91. module.exports = {
  92. module: {
  93. rules: [
  94. {
  95. test: /\.css$/,
  96. use: extractCSS.extract([ 'css-loader', 'postcss-loader' ])
  97. },
  98. {
  99. test: /\.less$/i,
  100. use: extractLESS.extract([ 'css-loader', 'less-loader' ])
  101. },
  102. ]
  103. },
  104. plugins: [
  105. extractCSS,
  106. extractLESS
  107. ]
  108. };
  109. ```
  110. ### Extracting Sass or LESS
  111. The configuration is the same, switch out `sass-loader` for `less-loader` when necessary.
  112. ```js
  113. const ExtractTextPlugin = require('extract-text-webpack-plugin');
  114. module.exports = {
  115. module: {
  116. rules: [
  117. {
  118. test: /\.scss$/,
  119. use: ExtractTextPlugin.extract({
  120. fallback: 'style-loader',
  121. use: ['css-loader', 'sass-loader']
  122. })
  123. }
  124. ]
  125. },
  126. plugins: [
  127. new ExtractTextPlugin('style.css')
  128. //if you want to pass in options, you can do so:
  129. //new ExtractTextPlugin({
  130. // filename: 'style.css'
  131. //})
  132. ]
  133. }
  134. ```
  135. ### `url()` Resolving
  136. If you are finding that urls are not resolving properly when you run webpack. You can expand your loader functionality with options. The `url: false` property allows your paths resolved without any changes.
  137. ```js
  138. const ExtractTextPlugin = require('extract-text-webpack-plugin');
  139. module.exports = {
  140. module: {
  141. rules: [
  142. {
  143. test: /\.scss$/,
  144. use: ExtractTextPlugin.extract({
  145. fallback: 'style-loader',
  146. use: [
  147. {
  148. loader: 'css-loader',
  149. options: {
  150. // If you are having trouble with urls not resolving add this setting.
  151. // See https://github.com/webpack-contrib/css-loader#url
  152. url: false,
  153. minimize: true,
  154. sourceMap: true
  155. }
  156. },
  157. {
  158. loader: 'sass-loader',
  159. options: {
  160. sourceMap: true
  161. }
  162. }
  163. ]
  164. })
  165. }
  166. ]
  167. }
  168. }
  169. ```
  170. ### Modify filename
  171. `filename` parameter could be `Function`. It passes `getPath` to process the format like `css/[name].css` and returns the real file name, `css/js/a.css`. You can replace `css/js` with `css` then you will get the new path `css/a.css`.
  172. ```js
  173. entry: {
  174. 'js/a': "./a"
  175. },
  176. plugins: [
  177. new ExtractTextPlugin({
  178. filename: (getPath) => {
  179. return getPath('css/[name].css').replace('css/js', 'css');
  180. },
  181. allChunks: true
  182. })
  183. ]
  184. ```
  185. <h2 align="center">Maintainers</h2>
  186. <table>
  187. <tbody>
  188. <tr>
  189. <td align="center">
  190. <img width="150" height="150"
  191. src="https://avatars3.githubusercontent.com/u/166921?v=3&s=150">
  192. </br>
  193. <a href="https://github.com/bebraw">Juho Vepsäläinen</a>
  194. </td>
  195. <td align="center">
  196. <img width="150" height="150"
  197. src="https://avatars2.githubusercontent.com/u/8420490?v=3&s=150">
  198. </br>
  199. <a href="https://github.com/d3viant0ne">Joshua Wiens</a>
  200. </td>
  201. <td align="center">
  202. <img width="150" height="150"
  203. src="https://avatars3.githubusercontent.com/u/533616?v=3&s=150">
  204. </br>
  205. <a href="https://github.com/SpaceK33z">Kees Kluskens</a>
  206. </td>
  207. <td align="center">
  208. <img width="150" height="150"
  209. src="https://avatars3.githubusercontent.com/u/3408176?v=3&s=150">
  210. </br>
  211. <a href="https://github.com/TheLarkInn">Sean Larkin</a>
  212. </td>
  213. </tr>
  214. <tbody>
  215. </table>
  216. [npm]: https://img.shields.io/npm/v/extract-text-webpack-plugin.svg
  217. [npm-url]: https://npmjs.com/package/extract-text-webpack-plugin
  218. [node]: https://img.shields.io/node/v/extract-text-webpack-plugin.svg
  219. [node-url]: https://nodejs.org
  220. [deps]: https://david-dm.org/webpack-contrib/extract-text-webpack-plugin.svg
  221. [deps-url]: https://david-dm.org/webpack-contrib/extract-text-webpack-plugin
  222. [tests]: http://img.shields.io/travis/webpack-contrib/extract-text-webpack-plugin.svg
  223. [tests-url]: https://travis-ci.org/webpack-contrib/extract-text-webpack-plugin
  224. [cover]: https://coveralls.io/repos/github/webpack-contrib/extract-text-webpack-plugin/badge.svg
  225. [cover-url]: https://coveralls.io/github/webpack-contrib/extract-text-webpack-plugin
  226. [chat]: https://badges.gitter.im/webpack/webpack.svg
  227. [chat-url]: https://gitter.im/webpack/webpack