/
bundle-npm-module.ts
115 lines (109 loc) · 3.8 KB
/
bundle-npm-module.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import type { Plugin, RollupCache } from 'rollup';
import { rollup } from 'rollup';
import { promises as fs } from 'fs';
import commonjs from '@rollup/plugin-commonjs';
import { environmentVariablesPlugin } from './plugins/environment-variables-plugin';
import * as esbuild from 'esbuild';
import { parse } from 'cjs-module-lexer';
// @ts-expect-error @types/node@12 doesn't like this import
import { createRequire } from 'module';
import { isBareImport } from './extensions-and-detection';
let npmCache: RollupCache | undefined;
/**
* Any package names in this set will need to have their named exports detected manually via require()
* because the export names cannot be statically analyzed
*/
const dynamicCJSModules = new Set(['prop-types', 'react-dom', 'react']);
/**
* Bundle am npm module entry path into a single file
* @param mod The full path of the module to bundle, including subpackage/path
* @param id The imported identifier
* @param optimize Whether the bundle should be a minified/optimized bundle, or the default quick non-optimized bundle
*/
export const bundleNpmModule = async (
mod: string,
id: string,
optimize: boolean,
envVars: Record<string, string>,
) => {
let namedExports: string[] = [];
if (dynamicCJSModules.has(id)) {
let isValidCJS = true;
try {
const text = await fs.readFile(mod, 'utf8');
// Goal: Determine if it is ESM or CJS.
// Try to parse it with cjs-module-lexer, if it fails, assume it is ESM
// eslint-disable-next-line @cloudfour/typescript-eslint/await-thenable
await parse(text);
} catch {
isValidCJS = false;
}
if (isValidCJS) {
const require = createRequire(import.meta.url);
// eslint-disable-next-line @cloudfour/typescript-eslint/no-var-requires
const imported = require(mod);
if (typeof imported === 'object' && !imported.__esModule)
namedExports = Object.keys(imported);
}
}
const virtualEntry = '\0virtualEntry';
const hasSyntheticNamedExports = namedExports.length > 0;
const bundle = await rollup({
input: hasSyntheticNamedExports ? virtualEntry : mod,
cache: npmCache,
shimMissingExports: true,
treeshake: true,
preserveEntrySignatures: 'allow-extension',
plugins: [
hasSyntheticNamedExports &&
({
// This plugin handles special-case packages whose named exports cannot be found via static analysis
// For these packages, the package is require()'d, and the named exports are determined that way.
// A virtual entry exports the named exports from the real entry package
name: 'cjs-named-exports',
resolveId(id) {
if (id === virtualEntry) return virtualEntry;
},
load(id) {
if (id === virtualEntry) {
const code = `export * from '${mod}'
export {${namedExports.join(', ')}} from '${mod}'
export { default } from '${mod}'`;
return code;
}
},
} as Plugin),
pluginNodeResolve(),
environmentVariablesPlugin(envVars),
commonjs({
extensions: ['.js', '.cjs', ''],
sourceMap: false,
transformMixedEsModules: true,
}),
(optimize && {
name: 'esbuild-minify',
renderChunk: async (code) => {
const output = await esbuild.transform(code, {
minify: true,
legalComments: 'none',
});
return { code: output.code };
},
}) as Plugin,
].filter(Boolean),
});
npmCache = bundle.cache;
const { output } = await bundle.generate({
format: 'es',
indent: false,
exports: 'named',
preferConst: true,
});
return output[0].code;
};
const pluginNodeResolve = (): Plugin => ({
name: 'node-resolve',
resolveId(id) {
if (isBareImport(id)) return { id, external: true };
},
});