fix(bytecodePlugin): bytecode loader is not referenced correctly in the chunks

This commit is contained in:
alex8088 2023-03-26 21:17:29 +08:00
parent 1cb7d419a2
commit f33c86383e

View file

@ -8,6 +8,7 @@ import * as babel from '@babel/core'
import MagicString from 'magic-string' import MagicString from 'magic-string'
import type { SourceMapInput } from 'rollup' import type { SourceMapInput } from 'rollup'
import { getElectronPath } from '../electron' import { getElectronPath } from '../electron'
import { toRelativePath } from '../utils'
// Inspired by https://github.com/bytenode/bytenode // Inspired by https://github.com/bytenode/bytenode
@ -182,7 +183,8 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
return re.code || '' return re.code || ''
} }
const requireBytecodeLoaderStr = '"use strict";\nrequire("./bytecode-loader.js");' const useStrict = '"use strict";'
const bytecodeModuleLoader = 'bytecode-loader.js'
let config: ResolvedConfig let config: ResolvedConfig
let useInRenderer = false let useInRenderer = false
@ -249,7 +251,7 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
type: 'asset', type: 'asset',
source: bytecodeModuleLoaderCode.join('\n') + '\n', source: bytecodeModuleLoaderCode.join('\n') + '\n',
name: 'Bytecode Loader File', name: 'Bytecode Loader File',
fileName: 'bytecode-loader.js' fileName: bytecodeModuleLoader
}) })
} }
}, },
@ -266,6 +268,9 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
const newFileName = path.resolve(path.dirname(chunkFileName), `_${path.basename(chunkFileName)}`) const newFileName = path.resolve(path.dirname(chunkFileName), `_${path.basename(chunkFileName)}`)
fs.renameSync(chunkFileName, newFileName) fs.renameSync(chunkFileName, newFileName)
} }
const getBytecodeLoaderBlock = (chunkFileName: string): string => {
return `require("${toRelativePath(bytecodeModuleLoader, chunkFileName)}");`
}
await Promise.all( await Promise.all(
bundles.map(async name => { bundles.map(async name => {
const chunk = output[name] const chunk = output[name]
@ -292,7 +297,9 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
if (!removeBundleJS) { if (!removeBundleJS) {
keepBundle(chunkFileName) keepBundle(chunkFileName)
} }
const code = requireBytecodeLoaderStr + `\nrequire("./${normalizePath(name + 'c')}");\n` const bytecodeLoaderBlock = getBytecodeLoaderBlock(chunk.fileName)
const bytecodeModuleBlock = `require("./${path.basename(name) + 'c'}");`
const code = `${useStrict}\n${bytecodeLoaderBlock}\n${bytecodeModuleBlock}\n`
fs.writeFileSync(chunkFileName, code) fs.writeFileSync(chunkFileName, code)
} else { } else {
if (removeBundleJS) { if (removeBundleJS) {
@ -318,7 +325,8 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
for (const importerId of dynamicImporters) idsToHandle.add(importerId) for (const importerId of dynamicImporters) idsToHandle.add(importerId)
} }
} }
_code = hasBytecodeMoudle ? _code.replace('"use strict";', requireBytecodeLoaderStr) : _code const bytecodeLoaderBlock = getBytecodeLoaderBlock(chunk.fileName)
_code = hasBytecodeMoudle ? _code.replace(useStrict, `${useStrict}\n${bytecodeLoaderBlock}`) : _code
} }
fs.writeFileSync(chunkFileName, _code) fs.writeFileSync(chunkFileName, _code)
} }