108 lines
3.1 KiB
JavaScript
108 lines
3.1 KiB
JavaScript
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 = [
|
|
{
|
|
entry: './src/Modules/WebsiteManager/resources/assets/entry.js',
|
|
name: 'WebsiteManager',
|
|
fileName: 'WebsiteManager',
|
|
},
|
|
{
|
|
entry: './src/Modules/GrapesJS/resources/assets/entryHead.js',
|
|
name: 'PageBuilderHead',
|
|
fileName: 'PageBuilderHead',
|
|
},
|
|
{
|
|
entry: './src/Modules/GrapesJS/resources/assets/entryBody.js',
|
|
name: 'PageBuilderBody',
|
|
fileName: 'PageBuilderBody',
|
|
},
|
|
];
|
|
|
|
// 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) => {
|
|
if (assetInfo.name === 'style.css') return `${libItem.fileName}.css`;
|
|
return assetInfo.name;
|
|
},
|
|
},
|
|
},
|
|
},
|
|
});
|
|
});
|