chore: migrate to `magic-string` (#3482)

This commit is contained in:
Anthony Fu 2022-03-03 18:01:14 +08:00 committed by GitHub
parent 494f85a2b2
commit ecee32988d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 40 additions and 26 deletions

View File

@ -49,7 +49,7 @@
"hash-sum": "^2.0.0", "hash-sum": "^2.0.0",
"hookable": "^5.1.1", "hookable": "^5.1.1",
"knitwork": "^0.1.0", "knitwork": "^0.1.0",
"magic-string-extra": "^0.1.2", "magic-string": "^0.26.0",
"mlly": "^0.4.3", "mlly": "^0.4.3",
"murmurhash-es": "^0.1.1", "murmurhash-es": "^0.1.1",
"nitropack": "npm:nitropack-edge@latest", "nitropack": "npm:nitropack-edge@latest",

View File

@ -2,7 +2,7 @@ import { createUnplugin } from 'unplugin'
import { parseQuery, parseURL } from 'ufo' import { parseQuery, parseURL } from 'ufo'
import { Component } from '@nuxt/schema' import { Component } from '@nuxt/schema'
import { genImport } from 'knitwork' import { genImport } from 'knitwork'
import MagicString from 'magic-string-extra' import MagicString from 'magic-string'
interface LoaderOptions { interface LoaderOptions {
getComponents(): Component[] getComponents(): Component[]
@ -50,5 +50,10 @@ function transform (code: string, id: string, components: Component[]) {
s.prepend(imports + '\n') s.prepend(imports + '\n')
} }
return s.toRollupResult(true, { source: id, includeContent: true }) if (s.hasChanged()) {
return {
code: s.toString(),
map: s.generateMap({ source: id, includeContent: true })
}
}
} }

View File

@ -1,7 +1,7 @@
import { createUnplugin } from 'unplugin' import { createUnplugin } from 'unplugin'
import { parseQuery, parseURL, withQuery } from 'ufo' import { parseQuery, parseURL, withQuery } from 'ufo'
import { findStaticImports, findExports } from 'mlly' import { findStaticImports, findExports } from 'mlly'
import MagicString from 'magic-string-extra' import MagicString from 'magic-string'
export interface TransformMacroPluginOptions { export interface TransformMacroPluginOptions {
macros: Record<string, string> macros: Record<string, string>
@ -17,9 +17,15 @@ export const TransformMacroPlugin = createUnplugin((options: TransformMacroPlugi
return pathname.endsWith('.vue') || !!parseQuery(search).macro return pathname.endsWith('.vue') || !!parseQuery(search).macro
}, },
transform (code, id) { transform (code, id) {
const s = new MagicString(code, { sourcemapOptions: { source: id, includeContent: true } }) const s = new MagicString(code)
const { search } = parseURL(id) const { search } = parseURL(id)
function result () {
if (s.hasChanged()) {
return { code: s.toString(), map: s.generateMap({ source: id, includeContent: true }) }
}
}
// Tree-shake out any runtime references to the macro. // Tree-shake out any runtime references to the macro.
// We do this first as it applies to all files, not just those with the query // We do this first as it applies to all files, not just those with the query
for (const macro in options.macros) { for (const macro in options.macros) {
@ -30,7 +36,7 @@ export const TransformMacroPlugin = createUnplugin((options: TransformMacroPlugi
} }
if (!parseQuery(search).macro) { if (!parseQuery(search).macro) {
return s.toRollupResult() return result()
} }
// [webpack] Re-export any imports from script blocks in the components // [webpack] Re-export any imports from script blocks in the components
@ -39,7 +45,7 @@ export const TransformMacroPlugin = createUnplugin((options: TransformMacroPlugi
if (scriptImport) { if (scriptImport) {
const specifier = withQuery(scriptImport.specifier.replace('?macro=true', ''), { macro: 'true' }) const specifier = withQuery(scriptImport.specifier.replace('?macro=true', ''), { macro: 'true' })
s.overwrite(0, code.length, `export { meta } from "${specifier}"`) s.overwrite(0, code.length, `export { meta } from "${specifier}"`)
return s.toRollupResult() return result()
} }
const currentExports = findExports(code) const currentExports = findExports(code)
@ -50,7 +56,7 @@ export const TransformMacroPlugin = createUnplugin((options: TransformMacroPlugi
if (match.specifier && match._type === 'named') { if (match.specifier && match._type === 'named') {
// [webpack] Export named exports rather than the default (component) // [webpack] Export named exports rather than the default (component)
s.overwrite(match.start, match.end, `export {${Object.values(options.macros).join(', ')}} from "${match.specifier}"`) s.overwrite(match.start, match.end, `export {${Object.values(options.macros).join(', ')}} from "${match.specifier}"`)
return s.toRollupResult() return result()
} else if (!options.dev) { } else if (!options.dev) {
// ensure we tree-shake any _other_ default exports out of the macro script // ensure we tree-shake any _other_ default exports out of the macro script
s.overwrite(match.start, match.end, '/*#__PURE__*/ false &&') s.overwrite(match.start, match.end, '/*#__PURE__*/ false &&')
@ -70,7 +76,7 @@ export const TransformMacroPlugin = createUnplugin((options: TransformMacroPlugi
s.append(`\nexport const ${options.macros[macro]} = ${macroContent}`) s.append(`\nexport const ${options.macros[macro]} = ${macroContent}`)
} }
return s.toRollupResult() return result()
} }
} }
}) })

View File

@ -15,7 +15,7 @@
"devDependencies": { "devDependencies": {
"@nuxt/schema": "3.0.0", "@nuxt/schema": "3.0.0",
"@types/cssnano": "^5", "@types/cssnano": "^5",
"magic-string-extra": "^0.1.2", "magic-string": "^0.26.0",
"unbuild": "latest", "unbuild": "latest",
"vue": "3.2.31" "vue": "3.2.31"
}, },
@ -33,7 +33,6 @@
"fs-extra": "^10.0.1", "fs-extra": "^10.0.1",
"get-port-please": "^2.4.3", "get-port-please": "^2.4.3",
"knitwork": "^0.1.0", "knitwork": "^0.1.0",
"magic-string": "^0.25.7",
"mlly": "^0.4.3", "mlly": "^0.4.3",
"p-debounce": "^4.0.0", "p-debounce": "^4.0.0",
"pathe": "^0.2.0", "pathe": "^0.2.0",

View File

@ -1,7 +1,7 @@
import { createUnplugin } from 'unplugin' import { createUnplugin } from 'unplugin'
import escapeRE from 'escape-string-regexp' import escapeRE from 'escape-string-regexp'
import type { Plugin } from 'vite' import type { Plugin } from 'vite'
import MagicString from 'magic-string-extra' import MagicString from 'magic-string'
interface DynamicBasePluginOptions { interface DynamicBasePluginOptions {
env: 'dev' | 'server' | 'client' env: 'dev' | 'server' | 'client'
@ -93,7 +93,12 @@ export const DynamicBasePlugin = createUnplugin(function (options: DynamicBasePl
s.prepend('import { joinURL } from "ufo";\n') s.prepend('import { joinURL } from "ufo";\n')
} }
return s.toRollupResult(true, { source: id, includeContent: true }) if (s.hasChanged()) {
return {
code: s.toString(),
map: s.generateMap({ source: id, includeContent: true })
}
}
} }
} }
}) })

