mirror of
https://github.com/alex8088/electron-vite.git
synced 2026-11-02 22:56:58 +08:00
fix(esmShim)!: use this.parse to find the last static import (#906)
This commit is contained in:
parent
08acca3585
commit
a135566fa5
@ -1,9 +1,3 @@
|
|||||||
/*
|
|
||||||
* The core of this plugin was conceived by pi0 and is taken from the following repository:
|
|
||||||
* https://github.com/unjs/unbuild/blob/main/src/builder/plugins/cjs.ts
|
|
||||||
* license: https://github.com/unjs/unbuild/blob/main/LICENSE
|
|
||||||
*/
|
|
||||||
|
|
||||||
import MagicString from 'magic-string'
|
import MagicString from 'magic-string'
|
||||||
import type { Plugin } from 'vite'
|
import type { Plugin } from 'vite'
|
||||||
|
|
||||||
@ -13,37 +7,22 @@ const CJSyntaxRe = /__filename|__dirname|require\(|require\.resolve\(/
|
|||||||
|
|
||||||
const CJSShim_normal = `
|
const CJSShim_normal = `
|
||||||
// -- CommonJS Shims --
|
// -- CommonJS Shims --
|
||||||
import __cjs_url__ from 'node:url';
|
import __shims_url__ from 'node:url';
|
||||||
import __cjs_path__ from 'node:path';
|
import __shims_path__ from 'node:path';
|
||||||
import __cjs_mod__ from 'node:module';
|
import __shims_module__ from 'node:module';
|
||||||
const __filename = __cjs_url__.fileURLToPath(import.meta.url);
|
const __filename = __shims_url__.fileURLToPath(import.meta.url);
|
||||||
const __dirname = __cjs_path__.dirname(__filename);
|
const __dirname = __shims_path__.dirname(__filename);
|
||||||
const require = __cjs_mod__.createRequire(import.meta.url);
|
const require = __shims_module__.createRequire(import.meta.url);
|
||||||
`
|
`
|
||||||
|
|
||||||
const CJSShim_node_20_11 = `
|
const CJSShim_node_20_11 = `
|
||||||
// -- CommonJS Shims --
|
// -- CommonJS Shims --
|
||||||
import __cjs_mod__ from 'node:module';
|
import __shims_module__ from 'node:module';
|
||||||
const __filename = import.meta.filename;
|
const __filename = import.meta.filename;
|
||||||
const __dirname = import.meta.dirname;
|
const __dirname = import.meta.dirname;
|
||||||
const require = __cjs_mod__.createRequire(import.meta.url);
|
const require = __shims_module__.createRequire(import.meta.url);
|
||||||
`
|
`
|
||||||
|
|
||||||
const ESMStaticImportRe =
|
|
||||||
/(?<=\s|^|;)import\s*([\s"']*(?<imports>[\p{L}\p{M}\w\t\n\r $*,/{}@.]+)from\s*)?["']\s*(?<specifier>(?<="\s*)[^"]*[^\s"](?=\s*")|(?<='\s*)[^']*[^\s'](?=\s*'))\s*["'][\s;]*/gmu
|
|
||||||
|
|
||||||
interface StaticImport {
|
|
||||||
end: number
|
|
||||||
}
|
|
||||||
|
|
||||||
function findStaticImports(code: string): StaticImport[] {
|
|
||||||
const matches: StaticImport[] = []
|
|
||||||
for (const match of code.matchAll(ESMStaticImportRe)) {
|
|
||||||
matches.push({ end: (match.index || 0) + match[0].length })
|
|
||||||
}
|
|
||||||
return matches
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function esmShimPlugin(): Plugin {
|
export default function esmShimPlugin(): Plugin {
|
||||||
const CJSShim = supportImportMetaPaths() ? CJSShim_node_20_11 : CJSShim_normal
|
const CJSShim = supportImportMetaPaths() ? CJSShim_node_20_11 : CJSShim_normal
|
||||||
|
|
||||||
@ -53,11 +32,14 @@ export default function esmShimPlugin(): Plugin {
|
|||||||
enforce: 'post',
|
enforce: 'post',
|
||||||
renderChunk(code, _chunk, { format, sourcemap }) {
|
renderChunk(code, _chunk, { format, sourcemap }) {
|
||||||
if (format === 'es') {
|
if (format === 'es') {
|
||||||
|
// Rolldown may remove unused shim bindings after this hook; referenced bindings remain available.
|
||||||
if (code.includes(CJSShim) || !CJSyntaxRe.test(code)) {
|
if (code.includes(CJSShim) || !CJSyntaxRe.test(code)) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
const lastESMImport = findStaticImports(code).pop()
|
const lastESMImport = this.parse(code)
|
||||||
|
.body.filter(node => node.type === 'ImportDeclaration')
|
||||||
|
.pop()
|
||||||
const indexToAppend = lastESMImport ? lastESMImport.end : 0
|
const indexToAppend = lastESMImport ? lastESMImport.end : 0
|
||||||
const s = new MagicString(code)
|
const s = new MagicString(code)
|
||||||
s.appendRight(indexToAppend, CJSShim)
|
s.appendRight(indexToAppend, CJSShim)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user