forked from discordier/sam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.js
124 lines (115 loc) · 3.11 KB
/
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
const path = require('path');
const babel = require('@rollup/plugin-babel').default;
const replace = require('@rollup/plugin-replace');
const version = process.env.VERSION || require('../package.json').version;
const banner =
'/**\n' +
' * This is SamJs.js v' + version + '\n' +
' *\n' +
' * A Javascript port of "SAM Software Automatic Mouth".\n' +
' *\n' +
' * (c) 2017-' + new Date().getFullYear() + ' Christian Schiffler\n' +
' *\n' +
' * @link(https://github.com/discordier/sam)\n' +
' *\n' +
' * @author 2017 Christian Schiffler <[email protected]>\n' +
' */';
const resolve = p => {
return path.resolve(__dirname, '../', p)
};
const buildSrc = [
{
name: '',
entry: 'src/index.es6',
dest: 'samjs',
moduleName: 'SamJs',
},
{
name: 'guessnum-demo',
entry: 'src/guessnum.es6',
dest: 'guessnum',
moduleName: 'GuessNum',
},
]
const builds = {}
buildSrc.map((file) => {
[
// Browser build
{
name: '',
suffix: '',
format: 'umd',
},
// CommonJS. Used by bundlers e.g. Webpack & Browserify
{
name: 'cjs',
suffix: '.common',
format: 'cjs',
},
// ES Modules. Used by bundlers that support ES Modules, e.g. Rollup & Webpack 2
{
name: 'esm',
suffix: '.esm',
format: 'es',
},
].map((build) => {
const name = (build.name ? '-' + build.name : '') + (file.name ? '-' + file.name : '')
builds['dev' + name] = {
entry: resolve(file.entry),
dest: resolve(`dist/${file.dest}${build.suffix}.js`),
moduleName: file.moduleName,
format: build.format,
env: 'development',
banner
}
builds['prod' + name] = {
entry: resolve(file.entry),
dest: resolve(`dist/${file.dest}${build.suffix}.min.js`),
moduleName: file.moduleName,
format: build.format,
env: 'production',
banner
}
})
})
function genConfig (opts) {
let moduleName = opts.moduleName;
const config = {
input: opts.entry,
output: {
file: opts.dest,
format: opts.format,
name: moduleName,
banner: opts.banner,
exports: 'default',
},
external: (opts.external || []).concat([/@babel\/runtime/]),
plugins: [
replace({
preventAssignment: true,
__VERSION__: version
}),
babel({
babelHelpers: 'runtime',
shouldPrintComment: () => opts.env === 'development',
minified: opts.env !== 'development'
})
].concat(opts.plugins || [])
};
if (opts.env) {
config.plugins.push(replace({
preventAssignment: true,
'process.env.NODE_ENV': JSON.stringify(opts.env),
'process.env.DEBUG_SAM': JSON.stringify(opts.env === 'development'),
}))
// convert "const" to "let", as this saves two bytes per instance.
config.plugins.push(replace({preventAssignment: true, 'const ': 'let '}))
}
return config
}
if (process.env.TARGET) {
module.exports = genConfig(builds[process.env.TARGET])
} else {
exports.getBuild = name => genConfig(builds[name]);
exports.getAllBuilds = () => Object.keys(builds).map(name => genConfig(builds[name]))
}