Skip to content

Commit

Permalink
Extract more common code
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Mar 29, 2019
1 parent 858bb9d commit 0089eb9
Show file tree
Hide file tree
Showing 23 changed files with 81 additions and 53 deletions.
16 changes: 16 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
"pretty-ms": "^4.0.0",
"remap-istanbul": "^0.13.0",
"require-relative": "^0.8.7",
"requirejs": "^2.3.6",
"rollup": "^1.7.0",
"rollup-plugin-alias": "^1.5.1",
"rollup-plugin-buble": "^0.19.6",
Expand Down
3 changes: 1 addition & 2 deletions src/Chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -800,8 +800,7 @@ export default class Chunk {
const module = this.orderedModules[i];
const code = this.renderedModuleSources[i];
for (const importMeta of module.importMetas) {
if (importMeta.renderFinalMechanism(code, this.id, options.format, options.compact))
usesMechanism = true;
if (importMeta.renderFinalMechanism(code, this.id, options.format)) usesMechanism = true;
}
}
return usesMechanism;
Expand Down
59 changes: 32 additions & 27 deletions src/ast/nodes/MetaProperty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,45 @@ import MemberExpression from './MemberExpression';
import * as NodeType from './NodeType';
import { NodeBase } from './shared/Node';

// TODO Lukas extract more common elements from mechanisms
// TODO Lukas make relative mechanism more uniform
// TODO Lukas reference absolute mechanism in relative mechanism

const globalImportMetaUrlMechanism = (_: string) =>
`(typeof document${_}!==${_}'undefined'${_}?${_}document.currentScript${_}&&${_}document.currentScript.src${_}||${_}document.baseURI${_}:${_}new${_}(typeof URL${_}!==${_}'undefined'${_}?${_}URL${_}:${_}require('ur'+'l').URL)('file:'${_}+${_}__filename).href)`;
const getResolveUrl = (path: string, URL: string = 'URL') => `new ${URL}(${path}).href`;
const amdModuleUrl = `(typeof process !== 'undefined' && process.versions && process.versions.node ? 'file:' : '') + module.uri`;
const getURLFromGlobalOrCjs = `(typeof URL !== 'undefined' ? URL : require('ur'+'l').URL)`;

