-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
106 lines (104 loc) · 3.33 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
import elm from 'rollup-plugin-elm'
import serve from 'rollup-plugin-serve'
import { terser } from 'rollup-plugin-terser'
import resolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import processEnv from './rollup-plugin-process-env.js'
import html from 'rollup-plugin-bundle-html'
import CleanCSS from 'clean-css'
import path from 'path'
import fse from 'fs-extra'
import { createFilter } from 'rollup-pluginutils'
const production = process.env.NODE_ENV === 'production'
const dir = production ? '.' : './dist'
export default {
input: 'src/index.js',
output: {
file: `${dir}/bundle.js`,
format: 'iife',
sourcemap: !production
},
plugins: [
resolve({
browser: true
}),
commonjs(),
elm({
exclude: 'elm_stuff/**',
compiler: {
optimize: production,
debug: !production
}
}),
html({
template: 'src/template.html',
dest: dir,
filename: 'index.html',
externals: [
{ type: 'css', file: 'https://fonts.googleapis.com/css?family=Playfair+Display:400,700&display=swap' }
],
// It otherwise looks for all JS/CSS files in the dest folder, which is rather
// undesirable
ignore: `^\\./(?!${production ? '' : 'dist/'}bundle\\.(js|css))`
}),
// Based on
// https://github.com/RJHwang/rollup-plugin-css-porter/blob/master/src/index.js
// and
// https://github.com/Evercoder/rollup-plugin-css-bundle/blob/master/src/index.js
// to preserve override order and be able to minify
(() => {
const styles = {}
const minifier = new CleanCSS({
format: production ? undefined : 'beautify',
level: production ? 2 : 0,
// ??? This works?
rebaseTo: path.resolve(__dirname, production ? '..' : '../images')
})
const cssFilter = createFilter('**/*.css')
return {
name: 'CSS bundlifier',
transform (code, id) {
if (!cssFilter(id)) return
styles[id] = code
return ''
},
generateBundle (opts, bundle) {
for (const [file, { modules }] of Object.entries(bundle)) {
const moduleNames = Array.isArray(modules) ? modules : Object.keys(modules)
const css = Object.entries(styles)
.sort(([a], [b]) => moduleNames.indexOf(a) - moduleNames.indexOf(b))
.map(([_, css]) => css)
.join('\n')
const output = minifier.minify(css)
if (output.errors.length) throw new Error(output.errors.join('\n'))
if (output.warnings.length) console.warn(output.warnings.join('\n'))
fse.outputFile(
path.join(
path.dirname(opts.file),
path.basename(file, path.extname(opts.file)) + '.css'
),
output.styles
)
}
}
}
})(),
processEnv(),
process.env.ROLLUP_WATCH && serve({
contentBase: production ? [''] : ['dist', 'images'],
port: 8080
}),
production && terser({
compress: {
pure_funcs: ['F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9'],
pure_getters: true,
keep_fargs: false,
unsafe_comps: true,
unsafe: true
}
})
],
watch: {
include: ['src/**', 'css/**']
}
}