This repository has been archived by the owner on May 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rollup.config.js
110 lines (99 loc) · 2.54 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
import babel from 'rollup-plugin-babel'
import resolve from 'rollup-plugin-node-resolve'
import postcss from 'rollup-plugin-postcss'
import copy from 'rollup-plugin-copy-assets'
import { terser } from 'rollup-plugin-terser'
import * as path from 'path'
import serve from 'rollup-plugin-serve'
const babelConfig = require('./.babelrc.js')
const development = process.env.NODE_ENV === 'development'
const createBabelConfig = (modules = false) => {
const babelConfigModule = JSON.parse(JSON.stringify(babelConfig))
babelConfigModule.presets[0][1].targets = { esmodules: modules }
return babelConfigModule
}
const babelExts = ['.js', '.ts', '.tsx', '.mjs']
const plugins = {
resolve: resolve({ extensions: ['.js', '.ts', '.tsx', '.mjs', '.sss'] }),
babel: babel({
babelrc: false,
extensions: babelExts,
...createBabelConfig(false)
}),
babelModule: babel({
babelrc: false,
extensions: babelExts,
...createBabelConfig(true)
}),
terser: terser({
module: true,
compress: {
passes: 2,
unsafe_comps: true,
unsafe_math: true
}
}),
postcss: postcss({
extract: 'dist/index.css',
minimize: true,
modules: {
generateScopedName: development
? '[local]-[hash:base64:4]'
: '[hash:base64:4]'
}
}),
copy: copy({
assets: [
'./src/index.html',
'./src/assets',
'./src/_redirects',
'./src/_headers',
'./src/browserconfig.xml',
'./src/manifest.json'
]
})
}
const createConfig = ({
input,
outFormats = ['iife'],
plugins: appendPlugins = []
}) => {
const p = [plugins.resolve, ...appendPlugins]
if (!development) {
p.push(plugins.terser)
}
return outFormats.map(format => {
return {
input: format === 'es' ? [input] : input,
plugins: p.concat(format === 'es' ? plugins.babelModule : plugins.babel),
experimentalCodeSplitting: true,
output: {
...(format !== 'es'
? {
file: path.join(
'dist',
path.basename(input).replace(/\.[^/.]+$/, '') + '.nomodule.js'
)
}
: { dir: 'dist' }),
format,
sourcemap: development
}
}
})
}
const swConfig = createConfig({
input: './src/sw.ts',
plugins: [plugins.copy]
})
const config = createConfig({
input: './src/index.tsx',
outFormats: ['iife', 'es'],
plugins: [plugins.postcss]
})
if (development) {
config[0].plugins.push(
serve({ historyApiFallback: true, contentBase: 'dist' })
)
}
export default [...config, ...swConfig]