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

Commit f91e7bf

Browse files
committedFeb 21, 2017
Fix indentation
1 parent a379ec0 commit f91e7bf

File tree

5 files changed

+124
-113
lines changed

5 files changed

+124
-113
lines changed
 

‎.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test/expected

‎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

+48-48
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,52 @@ var path = require("path");
55
var loaderUtils = require("loader-utils");
66
module.exports = function() {};
77
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-
});
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+
});
5656
};

‎package.json

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@
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+
}
815
},
916
"peerDependencies": {
1017
"webpack": ">=0.9 <2 || ^2.1.0-beta || ^2.2.0"
@@ -14,6 +21,9 @@
1421
},
1522
"devDependencies": {
1623
"del": "^2.2.2",
24+
"eslint": "^3.16.0",
25+
"eslint-config-webpack": "^1.0.0",
26+
"eslint-plugin-import": "^2.2.0",
1727
"mocha": "^3.2.0",
1828
"webpack": "^2.2.1"
1929
},

‎test/index.js

+45-45
Original file line numberDiff line numberDiff line change
@@ -8,54 +8,54 @@ process.chdir(__dirname);
88
const readFile = file => fs.readFileSync(file, 'utf-8');
99

1010
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-
});
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+
});
2323
};
2424

2525
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-
});
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+
});
3737

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-
});
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+
});
5050

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-
});
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+
});
6161
});

0 commit comments

Comments
 (0)
This repository has been archived.