update asset building for core

This commit is contained in:
2026-11-22 20:35:52 +01:00
parent 671b7999e7
commit f072349ed2
12 changed files with 24405 additions and 23536 deletions
+68
View File
@@ -1,4 +1,9 @@
import { build } from 'vite';
import path from 'path';
import { fileURLToPath } from 'url';
import fs from 'fs';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// libraries
const libraries = [
@@ -19,13 +24,76 @@ const libraries = [
},
];
// GrapesJS ships a pre-bundled Webpack bundle as its ESM entry (grapes.mjs).
// Vite cannot re-bundle it correctly because the internal __webpack_require__
// calls break when Vite replaces the module system. Use a custom plugin to
// inject the raw GrapesJS UMD bundle and resolve the import to it.
function grapesjsBundlePlugin() {
const grapesMinPath = path.resolve(__dirname, 'node_modules/grapesjs/dist/grapes.min.js');
const grapesMinContent = fs.readFileSync(grapesMinPath, 'utf-8');
return [
{
name: 'grapesjs-resolve',
resolveId(id, importer) {
if (id === 'grapesjs') {
return { id: '\0grapesjs-umd', moduleSideEffects: true };
}
return null;
},
load(id) {
if (id === '\0grapesjs-umd') {
// Return the UMD bundle as a raw module that sets window.grapesjs
return `window.grapesjs = undefined;
${grapesMinContent}
var _grapesjs = window.grapesjs;
export default _grapesjs;`;
}
return null;
},
// Prevent Rollup from trying to scan/transform this module
moduleParsed(info) {
if (info.id === '\0grapesjs-umd') {
info.isEntry = false;
}
},
},
{
name: 'grapesjs-output',
renderChunk(code) {
// The GrapesJS UMD bundle uses __webpack_modules__ which Rollup might rename.
// We need to ensure the output preserves the Webpack bundle structure.
return null;
},
},
];
}
// build
libraries.forEach(async (libItem) => {
await build({
configFile: false,
plugins: [grapesjsBundlePlugin()],
build: {
lib: libItem,
emptyOutDir: false,
minify: 'terser',
terserOptions: {
mangle: {
// Reserve names that are used as globals in pagebuilder.php to prevent
// the minifier from generating them as mangled variable names
reserved: ['config', 'editor', 'window', 'document', 'console', 'CKEDITOR'],
},
compress: {
// Preserve __webpack_* variable names used by the GrapesJS Webpack bundle
keep_fargs: false,
},
format: {
// Preserve __webpack_* variable names from being mangled
keep_quoted_props: true,
},
},
rollupOptions: {
output: {
assetFileNames: (assetInfo) => {