unjs / unplugin

Unified plugin system for Vite, Rollup, Webpack, esbuild, Rolldown, and more

Home Page:https://unplugin.unjs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

why not pass the transform sourcemap to webpack

luhc228 opened this issue · comments

Hi~ I'm now using unplugin to write a plugin and the plugin will be transformed to webpack plugin and esbuild plugin.

I only use one plugin to transform the .jsx file. In other words, there is only one loader to handle .jsx file. So the transform function's param, map is undefined. But after I transform the .jsx file, I get the res.map value which is not the undefined. I am confused why the unplugin use the map first but not the res.map?

callback(null, res.code, map == null ? map : (res.map || map))

Here is how I use the unplugin transform hook:
https://github.com/ice-lab/ice-next/blob/f5755a0c0afdda87b97393ce6dc44f0ac9b4db84/packages/webpack-config/src/unPlugins/compilation.ts#L104

webpack loader load order: https://webpack.js.org/contribute/writing-a-loader/#complex-usage

If there was no map from the beginning and we provide a map, it will cause Webpack to throw an error on merging the sourcemap with null

I make a change like these in node_modules/unplugin/dist/webpack/transform.js, it works. So will the unplugin makes a change? @antfu

- callback(null, res.code, map == null ? map : (res.map || map)) 
+ callback(null, res.code, res.map || map)

I would need a reproduction to confirm this is a valid fix.

@antfu Thank you for your replying.

Here is my demo which can reproduct the problem.

You can install the dependencies and the run the script npm start to start the dev server.

Well, u can see this line. I console the transform result and the sourcemap is correct.

But the final sourcemap is incorrect.

image