-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
npmHelpers.js
120 lines (99 loc) · 4.09 KB
/
npmHelpers.js
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
116
117
118
119
120
/**
* Rollup config docs: https://rollupjs.org/guide/en/#big-list-of-options
*/
import { builtinModules } from 'module';
import * as path from 'path';
import deepMerge from 'deepmerge';
import {
makeExtractPolyfillsPlugin,
makeNodeResolvePlugin,
makeCleanupPlugin,
makeSucrasePlugin,
makeDebugBuildStatementReplacePlugin,
} from './plugins/index.js';
import { mergePlugins } from './utils';
const packageDotJSON = require(path.resolve(process.cwd(), './package.json'));
export function makeBaseNPMConfig(options = {}) {
const {
entrypoints = ['src/index.ts'],
esModuleInterop = false,
hasBundles = false,
packageSpecificConfig = {},
} = options;
const nodeResolvePlugin = makeNodeResolvePlugin();
const sucrasePlugin = makeSucrasePlugin();
const debugBuildStatementReplacePlugin = makeDebugBuildStatementReplacePlugin();
const cleanupPlugin = makeCleanupPlugin();
const extractPolyfillsPlugin = makeExtractPolyfillsPlugin();
const defaultBaseConfig = {
input: entrypoints,
output: {
// an appropriately-named directory will be added to this base value when we specify either a cjs or esm build
dir: hasBundles ? 'build/npm' : 'build',
sourcemap: true,
// output individual files rather than one big bundle
preserveModules: true,
// Allow wrappers or helper functions generated by rollup to use any ES6 features except symbols. (Symbols in
// general are fine, but the `[Symbol.toStringTag]: 'Module'` which Rollup adds alongside `__esModule:
// true` in CJS modules makes it so that Jest <= 29.2.2 crashes when trying to mock generated `@sentry/xxx`
// packages. See XXXXX.)
generatedCode: {
preset: 'es2015',
symbols: false,
},
// don't add `"use strict"` to the top of cjs files
strict: false,
// do TS-3.8-style exports
// exports.dogs = are.great
// rather than TS-3.9-style exports
// Object.defineProperty(exports, 'dogs', {
// enumerable: true,
// get: () => are.great,
// });
externalLiveBindings: false,
// Don't call `Object.freeze` on the results of `import * as someModule from '...'`
// (We don't need it, so why waste the bytes?)
freeze: false,
// Equivalent to `esModuleInterop` in tsconfig.
// Controls whether rollup emits helpers to handle special cases where turning
// `import * as dogs from 'dogs'`
// into
// `const dogs = require('dogs')`
// doesn't work.
//
// `auto` -> emit helpers
// `esModule` -> don't emit helpers
interop: esModuleInterop ? 'auto' : 'esModule',
},
plugins: [
nodeResolvePlugin,
sucrasePlugin,
debugBuildStatementReplacePlugin,
cleanupPlugin,
extractPolyfillsPlugin,
],
// don't include imported modules from outside the package in the final output
external: [
...builtinModules,
...Object.keys(packageDotJSON.dependencies || {}),
...Object.keys(packageDotJSON.devDependencies || {}),
...Object.keys(packageDotJSON.peerDependencies || {}),
],
// TODO `'smallest'` will get rid of `isDebugBuild()` by evaluating it and inlining the result and then treeshaking
// from there. The current setting (false) prevents this, in case we want to leave it there for users to use in
// their own bundling. That said, we don't yet know for sure that that works, so come back to this.
// treeshake: 'smallest',
treeshake: false,
};
return deepMerge(defaultBaseConfig, packageSpecificConfig, {
// Plugins have to be in the correct order or everything breaks, so when merging we have to manually re-order them
customMerge: key => (key === 'plugins' ? mergePlugins : undefined),
});
}
export function makeNPMConfigVariants(baseConfig) {
const variantSpecificConfigs = [
{ output: { format: 'cjs', dir: path.join(baseConfig.output.dir, 'cjs') } },
{ output: { format: 'esm', dir: path.join(baseConfig.output.dir, 'esm') } },
];
return variantSpecificConfigs.map(variant => deepMerge(baseConfig, variant));
}