/
defaultPlugin.ts
96 lines (86 loc) · 3.45 KB
/
defaultPlugin.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
89
90
91
92
93
94
95
96
import { InputOptions, Plugin } from '../rollup/types';
import { error } from './error';
import { lstatSync, readdirSync, readFileSync, realpathSync } from './fs';
import { basename, dirname, isAbsolute, resolve } from './path';
export function getRollupDefaultPlugin(options: InputOptions): Plugin {
return {
name: 'Rollup Core',
resolveId: createResolveId(options),
load(id) {
return readFileSync(id, 'utf-8');
},
resolveDynamicImport(specifier, parentId) {
if (typeof specifier === 'string' && !this.isExternal(specifier, parentId, false))
return <Promise<string>>this.resolveId(specifier, parentId);
},
resolveImportMeta(prop, { chunkId, format }) {
const mechanism = importMetaUrlMechanisms[format] && importMetaUrlMechanisms[format](chunkId);
if (mechanism) {
return prop === null ? `({ url: ${mechanism} })` : prop === 'url' ? mechanism : 'undefined';
}
}
};
}
function findFile(file: string, preserveSymlinks: boolean): string | void {
try {
const stats = lstatSync(file);
if (!preserveSymlinks && stats.isSymbolicLink())
return findFile(realpathSync(file), preserveSymlinks);
if ((preserveSymlinks && stats.isSymbolicLink()) || stats.isFile()) {
// check case
const name = basename(file);
const files = readdirSync(dirname(file));
if (files.indexOf(name) !== -1) return file;
}
} catch (err) {
// suppress
}
}
function addJsExtensionIfNecessary(file: string, preserveSymlinks: boolean) {
let found = findFile(file, preserveSymlinks);
if (found) return found;
found = findFile(file + '.mjs', preserveSymlinks);
if (found) return found;
found = findFile(file + '.js', preserveSymlinks);
return found;
}
function createResolveId(options: InputOptions) {
return function(importee: string, importer: string) {
if (typeof process === 'undefined') {
error({
code: 'MISSING_PROCESS',
message: `It looks like you're using Rollup in a non-Node.js environment. This means you must supply a plugin with custom resolveId and load functions`,
url: 'https://rollupjs.org/guide/en#a-simple-example'
});
}
// external modules (non-entry modules that start with neither '.' or '/')
// are skipped at this stage.
if (importer !== undefined && !isAbsolute(importee) && importee[0] !== '.') return null;
// `resolve` processes paths from right to left, prepending them until an
// absolute path is created. Absolute importees therefore shortcircuit the
// resolve call and require no special handing on our part.
// See https://nodejs.org/api/path.html#path_path_resolve_paths
return addJsExtensionIfNecessary(
resolve(importer ? dirname(importer) : resolve(), importee),
options.preserveSymlinks
);
};
}
const getResolveUrl = (path: string, URL: string = 'URL') => `new ${URL}(${path}).href`;
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)})`
};