Repository navigation
Expand file tree
/
Copy pathvite.config.js
More file actions
96 lines (91 loc) · 2.79 KB
/
Copy pathvite.config.js
File metadata and controls
96 lines (91 loc) · 2.79 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
import { defineConfig } from 'vite';
import { resolve } from 'node:path';
/**
* Separate bundles for the plugin's different loading schedules.
*
* They are separate because they load on entirely different schedules: `form`
* ships to every visitor on a page with a form on it, `builder` only to somebody
* editing one, `entries` only to somebody reading submissions, and `widget` only
* to somebody who has put the widget on their desktop. The small `dock` and
* `titlebar` bundles load with OpenStation, without pulling in the builder.
* A single bundle would make every visitor download the form builder.
*
* Each target builds twice: `--mode development` emits the readable file
* WordPress serves under `SCRIPT_DEBUG`, `--mode production` the minified one.
* `emptyOutDir` is off so the second pass does not delete the first pass's
* output — and so `builder` does not delete `form`.
*
* Which target a pass builds comes from `ATF_TARGET`, because Vite's library
* mode takes one entry per config.
*/
const TARGETS = {
form: {
entry: 'src/form.ts',
fileBase: 'form',
iifeName: 'allTerrainFormsFront',
},
builder: {
entry: 'src/builder.ts',
fileBase: 'builder',
iifeName: 'allTerrainFormsBuilder',
},
entries: {
entry: 'src/entries.ts',
fileBase: 'entries',
iifeName: 'allTerrainFormsEntries',
},
widget: {
entry: 'src/widget.ts',
fileBase: 'widget',
iifeName: 'allTerrainFormsWidget',
},
analytics: {
entry: 'src/analytics.ts',
fileBase: 'analytics',
iifeName: 'allTerrainFormsAnalytics',
},
titlebar: {
entry: 'src/titlebar.ts',
fileBase: 'titlebar',
iifeName: 'allTerrainFormsTitlebar',
},
dock: {
entry: 'src/dock.ts',
fileBase: 'dock',
iifeName: 'allTerrainFormsDock',
},
};
export default defineConfig( ( { mode } ) => {
const name = process.env.ATF_TARGET || 'form';
const target = TARGETS[ name ];
if ( ! target ) {
throw new Error(
`Unknown ATF_TARGET "${ name }". Expected one of: ${ Object.keys( TARGETS ).join( ', ' ) }.`
);
}
const isProd = mode === 'production';
return {
...( process.env.ATF_MIO_SOURCE ? { server: { fs: { allow: [ process.cwd(), resolve( process.env.ATF_MIO_SOURCE ) ] } } } : {} ),
build: {
outDir: 'assets/js',
// CommonJS auto-detection can wrap AJV differently with a different
// module load order. Preserve require semantics consistently so the
// committed bundles match clean CI builds.
commonjsOptions: { strictRequires: true },
emptyOutDir: false,
target: 'es2020',
minify: isProd ? 'esbuild' : false,
sourcemap: false,
lib: {
entry: target.entry,
formats: [ 'iife' ],
name: target.iifeName,
fileName: () => `${ target.fileBase }${ isProd ? '.min' : '' }.js`,
},
},
test: {
environment: 'jsdom',
include: [ 'tests/vitest/**/*.test.ts' ],
},
};
} );