-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathrollup.config.mjs
121 lines (116 loc) · 2.97 KB
/
rollup.config.mjs
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
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',
'camel-case',
'./load-module-native.js',
],
treeshake: { moduleSideEffects: 'no-external' },
onwarn,
output: [
{
file: 'lib/awilix.module.mjs',
format: 'es',
},
],
plugins: [
// Copy the native module loader
copy({
targets: [{ src: 'src/load-module-native.js', 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:': '',
"import { camelCase } from 'camel-case'":
'const camelCase = null as any',
[`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)
}