Skip to content
This repository was archived by the owner on Sep 9, 2021. It is now read-only.

Commit 614a241

Browse files
authoredFeb 22, 2017
Merge pull request #51 from webpack-contrib/options
Options
2 parents ec42ef4 + fc0a874 commit 614a241

File tree

13 files changed

+75
-15
lines changed

13 files changed

+75
-15
lines changed
 

‎index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports.pitch = function pitch(request) {
1111
if (!this.webpack) throw new Error('Only usable with webpack');
1212
this.cacheable(false);
1313
const callback = this.async();
14-
const query = loaderUtils.parseQuery(this.query);
14+
const query = loaderUtils.getOptions(this) || {};
1515
const filename = loaderUtils.interpolateName(this, query.name || '[hash].worker.js', {
1616
context: query.context || this.options.context,
1717
regExp: query.regExp,

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"webpack": ">=0.9 <2 || ^2.1.0-beta || ^2.2.0"
3232
},
3333
"dependencies": {
34-
"loader-utils": "0.2.x"
34+
"loader-utils": "^1.0.2"
3535
},
3636
"devDependencies": {
3737
"del": "^2.2.2",

‎test/fixtures/inline-options/entry.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const w1 = require('./w1.js');
2+
const w2 = require('./w2.js');

‎test/fixtures/inline-options/w1.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// w1 inlined via options

‎test/fixtures/inline-options/w2.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// w2 inlined via options
File renamed without changes.
File renamed without changes.

‎test/fixtures/name-options/entry.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const w1 = require('./w1.js');
2+
const s2 = require('./w2.js');

‎test/fixtures/name-options/w1.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// w1 via worker options

‎test/fixtures/name-options/w2.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// w2 via worker options
File renamed without changes.
File renamed without changes.

‎test/index.js

+65-13
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ process.chdir(__dirname);
99

1010
const readFile = file => fs.readFileSync(file, 'utf-8');
1111

12-
const makeBundle = name => del(`expected/${name}`).then(() => {
13-
const bundle = webpack({
12+
const makeBundle = (name, options) => del(`expected/${name}`).then(() => {
13+
const config = Object.assign({
1414
entry: `./fixtures/${name}/entry.js`,
1515
output: {
1616
path: `expected/${name}`,
1717
filename: 'bundle.js',
1818
},
19-
});
19+
}, options);
20+
const bundle = webpack(config);
2021
return new Promise((resolve, reject) => {
2122
bundle.run((err, stats) => {
2223
if (err) {
@@ -41,27 +42,78 @@ describe('worker-loader', () => {
4142
})
4243
);
4344

44-
it('should create chunk with specified name', () =>
45-
makeBundle('name').then((stats) => {
46-
const workerFile = 'expected/name/namedWorker.js';
47-
const receivedWorkerFile = stats.toJson('minimal').children
45+
it('should create chunk with specified name in query', () =>
46+
makeBundle('name-query').then((stats) => {
47+
const file = stats.toJson('minimal').children
48+
.map(item => item.chunks)
49+
.reduce((acc, item) => acc.concat(item), [])
50+
.map(item => item.files)
51+
.map(item => `expected/name-query/${item}`)[0];
52+
assert.equal(file, 'expected/name-query/namedWorker.js');
53+
assert.notEqual(readFile(file).indexOf('// named worker test mark'), -1);
54+
})
55+
);
56+
57+
it('should create named chunks with workers via options', () =>
58+
makeBundle('name-options', {
59+
module: {
60+
rules: [
61+
{
62+
test: /(w1|w2)\.js$/,
63+
loader: '../index.js',
64+
options: {
65+
name: '[name].js',
66+
},
67+
},
68+
],
69+
},
70+
}).then((stats) => {
71+
const files = stats.toJson('minimal').children
4872
.map(item => item.chunks)
4973
.reduce((acc, item) => acc.concat(item), [])
5074
.map(item => item.files)
51-
.map(item => `expected/name/${item}`)[0];
52-
assert.equal(receivedWorkerFile, workerFile);
53-
assert.notEqual(readFile(workerFile).indexOf('// named worker test mark'), -1);
75+
.map(item => `expected/name-options/${item}`);
76+
const w1 = files.find(file => file === 'expected/name-options/w1.js');
77+
const w2 = files.find(file => file === 'expected/name-options/w2.js');
78+
assert(w1);
79+
assert(w2);
80+
assert.notEqual(readFile(w1).indexOf('// w1 via worker options'), -1);
81+
assert.notEqual(readFile(w2).indexOf('// w2 via worker options'), -1);
5482
})
5583
);
5684

57-
it('should inline worker with inline option', () =>
58-
makeBundle('inline').then((stats) => {
85+
it('should inline worker with inline option in query', () =>
86+
makeBundle('inline-query').then((stats) => {
5987
const bundleFile = stats.toJson('minimal').chunks
6088
.map(item => item.files)
6189
.reduce((acc, item) => acc.concat(item), [])
62-
.map(item => `expected/inline/${item}`)[0];
90+
.map(item => `expected/inline-query/${item}`)[0];
6391
assert(bundleFile);
6492
assert.notEqual(readFile(bundleFile).indexOf('// inlined worker test mark'), -1);
6593
})
6694
);
95+
96+
it('should inline worker with inline in options', () =>
97+
makeBundle('inline-options', {
98+
module: {
99+
rules: [
100+
{
101+
test: /(w1|w2)\.js$/,
102+
loader: '../index.js',
103+
options: {
104+
inline: true,
105+
},
106+
},
107+
],
108+
},
109+
}).then((stats) => {
110+
const bundleFile = stats.toJson('minimal').chunks
111+
.map(item => item.files)
112+
.reduce((acc, item) => acc.concat(item), [])
113+
.map(item => `expected/inline-options/${item}`)[0];
114+
assert(bundleFile);
115+
assert.notEqual(readFile(bundleFile).indexOf('// w1 inlined via options'), -1);
116+
assert.notEqual(readFile(bundleFile).indexOf('// w2 inlined via options'), -1);
117+
})
118+
);
67119
});

0 commit comments

Comments
 (0)
This repository has been archived.