Skip to content

Commit

Permalink
fix: revert sourcemp handling for webpack and improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Sep 16, 2021
1 parent 70c2c64 commit b57cf3f
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 22 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"fs-extra": "^10.0.0",
"jest": "^27.2.0",
"jiti": "^1.12.0",
"magic-string": "^0.25.7",
"rollup": "^2.56.3",
"standard-version": "^9.3.1",
"ts-jest": "^27.0.5",
Expand Down
18 changes: 1 addition & 17 deletions src/webpack/loaders/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,7 @@ export default async function transform (this: LoaderContext<any>, source: strin
if (res == null) {
callback(null, source, map)
} else if (typeof res !== 'string') {
// only pass sourcemap when sourcemap is provided upstream
const newMap = map == null ? map : (res.map || map)
// clean up nullish sources produced by magic-string
if (newMap && Array.isArray(newMap.sources)) {
const excluded: number[] = []
newMap.sources = newMap.sources.filter((i:any, idx:number) => {
if (i == null) {
excluded.push(idx)
return false
}
return true
})
newMap.sources = newMap.sources.filter((_: any, idx: number) => {
return !excluded.includes(idx)
})
}
callback(null, res.code, newMap)
callback(null, res.code, map == null ? map : (res.map || map))
} else {
callback(null, res, map)
}
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/transform/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ const { rollup } = require('./unplugin')
export default {
input: './src/main.js',
output: {
dir: './dist/rollup'
dir: './dist/rollup',
sourcemap: true
},
plugins: [
rollup({ msg: 'Rollup' })
Expand Down
18 changes: 16 additions & 2 deletions test/fixtures/transform/unplugin.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
const { createUnplugin } = require('unplugin')
const MagicString = require('magic-string')

module.exports = createUnplugin((options) => {
return {
name: 'transform-fixture',
transformInclude (id) {
return id.match(/[/\\]target\.js$/)
},
transform (code) {
return code.replace('__UNPLUGIN__', `[Injected ${options.msg}]`)
transform (code, id) {
const s = new MagicString(code)
const index = code.indexOf('__UNPLUGIN__')
if (index === -1) {
return null
}

s.overwrite(index, index + '__UNPLUGIN__'.length, `[Injected ${options.msg}]`)
return {
code: s.toString(),
map: s.generateMap({
source: id,
includeContent: true
})
}
}
}
})
3 changes: 2 additions & 1 deletion test/fixtures/transform/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = {
name: 'main',
fileName: 'main.js'
},
outDir: 'dist/vite'
outDir: 'dist/vite',
sourcemap: true
}
}
3 changes: 2 additions & 1 deletion test/fixtures/transform/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ module.exports = {
},
plugins: [
webpack({ msg: 'Webpack' })
]
],
devtool: 'source-map'
}
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3745,6 +3745,13 @@ lru-cache@^6.0.0:
dependencies:
yallist "^4.0.0"

magic-string@^0.25.7:
version "0.25.7"
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.7.tgz#3f497d6fd34c669c6798dcb821f2ef31f5445051"
integrity sha512-4CrMT5DOHTDk4HYDlzmwu4FVCcIYI8gauveasrdCu2IKIFOJ3f0v/8MDGJCDL9oD2ppz/Av1b0Nj345H9M+XIA==
dependencies:
sourcemap-codec "^1.4.4"

make-dir@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-3.1.0.tgz#415e967046b3a7f1d185277d84aa58203726a13f"
Expand Down Expand Up @@ -4712,6 +4719,11 @@ source-map@^0.7.3, source-map@~0.7.2:
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.7.3.tgz#5302f8169031735226544092e64981f751750383"
integrity sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ==

sourcemap-codec@^1.4.4:
version "1.4.8"
resolved "https://registry.yarnpkg.com/sourcemap-codec/-/sourcemap-codec-1.4.8.tgz#ea804bd94857402e6992d05a38ef1ae35a9ab4c4"
integrity sha512-9NykojV5Uih4lgo5So5dtw+f0JgJX30KCNI8gwhz2J9A15wD0Ml6tjHKwf6fTSa6fAdVBdZeNOs9eJ71qCk8vA==

spdx-correct@^3.0.0:
version "3.1.1"
resolved "https://registry.yarnpkg.com/spdx-correct/-/spdx-correct-3.1.1.tgz#dece81ac9c1e6713e5f7d1b6f17d468fa53d89a9"
Expand Down

0 comments on commit b57cf3f

Please sign in to comment.