forked from sonofmagic/weapp-tailwindcss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config.ts
127 lines (120 loc) · 3.27 KB
/
rollup.config.ts
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
import { readFileSync } from 'node:fs'
import * as path from 'node:path'
import typescript from '@rollup/plugin-typescript'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import json from '@rollup/plugin-json'
import { visualizer } from 'rollup-plugin-visualizer'
import type { PackageJson } from 'pkg-types'
// import terser from '@rollup/plugin-terser'
// import { resolve } from 'path'
import type { RollupOptions } from 'rollup'
import lodash from 'lodash'
const { omit } = lodash
const pkg = JSON.parse(
readFileSync('./package.json', {
encoding: 'utf8'
})
) as PackageJson
const isProd = process.env.NODE_ENV === 'production'
const isDev = process.env.NODE_ENV === 'development'
const isDemo = process.env.NODE_ENV === 'demo'
interface IEntry {
name?: string
input?: RollupOptions['input']
output?: RollupOptions['output'][]
}
const createSharedConfig: (entry: IEntry) => RollupOptions = (entry) => {
return {
makeAbsoluteExternalsRelative: true,
preserveEntrySignatures: 'strict',
plugins: [
json(),
nodeResolve({
preferBuiltins: true
}),
commonjs(),
typescript({
tsconfig: isDemo ? './tsconfig.demo.json' : './tsconfig.build.json',
sourceMap: isDev || isDemo,
declaration: false
}),
isProd
? visualizer({
// emitFile: true,
filename: `stats/${entry.name}.html`
})
: undefined
],
external: [...(pkg.dependencies ? Object.keys(pkg.dependencies) : []), 'webpack', 'loader-utils']
}
}
// 没有必要压缩徒增调试成本
// if (isProd) {
// sharedConfig.plugins.push(terser())
// }
const mainOutputOptions: Partial<RollupOptions['output']> = {
sourcemap: isDev || isDemo,
exports: 'auto',
esModule: true,
generatedCode: {
reservedNamesAsProps: false
},
interop: 'compat',
systemNullSetters: false,
sourcemapPathTransform: (relativeSourcePath, sourcemapPath) => {
if (isDemo) {
return path.resolve(path.dirname(sourcemapPath), '../../../', relativeSourcePath.replaceAll(/\.\.[/\\]/g, ''))
}
return relativeSourcePath
}
}
const entries: IEntry[] = [
{
name: 'bundle',
input: {
index: 'src/index.ts',
webpack: 'src/webpack.ts',
gulp: 'src/gulp.ts',
postcss: 'src/postcss.ts',
cli: 'src/cli.ts',
replace: 'src/replace.ts',
vite: 'src/vite.ts',
'weapp-tw-runtime-loader': 'src/webpack/loaders/weapp-tw-runtime-loader.ts'
},
output: [
{
dir: isDemo ? 'demo/web/weapp-tw-dist' : 'dist',
format: 'cjs',
...mainOutputOptions
},
{
dir: isDemo ? 'demo/web/weapp-tw-dist' : 'dist',
format: 'esm',
...mainOutputOptions,
entryFileNames: '[name].mjs',
chunkFileNames: '[name]-[hash].mjs'
}
]
}
// {
// name: 'loader',
// input: {
// 'weapp-tw-runtime-loader': 'src/webpack/loaders/weapp-tw-runtime-loader.ts'
// },
// output: [
// {
// dir: 'lib',
// format: 'cjs',
// ...mainOutputOptions
// }
// ]
// }
]
const config = entries.map((x) => {
return {
...omit(x, ['name']),
...createSharedConfig(x)
}
}) as RollupOptions[]
export default config