forked from knockout/tko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
101 lines (88 loc) · 2.82 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
import * as fs from 'fs'
import * as path from 'path'
import alias from 'rollup-plugin-alias'
import babelMinify from 'rollup-plugin-babel-minify'
import nodeResolve from 'rollup-plugin-node-resolve'
import replace from 'rollup-plugin-replace'
import typescript from 'rollup-plugin-typescript'
import rollupVisualizer from 'rollup-plugin-visualizer'
import license from 'rollup-plugin-license'
import * as pkg from './package.json'
const { LERNA_PACKAGE_NAME, LERNA_ROOT_PATH } = process.env
const PACKAGE_ROOT_PATH = path.join(LERNA_ROOT_PATH, 'packages', LERNA_PACKAGE_NAME)
const IS_BROWSER_BUNDLE = LERNA_PACKAGE_NAME === 'tko'
const INPUT_FILE = path.join(PACKAGE_ROOT_PATH, 'src/index.js')
const TKO_MODULES = getTkoModules()
const banner = `/*!
* <%= pkg.description %> 🥊 <%= pkg.name %>@${pkg.version}
* (c) The Knockout.js Team - ${pkg.homepage}
* License: ${pkg.licenses[0].type} (${pkg.licenses[0].url})
*/
`
/* Use TypeScript instead of babel for transpilation for IE6 compat, plus it's faster */
const TYPESCRIPT_CONFIG = {
include: '**/*.js',
exclude: 'node_modules',
typescript: require('typescript')
}
/* Plugins used for all builds */
const UNIVERSAL_PLUGINS = [
/* Replace {{VERSION}} with pkg.json's `version` */
replace({ delimiters: ['{{', '}}'], VERSION: pkg.version }),
nodeResolve({ module: true }),
rollupVisualizer({ filename: 'visual.html' }),
license({ sourceMap: true, banner })
]
export default [
/* tko.<module?>.es6.js */
createRollupConfig(),
/* tko.<module?>.js */
createRollupConfig({ transpile: true }),
...(IS_BROWSER_BUNDLE
? [
/* tko.es6.min.js */
createRollupConfig({ minify: true }),
/* tko.min.js */
createRollupConfig({ minify: true, transpile: true })
]
: []
)
]
function getTkoModules () {
return fs.readdirSync(path.resolve(__dirname, 'packages'))
}
function getTkoES6Aliases () {
return TKO_MODULES.reduce((accum, tkoModule) => Object.assign(accum, {
[tkoModule]: path.resolve(
LERNA_ROOT_PATH,
`packages/${tkoModule}/dist/${tkoModule}.es6.js`
)
}))
}
function createRollupConfig ({ minify, transpile } = {}) {
let filename = path.join(PACKAGE_ROOT_PATH, 'dist', LERNA_PACKAGE_NAME)
const plugins = [...UNIVERSAL_PLUGINS]
if (transpile) {
plugins.push(typescript(TYPESCRIPT_CONFIG))
} else {
/* must come before resolve plugin */
plugins.unshift(alias(getTkoES6Aliases()))
filename += '.es6'
}
if (minify) {
plugins.push(babelMinify({ comments: false }))
filename += '.min'
}
filename += '.js'
return {
plugins,
input: INPUT_FILE,
external: IS_BROWSER_BUNDLE ? undefined : TKO_MODULES,
output: {
file: filename,
format: IS_BROWSER_BUNDLE ? 'umd' : 'es',
name: LERNA_PACKAGE_NAME === 'tko' ? 'ko' : LERNA_PACKAGE_NAME
},
sourcemap: true
}
}