const importMetaUrlMechanisms: Record<string, (_: string) => string> = {
amd: (_: string) =>
`new URL((typeof process${_}!==${_}'undefined'${_}&&${_}process.versions${_}&&${_}process.versions.node${_}?${_}'file:'${_}:${_}'')${_}+${_}module.uri).href`,
cjs: (_: string) =>
`new${_}(typeof URL${_}!==${_}'undefined'${_}?${_}URL${_}:${_}require('ur'+'l').URL)((process.browser${_}?${_}''${_}:${_}'file:')${_}+${_}__filename,${_}process.browser${_}&&${_}document.baseURI).href`,
const globalImportMetaUrlMechanism = `(typeof document !== 'undefined' ? document.currentScript && document.currentScript.src || document.baseURI : ${getResolveUrl(
`'file:' + __filename`,
getURLFromGlobalOrCjs
)})`;

const importMetaUrlMechanisms: Record<string, string> = {
amd: getResolveUrl(amdModuleUrl),
cjs: getResolveUrl(
`(process.browser ? '' : 'file:') + __filename, process.browser && document.baseURI`,
getURLFromGlobalOrCjs
),
iife: globalImportMetaUrlMechanism,
umd: globalImportMetaUrlMechanism
};

const globalRelUrlMechanism = (relPath: string, _: string) => {
return `new${_}(typeof URL${_}!==${_}'undefined'${_}?${_}URL${_}:${_}require('ur'+'l').URL)((typeof document${_}!==${_}'undefined'${_}?${_}document.currentScript${_}&&${_}document.currentScript.src${_}||${_}document.baseURI${_}:${_}'file:'${_}+${_}__filename)${_}+${_}'/../${relPath}').href`;
const globalRelUrlMechanism = (relPath: string) => {
return getResolveUrl(
`(typeof document !== 'undefined' ? document.currentScript && document.currentScript.src || document.baseURI : 'file:' + __filename) + '/../${relPath}'`,
getURLFromGlobalOrCjs
);
};

const relUrlMechanisms: Record<string, (relPath: string, _: string) => string> = {
amd: (relPath: string, _: string) =>
`new URL((typeof process${_}!==${_}'undefined'${_}&&${_}process.versions${_}&&${_}process.versions.node${_}?${_}'file:'${_}:${_}'')${_}+${_}module.uri${_}+${_}'/../${relPath}').href`,
cjs: (relPath: string, _: string) =>
`new${_}(typeof URL${_}!==${_}'undefined'${_}?${_}URL${_}:${_}require('ur'+'l').URL)((process.browser${_}?${_}''${_}:${_}'file:')${_}+${_}__dirname${_}+${_}'/${relPath}',${_}process.browser${_}&&${_}document.baseURI).href`,
es: (relPath: string, _: string) => `new URL('../${relPath}',${_}import.meta.url).href`,
const relUrlMechanisms: Record<string, (relPath: string) => string> = {
amd: (relPath: string) => getResolveUrl(`${amdModuleUrl} + '/../${relPath}'`),
cjs: (relPath: string) =>
getResolveUrl(
`(process.browser ? '' : 'file:') + __dirname + '/${relPath}', process.browser && document.baseURI`,
getURLFromGlobalOrCjs
),
es: (relPath: string) => getResolveUrl(`'../${relPath}', import.meta.url`), // TODO Lukas does this pattern make sense in more situations?
iife: globalRelUrlMechanism,
system: (relPath: string, _: string) => `new URL('../${relPath}',${_}module.url).href`,
system: (relPath: string) => getResolveUrl(`'../${relPath}', module.url`),
umd: globalRelUrlMechanism
};

Expand All @@ -56,16 +68,10 @@ export default class MetaProperty extends NodeBase {
super.render(code, options);
}

renderFinalMechanism(
code: MagicString,
chunkId: string,
format: string,
compact: boolean
): boolean {
renderFinalMechanism(code: MagicString, chunkId: string, format: string): boolean {
// TODO Lukas why?
if (!this.rendered) return false;

const _ = compact ? '' : ' ';
if (this.parent instanceof MemberExpression === false) return false;

const parent = <MemberExpression>this.parent;
Expand All @@ -80,16 +86,15 @@ export default class MetaProperty extends NodeBase {
if (importMetaProperty.startsWith('ROLLUP_ASSET_URL_')) {
const assetFileName = this.context.getAssetFileName(importMetaProperty.substr(17));
const relPath = normalize(relative(dirname(chunkId), assetFileName));
code.overwrite(parent.start, parent.end, relUrlMechanisms[format](relPath, _));
code.overwrite(parent.start, parent.end, relUrlMechanisms[format](relPath));
return true;
}

if (format === 'system') {
code.overwrite(this.meta.start, this.meta.end, 'module');
} else if (importMetaProperty === 'url') {
const importMetaUrlMechanism = importMetaUrlMechanisms[format];
if (importMetaUrlMechanism)
code.overwrite(parent.start, parent.end, importMetaUrlMechanism(_));
if (importMetaUrlMechanism) code.overwrite(parent.start, parent.end, importMetaUrlMechanism);
return true;
}

Expand Down
5 changes: 3 additions & 2 deletions test/chunking-form/samples/asset-emission/_config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module.exports = {
description: 'simple chunking',
solo: true,
description: 'supports emitting assets from plugin hooks',
options: {
input: ['main.js'],
plugins: {
transform (code, id) {
transform() {
const assetId = this.emitAsset('test.ext', 'hello world');
return `export default import.meta.ROLLUP_ASSET_URL_${assetId};`;
}
Expand Down
8 changes: 0 additions & 8 deletions test/form/samples/import-meta-url-compact/_config.js

This file was deleted.

1 change: 0 additions & 1 deletion test/form/samples/import-meta-url-compact/_expected/amd.js

This file was deleted.

1 change: 0 additions & 1 deletion test/form/samples/import-meta-url-compact/_expected/cjs.js

This file was deleted.

1 change: 0 additions & 1 deletion test/form/samples/import-meta-url-compact/_expected/es.js

This file was deleted.

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion test/form/samples/import-meta-url-compact/_expected/umd.js

This file was deleted.

1 change: 0 additions & 1 deletion test/form/samples/import-meta-url-compact/main.js

This file was deleted.

1 change: 1 addition & 0 deletions test/form/samples/import-meta-url/_config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module.exports = {
solo: true,
description: 'import.meta.url support'
};
2 changes: 1 addition & 1 deletion test/form/samples/import-meta-url/_expected/amd.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
define(['module'], function (module) { 'use strict';

console.log(new URL((typeof process !== 'undefined' && process.versions && process.versions.node ? 'file:' : '') + module.uri).href);
document.body.innerText = new URL((typeof process !== 'undefined' && process.versions && process.versions.node ? 'file:' : '') + module.uri).href;

});
2 changes: 1 addition & 1 deletion test/form/samples/import-meta-url/_expected/cjs.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
'use strict';

console.log(new (typeof URL !== 'undefined' ? URL : require('ur'+'l').URL)((process.browser ? '' : 'file:') + __filename, process.browser && document.baseURI).href);
document.body.innerText = new (typeof URL !== 'undefined' ? URL : require('ur'+'l').URL)((process.browser ? '' : 'file:') + __filename, process.browser && document.baseURI).href;
2 changes: 1 addition & 1 deletion test/form/samples/import-meta-url/_expected/es.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
console.log(import.meta.url);
document.body.innerText = import.meta.url;
2 changes: 1 addition & 1 deletion test/form/samples/import-meta-url/_expected/iife.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(function () {
'use strict';

console.log((typeof document !== 'undefined' ? document.currentScript && document.currentScript.src || document.baseURI : new (typeof URL !== 'undefined' ? URL : require('ur'+'l').URL)('file:' + __filename).href));
document.body.innerText = (typeof document !== 'undefined' ? document.currentScript && document.currentScript.src || document.baseURI : new (typeof URL !== 'undefined' ? URL : require('ur'+'l').URL)('file:' + __filename).href);

}());
2 changes: 1 addition & 1 deletion test/form/samples/import-meta-url/_expected/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ System.register([], function (exports, module) {
return {
execute: function () {

console.log(module.meta.url);
document.body.innerText = module.meta.url;

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

console.log((typeof document !== 'undefined' ? document.currentScript && document.currentScript.src || document.baseURI : new (typeof URL !== 'undefined' ? URL : require('ur'+'l').URL)('file:' + __filename).href));
document.body.innerText = (typeof document !== 'undefined' ? document.currentScript && document.currentScript.src || document.baseURI : new (typeof URL !== 'undefined' ? URL : require('ur'+'l').URL)('file:' + __filename).href);

}));
10 changes: 10 additions & 0 deletions test/form/samples/import-meta-url/index-amd.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script src="../../../../node_modules/requirejs/require.js" data-main="_actual/amd.js"></script>
</body>
</html>
10 changes: 10 additions & 0 deletions test/form/samples/import-meta-url/index-es.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script src="_actual/es.js" type="module"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion test/form/samples/import-meta-url/main.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
console.log(import.meta.url);
document.body.innerText = import.meta.url;

0 comments on commit 0089eb9

Please sign in to comment.