/
MetaProperty.ts
88 lines (79 loc) · 2.86 KB
/
MetaProperty.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import MagicString from 'magic-string';
import { dirname, normalize, relative } from '../../utils/path';
import { PluginDriver } from '../../utils/pluginDriver';
import Identifier from './Identifier';
import MemberExpression from './MemberExpression';
import * as NodeType from './NodeType';
import { NodeBase } from './shared/Node';
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 globalRelUrlMechanism = (relPath: string) => {
return getResolveUrl(
`(typeof document !== 'undefined' ? document.currentScript && document.currentScript.src || document.baseURI : 'file:' + __filename) + '/../${relPath}'`,
`(typeof URL !== 'undefined' ? URL : require('ur'+'l').URL)`
);
};
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`,
`(typeof URL !== 'undefined' ? URL : require('ur'+'l').URL)`
),
es: (relPath: string) => getResolveUrl(`'../${relPath}', import.meta.url`),
iife: globalRelUrlMechanism,
system: (relPath: string) => getResolveUrl(`'../${relPath}', module.url`),
umd: globalRelUrlMechanism
};
export default class MetaProperty extends NodeBase {
meta: Identifier;
property: Identifier;
type: NodeType.tMetaProperty;
initialise() {
if (this.meta.name === 'import') {
this.context.addImportMeta(this);
}
this.included = false;
}
renderFinalMechanism(
code: MagicString,
chunkId: string,
format: string,
pluginDriver: PluginDriver
): boolean {
if (!this.included) return false;
const parent = this.parent;
const importMetaProperty =
parent instanceof MemberExpression && typeof parent.propertyKey === 'string'
? parent.propertyKey
: null;
// support import.meta.ROLLUP_ASSET_URL_[ID]
if (importMetaProperty && importMetaProperty.startsWith('ROLLUP_ASSET_URL_')) {
const assetFileName = this.context.getAssetFileName(importMetaProperty.substr(17));
const relPath = normalize(relative(dirname(chunkId), assetFileName));
code.overwrite(
(parent as MemberExpression).start,
(parent as MemberExpression).end,
relUrlMechanisms[format](relPath)
);
return true;
}
const replacement = pluginDriver.hookFirstSync<string | void>('resolveImportMeta', [
importMetaProperty,
{
chunkId,
format,
moduleId: this.context.module.id
}
]);
if (typeof replacement === 'string') {
if (parent instanceof MemberExpression) {
code.overwrite(parent.start, parent.end, replacement);
} else {
code.overwrite(this.start, this.end, replacement);
}
return true;
}
return false;
}
}