Repository navigation
Expand file tree
/
Copy pathvite.config.js
More file actions
170 lines (165 loc) · 7.27 KB
/
Copy pathvite.config.js
File metadata and controls
170 lines (165 loc) · 7.27 KB
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
import { defineConfig, build } from 'vite';
import path from 'path';
import fs from 'fs';
import { fileURLToPath } from 'url';
import dts from 'vite-plugin-dts';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// <maplat-ui> の版(src/element/maplat-ui.ts の __MAPLAT_UI_VERSION__)。外側のビルドと入れ子の UMD のビルドの両方に入れる
const PACKAGE_VERSION = JSON.parse(fs.readFileSync(path.resolve(__dirname, 'package.json'), 'utf-8')).version;
const VERSION_DEFINE = { __MAPLAT_UI_VERSION__: JSON.stringify(PACKAGE_VERSION) };
// Detect build mode: 'package' for npm package, anything else for demo
const BUILD_MODE = process.env.BUILD_MODE;
const isPackageBuild = BUILD_MODE === 'package';
const PACKAGE_EXTERNAL = [
'ol',
'mapbox-gl',
'maplibre-gl',
/^.*\/service-worker\// // Exclude service-worker from library build
];
const PACKAGE_GLOBALS = {
ol: 'ol',
'mapbox-gl': 'mapboxgl',
'maplibre-gl': 'maplibregl'
};
const UMD_FOOTER = 'if (typeof window !== "undefined" && window.MaplatUi && window.MaplatUi.MaplatUi) { window.MaplatUi = window.MaplatUi.MaplatUi; }';
// CSS(SCSS)の設定。外側のビルドと、入れ子の UMD ビルド(`configFile: false` なので外側の設定を
// 継がない)の両方に同じものを渡す(1 か所で持つ)。
const CSS_OPTIONS = {
preprocessorOptions: {
scss: {
silenceDeprecations: ['legacy-js-api', 'import', 'color-functions', 'global-builtin', 'if-function'],
api: 'modern-compiler'
}
}
};
export default defineConfig(({ command }) => ({
// Dev server: always use '/'
// Package build: use '/' (library, base doesn't matter)
// Demo build: use '/Maplat/' for GitHub Pages
base: command === 'serve' ? '/' : (isPackageBuild ? '/' : '/Maplat/'),
define: VERSION_DEFINE,
resolve: {},
plugins: [
{
name: 'copy-locales',
closeBundle() {
const outDir = isPackageBuild ? 'dist' : 'dist-demo';
const srcDir = path.resolve(__dirname, 'assets/locales');
const destDir = path.resolve(__dirname, `${outDir}/assets/locales`);
if (fs.existsSync(srcDir)) {
if (!fs.existsSync(destDir)) {
fs.mkdirSync(destDir, { recursive: true });
}
fs.cpSync(srcDir, destDir, { recursive: true, force: true });
console.log(`Copied assets/locales to ${outDir}/assets/locales`);
}
}
},
{
name: 'serve-static-assets-files',
configureServer(server) {
server.middlewares.use((req, res, next) => {
if (req.url.startsWith('/assets/')) {
const file = path.join(__dirname, req.url);
if (fs.existsSync(file) && fs.statSync(file).isFile()) {
if (req.url.endsWith('.json')) res.setHeader('Content-Type', 'application/json');
if (req.url.endsWith('.woff')) res.setHeader('Content-Type', 'font/woff');
if (req.url.endsWith('.woff2')) res.setHeader('Content-Type', 'font/woff2');
if (req.url.endsWith('.ttf')) res.setHeader('Content-Type', 'font/ttf');
const stream = fs.createReadStream(file);
stream.pipe(res);
return;
}
}
next();
});
}
},
...(isPackageBuild ? [
dts({
insertTypesEntry: true,
entryRoot: 'src',
}),
{
// ES は複数の入口(`.` と `./element`)を 1 回で作れるが、UMD は Vite が
// 「output formats に umd/iife を含むときは複数の入口を許さない」と断る。
// そこで UMD(と CSS)だけを、ES のビルドが終わったあとに 1 入口で作り直す。
// `configFile: false` なので、このプラグインも外側の `css` の設定も入れ子のビルドには
// 入らない。CSS の設定は `CSS_OPTIONS` を明示して渡す(外側と同じ 1 つの定数)。
name: 'maplat-ui-legacy-umd',
apply: 'build',
async closeBundle() {
if (!isPackageBuild) return;
await build({
configFile: false,
logLevel: 'warn',
define: VERSION_DEFINE,
css: CSS_OPTIONS,
build: {
outDir: 'dist',
emptyOutDir: false,
copyPublicDir: false,
lib: {
entry: path.resolve(__dirname, 'src/index.ts'),
name: 'MaplatUi',
formats: ['umd'],
fileName: () => 'maplat_ui.umd.js',
cssFileName: 'maplat_ui'
},
rollupOptions: {
external: PACKAGE_EXTERNAL,
output: {
globals: PACKAGE_GLOBALS,
footer: UMD_FOOTER
}
}
}
});
}
}
] : [])
],
css: CSS_OPTIONS,
build: {
outDir: isPackageBuild ? 'dist' : 'dist-demo',
copyPublicDir: !isPackageBuild,
...(isPackageBuild ? {
lib: {
// 入口は 2 つ: 旧 UI(`.`)と UI Elements API(`./element`)。どちらも ES で出す。
entry: {
index: path.resolve(__dirname, 'src/index.ts'),
'element/index': path.resolve(__dirname, 'src/element/index.ts')
},
name: 'MaplatUi',
formats: ['es'],
fileName: (_format, entryName) =>
entryName === 'index'
? 'maplat_ui.es.js'
: `${entryName}.js`,
cssFileName: 'maplat_ui'
},
rollupOptions: {
external: PACKAGE_EXTERNAL,
output: {
assetFileNames: (assetInfo) => {
if (assetInfo.name && assetInfo.name.endsWith('.css')) return 'maplat_ui.css';
return assetInfo.name;
},
globals: PACKAGE_GLOBALS
}
}
} : {
// Demo build uses standard app mode (no lib config)
rollupOptions: {
// No external dependencies for demo - bundle everything except peer deps
}
})
},
test: {
environment: 'jsdom',
include: ['spec/**/*.test.ts', 'spec/**/*.spec.ts'],
exclude: ['**/node_modules/**', '**/dist/**', 'e2e/**'],
globals: true
}
}));