Skip to content

Commit

Permalink
Use different globals for file accesses
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed May 31, 2019
1 parent 39ee6f8 commit 89a3024
Show file tree
Hide file tree
Showing 20 changed files with 138 additions and 12 deletions.
31 changes: 21 additions & 10 deletions src/ast/nodes/MetaProperty.ts
@@ -1,5 +1,5 @@
import MagicString from 'magic-string';
import { accessedMetaPropertyGlobals } from '../../utils/defaultPlugin';
import { accessedFileUrlGlobals, accessedMetaUrlGlobals } from '../../utils/defaultPlugin';
import { dirname, normalize, relative } from '../../utils/path';
import { PluginDriver } from '../../utils/pluginDriver';
import { ObjectPathKey } from '../values';
Expand All @@ -16,16 +16,30 @@ export default class MetaProperty extends NodeBase {
property: Identifier;
type: NodeType.tMetaProperty;

bind() {
super.bind();
// TODO Lukas only when included, only when needed
this.scope.addAccessedGlobalsByFormat(accessedMetaPropertyGlobals);
}
private metaProperty?: string | null;

hasEffectsWhenAccessedAtPath(path: ObjectPathKey[]): boolean {
return path.length > 1;
}

include() {
if (!this.included) {
this.included = true;
const parent = this.parent;
const metaProperty = (this.metaProperty =
parent instanceof MemberExpression && typeof parent.propertyKey === 'string'
? parent.propertyKey
: null);
if (metaProperty) {
if (metaProperty === 'url') {
this.scope.addAccessedGlobalsByFormat(accessedMetaUrlGlobals);
} else if (metaProperty.startsWith(ASSET_PREFIX) || metaProperty.startsWith(CHUNK_PREFIX)) {
this.scope.addAccessedGlobalsByFormat(accessedFileUrlGlobals);
}
}
}
}

initialise() {
if (this.meta.name === 'import') {
this.context.addImportMeta(this);
Expand All @@ -41,10 +55,7 @@ export default class MetaProperty extends NodeBase {
): void {
if (!this.included) return;
const parent = this.parent;
const importMetaProperty =
parent instanceof MemberExpression && typeof parent.propertyKey === 'string'
? parent.propertyKey
: null;
const importMetaProperty = this.metaProperty as string | null;

if (
importMetaProperty &&
Expand Down
11 changes: 9 additions & 2 deletions src/utils/defaultPlugin.ts
Expand Up @@ -130,11 +130,18 @@ const relativeUrlMechanisms: Record<string, (relativePath: string) => string> =
)} : ${getRelativeUrlFromDocument(relativePath)})`
};

// TODO Lukas also for resolved file URLs
export const accessedMetaPropertyGlobals = {
export const accessedMetaUrlGlobals = {
amd: ['document', 'module', 'URL'],
cjs: ['document', 'require', 'URL'],
iife: ['document', 'URL'],
system: ['module'],
umd: ['document', 'require', 'URL']
};

export const accessedFileUrlGlobals = {
amd: ['document', 'module', 'URL'],
cjs: ['document', 'require', 'URL'],
iife: ['document', 'URL'],
system: ['module', 'URL'],
umd: ['document', 'require', 'URL']
};
23 changes: 23 additions & 0 deletions test/form/samples/asset-emission-tree-shaken-access/_config.js
@@ -0,0 +1,23 @@
const fs = require('fs');
const path = require('path');

module.exports = {
description: 'does not include format globals when tree-shaking an asset access',
options: {
plugins: {
resolveId(id, importee) {
if (id.endsWith('.svg')) {
return path.resolve(path.dirname(importee), id);
}
},
load(id) {
if (id.endsWith('.svg')) {
return `export default import.meta.ROLLUP_ASSET_URL_${this.emitAsset(
path.basename(id),
fs.readFileSync(id)
)};`;
}
}
}
}
};
@@ -0,0 +1,5 @@
define(function () { 'use strict';

console.log('main');

});
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,3 @@
'use strict';

console.log('main');
@@ -0,0 +1 @@
console.log('main');
@@ -0,0 +1,6 @@
(function () {
'use strict';

console.log('main');

}());
@@ -0,0 +1,10 @@
System.register([], function () {
'use strict';
return {
execute: function () {

console.log('main');

}
};
});
@@ -0,0 +1,8 @@
(function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
factory();
}(function () { 'use strict';

console.log('main');

}));
3 changes: 3 additions & 0 deletions test/form/samples/asset-emission-tree-shaken-access/logo.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions test/form/samples/asset-emission-tree-shaken-access/main.js
@@ -0,0 +1,6 @@
import logo from './logo.svg';

if (false) {
console.log(logo);
}
console.log('main');
3 changes: 3 additions & 0 deletions test/form/samples/treeshake-import-meta-props/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'also does not include format globals when tree-shaking import meta'
};
@@ -0,0 +1,5 @@
define(function () { 'use strict';

console.log('main');

});
@@ -0,0 +1,3 @@
'use strict';

console.log('main');
@@ -0,0 +1 @@
console.log('main');
@@ -0,0 +1,6 @@
(function () {
'use strict';

console.log('main');

}());
10 changes: 10 additions & 0 deletions test/form/samples/treeshake-import-meta-props/_expected/system.js
@@ -0,0 +1,10 @@
System.register([], function () {
'use strict';
return {
execute: function () {

console.log('main');

}
};
});
@@ -0,0 +1,8 @@
(function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
factory();
}(function () { 'use strict';

console.log('main');

}));
4 changes: 4 additions & 0 deletions test/form/samples/treeshake-import-meta-props/main.js
@@ -0,0 +1,4 @@
const removed1 = import.meta;
const removed2 = import.meta.url;
const removed3 = import.meta.unknown;
console.log('main');

0 comments on commit 89a3024

Please sign in to comment.