-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvite.config.remote.js
119 lines (105 loc) · 2.82 KB
/
vite.config.remote.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
/**
* WordPress dependencies
*/
import { defaultRequestToExternal } from '@wordpress/dependency-extraction-webpack-plugin/lib/util';
import { nodePolyfills } from 'vite-plugin-node-polyfills';
/**
* External dependencies
*/
import { resolve } from 'path';
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import MagicString from 'magic-string';
export default defineConfig( {
base: '',
build: {
outDir: '../dist',
rollupOptions: {
input: resolve( __dirname, 'src/remote.html' ),
external: externalize,
},
target: 'esnext',
},
plugins: [ nodePolyfills(), react(), wordPressExternals() ],
root: 'src',
css: {
preprocessorOptions: {
scss: {
quietDeps: true,
},
},
},
} );
function externalize( id ) {
const externalDefinition = defaultRequestToExternal( id );
return (
!! externalDefinition &&
! id.endsWith( '.css' ) &&
! [ 'apiFetch', 'i18n', 'url', 'hooks' ].includes(
externalDefinition[ externalDefinition.length - 1 ]
)
);
}
/**
* Transform code by replacing WordPress imports with global definitions.
*
* @return {Object} The transformed code and map.
*/
function wordPressExternals() {
return {
name: 'wordpress-externals-plugin',
transform( code, id ) {
const magicString = new MagicString( code );
let hasReplacements = false;
// Match WordPress and React JSX runtime import statements
const regex =
/import\s*(?:{([^}]+)}\s*from)?\s*['"](@wordpress\/([^'"]+)|react\/jsx-runtime)['"];/g;
let match;
while ( ( match = regex.exec( code ) ) !== null ) {
const [ fullMatch, imports, module ] = match;
const externalDefinition = defaultRequestToExternal( module );
if (
! externalDefinition ||
/@wordpress\/(api-fetch|i18n|url)/.test( id )
) {
continue; // Exclude the module from externalization
}
hasReplacements = true;
if ( ! imports ) {
// Remove the side effect import entirely
magicString.remove(
match.index,
match.index + fullMatch.length
);
continue;
}
const importList = imports.split( ',' ).map( ( i ) => {
const parts = i.trim().split( /\s+as\s+/ );
if ( parts.length === 2 ) {
// Convert import "as" syntax to destructuring assignment
return `${ parts[ 0 ] }: ${ parts[ 1 ] }`;
}
return i.trim();
} );
const definitionArray = Array.isArray( externalDefinition )
? externalDefinition
: [ externalDefinition ];
const replacement = `const { ${ importList.join(
', '
) } } = window.${ definitionArray.join( '.' ) };`;
magicString.overwrite(
match.index,
match.index + fullMatch.length,
replacement
);
}
if ( ! hasReplacements ) {
return null;
}
return {
code: magicString.toString(),
map: magicString.generateMap( { hires: true } ),
};
},
};
}