Skip to content

Commit

Permalink
Fix UMD import.meta.url inside web workers (#4186)
Browse files Browse the repository at this point in the history
  • Loading branch information
ceifa committed Jul 24, 2021
1 parent 959f040 commit 2a29db9
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 23 deletions.
20 changes: 12 additions & 8 deletions src/ast/nodes/MetaProperty.ts
Expand Up @@ -185,9 +185,11 @@ const accessedFileUrlGlobals = {

const getResolveUrl = (path: string, URL = 'URL') => `new ${URL}(${path}).href`;

const getRelativeUrlFromDocument = (relativePath: string) =>
const getRelativeUrlFromDocument = (relativePath: string, umd = false) =>
getResolveUrl(
`'${relativePath}', document.currentScript && document.currentScript.src || document.baseURI`
`'${relativePath}', ${
umd ? `typeof document === 'undefined' ? location.href : ` : ''
}document.currentScript && document.currentScript.src || document.baseURI`
);

const getGenericImportMetaMechanism =
Expand All @@ -200,8 +202,10 @@ const getGenericImportMetaMechanism =
: 'undefined';
};

const getUrlFromDocument = (chunkId: string) =>
`(document.currentScript && document.currentScript.src || new URL('${chunkId}', document.baseURI).href)`;
const getUrlFromDocument = (chunkId: string, umd = false) =>
`${
umd ? `typeof document === 'undefined' ? location.href : ` : ''
}(document.currentScript && document.currentScript.src || new URL('${chunkId}', document.baseURI).href)`;

const relativeUrlMechanisms: Record<InternalModuleFormat, (relativePath: string) => string> = {
amd: relativePath => {
Expand All @@ -217,10 +221,10 @@ const relativeUrlMechanisms: Record<InternalModuleFormat, (relativePath: string)
iife: relativePath => getRelativeUrlFromDocument(relativePath),
system: relativePath => getResolveUrl(`'${relativePath}', module.meta.url`),
umd: relativePath =>
`(typeof document === 'undefined' ? ${getResolveUrl(
`(typeof document === 'undefined' && typeof location === 'undefined' ? ${getResolveUrl(
`'file:' + __dirname + '/${relativePath}'`,
`(require('u' + 'rl').URL)`
)} : ${getRelativeUrlFromDocument(relativePath)})`
)} : ${getRelativeUrlFromDocument(relativePath, true)})`
};

const importMetaMechanisms: Record<string, (prop: string | null, chunkId: string) => string> = {
Expand All @@ -236,9 +240,9 @@ const importMetaMechanisms: Record<string, (prop: string | null, chunkId: string
system: prop => (prop === null ? `module.meta` : `module.meta.${prop}`),
umd: getGenericImportMetaMechanism(
chunkId =>
`(typeof document === 'undefined' ? ${getResolveUrl(
`(typeof document === 'undefined' && typeof location === 'undefined' ? ${getResolveUrl(
`'file:' + __filename`,
`(require('u' + 'rl').URL)`
)} : ${getUrlFromDocument(chunkId)})`
)} : ${getUrlFromDocument(chunkId, true)})`
)
};
2 changes: 1 addition & 1 deletion test/form/samples/configure-file-url/_expected/umd.js
Expand Up @@ -7,7 +7,7 @@

var asset2 = 'resolved';

var asset3 = (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __dirname + '/assets/asset-unresolved-8dcd7fca.txt').href : new URL('assets/asset-unresolved-8dcd7fca.txt', document.currentScript && document.currentScript.src || document.baseURI).href);
var asset3 = (typeof document === 'undefined' && typeof location === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __dirname + '/assets/asset-unresolved-8dcd7fca.txt').href : new URL('assets/asset-unresolved-8dcd7fca.txt', typeof document === 'undefined' ? location.href : document.currentScript && document.currentScript.src || document.baseURI).href);

console.log(asset1, asset2, asset3);

Expand Down
Expand Up @@ -21,7 +21,7 @@

import('external').then(console.log);
exports['default'] = 0;
console.log((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('umd.js', document.baseURI).href)));
console.log((typeof document === 'undefined' && typeof location === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : typeof document === 'undefined' ? location.href : (document.currentScript && document.currentScript.src || new URL('umd.js', document.baseURI).href)));

function nested1() {
const _interopDefault = 1;
Expand All @@ -35,7 +35,7 @@

import('external').then(console.log);
exports['default'] = 1;
console.log((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('umd.js', document.baseURI).href)));
console.log((typeof document === 'undefined' && typeof location === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : typeof document === 'undefined' ? location.href : (document.currentScript && document.currentScript.src || new URL('umd.js', document.baseURI).href)));
}

nested1();
Expand Down
Expand Up @@ -7,7 +7,7 @@

var asset2 = 'resolved';

var asset3 = (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __dirname + '/assets/asset-unresolved-8dcd7fca.txt').href : new URL('assets/asset-unresolved-8dcd7fca.txt', document.currentScript && document.currentScript.src || document.baseURI).href);
var asset3 = (typeof document === 'undefined' && typeof location === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __dirname + '/assets/asset-unresolved-8dcd7fca.txt').href : new URL('assets/asset-unresolved-8dcd7fca.txt', typeof document === 'undefined' ? location.href : document.currentScript && document.currentScript.src || document.baseURI).href);

