-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrollup.config.js
191 lines (181 loc) · 4.9 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* Copyright (c) 2019-present, Jhuix (Hui Jin) <jhuix0117@gmail.com>. All rights reserved.
* Use of this source code is governed by a MIT license that can be found in the LICENSE file.
*/
import path from 'path';
// 帮助寻找node_modules里的包
import resolve from '@rollup/plugin-node-resolve';
// rollup 的 babel 插件,ES6转ES5
import babel from '@rollup/plugin-babel';
// 将非ES6语法的包转为ES6可用
import commonjs from '@rollup/plugin-commonjs';
// 混淆JS文件
import terser from '@rollup/plugin-terser';
// 集成zlib crypto等库
import builtins from 'rollup-plugin-node-builtins';
import globals from 'rollup-plugin-node-globals';
import json from '@rollup/plugin-json';
import copy from 'rollup-plugin-copy';
import pkg from './package.json';
//postcss plugins
import postcss from 'rollup-plugin-postcss';
import autoprefixer from 'autoprefixer';
import simplevars from 'postcss-simple-vars';
import nested from 'postcss-nested';
import banner from 'postcss-banner';
const isMinBuild = process.env.MIN === 'true';
const isFormatCJS = process.env.TARGET === 'cjs';
const isDemoBuild = process.env.DEMO === 'true';
const version = process.env.VERSION || pkg.version;
const out_file = (isFormatCJS ? pkg.module : pkg.main).replace(
'.min.js',
isMinBuild ? '.min.js' : '.js'
);
const filename = path.basename(out_file);
const jsbanner =
'/*!\n' +
` * ${filename} v${version}\n` +
` * Copyright (c) 2019-present, Jhuix (Hui Jin) <jhuix0117@gmail.com>\n` +
' * Released under the MIT License.\n' +
' */';
const cssbanner =
`css of showdowns v${version}\n` +
'Copyright (c) 2019-present, Jhuix (Hui Jin) <jhuix0117@gmail.com>\n' +
'Released under the MIT License.';
const config = {
input: 'src/showdowns.js',
output: {
file: out_file,
// 输出CJS格式,NODE.js模块规范通用;
// 输出UMD格式,各种模块规范通用.
format: isFormatCJS ? 'cjs' : 'umd',
// 打包后的全局变量,如浏览器端 window.showdowns;
name: 'showdowns',
sourcemap: true,
banner: jsbanner,
globals: {
ABCJS: 'ABCJS',
raphael: 'Raphael',
'flowchart.js': 'flowchart',
'@viz-js/viz': 'Viz',
mermaid: 'mermaid',
katex: 'katex',
wavedrom: 'WaveDrom',
vega: 'vega',
'vega-lite': 'vegaLite',
'vega-embed': 'vegaEmbed',
'@rokt33r/js-sequence-diagrams': 'Diagram',
'katex/dist/contrib/auto-render': 'renderMathInElement'
}
},
onwarn: (msg, warn) => {
if (!/Circular/.test(msg)) {
warn(msg);
}
},
// 作用:指出应将哪些模块视为外部模块,否则会被打包进最终的代码里
external: [
'abcjs',
'mermaid',
'katex',
'raphael',
'flowchart.js',
'@viz-js/viz',
'wavedrom',
'vega',
'vega-lite',
'vega-embed',
'@rokt33r/js-sequence-diagrams',
'katex/dist/contrib/auto-render'
],
plugins: [
json(),
postcss({
use: isFormatCJS ? ['nmcss', 'less'] : ['less'],
extract: true,
minimize: isMinBuild,
extensions: ['.css', '.less'],
plugins: [
autoprefixer(),
simplevars(),
nested(),
banner({
banner: cssbanner,
important: true
})
],
loaders: [
{
//生成CJS格式时,不处理node_modules目录下的相关CSS文件
name: 'nmcss',
test: /.*?(\\|\/)node_modules(\\|\/).*?\.css$/,
process: (context, payload) => {
return new Promise((resolve, reject) => {
resolve({ code: '' });
});
}
}
]
})
]
};
if (isFormatCJS) {
config.external.push('showdown', 'zlib', 'showdown-katex/src/asciimath-to-tex');
} else {
config.plugins.push(
babel({
exclude: '**/node_modules/**',
babelHelpers: 'bundled'
}),
resolve({
browser: true,
preferBuiltins: true,
// 选择module目录传递给解析插件
moduleDirectories: ['node_modules']
}),
commonjs({
include: ['node_modules/**']
}),
globals(),
builtins()
);
}
if (isMinBuild) {
config.plugins.push(
terser({
// include: [/^.+\.min\.js$/],
compress: {
pure_getters: true,
unsafe: true,
unsafe_comps: true,
drop_console: !isDemoBuild
}
})
);
}
if (!isFormatCJS) {
if (isDemoBuild) {
config.output.file = 'docs/' + config.output.file;
config.plugins.push(
copy({
targets: [
{ src: 'public/*', dest: 'docs' },
{ src: 'demo', dest: 'docs' },
{ src: 'logo.png', dest: 'docs' },
{ src: 'favicon.ico', dest: 'docs' },
{ src: 'dist/showdowns.min.*', dest: 'docs/dist' }
]
})
);
} else {
config.plugins.push(
copy({
targets: [
// Publish common dist resource
{ src: 'public/dist/*', dest: 'dist' }
]
})
);
}
}
export default config;