rollup-plugin-web-worker-loader
Advanced tools
Comparing version 0.1.1 to 0.1.2
{ | ||
"name": "rollup-plugin-web-worker-loader", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Rollup plugin to bundle web worker code to later be instantiated as a Worker", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -6,3 +6,4 @@ export function createWorkerFactory(fn, sourcemap = null) { | ||
const body = source.substring(start, end) + (sourcemap ? `//# sourceMappingURL=${sourcemap}` : ''); | ||
const blob = new Blob([ body ], { type: 'application/javascript' }); | ||
const lines = body.split('\n').map(line => line.substring(4) + '\n'); | ||
const blob = new Blob(lines, { type: 'application/javascript' }); | ||
const url = URL.createObjectURL(blob); | ||
@@ -9,0 +10,0 @@ return function WorkerFactory(options) { |
@@ -15,2 +15,3 @@ const path = require('path'); | ||
let projectOptions = null; | ||
let basePath = null; | ||
@@ -21,11 +22,14 @@ return { | ||
options: options => { | ||
projectOptions = Object.assign({}, options); | ||
if (options.plugins && options.plugins.length) { | ||
const plugins = []; | ||
options.plugins.forEach(plugin => { | ||
if (bannedPluginNames.indexOf(plugin.name) === -1) { | ||
plugins.push(plugin); | ||
} | ||
}); | ||
projectOptions.plugins = plugins; | ||
if (!projectOptions) { | ||
projectOptions = Object.assign({}, options); | ||
if (options.plugins && options.plugins.length) { | ||
const plugins = []; | ||
options.plugins.forEach(plugin => { | ||
if (bannedPluginNames.indexOf(plugin.name) === -1) { | ||
plugins.push(plugin); | ||
} | ||
}); | ||
projectOptions.plugins = plugins; | ||
basePath = path.dirname(options.input); | ||
} | ||
} | ||
@@ -62,2 +66,3 @@ | ||
rollup.rollup(inputOptions).then(bundle => { | ||
exclude.delete(id); | ||
bundle.generate({ format: 'es', name: id, sourcemap: true }).then( result => { | ||
@@ -72,6 +77,9 @@ const output = result.output; | ||
} | ||
exclude.delete(id); | ||
if (chunk !== null) { | ||
let source = utils.extractSource(chunk.code, chunk.exports); | ||
resolve({ code: utils.buildWorkerCode(source, sourcemap ? chunk.map : null) }); | ||
let map = null; | ||
if (sourcemap) { | ||
map = utils.fixMapSources(chunk, basePath); | ||
} | ||
resolve({ code: utils.buildWorkerCode(source, map) }); | ||
} else { | ||
@@ -81,3 +89,3 @@ resolve(null); | ||
}).catch(reject); | ||
}).catch(reject); | ||
}).catch(reason => { exclude.delete(id); reject(reason); }); | ||
} else { | ||
@@ -89,3 +97,3 @@ resolve(null); | ||
transform: (code, id) => { | ||
if (idMap.has(id)) { | ||
if (idMap.has(id) && !exclude.has(id)) { | ||
const inputOptions = idMap.get(id); | ||
@@ -92,0 +100,0 @@ return { code, map: `{"version":3,"file":"${path.basename(inputOptions.input)}","sources":[],"sourcesContent":[],"names":[],"mappings":""}` }; |
@@ -0,1 +1,3 @@ | ||
const path = require('path'); | ||
function extractSource(code, exports) { | ||
@@ -37,5 +39,60 @@ let source; | ||
function findFullPath(file, length, modules) { | ||
const keys = Object.keys(modules); | ||
for (let i = 0; i < keys.length; ++i) { | ||
if (keys[i].endsWith(file) && modules[keys[i]].originalLength === length) { | ||
return keys[i]; | ||
} | ||
} | ||
return null; | ||
} | ||
function fixMapSources(chunk, basePath) { | ||
const map = chunk.map; | ||
const newSourcesComponents = []; | ||
let maxUpFolder = 0; | ||
for (let i = 0; i < map.sources.length; ++i) { | ||
const full = findFullPath(map.sources[i], map.sourcesContent[i].length, chunk.modules); | ||
if (full) { | ||
const relative = path.relative(basePath, full); | ||
const base = path.dirname(relative); | ||
const components = base.split(path.sep); | ||
const newComponents = []; | ||
let upFolder = 0; | ||
for (let component of components) { | ||
if (component === '..') { | ||
++upFolder; | ||
} else { | ||
newComponents.push(component); | ||
} | ||
} | ||
newComponents.push(path.basename(full)); | ||
maxUpFolder = Math.max(maxUpFolder, upFolder); | ||
newSourcesComponents[i] = newComponents; | ||
} else { | ||
newSourcesComponents[i] = null; | ||
} | ||
} | ||
const basePathComponents = basePath.split(path.sep); | ||
const newBaseComponents = []; | ||
for (let i = 0; i < maxUpFolder; ++i) { | ||
newBaseComponents.unshift(basePathComponents.pop()); | ||
} | ||
const newBase = path.resolve('/web-worker', ...newBaseComponents); | ||
for (let i = 0; i < map.sources.length; ++i) { | ||
if (newSourcesComponents[i]) { | ||
map.sources[i] = 'worker:/' + path.resolve(newBase, ...newSourcesComponents[i]); | ||
} | ||
} | ||
return map; | ||
} | ||
module.exports = { | ||
extractSource, | ||
buildWorkerCode, | ||
fixMapSources, | ||
}; |
29599
186