Skip to content

Commit 53bb481

Browse files
committed
feat - Use use/fallback pair at extract over loader/fallbackLoader`
Closes #382.
1 parent e8e9780 commit 53bb481

File tree

11 files changed

+75
-43
lines changed

11 files changed

+75
-43
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ module.exports = {
3434
{
3535
test: /\.css$/,
3636
use: ExtractTextPlugin.extract({
37-
fallbackLoader: "style-loader",
38-
loader: "css-loader"
37+
fallback: "style-loader",
38+
use: "css-loader"
3939
})
4040
}
4141
]
@@ -85,8 +85,8 @@ Creates an extracting loader from an existing loader. Supports loaders of type `
8585

8686
|Name|Type|Description|
8787
|:--:|:--:|:----------|
88-
|**`options.loader`**|`{String}`/`{Object}`|Loader(s) that should be used for converting the resource to a CSS exporting module _(required)_|
89-
|**`options.fallbackLoader`**|`{String}`/`{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`)|
88+
|**`options.use`**|`{String}`/`{Object}`|Loader(s) that should be used for converting the resource to a CSS exporting module _(required)_|
89+
|**`options.fallback`**|`{String}`/`{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`)|
9090
|**`options.publicPath`**|`{String}`|Override the `publicPath` setting for this loader|
9191

9292

example/dep.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
require("./style2.css");
2-
require(["./dep2"]);
2+
require(["./dep2"]);

example/dep2.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
require("./style3.css");
1+
require("./style3.css");

example/entry.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
require("./common.css");
22
require("./style.css");
3-
require("./dep");
3+
require("./dep");