console.log(asset1, asset2, asset3);

Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/deprecated/emit-asset/_expected/umd.js
Expand Up @@ -3,7 +3,7 @@
factory();
}((function () { 'use strict';

var logo = (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __dirname + '/assets/logo-25585ac1.svg').href : new URL('assets/logo-25585ac1.svg', document.currentScript && document.currentScript.src || document.baseURI).href);
var logo = (typeof document === 'undefined' && typeof location === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __dirname + '/assets/logo-25585ac1.svg').href : new URL('assets/logo-25585ac1.svg', typeof document === 'undefined' ? location.href : document.currentScript && document.currentScript.src || document.baseURI).href);

function showImage(url) {
console.log(url);
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/emit-asset-file/_expected/umd.js
Expand Up @@ -3,7 +3,7 @@
factory();
}((function () { 'use strict';

var logo = (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __dirname + '/assets/logo-25585ac1.svg').href : new URL('assets/logo-25585ac1.svg', document.currentScript && document.currentScript.src || document.baseURI).href);
var logo = (typeof document === 'undefined' && typeof location === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __dirname + '/assets/logo-25585ac1.svg').href : new URL('assets/logo-25585ac1.svg', typeof document === 'undefined' ? location.href : document.currentScript && document.currentScript.src || document.baseURI).href);

function showImage(url) {
console.log(url);
Expand Down
10 changes: 5 additions & 5 deletions test/form/samples/emit-uint8array-no-buffer/_expected/umd.js
Expand Up @@ -3,15 +3,15 @@
factory();
}((function () { 'use strict';

var asset1a = (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __dirname + '/assets/asset-dc5cb674').href : new URL('assets/asset-dc5cb674', document.currentScript && document.currentScript.src || document.baseURI).href);
var asset1a = (typeof document === 'undefined' && typeof location === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __dirname + '/assets/asset-dc5cb674').href : new URL('assets/asset-dc5cb674', typeof document === 'undefined' ? location.href : document.currentScript && document.currentScript.src || document.baseURI).href);

var asset1b = (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __dirname + '/assets/asset-dc5cb674').href : new URL('assets/asset-dc5cb674', document.currentScript && document.currentScript.src || document.baseURI).href);
var asset1b = (typeof document === 'undefined' && typeof location === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __dirname + '/assets/asset-dc5cb674').href : new URL('assets/asset-dc5cb674', typeof document === 'undefined' ? location.href : document.currentScript && document.currentScript.src || document.baseURI).href);

var asset2a = (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __dirname + '/assets/asset-52cbd095').href : new URL('assets/asset-52cbd095', document.currentScript && document.currentScript.src || document.baseURI).href);
var asset2a = (typeof document === 'undefined' && typeof location === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __dirname + '/assets/asset-52cbd095').href : new URL('assets/asset-52cbd095', typeof document === 'undefined' ? location.href : document.currentScript && document.currentScript.src || document.baseURI).href);

var asset2b = (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __dirname + '/assets/asset-52cbd095').href : new URL('assets/asset-52cbd095', document.currentScript && document.currentScript.src || document.baseURI).href);
var asset2b = (typeof document === 'undefined' && typeof location === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __dirname + '/assets/asset-52cbd095').href : new URL('assets/asset-52cbd095', typeof document === 'undefined' ? location.href : document.currentScript && document.currentScript.src || document.baseURI).href);

var asset99a = (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __dirname + '/assets/asset-c568a840').href : new URL('assets/asset-c568a840', document.currentScript && document.currentScript.src || document.baseURI).href);
var asset99a = (typeof document === 'undefined' && typeof location === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __dirname + '/assets/asset-c568a840').href : new URL('assets/asset-c568a840', typeof document === 'undefined' ? location.href : document.currentScript && document.currentScript.src || document.baseURI).href);

console.log(asset1a, asset1b, asset2a, asset2b, asset99a);

Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/import-meta-url/_expected/umd.js
Expand Up @@ -11,6 +11,6 @@
}
}

log((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('umd.js', document.baseURI).href)));
log((typeof document === 'undefined' && typeof location === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : typeof document === 'undefined' ? location.href : (document.currentScript && document.currentScript.src || new URL('umd.js', document.baseURI).href)));

})));
2 changes: 1 addition & 1 deletion test/form/samples/import-meta/_expected/umd.js
Expand Up @@ -3,6 +3,6 @@
factory();
}((function () { 'use strict';

console.log(({ url: (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('umd.js', document.baseURI).href)) }));
console.log(({ url: (typeof document === 'undefined' && typeof location === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : typeof document === 'undefined' ? location.href : (document.currentScript && document.currentScript.src || new URL('umd.js', document.baseURI).href)) }));

})));
4 changes: 2 additions & 2 deletions test/form/samples/resolve-import-meta-url/_expected/umd.js
Expand Up @@ -7,9 +7,9 @@
console.log('resolved');
console.log('resolved');

console.log((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('umd.js', document.baseURI).href)));
console.log((typeof document === 'undefined' && typeof location === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : typeof document === 'undefined' ? location.href : (document.currentScript && document.currentScript.src || new URL('umd.js', document.baseURI).href)));
console.log(undefined);
console.log(({ url: (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('umd.js', document.baseURI).href)) }));
console.log(({ url: (typeof document === 'undefined' && typeof location === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : typeof document === 'undefined' ? location.href : (document.currentScript && document.currentScript.src || new URL('umd.js', document.baseURI).href)) }));

console.log('url=umd.js:resolve-import-meta-url/main.js');
console.log('privateProp=umd.js:resolve-import-meta-url/main.js');
Expand Down

0 comments on commit 2a29db9

Please sign in to comment.