-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathrollup.config.mjs
More file actions
126 lines (121 loc) · 3.45 KB
/
Copy pathrollup.config.mjs
File metadata and controls
126 lines (121 loc) · 3.45 KB
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
121
122
123
124
125
126
import typescript from 'rollup-plugin-typescript2'
import typescriptCompiler from 'typescript'
import replace from '@rollup/plugin-replace'
import commonjs from '@rollup/plugin-commonjs'
import resolve from '@rollup/plugin-node-resolve'
import copy from 'rollup-plugin-copy'
const comment = '/* removed in browser build */'
const ignoredWarnings = ['UNUSED_EXTERNAL_IMPORT']
const tsOpts = {
cacheRoot: './node_modules/.rpt2',
typescript: typescriptCompiler,
tsconfig: 'tsconfig.build.json',
tsconfigOverride: {
compilerOptions: {
// Don't emit declarations, that's done by the regular build.
declaration: false,
module: 'ESNext',
},
},
}
export default [
// Build 1: ES modules for Node.
{
input: 'src/awilix.ts',
external: ['fast-glob', 'path', 'url', 'util'],
treeshake: { moduleSideEffects: 'no-external' },
onwarn,
output: [
{
file: 'lib/awilix.module.mjs',
format: 'es',
},
],
plugins: [
// The source imports ./load-module-native.js (the CJS version) so that
// tsc emits require("./load-module-native.js") in the CJS build.
// For this ESM bundle we remap it to the .mjs version.
// Returning a relative id with external: true tells rollup to preserve
// the path as-is in the output (see https://rollupjs.org/plugin-development/#resolveid).
{
name: 'rewrite-native-loader',
resolveId(source) {
if (source === './load-module-native.js') {
return { id: './load-module-native.mjs', external: true }
}
return null
},
},
// Copy the native module loaders and their type declarations to lib/
copy({
targets: [{ src: 'src/load-module-native.*', dest: 'lib' }],
}),
typescript(tsOpts),
],
},
// Build 2: ES modules for browser builds.
{
input: 'src/awilix.ts',
external: ['fast-glob', 'path', 'url', 'util'],
treeshake: { moduleSideEffects: 'no-external' },
onwarn,
output: [
{
name: 'Awilix',
file: 'lib/awilix.browser.mjs',
format: 'es',
},
{
name: 'Awilix',
file: 'lib/awilix.umd.js',
format: 'umd',
},
],
plugins: [
// Removes stuff that won't work in the browser
// which also means node-only stuff like `path`, `util` and `fast-glob`
// will be shaken off.
replace({
delimiters: ['', ''],
preventAssignment: true,
'loadModules,':
'loadModules: () => { throw new Error("loadModules is not supported in the browser.") },',
'[util.inspect.custom]: inspect,': comment,
'[util.inspect.custom]: toStringRepresentationFn,': comment,
'case util.inspect.custom:': '',
[`export {
type GlobWithOptions,
type ListModulesOptions,
type ModuleDescriptor,
listModules,
} from './list-modules'`]: comment,
"import * as util from 'util'": '',
}),
typescript(
Object.assign({}, tsOpts, {
tsconfigOverride: {
compilerOptions: {
target: 'ES2020',
declaration: false,
noUnusedLocals: false,
module: 'ESNext',
},
},
}),
),
resolve({
preferBuiltins: true,
}),
commonjs(),
],
},
]
/**
* Ignores certain warnings.
*/
function onwarn(warning, next) {
if (ignoredWarnings.includes(warning.code)) {
return
}
next(warning)
}