forked from kriasoft/isomorphic-style-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
117 lines (109 loc) · 3.23 KB
/
build.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
/**
* Isomorphic CSS style loader for Webpack
*
* Copyright © 2015-present Kriasoft, LLC. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE.txt file in the root directory of this source tree.
*/
const fs = require('fs-extra')
const path = require('path')
const rollup = require('rollup')
const babel = require('rollup-plugin-babel')
const { uglify } = require('rollup-plugin-uglify')
const commonjs = require('rollup-plugin-commonjs')
const nodeResolve = require('rollup-plugin-node-resolve')
const pkg = require('../package.json')
// The source files to be compiled by Rollup
const files = [
{
input: 'dist/src/index.js',
output: 'dist/index.js',
format: 'cjs',
external: ['loader-utils'],
},
{
input: 'dist/src/withStyles.js',
output: 'dist/withStyles.js',
format: 'cjs',
external: ['react', 'hoist-non-react-statics', path.resolve('dist/src/StyleContext.js')],
paths: { [path.resolve('dist/src/StyleContext.js')]: './StyleContext.js' },
},
{
input: 'dist/src/useStyles.js',
output: 'dist/useStyles.js',
format: 'cjs',
external: ['react', path.resolve('dist/src/StyleContext.js')],
paths: { [path.resolve('dist/src/StyleContext.js')]: './StyleContext.js' },
},
{
input: 'dist/src/StyleContext.js',
output: 'dist/StyleContext.js',
format: 'cjs',
external: ['react'],
},
{
input: 'dist/src/insertCss.js',
output: 'dist/insertCss.js',
format: 'cjs',
},
]
async function build() {
// Clean up the output directory
await fs.emptyDir('dist')
// Copy source code, readme and license
await Promise.all([
fs.copy('src', 'dist/src'),
fs.copy('README.md', 'dist/README.md'),
fs.copy('LICENSE.txt', 'dist/LICENSE.txt'),
])
// Compile source code into a distributable format with Babel
await Promise.all(
files.map(async (file) => {
const bundle = await rollup.rollup({
input: file.input,
external: file.external,
plugins: [
...(file.format === 'umd' ? [nodeResolve({ browser: true }), commonjs()] : []),
babel({
babelrc: false,
presets: [
'@babel/preset-react',
[
'@babel/preset-env',
{
corejs: 3,
modules: false,
loose: true,
useBuiltIns: 'entry',
},
],
],
comments: false,
}),
...(file.output.endsWith('.min.js') ? [uglify({ output: { comments: '/^!/' } })] : []),
],
})
bundle.write({
file: file.output,
format: file.format,
interop: false,
sourcemap: true,
name: file.name,
banner:
'/*! Isomorphic Style Loader' +
' | MIT License' +
' | https://github.com/kriasoft/isomorphic-style-loader */\n',
globals: file.globals,
paths: file.paths,
})
}),
)
// Create package.json for npm publishing
const libPkg = { ...pkg, main: 'index.js' }
delete libPkg.private
delete libPkg.devDependencies
delete libPkg.scripts
await fs.outputJson('dist/package.json', libPkg, { spaces: 2 })
}
module.exports = build()