generated from nachocodoner/simpletasks-webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.js
148 lines (135 loc) · 4.12 KB
/
vite.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
// vite.config.js
import { defineConfig } from 'vite';
import { visualizer } from 'rollup-plugin-visualizer';
import copy from 'rollup-plugin-copy';
import stripCode from 'rollup-plugin-strip-code';
const isDevelopment = process.env.npm_lifecycle_script.includes('mode=development');
const enableBundleVisualizer = process.env.ENABLE_BUNDLE_VISUALIZER === 'true';
const developmentVsProductionDefineConfig = isDevelopment ? {
'Meteor.isDevelopment': JSON.stringify(true),
'Meteor.isProduction': JSON.stringify(false),
} : {
'Meteor.isDevelopment': JSON.stringify(false),
'Meteor.isProduction': JSON.stringify(true),
};
function excludeBlockStrip(excludeConfig) {
return excludeConfig?.exclude
? {
...stripCode({
start_comment: `${excludeConfig.exclude}:start`,
end_comment: `${excludeConfig.exclude}:end`,
}),
enforce: 'pre',
apply: 'build',
}
: undefined;
}
const excludeBlockStripByMode = isDevelopment
? excludeBlockStrip({ exclude: 'production' })
: excludeBlockStrip({ exclude: 'development' });
const clientCommonConfig = {
main: './ui/main.jsx',
build: {
outDir: 'client',
emptyOutDir: false,
target: 'modules',
rollupOptions: {
input: {
main: './ui/main.jsx',
},
output: {
entryFileNames: 'client.js',
},
external: [
/^(meteor.*|react|react-dom)/,
],
plugins: [
copy({ targets: [{ src: './ui/main.html', dest: 'client' }] }),
enableBundleVisualizer && visualizer({ open: true, filename: 'public/stats.html' }),
].filter(Boolean),
},
minify: false,
},
define: {
'Meteor.isClient': JSON.stringify(true),
'Meteor.isServer': JSON.stringify(false),
'Meteor.isTest': JSON.stringify(false),
...developmentVsProductionDefineConfig,
},
plugins: [
excludeBlockStrip({ exclude: 'server' }),
excludeBlockStripByMode,
],
};
const serverCommonConfig = {
main: './api/main.js',
build: {
outDir: 'server',
emptyOutDir: false,
target: 'modules',
rollupOptions: {
input: {
main: './api/main.js',
},
output: {
entryFileNames: 'server.js',
},
external: [
/^meteor\/.*/,
],
plugins: [
enableBundleVisualizer && visualizer({ open: true, filename: 'public/stats.html' }),
].filter(Boolean),
},
polyfillDynamicImport: false,
minify: false,
},
define: {
'Meteor.isClient': JSON.stringify(true),
'Meteor.isServer': JSON.stringify(false),
'Meteor.isTest': JSON.stringify(false),
...developmentVsProductionDefineConfig,
},
plugins: [
excludeBlockStrip({ exclude: 'client' }),
excludeBlockStripByMode,
],
};
const clientDevelopmentConfig = {
...clientCommonConfig,
};
const serverDevelopmentConfig = {
...serverCommonConfig,
};
const clientProductionConfig = {
...clientCommonConfig,
build: {
...clientCommonConfig.build,
minify: true,
},
};
const serverProductionConfig = {
...serverCommonConfig,
build: {
...serverCommonConfig.build,
minify: true,
},
};
const target = process.env.TARGET || 'client';
const isClient = target === 'client';
const isServer = target === 'server';
// eslint-disable-next-line import/no-default-export
export default defineConfig(({ mode }) => {
switch (mode) {
case 'development':
if (isClient) return clientDevelopmentConfig;
if (isServer) return serverDevelopmentConfig;
return clientDevelopmentConfig;
case 'production':
if (isClient) return clientProductionConfig;
if (isServer) return serverProductionConfig;
return clientProductionConfig;
default:
return clientProductionConfig;
}
});