-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrollup.config.js
99 lines (83 loc) · 2.22 KB
/
rollup.config.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
import path from 'path';
import esbuild from 'rollup-plugin-esbuild';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import * as fs from 'fs-extra';
import url from '@rollup/plugin-url';
import { default as svgr } from '@svgr/rollup';
const external = (id) => !id.startsWith('.') && !path.isAbsolute(id);
const resolveApp = function (relativePath) {
return path.resolve(relativePath);
};
const distName = 'dist';
const distPath = resolveApp(distName);
const packageName = 'web-components';
async function cleanDistFolder() {
await fs.remove(distPath);
}
function getOutputName(options) {
const fileName = [
packageName,
options.format,
options.env,
options.minify ? 'min' : '',
'js',
]
.filter(Boolean)
.join('.');
if (options.absolute) {
return path.join(distPath, fileName);
}
return fileName;
}
const cjsProdFileName = getOutputName({
format: 'cjs',
env: 'production',
minify: true,
});
const cjsDevFileName = getOutputName({ format: 'cjs', env: 'development' });
function writeCjsEntryFile() {
const contents = `'use strict'
if (process.env.NODE_ENV === 'production') {
module.exports = require('./${cjsProdFileName}')
} else {
module.exports = require('./${cjsDevFileName}')
}
`;
return fs.outputFile(path.join(distPath, 'index.js'), contents);
}
function createRollupConfig(options) {
const minify = options.env === 'production' && options.format !== 'esm';
return {
input: 'src/main.tsx',
output: {
file: getOutputName({ ...options, minify, absolute: true }),
format: options.format,
},
plugins: [
nodeResolve(),
url({ emitFiles: false }),
svgr({
svgo: false,
titleProp: true,
ref: true,
}),
esbuild({ minify }),
],
external: (id) => {
const packagesToIncludeInBundle = [];
if (packagesToIncludeInBundle.includes(id)) {
return false;
}
return external(id);
},
};
}
export default async function () {
await cleanDistFolder();
await writeCjsEntryFile();
return [
createRollupConfig({ format: 'cjs', env: 'development' }),
createRollupConfig({ format: 'cjs', env: 'production' }),
createRollupConfig({ format: 'esm' }),
];
}