-
Notifications
You must be signed in to change notification settings - Fork 14
/
rollup.config.js
125 lines (107 loc) · 3.6 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import path from 'path';
import globby from 'globby';
import fs from 'fs-extra';
import buble from 'rollup-plugin-buble';
import { terser } from 'rollup-plugin-terser';
import resolve from 'rollup-plugin-node-resolve';
import postcss from 'rollup-plugin-postcss';
import replace from 'rollup-plugin-replace';
import alias from 'rollup-plugin-alias';
import VuePlugin from 'rollup-plugin-vue';
require('dotenv').config();
const isProdBuild = process.env.NODE_ENV === 'production';
const resourcePath = path.resolve(__dirname, 'src', 'main', 'resources');
const ideaResourcePath = path.resolve(__dirname, 'out', 'production', 'resources');
const sourcePath = path.resolve(resourcePath, 'src', '**/*.mjs');
const destinationPath = path.resolve(resourcePath, 'static', 'js', 'build');
function getOutputFileName(file) {
const fileName = path.basename(file, path.extname(file));
const pathDiff = path.dirname(path.relative(path.resolve(resourcePath, 'src'), file));
return path.resolve(destinationPath, pathDiff, fileName + '.min.js');
}
function getModuleName(file) {
const fileName = path.basename(file, path.extname(file));
return fileName.replace(/-/g, '_');
}
function getOutputFileContent(
outputFileName,
outputFile,
outputOptions
) {
if (typeof outputFile.code === 'string') {
let source = outputFile.code;
if (outputOptions.sourcemap && outputFile.map) {
const url =
outputOptions.sourcemap === 'inline'
? outputFile.map.toUrl()
: `${path.basename(outputFileName)}.map`;
// https://github.com/rollup/rollup/blob/master/src/utils/sourceMappingURL.ts#L1
source += `//# source` + `MappingURL=${url}\n`
}
return source
} else {
return outputFile
}
}
function copy() {
return {
name: 'copy',
generateBundle(outputOptions, bundle, isWrite) {
if (!isWrite)
return;
const relativePath = path.relative(resourcePath, outputOptions.file);
const ideaOutputPath = path.resolve(ideaResourcePath, relativePath);
const directory = path.dirname(ideaOutputPath);
return Promise.all(Object.keys(bundle).map(async fileName => {
const fileEntry = bundle[fileName];
const fileContent = getOutputFileContent(
fileName,
fileEntry,
outputOptions,
);
await fs.mkdirp(directory);
await fs.writeFile(ideaOutputPath, fileContent);
}));
}
}
}
async function getConfig() {
const paths = await globby(sourcePath);
const plugins = [
postcss({
minimize: isProdBuild,
plugins: []
}),
resolve(),
alias({
'vue-compiler': 'node_modules/vue/dist/vue.esm.js'
}),
VuePlugin(),
replace({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
}),
buble({
exclude: ['**/vue/dist/vue*.esm.js'],
transforms: {
dangerousForOf: true
},
objectAssign: 'Object.assign'
}),
copy()
];
if (isProdBuild) {
plugins.push(terser());
}
return paths.map(file => ({
input: file,
output: {
file: getOutputFileName(file),
name: getModuleName(file),
format: 'iife',
sourcemap: isProdBuild
},
context: 'window',
plugins: plugins
}));
}
export default getConfig();