diff --git a/lib/impl/CodeGenerator.js b/lib/impl/CodeGenerator.js index 67a3138..ab229dd 100644 --- a/lib/impl/CodeGenerator.js +++ b/lib/impl/CodeGenerator.js @@ -31,7 +31,12 @@ function generateFile (context, filePath) { map.sources[map.sources.length - 1] = sourcePath; code += `\\\n${ConvertSourceMap.fromObject(map).toComment()}`; } else { - code += `\\\n//# sourceURL=${sourcePath}`; + let sourceURL = sourcePath; + if (sourceURL[0] === '\0') { + sourceURL = sourcePath.slice(1); + } + + code += `\\\n//# sourceURL=${sourceURL}`; } return ` diff --git a/test/cases/misc.js b/test/cases/misc.js index 75edf00..71a3036 100644 --- a/test/cases/misc.js +++ b/test/cases/misc.js @@ -136,4 +136,38 @@ describe ('Misc', () => { expect(compiled).to.be.false; expect(thrown).to.be.true; }); + + it ('should remove null byte from sourceURL comments', async () => { + fs.stub('./src/main.js', () => 'console.log("hello");'); + + let VIRTUAL_MODULE_ID = '\0virtual-module'; + + let myplugin = { + resolveId (id) { + if (id === VIRTUAL_MODULE_ID) { + return VIRTUAL_MODULE_ID; + } + }, + + load (id) { + if (id === VIRTUAL_MODULE_ID) { + return 'export default 123'; + } + }, + + transform (code, id) { + if (id.indexOf('main.js') > -1) { + return `import MyNumber from '${VIRTUAL_MODULE_ID}';${code}` + } + } + }; + + let bundle = await nollup({ + input: './src/main.js', + plugins: [myplugin] + }); + + let { output } = await bundle.generate({ format: 'esm' }); + expect(output[0].code.indexOf('\0')).to.equal(-1); + }); });