-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathrollup.config.mjs
111 lines (106 loc) · 2.91 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
import babelPlugin from '@rollup/plugin-babel';
import commonjsPlugin from '@rollup/plugin-commonjs';
import jsonPlugin from '@rollup/plugin-json';
import resolvePlugin from '@rollup/plugin-node-resolve';
import replacePlugin from '@rollup/plugin-replace';
import terserPlugin from '@rollup/plugin-terser';
import { createRequire } from 'module';
import { dirname, isAbsolute, relative, resolve } from 'path';
import { readPackageUp } from 'read-package-up';
import { defineConfig } from 'rollup';
async function getVersion(module) {
const require = createRequire(import.meta.url);
const cwd = dirname(require.resolve(module));
const { packageJson } = await readPackageUp({ cwd });
return packageJson.version;
}
function defineExternal(externals) {
return (id) =>
externals.some((pattern) => {
if (typeof pattern === 'function') return pattern(id);
if (pattern && typeof pattern.test === 'function')
return pattern.test(id);
if (isAbsolute(pattern))
return !relative(pattern, resolve(id)).startsWith('..');
return id === pattern || id.startsWith(pattern + '/');
});
}
function definePlugins(options) {
const {
esm = true,
extensions = ['.ts', '.tsx', '.mjs', '.js', '.jsx'],
replaceValues,
browser = false,
minimize = isProd,
commonjs,
} = options;
return [
babelPlugin({
// import helpers from '@babel/runtime'
babelHelpers: 'runtime',
plugins: [
[
import.meta.resolve('@babel/plugin-transform-runtime'),
{
useESModules: esm,
version: '^7.5.0', // see https://github.com/babel/babel/issues/10261#issuecomment-514687857
},
],
],
exclude: 'node_modules/**',
extensions,
}),
replacePlugin({
values: {
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
...replaceValues,
},
preventAssignment: true,
}),
resolvePlugin({ browser, extensions }),
commonjsPlugin(commonjs),
jsonPlugin(),
minimize && terserPlugin({ ...minimize }),
].filter(Boolean);
}
const isProd = process.env.NODE_ENV === 'production';
const replaceValues = {
'process.env.TOOLBAR_VERSION': JSON.stringify(
await getVersion('markmap-toolbar'),
),
};
const external = defineExternal(['path', 'vscode']);
export default defineConfig([
{
input: {
extension: 'src/extension.ts',
postbuild: 'src/postbuild.ts',
},
plugins: definePlugins({
replaceValues,
}),
external,
output: {
format: 'cjs',
dir: 'dist',
},
},
{
input: {
app: 'src/app.ts',
},
plugins: definePlugins({
replaceValues,
}),
external: ['markmap-toolbar', 'markmap-view'],
output: {
format: 'iife',
dir: 'dist',
globals: {
'markmap-common': 'markmap',
'markmap-toolbar': 'markmap',
'markmap-view': 'markmap',
},
},
},
]);