-
Notifications
You must be signed in to change notification settings - Fork 12
/
rollup.config.js
74 lines (67 loc) · 1.96 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
/* Copyright 2021 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
import fs from 'fs-extra';
import * as path from 'path';
import typescript from '@rollup/plugin-typescript';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import replace from '@rollup/plugin-replace';
const buildFolder = 'build/';
const distFolder = 'dist/';
const __dirname = process.cwd();
const envManifest = process.argv.includes('--configDev') ? 'manifest.dev.json' : 'manifest.prod.json';
export default [
{
input: 'src/background.ts',
output: {
file: `${buildFolder}background.js`,
},
plugins: [
typescript(),
replace({
'preventAssignment': true,
'process.env.NODE_ENV': JSON.stringify('production'),
}),
nodeResolve(),
],
},
{
input: 'src/lib/content-script.ts',
output: {
file: `${buildFolder}lib/content-script.js`,
},
plugins: [
typescript(),
copy('src/css', `${buildFolder}css`),
copy('src/images', `${buildFolder}images`),
mergeJson(['src/manifest.json', 'manifest.version.json', envManifest], `${buildFolder}manifest.json`),
],
},
{
input: 'src/module.ts',
output: {
file: `${distFolder}index.js`,
format: 'cjs',
},
plugins: [typescript({ target: 'es6', lib: ['es5', 'es6', 'ESNext', 'dom'] }), nodeResolve()],
},
];
function copy(source, destination) {
return {
name: 'copy',
writeBundle(output) {
fs.copySync(path.resolve(__dirname, source), path.resolve(__dirname, destination));
},
};
}
function mergeJson(jsonFiles, target) {
return {
name: 'mergeJson',
writeBundle(output) {
const [baseFile, ...otherJsonFiles] = jsonFiles
.map(file => path.resolve(__dirname, file))
.filter(file => fs.existsSync(file))
.map(file => JSON.parse(fs.readFileSync(file, 'utf-8')));
fs.writeFileSync(target, JSON.stringify(Object.assign({}, baseFile, ...otherJsonFiles), null, ' '));
},
};
}