fix(bytecodePlugin): replace bytecode module regex (#54)

This commit is contained in:
alex8088 2022-11-13 01:19:58 +08:00
parent 21e10c023a
commit 23ae7636cd

View file

@ -154,6 +154,7 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
const _chunkAlias = Array.isArray(chunkAlias) ? chunkAlias : [chunkAlias] const _chunkAlias = Array.isArray(chunkAlias) ? chunkAlias : [chunkAlias]
const bytecodeChunks: string[] = [] const bytecodeChunks: string[] = []
const nonEntryChunks: string[] = []
const transformAllChunks = _chunkAlias.length === 0 const transformAllChunks = _chunkAlias.length === 0
const isBytecodeChunk = (chunkName: string): boolean => { const isBytecodeChunk = (chunkName: string): boolean => {
@ -190,6 +191,9 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
} }
if (chunk.type === 'chunk' && isBytecodeChunk(chunk.name)) { if (chunk.type === 'chunk' && isBytecodeChunk(chunk.name)) {
bytecodeChunks.push(chunk.fileName) bytecodeChunks.push(chunk.fileName)
if (!chunk.isEntry) {
nonEntryChunks.push(chunk.fileName)
}
if (transformArrowFunctions) { if (transformArrowFunctions) {
return { return {
code: _transform(code) code: _transform(code)
@ -215,7 +219,8 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
const bundles = Object.keys(output) const bundles = Object.keys(output)
const outDir = options.dir! const outDir = options.dir!
bytecodeFiles = [] bytecodeFiles = []
const bytecodeRE = new RegExp(bytecodeChunks.map(chunk => `(${chunk})`).join('|'), 'g') const pattern = nonEntryChunks.length ? nonEntryChunks.map(chunk => `(${chunk})`).join('|') : null
const bytecodeRE = pattern ? new RegExp(`require\\(\\S*(?=(${pattern})\\S*\\))`, 'g') : null
const keepBundle = (chunkFileName: string): void => { const keepBundle = (chunkFileName: string): void => {
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)
@ -225,12 +230,13 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
const chunk = output[name] const chunk = output[name]
if (chunk.type === 'chunk') { if (chunk.type === 'chunk') {
let _code = chunk.code let _code = chunk.code
if (_code.match(bytecodeRE)) { if (bytecodeRE && _code.match(bytecodeRE)) {
let match: RegExpExecArray | null let match: RegExpExecArray | null
const s = new MagicString(_code) const s = new MagicString(_code)
while ((match = bytecodeRE.exec(_code))) { while ((match = bytecodeRE.exec(_code))) {
const [chunkName] = match const [prefix, chunkName] = match
s.overwrite(match.index, match.index + chunkName.length, chunkName + 'c', { const len = prefix.length + chunkName.length
s.overwrite(match.index, match.index + len, prefix + chunkName + 'c', {
contentOnly: true contentOnly: true
}) })
} }