-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
rollup.config.mjs
71 lines (65 loc) · 1.86 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
// @ts-check
import * as FS from 'node:fs'
import addGitMsg from 'rollup-plugin-add-git-msg'
import { babel } from '@rollup/plugin-babel'
import commonjs from '@rollup/plugin-commonjs'
import { nodeResolve } from '@rollup/plugin-node-resolve'
// List of njs built-in modules.
//
// - xml has added in njs 0.7.11
// - zlib was added in njs 0.7.12
const njsExternals = ['crypto', 'fs', 'querystring', 'xml', 'zlib']
const isEnvProd = process.env.NODE_ENV === 'production'
/**
* Plugin to fix syntax of the default export to be compatible with njs.
* (https://github.com/rollup/rollup/pull/4182#issuecomment-1002241017)
*
* If you use njs >=0.7.12, you can remove this.
*
* @return {import('rollup').OutputPlugin}
*/
const fixExportDefault = () => ({
name: 'fix-export-default',
renderChunk: (code) => ({
code: code.replace(/\bexport { (\S+) as default };/, 'export default $1;'),
map: null,
}),
})
/**
* @type {import('./package.json')}
*/
const pkg = JSON.parse(FS.readFileSync('./package.json', 'utf8'))
/**
* @type {import('rollup').RollupOptions}
*/
const options = {
input: 'src/index.ts',
external: njsExternals,
plugins: [
// Transpile TypeScript sources to JS.
babel({
babelHelpers: 'bundled',
envName: 'njs',
extensions: ['.ts', '.mjs', '.js'],
}),
// Resolve node modules.
nodeResolve({
extensions: ['.mjs', '.js', '.json', '.ts'],
}),
// Convert CommonJS modules to ES6 modules.
// @ts-ignore XXX: workaround for https://github.com/rollup/plugins/issues/1329
commonjs(),
// Fix syntax of the default export.
fixExportDefault(),
// Plugins to use in production mode only.
...isEnvProd ? [
// Add git tag, commit SHA, build date and copyright at top of the file.
addGitMsg(),
] : [],
],
output: {
file: pkg.main,
format: 'es',
},
}
export default options