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 44e3879 commit 0a4c038
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 43 deletions.
36 changes: 15 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Expand Up @@ -59,7 +59,7 @@
"homepage": "https://github.com/rollup/rollup",
"dependencies": {
"@types/estree": "0.0.39",
"@types/node": "^11.13.2",
"@types/node": "^11.13.4",
"acorn": "^6.1.1"
},
"devDependencies": {
Expand Down Expand Up @@ -98,12 +98,12 @@
"remap-istanbul": "^0.13.0",
"require-relative": "^0.8.7",
"requirejs": "^2.3.6",
"rollup": "^1.9.1",
"rollup": "^1.9.3",
"rollup-plugin-alias": "^1.5.1",
"rollup-plugin-buble": "^0.19.6",
"rollup-plugin-commonjs": "^9.3.4",
"rollup-plugin-json": "^4.0.0",
"rollup-plugin-node-resolve": "^4.2.2",
"rollup-plugin-node-resolve": "^4.2.3",
"rollup-plugin-replace": "^2.2.0",
"rollup-plugin-string": "^3.0.0",
"rollup-plugin-terser": "^4.0.4",
Expand All @@ -115,7 +115,7 @@
"source-map": "^0.6.1",
"source-map-support": "^0.5.12",
"sourcemap-codec": "^1.4.4",
"systemjs": "^3.1.0",
"systemjs": "^3.1.1",
"terser": "^3.17.0",
"tslib": "^1.9.3",
"tslint": "^5.15.0",
Expand Down
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 0a4c038

Please sign in to comment.