-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.js
More file actions
101 lines (97 loc) · 3.47 KB
/
Copy pathvite.config.js
File metadata and controls
101 lines (97 loc) · 3.47 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
import { defineConfig } from 'vite';
import { resolve } from 'path';
import { copyFileSync, mkdirSync } from 'fs';
/**
* WordPress packages are loaded by WordPress core as window globals
* (wp.apiFetch, wp.i18n, …). We expose only the named exports actually
* used by our code so the built files contain zero `import` statements
* and can be enqueued as classic scripts — no `type="module"` required.
*/
const WP_GLOBALS = {
'@wordpress/api-fetch': {
module: 'wp.apiFetch',
named: [], // default-only import
},
'@wordpress/i18n': {
module: 'wp.i18n',
named: [ '__', '_n', '_x', 'sprintf', 'isRTL' ],
},
};
/** Vite plugin: resolve @wordpress/* to inline global-accessor shims. */
function wpExternals() {
return {
name: 'wp-externals',
resolveId( id ) {
return id in WP_GLOBALS ? `\0wp-external:${ id }` : null;
},
load( id ) {
if ( ! id.startsWith( '\0wp-external:' ) ) return null;
const pkg = id.slice( '\0wp-external:'.length );
const { module: glob, named } = WP_GLOBALS[ pkg ];
const lines = [ `const _mod = window.${ glob };` ];
lines.push( `export default _mod;` );
if ( named.length ) {
lines.push( `export const { ${ named.join( ', ' ) } } = _mod;` );
}
return lines.join( '\n' );
},
};
}
/**
* Copy admin/src/shared/registry.json → admin/dist/shared/registry.json so it
* is included in release zips (admin/src is listed in .distignore, admin/dist
* is not). WareServer::get_shared_registry() reads from admin/dist/ in
* production and falls back to admin/src/ during local development.
*/
function copySharedRegistry() {
return {
name: 'copy-shared-registry',
closeBundle() {
mkdirSync( resolve( __dirname, 'admin/dist/shared' ), { recursive: true } );
copyFileSync(
resolve( __dirname, 'admin/src/shared/registry.json' ),
resolve( __dirname, 'admin/dist/shared/registry.json' ),
);
},
};
}
export default defineConfig( {
plugins: [ wpExternals(), copySharedRegistry() ],
build: {
outDir: 'admin/dist',
emptyOutDir: true,
manifest: true,
rollupOptions: {
// Preserve all exports from every entry point so shared lib bundles
// expose their full API even though no other entry in this build
// imports from them.
preserveEntrySignatures: 'exports-only',
input: {
bazaar: resolve( __dirname, 'admin/src/main.js' ),
shell: resolve( __dirname, 'admin/src/shell.js' ),
'zero-trust-sw': resolve( __dirname, 'admin/src/zero-trust-sw.js' ),
// Shared bundles — hosted by the shell, referenced via importmap.
// Content-hashed so they can be cached immutably by browsers and SWs.
'shared/react': resolve( __dirname, 'admin/src/shared/react.js' ),
'shared/react-dom': resolve( __dirname, 'admin/src/shared/react-dom.js' ),
'shared/react-jsx-runtime': resolve( __dirname, 'admin/src/shared/react-jsx-runtime.js' ),
'shared/vue': resolve( __dirname, 'admin/src/shared/vue.js' ),
},
output: {
// Shared libs get content-hash in their filename; other entries keep
// stable names so WordPress enqueue handles work without manifest lookups.
entryFileNames: ( chunkInfo ) =>
chunkInfo.name.startsWith( 'shared/' )
? '[name]-[hash].js'
: '[name].js',
chunkFileNames: '[name]-[hash].js',
assetFileNames: ( assetInfo ) => {
if ( assetInfo.name?.endsWith( '.css' ) ) {
return '[name].css';
}
return 'assets/[name]-[hash][extname]';
},
},
},
},
} );