export const jsModuleRemove = () => {
return {
name: 'js-module-remove',
enforce: 'post',
transformIndexHtml(htmlText: string, e: any) {
if (e?.server?.['_currentServerPort']) {
return htmlText;
}
const htmlArr = htmlText.match(/.*\n/g) || [];
let result = '';
htmlArr.forEach(v => {
v = v
.replace(/script ?nomodule\s?/g, 'script ')
.replace(/\s?crossorigin\s?/g, ' ')
.replace(/<link rel="modulepreload" href="[^"]+\.js">/g, '')
.replace(
"System.import(document.getElementById('vite-legacy-entry').getAttribute('src'))",
''
)
.replace(/data-src/g, 'src');
if (!v.includes(`script type="module"`)) {
result += v;
}
});
return result;
},
} as const;
};
import { jsModuleRemove } from './plugins/jsModuleRemove';
import legacy from '@vitejs/plugin-legacy';
export default defineConfig({
base: "./",
plugins: [
legacy({
targets: ["defaults", "not IE 11"],
}),
jsModuleRemove(),
],
});
参考链接: www.jianshu.com/p/14cce9ff6…