Skip to content

Commit

Permalink
Create new bundles from importScripts in workers (parcel-bundler#3828)
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Binns-Smith authored Nov 22, 2019
1 parent 79bc705 commit 2bb30e7
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
importScripts('https://unpkg.com/parcel');
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
importScripts('imported.js');
importScripts('imported.js', 'imported2.js');
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
new Worker('./external')
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
navigator.serviceWorker.register('importScripts.js');
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
new Worker('importScripts.js');
75 changes: 75 additions & 0 deletions packages/core/integration-tests/test/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,81 @@ describe('javascript', function() {
]);
});

for (let workerType of ['webworker', 'serviceworker']) {
it(`should split bundles when ${workerType}s use importScripts`, async function() {
let b = await bundle(
path.join(
__dirname,
`/integration/worker-import-scripts/index-${workerType}.js`
)
);

assertBundles(b, [
{
name: `index-${workerType}.js`,
assets: [`index-${workerType}.js`]
},
{
assets: ['importScripts.js']
},
{
assets: ['imported.js']
},
{
assets: ['imported2.js']
}
]);

let workerBundleFile = path.join(
distDir,
(await outputFS.readdir(distDir)).find(file =>
file.startsWith('importScripts')
)
);
let workerBundleContents = await outputFS.readFile(
workerBundleFile,
'utf8'
);

assert(
workerBundleContents.match(
/importScripts\("\/imported\.[0-9a-f]*\.js"\);\nimportScripts\("\/imported\.[0-9a-f]*\.js", "\/imported2\.[0-9a-f]*\.js"\);/
)
);
});
}

it('should not create bundles of external scripts referenced by importScripts', async function() {
let b = await bundle(
path.join(
__dirname,
'/integration/worker-import-scripts/index-external.js'
)
);

assertBundles(b, [
{name: 'index-external.js', assets: ['index-external.js']},
{assets: ['external.js']}
]);

let workerBundleFile = path.join(
distDir,
(await outputFS.readdir(distDir)).find(file =>
file.startsWith('external')
)
);
let workerBundleContents = await outputFS.readFile(
workerBundleFile,
'utf8'
);

assert(
workerBundleContents.includes(
'importScripts("https://unpkg.com/parcel");'
)
);
});

it('should support bundling service-workers', async function() {
let b = await bundle(
path.join(__dirname, '/integration/service-worker/a/index.js')
Expand Down
16 changes: 15 additions & 1 deletion packages/transformers/js/src/visitors/dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export default ({
return;
}

const isRegisterServiceWorker =
let isRegisterServiceWorker =
types.isStringLiteral(args[0]) &&
types.matchesPattern(callee, serviceWorkerPattern) &&
!hasBinding(ancestors, 'navigator') &&
Expand All @@ -84,6 +84,20 @@ export default ({
});
return;
}

let isImportScripts =
(asset.env.context === 'web-worker' ||
asset.env.context === 'service-worker') &&
callee.name === 'importScripts';

if (isImportScripts) {
for (let arg of args) {
if (types.isStringLiteral(arg)) {
addURLDependency(asset, arg);
}
}
return;
}
},

NewExpression(node, {asset, options}, ancestors) {
Expand Down

0 comments on commit 2bb30e7

Please sign in to comment.