View File

@ -3371,8 +3371,7 @@ __metadata:
fs-extra: ^10.0.1 fs-extra: ^10.0.1
get-port-please: ^2.4.3 get-port-please: ^2.4.3
knitwork: ^0.1.0 knitwork: ^0.1.0
magic-string: ^0.25.7 magic-string: ^0.26.0
magic-string-extra: ^0.1.2
mlly: ^0.4.3 mlly: ^0.4.3
p-debounce: ^4.0.0 p-debounce: ^4.0.0
pathe: ^0.2.0 pathe: ^0.2.0
@ -13771,15 +13770,6 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"magic-string-extra@npm:^0.1.2":
version: 0.1.2
resolution: "magic-string-extra@npm:0.1.2"
dependencies:
magic-string: ^0.25.7
checksum: 196dec128985162ff559752dd28f358bacd203e9e9e2bcb7d3eb8a1cdacc8a60ce1d49709f31de16f594adb60566959dbb04750560c3cbc872f520d062dba0ee
languageName: node
linkType: hard
"magic-string@npm:^0.25.7": "magic-string@npm:^0.25.7":
version: 0.25.7 version: 0.25.7
resolution: "magic-string@npm:0.25.7" resolution: "magic-string@npm:0.25.7"
@ -13789,6 +13779,15 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"magic-string@npm:^0.26.0":
version: 0.26.0
resolution: "magic-string@npm:0.26.0"
dependencies:
sourcemap-codec: ^1.4.8
checksum: 2065de79cdbb76fe1b073244d51a2094cabf317e95a4ce385d7ddbe1d1f16b864c0f2a1ae514872251e64edc44bf56c8bb6a7dbe5eff9c9c236f877bcdfec9f9
languageName: node
linkType: hard
"make-dir@npm:^1.0.0": "make-dir@npm:^1.0.0":
version: 1.3.0 version: 1.3.0
resolution: "make-dir@npm:1.3.0" resolution: "make-dir@npm:1.3.0"
@ -15437,7 +15436,7 @@ __metadata:
hash-sum: ^2.0.0 hash-sum: ^2.0.0
hookable: ^5.1.1 hookable: ^5.1.1
knitwork: ^0.1.0 knitwork: ^0.1.0
magic-string-extra: ^0.1.2 magic-string: ^0.26.0
mlly: ^0.4.3 mlly: ^0.4.3
murmurhash-es: ^0.1.1 murmurhash-es: ^0.1.1
nitropack: "npm:nitropack-edge@latest" nitropack: "npm:nitropack-edge@latest"
@ -19740,7 +19739,7 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"sourcemap-codec@npm:^1.4.4": "sourcemap-codec@npm:^1.4.4, sourcemap-codec@npm:^1.4.8":
version: 1.4.8 version: 1.4.8
resolution: "sourcemap-codec@npm:1.4.8" resolution: "sourcemap-codec@npm:1.4.8"
checksum: b57981c05611afef31605732b598ccf65124a9fcb03b833532659ac4d29ac0f7bfacbc0d6c5a28a03e84c7510e7e556d758d0bb57786e214660016fb94279316 checksum: b57981c05611afef31605732b598ccf65124a9fcb03b833532659ac4d29ac0f7bfacbc0d6c5a28a03e84c7510e7e556d758d0bb57786e214660016fb94279316