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

Commit ec42ef4

Browse files
authoredFeb 22, 2017
Merge pull request #50 from webpack-contrib/lint
Lint
2 parents a379ec0 + 200f428 commit ec42ef4

File tree

8 files changed

+161
-124
lines changed

8 files changed

+161
-124
lines changed
 

‎.eslintignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test/fixtures
2+
test/expected
3+
createInlineWorker.js

‎createInlineWorker.js

+19-19
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@
22

33
var URL = window.URL || window.webkitURL;
44
module.exports = function(content, url) {
5-
try {
6-
try {
7-
var blob;
8-
try { // BlobBuilder = Deprecated, but widely implemented
9-
var BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder;
10-
blob = new BlobBuilder();
11-
blob.append(content);
12-
blob = blob.getBlob();
13-
} catch(e) { // The proposed API
14-
blob = new Blob([content]);
15-
}
16-
return new Worker(URL.createObjectURL(blob));
17-
} catch(e) {
18-
return new Worker('data:application/javascript,' + encodeURIComponent(content));
19-
}
20-
} catch(e) {
21-
return new Worker(url);
22-
}
23-
}
5+
try {
6+
try {
7+
var blob;
8+
try { // BlobBuilder = Deprecated, but widely implemented
9+
var BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder;
10+
blob = new BlobBuilder();
11+
blob.append(content);
12+
blob = blob.getBlob();
13+
} catch(e) { // The proposed API
14+
blob = new Blob([content]);
15+
}
16+
return new Worker(URL.createObjectURL(blob));
17+
} catch(e) {
18+
return new Worker('data:application/javascript,' + encodeURIComponent(content));
19+
}
20+
} catch(e) {
21+
return new Worker(url);
22+
}
23+
}

‎index.js

+58-54
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,60 @@
1-
var WebWorkerTemplatePlugin = require("webpack/lib/webworker/WebWorkerTemplatePlugin");
2-
var SingleEntryPlugin = require("webpack/lib/SingleEntryPlugin");
3-
var path = require("path");
1+
'use strict';
42

