-
Notifications
You must be signed in to change notification settings - Fork 58
/
index.ts
92 lines (85 loc) · 3.22 KB
/
index.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
// @ts-ignore
import type { Plugin } from "rollup";
import inject, { RollupInjectOptions } from "@rollup/plugin-inject";
import { getModules } from "./modules";
import { posix, resolve } from "path";
import { randomBytes } from "crypto";
import POLYFILLS from './polyfills';
// Node import paths use POSIX separators
const { dirname, relative, join } = posix;
const PREFIX = `\0polyfill-node.`;
const PREFIX_LENGTH = PREFIX.length;
export interface NodePolyfillsOptions {
baseDir?: string;
sourceMap?: RollupInjectOptions['sourceMap'];
include?: Array<string | RegExp> | string | RegExp | null;
exclude?: Array<string | RegExp> | string | RegExp | null;
}
export default function (opts: NodePolyfillsOptions = {}): Plugin {
const mods = getModules();
const injectPlugin = inject({
include: opts.include === undefined ? ['node_modules/**/*.js'] : opts.include,
exclude: opts.exclude,
sourceMap: opts.sourceMap,
modules: {
process: PREFIX + "process",
Buffer: [PREFIX + "buffer", "Buffer"],
global: PREFIX + 'global',
__filename: FILENAME_PATH,
__dirname: DIRNAME_PATH,
},
});
const basedir = opts.baseDir || "/";
const dirs = new Map<string, string>();
return {
name: "polyfill-node",
resolveId(importee: string, importer?: string) {
// Fixes commonjs compatability: https://github.com/FredKSchott/rollup-plugin-polyfill-node/pull/42
if (importee[0] == '\0' && /\?commonjs-\w+$/.test(importee)) {
importee = importee.slice(1).replace(/\?commonjs-\w+$/, '');
}
if (importee === DIRNAME_PATH) {
const id = getRandomId();
dirs.set(id, dirname("/" + relative(basedir, importer!)));
return { id, moduleSideEffects: false };
}
if (importee === FILENAME_PATH) {
const id = getRandomId();
dirs.set(id, dirname("/" + relative(basedir, importer!)));
return { id, moduleSideEffects: false };
}
if (importee && importee.slice(-1) === "/") {
importee = importee.slice(0, -1);
}
if (importer && importer.startsWith(PREFIX) && importee.startsWith('.')) {
importee = PREFIX + join(importer.substr(PREFIX_LENGTH).replace('.js', ''), '..', importee) + '.js';
}
if (importee.startsWith(PREFIX)) {
importee = importee.substr(PREFIX_LENGTH);
}
if (mods.has(importee) || (POLYFILLS as any)[importee.replace('.js', '') + '.js']) {
return { id: PREFIX + importee.replace('.js', '') + '.js', moduleSideEffects: false };
}
return null;
},
load(id: string) {
if (dirs.has(id)) {
return `export default '${dirs.get(id)}'`;
}
if (id.startsWith(PREFIX)) {
const importee = id.substr(PREFIX_LENGTH).replace('.js', '');
return mods.get(importee) || (POLYFILLS as any)[importee + '.js'];
}
},
transform(code: string, id: string) {
if(id === PREFIX + 'global.js') return
// @ts-ignore
return injectPlugin.transform!.call(this, code, id.replace(PREFIX, resolve('node_modules', 'polyfill-node')));
},
};
}
function getRandomId() {
return randomBytes(15).toString("hex");
}
const DIRNAME_PATH = "\0node-polyfills:dirname";
const FILENAME_PATH = "\0node-polyfills:filename";