Skip to content

Commit f28df7f

Browse files
committed
[OUTPUT] The plugin can be configured to inline the worker code or emit a file with the worker’s content
1 parent 2a19a19 commit f28df7f

File tree

3 files changed

+94
-14
lines changed

3 files changed

+94
-14
lines changed

src/createWorkerFactory.js renamed to src/WorkerLoaderHelper.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export function createWorkerFactory(fn, sourcemap = null) {
1+
export function createInlineWorkerFactory(fn, sourcemap = null) {
22
const source = fn.toString();
33
const start = source.indexOf('\n', 10) + 1;
44
const end = source.indexOf('}', source.length - 1);
@@ -10,3 +10,9 @@ export function createWorkerFactory(fn, sourcemap = null) {
1010
return new Worker(url, options);
1111
};
1212
}
13+
14+
export function createURLWorkerFactory(url) {
15+
return function WorkerFactory(options) {
16+
return new Worker(url, options);
17+
}
18+
}

src/index.js

Lines changed: 75 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@ const bannedPluginNames = [
88

99
module.exports = function workerLoaderPlugin(config = null) {
1010
const sourcemap = (config && config.sourcemap) || false;
11+
const loadPath = config && config.hasOwnProperty('loadPath') ? config.loadPath : '';
12+
let inline = config && config.hasOwnProperty('inline') ? config.inline : true;
1113

1214
const idMap = new Map();
1315
const exclude = new Map();
1416
let projectOptions = null;
1517
let basePath = null;
18+
let configuredFileName = null;
1619

1720
return {
18-
name: 'worker-loader',
21+
name: 'web-worker-loader',
1922

2023
options(options ) {
2124
if (!projectOptions) {
@@ -37,7 +40,7 @@ module.exports = function workerLoaderPlugin(config = null) {
3740

3841
resolveId(importee, importer) {
3942
if (importee === 'rollup-plugin-web-worker-loader-helper') {
40-
return path.resolve(__dirname, 'createWorkerFactory.js');
43+
return path.resolve(__dirname, 'WorkerLoaderHelper.js');
4144
} else if (importee.indexOf('web-worker:') === 0) {
4245
const name = importee.split(':')[1];
4346
const folder = path.dirname(importer);
@@ -46,9 +49,15 @@ module.exports = function workerLoaderPlugin(config = null) {
4649

4750
const target = require.resolve(name, { paths });
4851
if (target && !idMap.has(importee)) {
49-
idMap.set(target, Object.assign({}, projectOptions, {
52+
const inputOptions = Object.assign({}, projectOptions, {
5053
input: target,
51-
}));
54+
});
55+
56+
idMap.set(target, {
57+
workerID: `web-worker-${idMap.size}.js`,
58+
chunk: null,
59+
inputOptions,
60+
});
5261

5362
return target;
5463
}
@@ -59,7 +68,31 @@ module.exports = function workerLoaderPlugin(config = null) {
5968
load(id) {
6069
return new Promise((resolve, reject) => {
6170
if (idMap.has(id) && !exclude.has(id)) {
62-
const inputOptions = idMap.get(id);
71+
if (!inline) {
72+
/* inline requires rollup version 1.9.2 or higher */
73+
const version = this.meta.rollupVersion.split('.');
74+
if (version.length !== 3) {
75+
this.warn('Unknown rollup version');
76+
inline = true;
77+
} else {
78+
const major = parseInt(version[0], 10);
79+
const minor = parseInt(version[1], 10);
80+
const patch = parseInt(version[2], 10);
81+
if (
82+
isNaN(major) ||
83+
isNaN(minor) ||
84+
isNaN(patch) ||
85+
major < 1 ||
86+
minor < 9 ||
87+
patch < 2
88+
) {
89+
this.warn(`Rollup version 1.9.2 or higher is required for emitting a worker file (current version:${this.meta.rollupVersion}). See https://github.com/rollup/rollup/issues/2801`);
90+
inline = true;
91+
}
92+
}
93+
}
94+
95+
const {inputOptions, workerID} = idMap.get(id);
6396
exclude.set(id, true);
6497
rollup.rollup(inputOptions).then(bundle => {
6598
exclude.delete(id);
@@ -79,12 +112,20 @@ module.exports = function workerLoaderPlugin(config = null) {
79112
this.addWatchFile(dep);
80113
}
81114

82-
let source = utils.extractSource(chunk.code, chunk.exports);
83115
let map = null;
84-
if (sourcemap) {
85-
map = utils.fixMapSources(chunk, basePath);
116+
let source;
117+
if (inline) {
118+
source = utils.extractSource(chunk.code, chunk.exports);
119+
map = null;
120+
if (sourcemap) {
121+
map = utils.fixMapSources(chunk, basePath);
122+
}
123+
} else {
124+
source = path.join(loadPath, workerID);
125+
chunk.fileName = workerID;
126+
idMap.get(id).chunk = chunk;
86127
}
87-
resolve({code: utils.buildWorkerCode(source, map)});
128+
resolve({code: utils.buildWorkerCode(source, map, inline)});
88129
} else {
89130
resolve(null);
90131
}
@@ -101,10 +142,34 @@ module.exports = function workerLoaderPlugin(config = null) {
101142

102143
transform(code, id) {
103144
if (idMap.has(id) && !exclude.has(id)) {
104-
const inputOptions = idMap.get(id);
145+
const {inputOptions} = idMap.get(id);
105146
return { code, map: `{"version":3,"file":"${path.basename(inputOptions.input)}","sources":[],"sourcesContent":[],"names":[],"mappings":""}` };
106147
}
107148
return null;
108149
},
150+
151+
outputOptions(options) {
152+
if (!inline && options.file && !options.dir) {
153+
configuredFileName = path.basename(options.file);
154+
return Object.assign({}, options, {
155+
file: null,
156+
dir: path.dirname(options.file),
157+
});
158+
}
159+
return null;
160+
},
161+
162+
generateBundle(options, bundle, isWrite) {
163+
if (!inline && isWrite) {
164+
if (configuredFileName && Object.keys(bundle).length === 1) {
165+
bundle[Object.keys(bundle)[0]].fileName = configuredFileName;
166+
}
167+
for (const worker of idMap) {
168+
if (worker[1].chunk && !bundle[worker[1].workerID]) {
169+
bundle[worker[1].workerID] = worker[1].chunk;
170+
}
171+
}
172+
}
173+
},
109174
};
110175
};

src/utils.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,20 @@ function extractSource(code, exports) {
2727
return `/* rollup-plugin-web-worker-loader */function () {\n${source}}\n`;
2828
}
2929

30-
function buildWorkerCode(code, sourcemap = null) {
30+
function buildWorkerCode(source, sourcemap = null, inline = true) {
31+
if (inline) {
32+
return `\
33+
/* eslint-disable */\n\
34+
import {createInlineWorkerFactory} from 'rollup-plugin-web-worker-loader-helper';\n\
35+
const WorkerFactory = createInlineWorkerFactory(${source.substring(0, source.length - 1)}, ${sourcemap ? `'${sourcemap.toUrl()}'` : 'null'});\n\
36+
export default WorkerFactory;\n\
37+
/* eslint-enable */\n`;
38+
}
39+
3140
return `\
3241
/* eslint-disable */\n\
33-
import {createWorkerFactory} from 'rollup-plugin-web-worker-loader-helper';\n\
34-
const WorkerFactory = createWorkerFactory(${code.substring(0, code.length - 1)}, ${sourcemap ? `'${sourcemap.toUrl()}'` : 'null'});\n\
42+
import {createURLWorkerFactory} from 'rollup-plugin-web-worker-loader-helper';\n\
43+
const WorkerFactory = createURLWorkerFactory('${source}');\n\
3544
export default WorkerFactory;\n\
3645
/* eslint-enable */\n`;
3746
}

0 commit comments

Comments
 (0)