-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
161 lines (119 loc) · 4.13 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
import typescript from '@rollup/plugin-typescript'
import commonjs from '@rollup/plugin-commonjs'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import terser from "@rollup/plugin-terser"
import dts from "rollup-plugin-dts"
import html from '@rollup/plugin-html'
import copy from 'rollup-plugin-copy'
import livereload from 'rollup-plugin-livereload'
import serve from 'rollup-plugin-serve'
// config
const basePath = '.'
const globalName = 'LeaferX.gridPlugin' // <script /> 插件的全局变量名
const supportPlatforms = ['web','worker','node','miniapp']
const external = {'@leafer-ui/core': 'LeaferUI'} // 声明外部依赖,不打进插件包,只引用
const port = 12121 // visit http://localhost:12121
// ------
const isDev = process.env.NODE_ENV === 'development'
const platformName = process.env.PLATFORM
const platform ={
'all': {
name: 'index', // output index.esm.js index.js
path: basePath,
withFormat: supportPlatforms.includes('node') ? ['cjs'] : false,
withGlobal: globalName,
withMin: 'min',
external
}
}
const plugins = [
nodeResolve({
browser: true,
preferBuiltins: false,
}),
typescript({
tsconfig: './tsconfig.json'
}),
commonjs()
]
let config
if(isDev) {
config = {
input: 'main.ts',
output: {
file: 'dev/bundle.js',
format: 'esm'
},
watch: { exclude: ['node_modules/**'] },
plugins: [
...plugins,
html({
title: "Leafer Plugin",
meta: [{charset: 'utf-8'}, {name: 'viewport', content: 'width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no'}]
}),
copy({ targets: [{ src: 'public/*', dest: 'dev/' }]}),
livereload(),
serve({contentBase: ['dev/'], port})
]
}
} else {
// build
config = [ { // types/index.d.ts
input: basePath + '/src/index.ts',
output: {
file: basePath + '/types/index.d.ts'
},
plugins: [ dts() ]
}]
let p = platform[platformName]
if(!(p instanceof Array)) p = [p]
const list = []
p.forEach(c =>{
if(c.input && c.output) {
list.push(c)
} else {
const input = c.input || c.path + '/src/index.ts'
const fileBase = c.path + '/dist/' + (c.name || platformName)
const global = c.withGlobal
const min = c.withMin
let external = c.external
list.push({external, input, output: fileBase + '.esm.js'})
if(c.withMin) list.push({ min, external, input, output: fileBase + '.esm.' + min + '.js'})
if(c.withFormat) {
c.withFormat.forEach(format =>{
const cjs = format === 'cjs'
list.push({external, input, output: fileBase + (cjs ? '.cjs' : '.' + format + '.js'), format})
if(c.withMin) list.push({ min, external, input, output: fileBase + (cjs ? '.' + min + '.cjs' :'.' + format + '.' + min + '.js'), format})
})
}
if(global) {
if(c.fullGlobal) external = null
list.push({global, external, input, output: fileBase + '.js'})
if(c.withMin) list.push({ global, min, external, input, output: fileBase + '.' + min + '.js'})
}
}
})
list.forEach(c => {
const item = {
external: c.external ? Object.keys(c.external) : null,
input: c.input,
plugins: [...plugins]
}
if(c.global) {
item.output = {
file: c.output,
name: c.global,
format: c.format || 'iife',
}
if(c.external) item.output.globals = c.external
} else {
item.output = {
file: c.output,
format: c.format || 'esm'
}
}
if(c.min) item.plugins.push(terser({ format: { comments: false} }))
config.push(item)
})
}
export default config