5-
var loaderUtils = require("loader-utils");
6-
module.exports = function() {};
7-
module.exports.pitch = function(request) {
8-
if(!this.webpack) throw new Error("Only usable with webpack");
9-
this.cacheable(false);
10-
var callback = this.async();
11-
var query = loaderUtils.parseQuery(this.query);
12-
var filename = loaderUtils.interpolateName(this, query.name || "[hash].worker.js", {
13-
context: query.context || this.options.context,
14-
regExp: query.regExp
15-
});
16-
var outputOptions = {
17-
filename: filename,
18-
chunkFilename: "[id]." + filename,
19-
namedChunkFilename: null
20-
};
21-
if(this.options && this.options.worker && this.options.worker.output) {
22-
for(var name in this.options.worker.output) {
23-
outputOptions[name] = this.options.worker.output[name];
24-
}
25-
}
26-
var workerCompiler = this._compilation.createChildCompiler("worker", outputOptions);
27-
workerCompiler.apply(new WebWorkerTemplatePlugin(outputOptions));
28-
workerCompiler.apply(new SingleEntryPlugin(this.context, "!!" + request, "main"));
29-
if(this.options && this.options.worker && this.options.worker.plugins) {
30-
this.options.worker.plugins.forEach(function(plugin) {
31-
workerCompiler.apply(plugin);
32-
});
33-
}
34-
var subCache = "subcache " + __dirname + " " + request;
35-
workerCompiler.plugin("compilation", function(compilation) {
36-
if(compilation.cache) {
37-
if(!compilation.cache[subCache])
38-
compilation.cache[subCache] = {};
39-
compilation.cache = compilation.cache[subCache];
40-
}
41-
});
42-
workerCompiler.runAsChild(function(err, entries, compilation) {
43-
if(err) return callback(err);
44-
if (entries[0]) {
45-
var workerFile = entries[0].files[0];
46-
var constructor = "new Worker(__webpack_public_path__ + " + JSON.stringify(workerFile) + ")";
47-
if(query.inline) {
48-
constructor = "require(" + JSON.stringify("!!" + path.join(__dirname, "createInlineWorker.js")) + ")(" +
49-
JSON.stringify(compilation.assets[workerFile].source()) + ", __webpack_public_path__ + " + JSON.stringify(workerFile) + ")";
50-
}
51-
return callback(null, "module.exports = function() {\n\treturn " + constructor + ";\n};");
52-
} else {
53-
return callback(null, null);
54-
}
55-
});
3+
const path = require('path');
4+
const WebWorkerTemplatePlugin = require('webpack/lib/webworker/WebWorkerTemplatePlugin');
5+
const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin');
6+
const loaderUtils = require('loader-utils');
7+
8+
module.exports = function workerLoader() {};
9+
10+
module.exports.pitch = function pitch(request) {
11+
if (!this.webpack) throw new Error('Only usable with webpack');
12+
this.cacheable(false);
13+
const callback = this.async();
14+
const query = loaderUtils.parseQuery(this.query);
15+
const filename = loaderUtils.interpolateName(this, query.name || '[hash].worker.js', {
16+
context: query.context || this.options.context,
17+
regExp: query.regExp,
18+
});
19+
const outputOptions = {
20+
filename,
21+
chunkFilename: `[id].${filename}`,
22+
namedChunkFilename: null,
23+
};
24+
if (this.options && this.options.worker && this.options.worker.output) {
25+
Object.keys(this.options.worker.output).forEach((name) => {
26+
outputOptions[name] = this.options.worker.output[name];
27+
});
28+
}
29+
const workerCompiler = this._compilation.createChildCompiler('worker', outputOptions);
30+
workerCompiler.apply(new WebWorkerTemplatePlugin(outputOptions));
31+
workerCompiler.apply(new SingleEntryPlugin(this.context, `!!${request}`, 'main'));
32+
if (this.options && this.options.worker && this.options.worker.plugins) {
33+
this.options.worker.plugins.forEach(plugin => workerCompiler.apply(plugin));
34+
}
35+
const subCache = `subcache ${__dirname} ${request}`;
36+
workerCompiler.plugin('compilation', (compilation) => {
37+
if (compilation.cache) {
38+
if (!compilation.cache[subCache]) {
39+
compilation.cache[subCache] = {};
40+
}
41+
compilation.cache = compilation.cache[subCache];
42+
}
43+
});
44+
workerCompiler.runAsChild((err, entries, compilation) => {
45+
if (err) return callback(err);
46+
if (entries[0]) {
47+
const workerFile = entries[0].files[0];
48+
let constructor = `new Worker(__webpack_public_path__ + ${JSON.stringify(workerFile)})`;
49+
if (query.inline) {
50+
constructor = `require(${JSON.stringify(`!!${path.join(__dirname, 'createInlineWorker.js')}`)})(${
51+
JSON.stringify(compilation.assets[workerFile].source())
52+
}, __webpack_public_path__ + ${
53+
JSON.stringify(workerFile)
54+
})`;
55+
}
56+
return callback(null, `module.exports = function() {\n\treturn ${constructor};\n};`);
57+
}
58+
return callback(null, null);
59+
});
5660
};

‎package.json

+25-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,28 @@
44
"author": "Tobias Koppers @sokra",
55
"description": "worker loader module for webpack",
66
"scripts": {
7-
"test": "mocha"
7+
"test": "mocha",
8+
"posttest": "eslint ."
9+
},
10+
"eslintConfig": {
11+
"extends": "webpack",
12+
"rules": {
13+
"linebreak-style": 0,
14+
"comma-dangle": [
15+
"error",
16+
{
17+
"arrays": "always-multiline",
18+
"objects": "always-multiline",
19+
"imports": "always-multiline",
20+
"exports": "always-multiline",
21+
"functions": "never"
22+
}
23+
],
24+
"no-underscore-dangle": 0,
25+
"no-param-reassign": 0,
26+
"prefer-destructuring": 0,
27+
"strict": 0
28+
}
829
},
930
"peerDependencies": {
1031
"webpack": ">=0.9 <2 || ^2.1.0-beta || ^2.2.0"
@@ -14,6 +35,9 @@
1435
},
1536
"devDependencies": {
1637
"del": "^2.2.2",
38+
"eslint": "^3.16.0",
39+
"eslint-config-webpack": "^1.0.0",
40+
"eslint-plugin-import": "^2.2.0",
1741
"mocha": "^3.2.0",
1842
"webpack": "^2.2.1"
1943
},

‎test/fixtures/inline/entry.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
import Worker from '../../../index.js?inline!./worker.js';
1+
const Worker = require('../../../index.js?inline!./worker.js');

‎test/fixtures/name/entry.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
import Worker from '../../../index.js?name=namedWorker.js!./worker.js';
1+
const Worker = require('../../../index.js?name=namedWorker.js!./worker.js');

‎test/fixtures/worker/entry.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
import Worker from '../../../index.js!./worker.js';
1+
const Worker = require('../../../index.js!./worker.js');

‎test/index.js

+53-47
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict';
2+
13
const assert = require('assert');
24
const fs = require('fs');
35
const del = require('del');
@@ -7,55 +9,59 @@ process.chdir(__dirname);
79

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

10-
const makeBundle = name => {
11-
return del(`expected/${name}`).then(() => {
12-
const bundle = webpack({
13-
entry: `./fixtures/${name}/entry.js`,
14-
output: {
15-
path: `expected/${name}`,
16-
filename: 'bundle.js'
17-
}
18-
});
19-
return new Promise((resolve, reject) => {
20-
bundle.run((err, stats) => err ? reject(err) : resolve(stats))
21-
});
22-
});
23-
};
12+
const makeBundle = name => del(`expected/${name}`).then(() => {
13+
const bundle = webpack({
14+
entry: `./fixtures/${name}/entry.js`,
15+
output: {
16+
path: `expected/${name}`,
17+
filename: 'bundle.js',
18+
},
19+
});
20+
return new Promise((resolve, reject) => {
21+
bundle.run((err, stats) => {
22+
if (err) {
23+
reject(err);
24+
} else {
25+
resolve(stats);
26+
}
27+
});
28+
});
29+
});
2430

2531
describe('worker-loader', () => {
26-
it('should create chunk with worker', () => {
27-
return makeBundle('worker').then(stats => {
28-
const workerFile = stats.toJson('minimal').children
29-
.map(item => item.chunks)
30-
.reduce((acc, item) => acc.concat(item), [])
31-
.map(item => item.files)
32-
.map(item => 'expected/worker/' + item)[0];
33-
assert(workerFile);
34-
assert.notEqual(readFile(workerFile).indexOf('// worker test mark'), -1);
35-
});
36-
});
32+
it('should create chunk with worker', () =>
33+
makeBundle('worker').then((stats) => {
34+
const workerFile = stats.toJson('minimal').children
35+
.map(item => item.chunks)
36+
.reduce((acc, item) => acc.concat(item), [])
37+
.map(item => item.files)
38+
.map(item => `expected/worker/${item}`)[0];
39+
assert(workerFile);
40+
assert.notEqual(readFile(workerFile).indexOf('// worker test mark'), -1);
41+
})
42+
);
3743

38-
it('should create chunk with specified name', () => {
39-
return makeBundle('name').then(stats => {
40-
const workerFile = 'expected/name/namedWorker.js'
41-
const receivedWorkerFile = stats.toJson('minimal').children
42-
.map(item => item.chunks)
43-
.reduce((acc, item) => acc.concat(item), [])
44-
.map(item => item.files)
45-
.map(item => 'expected/name/' + item)[0];
46-
assert.equal(receivedWorkerFile, workerFile);
47-
assert.notEqual(readFile(workerFile).indexOf('// named worker test mark'), -1);
48-
});
49-
});
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
48+
.map(item => item.chunks)
49+
.reduce((acc, item) => acc.concat(item), [])
50+
.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);
54+
})
55+
);
5056

51-
it('should inline worker with inline option', () => {
52-
return makeBundle('inline').then(stats => {
53-
const bundleFile = stats.toJson('minimal').chunks
54-
.map(item => item.files)
55-
.reduce((acc, item) => acc.concat(item), [])
56-
.map(item => 'expected/inline/' + item)[0];
57-
assert(bundleFile);
58-
assert.notEqual(readFile(bundleFile).indexOf('// inlined worker test mark'), -1);
59-
});
60-
});
57+
it('should inline worker with inline option', () =>
58+
makeBundle('inline').then((stats) => {
59+
const bundleFile = stats.toJson('minimal').chunks
60+
.map(item => item.files)
61+
.reduce((acc, item) => acc.concat(item), [])
62+
.map(item => `expected/inline/${item}`)[0];
63+
assert(bundleFile);
64+
assert.notEqual(readFile(bundleFile).indexOf('// inlined worker test mark'), -1);
65+
})
66+
);
6167
});

0 commit comments

Comments
 (0)
This repository has been archived.