91 lines
2.8 KiB
JavaScript
91 lines
2.8 KiB
JavaScript
import { defineConfig } from 'vite';
|
|
import { resolve } from 'path';
|
|
import { readFileSync } from 'fs';
|
|
|
|
function cssModulePlugin() {
|
|
return {
|
|
name: 'css-module-plugin',
|
|
enforce: 'pre',
|
|
|
|
// 1. Supprime la syntaxe 'with { type: "css" }' et réécrit les imports de '.css' en '.css.js'
|
|
transform(code, id) {
|
|
if (id.endsWith('.js') || id.endsWith('.mjs') || id.endsWith('.ts')) {
|
|
let cleanCode = code.replace(
|
|
/import\s+([\w\s{},*]+)\s+from\s+(['"][\w@\-.\/]+\.css['"])\s*(?:with|assert)\s*\{\s*type\s*:\s*['"]css['"]\s*\};?/gs,
|
|
'import $1 from $2;'
|
|
);
|
|
|
|
// Transforme les imports de style.css en style.css.js
|
|
cleanCode = cleanCode.replace(
|
|
/(import|export)\s+([\w\s{},*]+\s+from\s+)?(['"][\w@\-.\/]+\.css)(['"])/g,
|
|
'$1 $2$3.js$4'
|
|
);
|
|
|
|
return {
|
|
code: cleanCode,
|
|
map: null
|
|
};
|
|
}
|
|
},
|
|
|
|
// 2. Résout les imports '.css.js' vers le fichier réel absolu + '.js'
|
|
async resolveId(source, importer) {
|
|
if (source.endsWith('.css.js')) {
|
|
const rawSource = source.slice(0, -3); // Retire '.js' -> '.css'
|
|
const resolved = await this.resolve(rawSource, importer);
|
|
if (resolved) {
|
|
return resolved.id + '.js';
|
|
}
|
|
}
|
|
return null;
|
|
},
|
|
|
|
// 3. Charge le fichier CSS et le renvoie compilé sous forme de module JS
|
|
load(id) {
|
|
if (id.endsWith('.css.js')) {
|
|
const cssFilePath = id.slice(0, -3); // Retire '.js'
|
|
const cssContent = readFileSync(cssFilePath, 'utf-8');
|
|
const escapedCss = JSON.stringify(cssContent);
|
|
|
|
return `
|
|
const cssText = ${escapedCss};
|
|
if (typeof document !== 'undefined') {
|
|
const style = document.createElement('style');
|
|
style.appendChild(document.createTextNode(cssText));
|
|
document.head.appendChild(style);
|
|
}
|
|
let sheet = null;
|
|
if (typeof CSSStyleSheet !== 'undefined') {
|
|
sheet = new CSSStyleSheet();
|
|
sheet.replaceSync(cssText);
|
|
}
|
|
export default sheet;
|
|
`;
|
|
}
|
|
return null;
|
|
}
|
|
};
|
|
}
|
|
|
|
export default defineConfig({
|
|
plugins: [cssModulePlugin()],
|
|
build: {
|
|
target: 'esnext',
|
|
preserveEntrySignatures: 'exports-only',
|
|
rollupOptions: {
|
|
input: {
|
|
'panoramax-viewer': resolve(__dirname, 'index.js'),
|
|
'pmtiles': resolve(__dirname, 'node_modules/pmtiles/dist/index.js'),
|
|
'geojson-vt': resolve(__dirname, 'node_modules/geojson-vt/geojson-vt.js'),
|
|
'pbf': resolve(__dirname, 'node_modules/pbf/index.js')
|
|
},
|
|
output: {
|
|
entryFileNames: 'js/[name].js',
|
|
assetFileNames: 'css/[name].[ext]',
|
|
format: 'es'
|
|
}
|
|
},
|
|
outDir: resolve(__dirname, '../static/panoramax'),
|
|
emptyOutDir: true
|
|
}
|
|
});
|