|
6 | 6 | <h1>CSS Entry Plugin</h1>
|
7 | 7 | </div>
|
8 | 8 |
|
9 |
| -# css-entry-webpack-plugin |
10 |
| -A Webpack plugin that allows CSS files as the entry. |
| 9 | +[![Build Status][plugin-travis-shield]][plugin-travis-url] |
| 10 | +[![License][plugin-license-shield]][plugin-npm-url] |
| 11 | + |
| 12 | +A [Webpack][webpack-url] plugin that simplifies creation of CSS-only bundles. |
| 13 | + |
| 14 | +Installation |
| 15 | +------------ |
| 16 | +[![NPM Version][plugin-npm-version-shield]][plugin-npm-url] |
| 17 | +[![Dependency Status][plugin-npm-dependencies-shield]][plugin-npm-dependencies-url] |
| 18 | + |
| 19 | +Install the plugin using [npm][npm-url]: |
| 20 | +```shell |
| 21 | +$ npm install css-entry-webpack-plugin --save-dev |
| 22 | +``` |
| 23 | + |
| 24 | +[][plugin-npm-url] |
| 25 | + |
| 26 | +Basic Usage |
| 27 | +----------- |
| 28 | +The plugin will identify the entries that contain only CSS resources and will generate CSS bundles for them. |
| 29 | + |
| 30 | +webpack.config.js |
| 31 | +```js |
| 32 | +const CssEntryPlugin = require("css-entry-webpack-plugin"); |
| 33 | + |
| 34 | +module.exports = { |
| 35 | + entry: { |
| 36 | + "styles": ["src/style1.css", "src/style2.css"], |
| 37 | + "main": "src/index.js" |
| 38 | + }, |
| 39 | + output: { |
| 40 | + path: "dist", |
| 41 | + filename: "[name].bundle.js" |
| 42 | + }, |
| 43 | + module: { |
| 44 | + rules: [ |
| 45 | + // This is required |
| 46 | + { |
| 47 | + test: /\.css$/, |
| 48 | + use: "css-loader" |
| 49 | + } |
| 50 | + ] |
| 51 | + }, |
| 52 | + plugins: [ |
| 53 | + new CssEntryPlugin({ |
| 54 | + output: { |
| 55 | + filename: "[name].bundle.css" |
| 56 | + } |
| 57 | + }) |
| 58 | + ] |
| 59 | +}; |
| 60 | +``` |
| 61 | + |
| 62 | +will output two files |
| 63 | + |
| 64 | +`main.bundle.js` and `styles.bundle.css` |
| 65 | + |
| 66 | +API |
| 67 | +--- |
| 68 | + |
| 69 | +```js |
| 70 | +new CssEntryPlugin(options: String | Object) |
| 71 | +``` |
| 72 | + |
| 73 | +#### `options` |
| 74 | +Type: `String | Function | Object`<br> |
| 75 | +Optional |
| 76 | + |
| 77 | +Specifies the options for the `CssEntryPlugin`. |
| 78 | + |
| 79 | +The shorthand version allows you to specify the `output.filename` directly as a `String` or a `Function`, this will be equivalent to passing an object with `output.filename`. See [`output.filename`](#outputfilename) for details on the possible values. |
| 80 | + |
| 81 | +```js |
| 82 | +new CssEntryPlugin(/* option: String | Function */) |
| 83 | +// is equivalent to |
| 84 | +new CssEntryPlugin({ |
| 85 | + output: { |
| 86 | + filename: /* option */ |
| 87 | + } |
| 88 | +}) |
| 89 | +``` |
| 90 | + |
| 91 | +When specified as an `Object`, the following options are available: |
| 92 | + |
| 93 | +##### `output` |
| 94 | +Type: `Object`<br> |
| 95 | +Optional |
| 96 | + |
| 97 | +Specifies a set of options instructing the plugin on how and where to output your CSS bundles. It works in a similar fashion to [Webpack's `output` option](https://webpack.js.org/configuration/output/#output-filename). |
| 98 | + |
| 99 | +```js |
| 100 | +new CssEntryPlugin({ |
| 101 | + output: { /* output options */ } |
| 102 | +}) |
| 103 | +``` |
| 104 | + |
| 105 | +##### `output.filename` |
| 106 | +Type: `String | Function`<br> |
| 107 | +Default: `[name].css`<br> |
| 108 | +Optional |
| 109 | + |
| 110 | +This option determines the name of each CSS output bundle. The bundle is written to the directory specified by the [Webpack `output.path` option](https://webpack.js.org/configuration/output/#output-path). It works in a similar fashion to [Webpack's `output.filename` option](https://webpack.js.org/configuration/output/#output-filename) and [`ExtractTextPlugin`'s `filename` option](https://github.com/webpack-contrib/extract-text-webpack-plugin#options). |
| 111 | + |
| 112 | +For a single [`entry`](https://webpack.js.org/configuration/entry-context#entry) point, this can be a static name. |
| 113 | + |
| 114 | +```js |
| 115 | +filename: "bundle.css" |
| 116 | +``` |
| 117 | + |
| 118 | +However, when creating multiple bundles via more than one entry point, you should use a [template string](https://github.com/webpack/webpack/blob/master/lib/TemplatedPathPlugin.js) with one of the following substitutions to give each bundle a unique name. |
| 119 | + |
| 120 | +Using the entry name: |
| 121 | + |
| 122 | +```js |
| 123 | +filename: "[name].bundle.css" |
| 124 | +``` |
| 125 | + |
| 126 | +Using the internal chunk id: |
| 127 | + |
| 128 | +```js |
| 129 | +filename: "[id].bundle.css" |
| 130 | +``` |
| 131 | + |
| 132 | +The following substitutions are available in template strings: |
| 133 | + |
| 134 | +|Substitution|Description| |
| 135 | +|:----------:|:----------| |
| 136 | +|`[name]`|The module name or name of the chunk| |
| 137 | +|`[id]`|The number of the chunk or module identifier| |
| 138 | +|`[contenthash]`|The hash of the content of the extracted file| |
| 139 | + |
| 140 | +Any combination of these substitutions is allowed (eg. `"[name].[id].css"`). |
| 141 | + |
| 142 | +The option can also be specified as a `Function` which should return the `filename` as a string without substitutions. |
| 143 | + |
| 144 | +```js |
| 145 | +filename: function (getPath /* (template: string) => string */) { |
| 146 | + return "prefix-" + getPath("[name].[id].css"); |
| 147 | +} |
| 148 | +``` |
| 149 | + |
| 150 | +The `Function` has the signature `(getPath: ((template: string) => string)) => string` where `getPath` is a function passed as the first argument, that can be used to perform the substitutions on a given template string to retrieve the original path. |
| 151 | + |
| 152 | +Note this option is called `filename` but you are still allowed to use or return something like `"css/[name]/bundle.css"` to create a folder structure. |
| 153 | + |
| 154 | +Note this option only affects CSS output files for entries matched by this plugin (CSS entries). |
| 155 | + |
| 156 | +##### `entries` |
| 157 | +Type: `String | String[] | RegExp | Function`<br> |
| 158 | +Optional and mutually exclusive with [`ignoreEntries`](#ignoreentries) |
| 159 | + |
| 160 | +Specifies the entry or entries to consider as possible CSS entries. Other entries will be ignored. |
| 161 | + |
| 162 | +##### `ignoreEntries` |
| 163 | +Type: `String | String[] | RegExp | Function`<br> |
| 164 | +Optional and mutually exclusive with [`entries`](#entries) |
| 165 | + |
| 166 | +Specifies the entry or entries to ignore. Other entries will be considered as possible CSS entries. |
| 167 | + |
| 168 | +##### `extensions` |
| 169 | +Type: `String | String[]`<br> |
| 170 | +Default: `[".css", ".scss", ".less", ".styl"]`<br> |
| 171 | +Optional and mutually exclusive with [`test`](#test) |
| 172 | + |
| 173 | +Specifies which file extensions are valid for files/resources inside considered CSS entries. |
| 174 | + |
| 175 | +##### `test` |
| 176 | +Type: `RegExp | Function`<br> |
| 177 | +Optional and mutually exclusive with [`extensions`](#extensions) |
| 178 | + |
| 179 | +Specifies which files/resources are valid for considered CSS entries. |
| 180 | + |
| 181 | +##### `disable` |
| 182 | +Type: `Boolean`<br> |
| 183 | +Default: `false`<br> |
| 184 | +Optional |
| 185 | + |
| 186 | +Disables the plugin. |
| 187 | + |
| 188 | +[webpack-url]: https://webpack.js.org/ |
| 189 | +[npm-url]: https://www.npmjs.com/ |
| 190 | + |
| 191 | +[plugin-npm-url]: https://npmjs.com/package/css-entry-webpack-plugin |
| 192 | +[plugin-npm-dependencies-url]: https://david-dm.org/tomachristian/css-entry-webpack-plugin |
| 193 | +[plugin-travis-url]: https://travis-ci.org/tomachristian/css-entry-webpack-plugin |
| 194 | + |
| 195 | +[plugin-license-shield]: https://img.shields.io/github/license/mashape/apistatus.svg?style=flat-square |
| 196 | +[plugin-npm-version-shield]: https://img.shields.io/npm/v/css-entry-webpack-plugin.svg?style=flat-square |
| 197 | +[plugin-npm-dependencies-shield]: https://david-dm.org/tomachristian/css-entry-webpack-plugin.svg?style=flat-square |
| 198 | +[plugin-travis-shield]: https://img.shields.io/travis/tomachristian/css-entry-webpack-plugin/develop.svg?style=flat-square |
0 commit comments