-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared.config.ts
110 lines (100 loc) · 2.46 KB
/
shared.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
import { createRequire } from 'node:module';
import * as path from 'node:path';
import { defineConfig } from 'vite';
import atImport from 'postcss-import';
import atUrl from 'postcss-url';
import flexBugs from 'postcss-flexbugs-fixes';
import cssnano from 'cssnano';
import autoprefixer from 'autoprefixer';
import type { UserConfig } from 'vite';
const cwd = process.cwd();
// options
const buildOptions = JSON.parse(decodeURIComponent(process.env.BUILD_OPTIONS || '{}'));
const {
format,
external = '',
globals = '',
workspace,
files = [],
packageName,
packageSourceDir,
packageOptions = {}
} = buildOptions;
const usedForBrowser = /(iife|umd)/.test(format);
const external$ = !usedForBrowser
? [
/^node:/,
/^[a-zA-Z@]/,
...Object
.keys({
...packageOptions.dependencies,
...packageOptions.peerDependencies
})
.map(i => new RegExp(`^${i}$`))
]
: external.split(',')
.filter((i: string) => !!i)
.map((i: string) => new RegExp(`^${i}$`));
// alias
const replacement = (name: string) => path.resolve(cwd, `./packages/${name}`);
const { name } = createRequire(cwd)(path.resolve(cwd, workspace ? `${workspace}/index` : '', 'package.json'));
const alias = workspace && usedForBrowser
? [
{
find: new RegExp(`^${name}$`),
replacement: replacement('index')
},
{
find: new RegExp(`^${name}-(.*?)$`),
replacement: replacement('$1')
}
]
: [];
const getGlobalName = (name$: string) => name$.replace(/(_|-|^|.*\/)([^-_\/@])/g, (_match: any, _$1: any, $2: string) => $2.toUpperCase());
export default defineConfig({
define: usedForBrowser
? {
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
}
: {},
logLevel: 'silent',
plugins: [],
css: {
postcss: {
plugins: [
atImport(),
atUrl(),
flexBugs(),
cssnano(),
autoprefixer({ remove: false })
]
}
},
resolve: { alias },
build: {
minify: false,
target: 'esnext',
lib: {
entry: files.map((file: string) => path.resolve(packageSourceDir, file)),
formats: [format],
name: getGlobalName(packageName)
},
rollupOptions: {
external: external$,
output: {
exports: 'named',
globals: usedForBrowser
? (globals || external).split(',')
.filter((i: string) => !!i)
.reduce((pre: any, cur: string) => {
const [key, value] = cur.split(':');
if (key) {
pre[key] = value || getGlobalName(key);
}
return pre;
}, {})
: {}
}
}
}
}) as UserConfig;