/
rollup.config.site.js
54 lines (47 loc) · 1.45 KB
/
rollup.config.site.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
import glob from "glob"
import path from "path"
import { terser } from "rollup-plugin-terser"
import resolve from "@rollup/plugin-node-resolve"
import babel from "rollup-plugin-babel"
import commonjs from "@rollup/plugin-commonjs"
import alias from "@rollup/plugin-alias"
const absolutePath = dirPath => path.resolve(__dirname, dirPath)
const scriptFiles = glob.sync(absolutePath("site/_scripts/**/!(_)*.js"))
const scriptsTarget = "site/assets/js/"
const isProd = process.env.BABEL_ENV === "production"
const inputs = scriptFiles.reduce((files, input) => {
const parts = input.split("/")
const fileKey = parts[parts.length - 1]
return { [fileKey]: absolutePath(input), ...files }
}, {})
const outputs = Object.keys(inputs).reduce((files, file) => {
const inputPath = inputs[file]
const parts = inputPath.split("/")
const pathIndex = parts.indexOf("_scripts") + 1
const outputPath = parts.slice(pathIndex).join("/")
return { [file]: absolutePath(scriptsTarget + outputPath), ...files }
}, {})
const plugins = [
resolve(),
commonjs(),
babel({
exclude: "node_modules/**",
comments: false,
}),
alias({
entries: [{ find: "undernet", replacement: "../../src/js" }],
}),
]
if (isProd) plugins.push(terser())
const bundles = Object.keys(inputs).map(key => {
return {
input: inputs[key],
output: {
file: outputs[key],
format: "iife",
sourcemap: isProd ? false : true,
},
plugins,
}
})
export default bundles