example/webpack.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ module.exports = {
1414
module: {
1515
loaders: [
1616
{ test: /\.css$/, use: ExtractTextPlugin.extract({
17-
fallbackLoader: "style-loader",
18-
loader: "css-loader?sourceMap",
17+
fallback: "style-loader",
18+
use: "css-loader?sourceMap",
1919
publicPath: "../"
2020
}) },
2121
{ test: /\.png$/, loader: "file-loader" }

index.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var Chunk = require("webpack/lib/Chunk");
1010
var OrderUndefinedError = require("./OrderUndefinedError");
1111
var loaderUtils = require("loader-utils");
1212
var schemaTester = require('./schema/validator');
13-
var loaderSchema = require('./schema/loader-schema.json');
13+
var loaderSchema = require('./schema/loader-schema');
1414
var pluginSchema = require('./schema/plugin-schema.json');
1515

1616
var NS = fs.realpathSync(__dirname);
@@ -178,19 +178,25 @@ ExtractTextPlugin.prototype.extract = function(options) {
178178
"Example: if your old code looked like this:\n" +
179179
" ExtractTextPlugin.extract('style-loader', 'css-loader')\n\n" +
180180
"You would change it to:\n" +
181-
" ExtractTextPlugin.extract({ fallbackLoader: 'style-loader', loader: 'css-loader' })\n\n" +
181+
" ExtractTextPlugin.extract({ fallback: 'style-loader', use: 'css-loader' })\n\n" +
182182
"The available options are:\n" +
183-
" loader: string | object | loader[]\n" +
184-
" fallbackLoader: string | object | loader[]\n" +
183+
" use: string | object | loader[]\n" +
184+
" fallback: string | object | loader[]\n" +
185185
" publicPath: string\n");
186186
}
187+
if(options.fallbackLoader) {
188+
console.warn('fallbackLoader option has been deprecated - replace with "fallback"');
189+
}
190+
if(options.loader) {
191+
console.warn('loader option has been deprecated - replace with "use"');
192+
}
187193
if(Array.isArray(options) || isString(options) || typeof options.options === "object" || typeof options.query === 'object') {
188194
options = { loader: options };
189195
} else {
190196
schemaTester(loaderSchema, options);
191197
}
192-
var loader = options.loader;
193-
var before = options.fallbackLoader || [];
198+
var loader = options.use ||  options.loader;
199+
var before = options.fallback || options.fallbackLoader || [];
194200
if(isString(loader)) {
195201
loader = loader.split("!");
196202
}
@@ -201,6 +207,8 @@ ExtractTextPlugin.prototype.extract = function(options) {
201207
}
202208
options = mergeOptions({omit: before.length, remove: true}, options);
203209
delete options.loader;
210+
delete options.use;
211+
delete options.fallback;
204212
delete options.fallbackLoader;
205213
return [this.loader(options)]
206214
.concat(before, loader)
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{
1+
module.exports = {
22
"$schema": "http://json-schema.org/draft-04/schema#",
33
"type": "object",
44
"additionalProperties": false,
@@ -7,9 +7,13 @@
77
"disable": { "type": "boolean" },
88
"omit": { "type": "boolean" },
99
"remove": { "type": "boolean" },
10-
"fallbackLoader": { "type": ["string", "array", "object"] },
10+
"fallback": { "type": ["string", "array", "object"] },
1111
"filename": { "type": "string" },
12-
"loader": { "type": ["string", "array", "object"] },
13-
"publicPath": { "type": "string" }
12+
"use": { "type": ["string", "array", "object"] },
13+
"publicPath": { "type": "string" },
14+
15+
// deprecated
16+
"fallbackLoader": { "type": ["string", "array", "object"] },
17+
"loader": { "type": ["string", "array", "object"] }
1418
}
15-
}
19+
};

schema/plugin-schema.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"description": "",
1212
"type": "boolean"
1313
},
14-
"fallbackLoader": {
14+
"fallback": {
1515
"description": "A loader that webpack can fall back to if the original one fails.",
1616
"modes": {
1717
"type": "string",

test/cases/simple-query-object/webpack.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ module.exports = {
44
module: {
55
loaders: [
66
{ test: /\.css$/, use: ExtractTextPlugin.extract({
7-
fallbackLoader: "style-loader",
8-
loader: { loader: "css-loader", options: {
7+
fallback: "style-loader",
8+
use: { loader: "css-loader", options: {
99
sourceMap: true
1010
} }
1111
}) }

test/cases/simple-queryless-object/webpack.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ module.exports = {
44
module: {
55
loaders: [
66
{ test: /\.css$/, use: ExtractTextPlugin.extract({
7-
fallbackLoader: { loader: "style-loader" },
8-
loader: { loader: "css-loader", options: {
7+
fallback: { loader: "style-loader" },
8+
use: { loader: "css-loader", options: {
99
sourceMap: true
1010
} }
1111
}) }

test/extract.test.js

+38-18
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe("ExtractTextPlugin.extract()", function() {
1919

2020
it("does not throw if a correct config object is passed in", function() {
2121
should.doesNotThrow(function() {
22-
ExtractTextPlugin.extract({loader: 'css-loader'});
22+
ExtractTextPlugin.extract({use: 'css-loader'});
2323
});
2424
});
2525

@@ -66,22 +66,31 @@ describe("ExtractTextPlugin.extract()", function() {
6666
});
6767

6868
it("accepts a loader object", function() {
69-
ExtractTextPlugin.extract({ loader: "css-loader" }).should.deepEqual([
69+
ExtractTextPlugin.extract({ use: "css-loader" }).should.deepEqual([
7070
{ loader: loaderPath, options: { omit: 0, remove:true } },
7171
{ loader: "css-loader" }
7272
]);
7373
});
7474

7575
it("accepts a loader object with an options object", function() {
7676
ExtractTextPlugin.extract(
77-
{ loader: "css-loader", options: { modules: true } }
77+
{ use: "css-loader", options: { modules: true } }
7878
).should.deepEqual([
7979
{ loader: loaderPath, options: { omit: 0, remove:true } },
80-
{ loader: "css-loader", options: { modules: true } }
80+
{ use: "css-loader", options: { modules: true } }
8181
]);
8282
});
8383

8484
it("accepts a loader object with a (legacy) query object", function() {
85+
ExtractTextPlugin.extract(
86+
{ use: "css-loader", query: { modules: true } }
87+
).should.deepEqual([
88+
{ loader: loaderPath, options: { omit: 0, remove:true } },
89+
{ use: "css-loader", query: { modules: true } }
90+
]);
91+
});
92+
93+
it("accepts a loader object with a legacy loader field", function() {
8594
ExtractTextPlugin.extract(
8695
{ loader: "css-loader", query: { modules: true } }
8796
).should.deepEqual([
@@ -104,22 +113,33 @@ describe("ExtractTextPlugin.extract()", function() {
104113
});
105114
})
106115

107-
context("specifying fallbackLoader", function() {
108-
it("accepts a fallbackLoader string", function() {
116+
context("specifying fallback", function() {
117+
it("accepts a fallback string", function() {
118+
ExtractTextPlugin.extract({
119+
fallback: "style-loader",
120+
use: "css-loader"
121+
}).should.deepEqual([
122+
{ loader: loaderPath, options: { omit: 1, remove: true } },
123+
{ loader: "style-loader" },
124+
{ loader: "css-loader" }
125+
]);
126+
});
127+
128+
it("accepts a fallback string with legacy fallbackLoader option", function() {
109129
ExtractTextPlugin.extract({
110130
fallbackLoader: "style-loader",
111-
loader: "css-loader"
131+
use: "css-loader"
112132
}).should.deepEqual([
113133
{ loader: loaderPath, options: { omit: 1, remove: true } },
114134
{ loader: "style-loader" },
115135
{ loader: "css-loader" }
116136
]);
117137
});
118138

119-
it("accepts a chained fallbackLoader string", function() {
139+
it("accepts a chained fallback string", function() {
120140
ExtractTextPlugin.extract({
121-
fallbackLoader: "something-loader!style-loader",
122-
loader: "css-loader"
141+
fallback: "something-loader!style-loader",
142+
use: "css-loader"
123143
}).should.deepEqual([
124144
{ loader: loaderPath, options: { omit: 2, remove: true } },
125145
{ loader: "something-loader" },
@@ -128,24 +148,24 @@ describe("ExtractTextPlugin.extract()", function() {
128148
]);
129149
});
130150

131-
it("accepts a fallbackLoader object", function() {
151+
it("accepts a fallback object", function() {
132152
ExtractTextPlugin.extract({
133-
fallbackLoader: { loader: "style-loader" },
134-
loader: "css-loader"
153+
fallback: { loader: "style-loader" },
154+
use: "css-loader"
135155
}).should.deepEqual([
136156
{ loader: loaderPath, options: { omit: 1, remove: true } },
137157
{ loader: "style-loader" },
138158
{ loader: "css-loader" }
139159
]);
140160
});
141161

142-
it("accepts an array of fallbackLoader objects", function() {
162+
it("accepts an array of fallback objects", function() {
143163
ExtractTextPlugin.extract({
144-
fallbackLoader: [
164+
fallback: [
145165
{ loader: "something-loader" },
146166
{ loader: "style-loader" }
147167
],
148-
loader: "css-loader"
168+
use: "css-loader"
149169
}).should.deepEqual([
150170
{ loader: loaderPath, options: { omit: 2, remove: true } },
151171
{ loader: "something-loader" },
@@ -157,8 +177,8 @@ describe("ExtractTextPlugin.extract()", function() {
157177

158178
it("passes additional options to its own loader", function() {
159179
ExtractTextPlugin.extract({
160-
fallbackLoader: "style-loader",
161-
loader: "css-loader",
180+
fallback: "style-loader",
181+
use: "css-loader",
162182
publicPath: "/test"
163183
}).should.deepEqual([
164184
{ loader: loaderPath, options: { omit: 1, remove: true, publicPath: "/test" } },

0 commit comments

Comments
 (0)