Reputation: 782
I am attempting to create Rollup plugin to use in my Vite build which will transform a file dynamically during build. Using transform(code: string, id: string)
the transformation works as expected. The only issue is that I get a warning in the build that the transformed file's sourcemap does not match because it was transformed. The Rollup documentation says that if I transform the code, I need to manually create the sourcemap. How do you do that?
magic-string provides a simple way to generate such a map for elementary transformations like adding or removing code snippets.
Attempting to use magic-string doesn't seem to be getting me anywhere. Though I create the sourcemap and return it in the way described by the Rollup documentation, the resulting bundle sourcemap does not include the sourcemap for the
My plugin:
export default function () {
return {
name: 'exclude-msw',
transform(code: string, id: string) {
if (id.match(/main\.ts/)) {
const s = new MagicString(code)
//...
//my transformation --- basically, just removes a chunk of code from the file
//...
const map = s.generateMap({
source: id,
file: id + '.map',
includeContent: true
})
return {
code: s.toString(),
map: map.toString()
}
}
}
}
}
The resulting map still includes the original file's map... Am I doing something wrong? The documentation also says:
If the transformation does not move code, you can preserve existing sourcemaps by returning null:
return {
code: transformedCode,
map: null
};
So passing the new map is behaving the same as though I passed null... I don't understand what I am doing wrong. I tried making all kinds of changes but always end up with either the old mapping, or no mapping of the changed file at all.
How can I get the map for the new file's state in the Vite bundle's map?
Upvotes: 1
Views: 636