swc: SWC may generate invalid source map when inputSourceMap is provided
Describe the bug
https://stackblitz.com/~/github.com/noyobo/swc-issues
$ npm run build
[Error: failed to read input source map from user-provided sourcemap
Caused by:
bad reference to source #299] {
code: 'GenericFailure'
}
Input code
No response
Config
import {transform} from '@swc/core';
import fs from "node:fs";
const fileCode = fs.readFileSync("./a.js", "utf-8");
const fileMap = fs.readFileSync("./a.js.map", "utf-8");
// valid check https://evanw.github.io/source-map-visualization/
// transformed successfully
const result = await transform(fileCode, {
filename: 'a.js',
sourceMaps: true,
inputSourceMap: fileMap,
jsc: {target: 'es5'},
module: {type: "commonjs", strictMode: false},
})
// fs.writeFileSync("./a.transformed.js", result.code);
// fs.writeFileSync("./a.transformed.js.map", result.map);
// Using the result again to transform throws an error
// and verification failed in https://evanw.github.io/source-map-visualization/
await transform(result.code, {
filename: "a.js",
sourceMaps: true,
inputSourceMap: result.map,
module: {type: "commonjs", strictMode: false},
}).catch((e) => {
/**
* [Error: failed to read input source map from user-provided sourcemap
*
* Caused by:
* bad reference to source #299] {
* code: 'GenericFailure'
* }
*/
console.log(e);
});
Playground link (or link to the minimal reproduction)
https://stackblitz.com/~/github.com/noyobo/swc-issues
SWC Info output
No response
Expected behavior
The transformed sourceMap content can be used again
Actual behavior
No response
Version
1.4.11
Additional context
No response
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 20 (12 by maintainers)
Commits related to this issue
- fix(common): Fix source index for `inputSourceMap` (#8800) **Related issue:** - Closes #8789 — committed to swc-project/swc by kdy1 3 years ago
- fix(es): Allow input source map file to be omitted (#8951) **Description:** Some libraries generate source maps but do not upload them to npm, which causes SWC to fail. **Related issue:** - ... — committed to swc-project/swc by kdy1 3 years ago
- fix(common): Do not generate invalid source map (#9050) **Related issue:** - Closes #8789 — committed to kdy1/swc by kdy1 2 years ago
Seeing the same when updating from
1.4.11to1.5.5.@davidmurdoch https://github.com/swc-project/swc/pull/8951 should fix your issue, although I’m not sure if it fixes this issue