Skip to content

Commit

Permalink
feat: using magicStr to generate sourcemap
Browse files Browse the repository at this point in the history
  • Loading branch information
nonzzz committed Dec 5, 2023
1 parent 6299f94 commit 9b73b2c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"@rollup/pluginutils": "^5.0.2",
"@xn-sakina/rml-wasm": "^2.1.1",
"debug": "^4.3.4",
"magic-string": "^0.30.5",
"rs-module-lexer": "^2.1.1"
},
"ava": {
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ function transformPresetModule(api: ExternalPluginAPI): Plugin {
if (!api.filter(id)) return
if (!id.includes(nodeModules)) return
// coomonjs
code = transformCJSRequire(code, api.dependency.dependency)
const result = transformCJSRequire(code, dependency.dependency)
// esm
if (dependency.filter(code, id)) {
return transformWithBabel(code, dependency)
}
return { code }
return result
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from 'path'
import fs from 'fs'
import os from 'os'
import process from 'process'
import MagicString from 'magic-string'
import AggregateError from '@nolyfill/es-aggregate-error'
import { ModuleInfo } from './interface'

Expand Down Expand Up @@ -39,11 +40,12 @@ export function is(condit: boolean, message: string) {
export const _import = new Function('specifier', 'return import(specifier)')

export function transformCJSRequire(code: string, extenrals: Record<string, ModuleInfo>) {
const s = new MagicString(code)
for (const externalModule in extenrals) {
const reg = new RegExp(`require\\((["'\`])\\s*${externalModule}\\s*(\\1)\\)`, 'g')
code = code.replace(reg, extenrals[externalModule].global)
s.replace(reg, extenrals[externalModule].global)
}
return code
return { code: s.toString(), map: s.generateMap() }
}

export const MAX_CONCURRENT = (() => {
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4787,6 +4787,15 @@ __metadata:
languageName: node
linkType: hard

"magic-string@npm:^0.30.5":
version: 0.30.5
resolution: "magic-string@npm:0.30.5"
dependencies:
"@jridgewell/sourcemap-codec": ^1.4.15
checksum: da10fecff0c0a7d3faf756913ce62bd6d5e7b0402be48c3b27bfd651b90e29677e279069a63b764bcdc1b8ecdcdb898f29a5c5ec510f2323e8d62ee057a6eb18
languageName: node
linkType: hard

"make-dir@npm:^3.0.0":
version: 3.1.0
resolution: "make-dir@npm:3.1.0"
Expand Down Expand Up @@ -6769,6 +6778,7 @@ __metadata:
debug: ^4.3.4
eslint: ^8.51.0
eslint-config-kagura: ^2.0.1
magic-string: ^0.30.5
playwright: ^1.35.1
react: ^18.2.0
rs-module-lexer: ^2.1.1
Expand Down

0 comments on commit 9b73b2c

Please sign in to comment.