From ecee32988d1385d298a605dc767f4fb4145a467b Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Thu, 3 Mar 2022 18:01:14 +0800 Subject: [PATCH] chore: migrate to `magic-string` (#3482) --- packages/nuxt3/package.json | 2 +- packages/nuxt3/src/components/loader.ts | 9 ++++++-- packages/nuxt3/src/pages/macros.ts | 18 ++++++++++------ packages/vite/package.json | 3 +-- packages/vite/src/plugins/dynamic-base.ts | 9 ++++++-- yarn.lock | 25 +++++++++++------------ 6 files changed, 40 insertions(+), 26 deletions(-) diff --git a/packages/nuxt3/package.json b/packages/nuxt3/package.json index c44a05f897..aceb059c83 100644 --- a/packages/nuxt3/package.json +++ b/packages/nuxt3/package.json @@ -49,7 +49,7 @@ "hash-sum": "^2.0.0", "hookable": "^5.1.1", "knitwork": "^0.1.0", - "magic-string-extra": "^0.1.2", + "magic-string": "^0.26.0", "mlly": "^0.4.3", "murmurhash-es": "^0.1.1", "nitropack": "npm:nitropack-edge@latest", diff --git a/packages/nuxt3/src/components/loader.ts b/packages/nuxt3/src/components/loader.ts index 4a7e1af859..0f577a01f3 100644 --- a/packages/nuxt3/src/components/loader.ts +++ b/packages/nuxt3/src/components/loader.ts @@ -2,7 +2,7 @@ import { createUnplugin } from 'unplugin' import { parseQuery, parseURL } from 'ufo' import { Component } from '@nuxt/schema' import { genImport } from 'knitwork' -import MagicString from 'magic-string-extra' +import MagicString from 'magic-string' interface LoaderOptions { getComponents(): Component[] @@ -50,5 +50,10 @@ function transform (code: string, id: string, components: Component[]) { 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 }) + } + } } diff --git a/packages/nuxt3/src/pages/macros.ts b/packages/nuxt3/src/pages/macros.ts index 13334ba7c5..5bd7380a19 100644 --- a/packages/nuxt3/src/pages/macros.ts +++ b/packages/nuxt3/src/pages/macros.ts @@ -1,7 +1,7 @@ import { createUnplugin } from 'unplugin' import { parseQuery, parseURL, withQuery } from 'ufo' import { findStaticImports, findExports } from 'mlly' -import MagicString from 'magic-string-extra' +import MagicString from 'magic-string' export interface TransformMacroPluginOptions { macros: Record @@ -17,9 +17,15 @@ export const TransformMacroPlugin = createUnplugin((options: TransformMacroPlugi return pathname.endsWith('.vue') || !!parseQuery(search).macro }, transform (code, id) { - const s = new MagicString(code, { sourcemapOptions: { source: id, includeContent: true } }) + const s = new MagicString(code) 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. // We do this first as it applies to all files, not just those with the query for (const macro in options.macros) { @@ -30,7 +36,7 @@ export const TransformMacroPlugin = createUnplugin((options: TransformMacroPlugi } if (!parseQuery(search).macro) { - return s.toRollupResult() + return result() } // [webpack] Re-export any imports from script blocks in the components @@ -39,7 +45,7 @@ export const TransformMacroPlugin = createUnplugin((options: TransformMacroPlugi if (scriptImport) { const specifier = withQuery(scriptImport.specifier.replace('?macro=true', ''), { macro: 'true' }) s.overwrite(0, code.length, `export { meta } from "${specifier}"`) - return s.toRollupResult() + return result() } const currentExports = findExports(code) @@ -50,7 +56,7 @@ export const TransformMacroPlugin = createUnplugin((options: TransformMacroPlugi if (match.specifier && match._type === 'named') { // [webpack] Export named exports rather than the default (component) s.overwrite(match.start, match.end, `export {${Object.values(options.macros).join(', ')}} from "${match.specifier}"`) - return s.toRollupResult() + return result() } else if (!options.dev) { // ensure we tree-shake any _other_ default exports out of the macro script 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}`) } - return s.toRollupResult() + return result() } } }) diff --git a/packages/vite/package.json b/packages/vite/package.json index b18f1b8a81..34ab5b502b 100644 --- a/packages/vite/package.json +++ b/packages/vite/package.json @@ -15,7 +15,7 @@ "devDependencies": { "@nuxt/schema": "3.0.0", "@types/cssnano": "^5", - "magic-string-extra": "^0.1.2", + "magic-string": "^0.26.0", "unbuild": "latest", "vue": "3.2.31" }, @@ -33,7 +33,6 @@ "fs-extra": "^10.0.1", "get-port-please": "^2.4.3", "knitwork": "^0.1.0", - "magic-string": "^0.25.7", "mlly": "^0.4.3", "p-debounce": "^4.0.0", "pathe": "^0.2.0", diff --git a/packages/vite/src/plugins/dynamic-base.ts b/packages/vite/src/plugins/dynamic-base.ts index 67a48b9fd5..1380eb13b1 100644 --- a/packages/vite/src/plugins/dynamic-base.ts +++ b/packages/vite/src/plugins/dynamic-base.ts @@ -1,7 +1,7 @@ import { createUnplugin } from 'unplugin' import escapeRE from 'escape-string-regexp' import type { Plugin } from 'vite' -import MagicString from 'magic-string-extra' +import MagicString from 'magic-string' interface DynamicBasePluginOptions { env: 'dev' | 'server' | 'client' @@ -93,7 +93,12 @@ export const DynamicBasePlugin = createUnplugin(function (options: DynamicBasePl 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 }) + } + } } } }) diff --git a/yarn.lock b/yarn.lock index f528b165dd..27290df7a3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3371,8 +3371,7 @@ __metadata: fs-extra: ^10.0.1 get-port-please: ^2.4.3 knitwork: ^0.1.0 - magic-string: ^0.25.7 - magic-string-extra: ^0.1.2 + magic-string: ^0.26.0 mlly: ^0.4.3 p-debounce: ^4.0.0 pathe: ^0.2.0 @@ -13771,15 +13770,6 @@ __metadata: languageName: node 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": version: 0.25.7 resolution: "magic-string@npm:0.25.7" @@ -13789,6 +13779,15 @@ __metadata: languageName: node 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": version: 1.3.0 resolution: "make-dir@npm:1.3.0" @@ -15437,7 +15436,7 @@ __metadata: hash-sum: ^2.0.0 hookable: ^5.1.1 knitwork: ^0.1.0 - magic-string-extra: ^0.1.2 + magic-string: ^0.26.0 mlly: ^0.4.3 murmurhash-es: ^0.1.1 nitropack: "npm:nitropack-edge@latest" @@ -19740,7 +19739,7 @@ __metadata: languageName: node linkType: hard -"sourcemap-codec@npm:^1.4.4": +"sourcemap-codec@npm:^1.4.4, sourcemap-codec@npm:^1.4.8": version: 1.4.8 resolution: "sourcemap-codec@npm:1.4.8" checksum: b57981c05611afef31605732b598ccf65124a9fcb03b833532659ac4d29ac0f7bfacbc0d6c5a28a03e84c7510e7e556d758d0bb57786e214660016fb94279316