This repository has been archived by the owner on Jun 21, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vite.config.js
98 lines (87 loc) · 3.1 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
import * as path from "path";
import { defineConfig } from "vite";
import legacy from "@vitejs/plugin-legacy";
import { useDynamicPublicPath } from "vite-plugin-dynamic-publicpath";
import { peerDependencies } from "./package.json";
import { resolve } from "path";
export default defineConfig(({ mode }) => {
// Bundled mode:
// - JS distributed via pypi. npm package and frontend js toolchain not required.
// - All dependencies included in bundle.
//
// Unbundled mode:
// - JS distributed via npm. Frontend widgets require this to be built.
// - Some dependencies included in bundle.
// - Some dependencies externalised (see package.json peerDependencies field)
//
// Test-utils mode:
// - JS distributed via npm. Available via `import 'groundwork-django/test-utils'`
// - Some dependencies included in bundle.
// - Some dependencies externalised (see package.json peerDependencies field)
// - Some dependencies that will throw in test environments (such as mapbox) replaced with the mock we use internally
// for testing.
const isBundled = mode === "bundled";
const isDev = mode === "dev";
const isTestUtils = mode === "test-utils";
const isLibrary = !isBundled && !isDev;
const entrypoint = `frontend/index.${mode}.ts`;
const outDir = `build/${mode}/`;
const alias = !isTestUtils
? []
: TEST_MOCKS.map((moduleName) => ({
find: new RegExp(`^${moduleName}$`),
replacement: resolve(`__mocks__/${moduleName}.ts`),
}));
return {
// In bundled mode, we use the dynamicPublicPath plugin instead of a hardcoded asset path
base: isBundled ? "" : "/static/",
// Treat css as a static asset, so that we can do the head tag injection ourselves.
// Vite's css pipeline doesn't play nicely with libraries – it bundles everything into a root css file,
// which we don't want here.
// assetsInclude: isBundled ? undefined : ["mapbox-gl/dist/mapbox-gl.css"],
optimizeDeps: {
entries: [entrypoint],
},
plugins: compact([
isBundled &&
legacy({
polyfills: false,
targets: ["defaults", "not IE 11"],
}),
isBundled &&
// In bundled mode, we need to inject STATIC_URL into templates and pick it up with these
useDynamicPublicPath({
dynamicImportHandler: "window.__groundwork_dynamic_handler__",
dynamicImportPreload: "window.__groundwork_dynamic_preload__",
}),
]),
resolve: {
alias,
},
build: {
manifest: isBundled,
cssCodeSplit: true,
emptyOutDir: true,
outDir,
rollupOptions: {
external: isBundled ? [] : Object.keys(peerDependencies),
output: {
dir: outDir,
},
input: {
main: entrypoint,
},
},
lib: !isLibrary
? undefined
: {
entry: path.resolve(__dirname, entrypoint),
formats: isBundled ? ["cjs"] : ["es"],
name: "groundwork",
fileName: () => `index.js`,
},
},
};
});
const TEST_MOCKS = ["mapbox-gl"];
const compact = (array) => array.filter((item) => !!item);