File tree Expand file tree Collapse file tree 3 files changed +73
-0
lines changed Expand file tree Collapse file tree 3 files changed +73
-0
lines changed Original file line number Diff line number Diff line change @@ -38,6 +38,13 @@ npm install --save-dev mini-css-extract-plugin
38
38
39
39
### Configuration
40
40
41
+ #### ` publicPath `
42
+
43
+ Type: ` String|Function `
44
+ Default: the ` publicPath ` in ` webpackOptions.output `
45
+
46
+ Specifies a custom public path for the target file(s).
47
+
41
48
#### Minimal example
42
49
43
50
** webpack.config.js**
@@ -74,6 +81,45 @@ module.exports = {
74
81
}
75
82
```
76
83
84
+ #### ` publicPath ` function example
85
+
86
+ ** webpack.config.js**
87
+
88
+ ``` js
89
+ const MiniCssExtractPlugin = require (" mini-css-extract-plugin" );
90
+ module .exports = {
91
+ plugins: [
92
+ new MiniCssExtractPlugin ({
93
+ // Options similar to the same options in webpackOptions.output
94
+ // both options are optional
95
+ filename: " [name].css" ,
96
+ chunkFilename: " [id].css"
97
+ })
98
+ ],
99
+ module: {
100
+ rules: [
101
+ {
102
+ test: / \. css$ / ,
103
+ use: [
104
+ {
105
+ loader: MiniCssExtractPlugin .loader ,
106
+ options: {
107
+ publicPath : (resourcePath , context ) => {
108
+ // publicPath is the relative path of the resource to the context
109
+ // e.g. for ./css/admin/main.css the publicPath will be ../../
110
+ // while for ./css/main.css the publicPath will be ../
111
+ return path .relative (path .dirname (resourcePath), context) + ' /'
112
+ },
113
+ }
114
+ },
115
+ " css-loader"
116
+ ]
117
+ }
118
+ ]
119
+ }
120
+ }
121
+ ```
122
+
77
123
#### Advanced configuration example
78
124
79
125
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 ` .
Original file line number Diff line number Diff line change @@ -6,6 +6,9 @@ import NodeTargetPlugin from 'webpack/lib/node/NodeTargetPlugin';
6
6
import LibraryTemplatePlugin from 'webpack/lib/LibraryTemplatePlugin' ;
7
7
import SingleEntryPlugin from 'webpack/lib/SingleEntryPlugin' ;
8
8
import LimitChunkCountPlugin from 'webpack/lib/optimize/LimitChunkCountPlugin' ;
9
+ import validateOptions from 'schema-utils' ;
10
+
11
+ import schema from './options.json' ;
9
12
10
13
const MODULE_TYPE = 'css/mini-extract' ;
11
14
const pluginName = 'mini-css-extract-plugin' ;
@@ -29,12 +32,17 @@ const findModuleById = (modules, id) => {
29
32
30
33
export function pitch ( request ) {
31
34
const query = loaderUtils . getOptions ( this ) || { } ;
35
+
36
+ validateOptions ( schema , query , 'Mini CSS Extract Plugin Loader' ) ;
37
+
32
38
const loaders = this . loaders . slice ( this . loaderIndex + 1 ) ;
33
39
this . addDependency ( this . resourcePath ) ;
34
40
const childFilename = '*' ; // eslint-disable-line no-path-concat
35
41
const publicPath =
36
42
typeof query . publicPath === 'string'
37
43
? query . publicPath
44
+ : typeof query . publicPath === 'function'
45
+ ? query . publicPath ( this . resourcePath , this . rootContext )
38
46
: this . _compilation . outputOptions . publicPath ;
39
47
const outputOptions = {
40
48
filename : childFilename ,
Original file line number Diff line number Diff line change
1
+ {
2
+ "additionalProperties" : true ,
3
+ "properties" : {
4
+ "publicPath" : {
5
+ "anyOf" : [
6
+ {
7
+ "type" : " string"
8
+ },
9
+ {
10
+ "instanceof" : " Function"
11
+ }
12
+ ]
13
+ }
14
+ },
15
+ "errorMessages" : {
16
+ "publicPath" : " should be {String} or {Function} (https://github.com/webpack-contrib/mini-css-extract-plugin#publicpath)"
17
+ },
18
+ "type" : " object"
19
+ }
You can’t perform that action at this time.
0 commit comments