-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
114 lines (106 loc) · 2.68 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
111
112
113
114
import * as path from 'path'
import * as fs from 'fs'
import peerDepsExternal from 'rollup-plugin-peer-deps-external'
import resolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import typescript from 'rollup-plugin-typescript2'
import postcss from 'rollup-plugin-postcss'
import json from '@rollup/plugin-json'
import babel from '@rollup/plugin-babel'
import dts from 'rollup-plugin-dts'
import sass from 'sass'
const entry = 'src/index.ts'
const componentsDir = 'src/packages'
const componentsName = fs.readdirSync(path.resolve(componentsDir))
const componentsEntry = componentsName.map(
(name) => `${componentsDir}/${name}/index.ts`
)
const isProd = process.env.NODE_ENV === 'production'
const BABEL_ENV = process.env.BABEL_ENV
const babelOptions = {
presets: ['@babel/preset-env'],
extensions: ['.js', '.jsx', '.ts', '.tsx', '.scss'],
exclude: 'node_modules/**'
}
const commonPlugins = [
peerDepsExternal(),
resolve(),
commonjs({ sourceMap: !isProd }),
typescript(),
babel({
...babelOptions,
exclude: 'node_modules/**',
}),
json(),
]
const externalConfig = [
id => /\/__expample__|main.tsx/.test(id),
'react',
'react-dom',
'classname',
'node_modules/**'
]
// sass打包
const processScss = function (context) {
return new Promise((resolve, reject) => {
sass.compile(
{
file: context
},
function (err, result) {
if (!err) {
resolve(result)
} else {
reject(result)
}
}
);
sass.compile(context, {}).then(
function (output) {
if (output && output.css) {
resolve(output.css)
} else {
reject({})
}
},
function (err) {
reject(err)
}
)
})
}
const esmOutput = {
preserveModules: true,
preserveModulesRoot: 'src',
exports: 'named',
assetFileNames: ({ name }) => {
const { ext, dir, base } = path.parse(name);
if (ext !== '.css') return '[name].[ext]';
return path.join(dir, 'style', base);
}
}
export default () => {
switch (BABEL_ENV) {
case 'esm':
return [
{
input: [entry, ...componentsEntry],
output: { ...esmOutput, dir: 'dist', format: 'es' },
external: externalConfig,
plugins: [postcss({
extract: true,
process: processScss
}), ...commonPlugins]
},
{
input: [entry, ...componentsEntry],
output: { ...esmOutput, dir: 'dist/type', format: 'es' },
external: externalConfig,
plugins: [postcss({
extract: true,
process: processScss
}), ...commonPlugins, dts()]
}
]
}
}