Skip to content

Commit

Permalink
Improve SystemJS handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Apr 11, 2019
1 parent b602fc5 commit 13b0d25
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
44 changes: 28 additions & 16 deletions src/utils/defaultPlugin.ts
Expand Up @@ -18,9 +18,9 @@ export function getRollupDefaultPlugin(options: InputOptions): Plugin {
return relativeUrlMechanisms[format](relativeAssetPath);
},
resolveImportMeta(prop, { chunkId, format }) {
const mechanism = importMetaUrlMechanisms[format] && importMetaUrlMechanisms[format](chunkId);
const mechanism = importMetaMechanisms[format] && importMetaMechanisms[format](prop, chunkId);
if (mechanism) {
return prop === null ? `({ url: ${mechanism} })` : prop === 'url' ? mechanism : 'undefined';
return mechanism;
}
}
};
Expand Down Expand Up @@ -82,20 +82,32 @@ const getResolveUrl = (path: string, URL: string = 'URL') => `new ${URL}(${path}
const getUrlFromDocument = (chunkId: string) =>
`(document.currentScript && document.currentScript.src || new URL('${chunkId}', document.baseURI).href)`;

const importMetaUrlMechanisms: Record<string, (chunkId: string) => string> = {
amd: () => getResolveUrl(`module.uri, document.baseURI`),
cjs: chunkId =>
`(typeof document === 'undefined' ? ${getResolveUrl(
`'file:' + __filename`,
`(require('u' + 'rl').URL)`
)} : ${getUrlFromDocument(chunkId)})`,
iife: chunkId => getUrlFromDocument(chunkId),
system: () => `module.meta.url`,
umd: chunkId =>
`(typeof document === 'undefined' ? ${getResolveUrl(
`'file:' + __filename`,
`(require('u' + 'rl').URL)`
)} : ${getUrlFromDocument(chunkId)})`
const getGenericImportMetaMechanism = (getUrl: (chunkId: string) => string) => (
prop: string | null,
chunkId: string
) => {
const urlMechanism = getUrl(chunkId);
return prop === null ? `({ url: ${urlMechanism} })` : prop === 'url' ? urlMechanism : 'undefined';
};

const importMetaMechanisms: Record<string, (prop: string | null, chunkId: string) => string> = {
amd: getGenericImportMetaMechanism(() => getResolveUrl(`module.uri, document.baseURI`)),
cjs: getGenericImportMetaMechanism(
chunkId =>
`(typeof document === 'undefined' ? ${getResolveUrl(
`'file:' + __filename`,
`(require('u' + 'rl').URL)`
)} : ${getUrlFromDocument(chunkId)})`
),
iife: getGenericImportMetaMechanism(chunkId => getUrlFromDocument(chunkId)),
system: prop => (prop === null ? `module.meta` : `module.meta.${prop}`),
umd: getGenericImportMetaMechanism(
chunkId =>
`(typeof document === 'undefined' ? ${getResolveUrl(
`'file:' + __filename`,
`(require('u' + 'rl').URL)`
)} : ${getUrlFromDocument(chunkId)})`
)
};

const getRelativeUrlFromDocument = (relativePath: string) =>
Expand Down
Expand Up @@ -8,8 +8,8 @@ System.register([], function (exports, module) {
console.log('resolved');

console.log(module.meta.url);
console.log(undefined);
console.log(({ url: module.meta.url }));
console.log(module.meta.privateProp);
console.log(module.meta);

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

0 comments on commit 13b0d25

Please sign in to comment.