diff --git a/integration/example/build-node-modules.civet b/integration/example/build-node-modules.civet new file mode 100644 index 000000000..f2dfeff75 --- /dev/null +++ b/integration/example/build-node-modules.civet @@ -0,0 +1,14 @@ +// This file is used in test/integration.civet to test sourcemap source for node_modules +esbuild from esbuild + +civetPlugin from ../../dist/unplugin/esbuild.mjs + +esbuild.build { + +sourcemap + entryPoints: ['source/uses-pkg.civet'], + outdir: 'dist', + bundle: true, + plugins: [ + civetPlugin({}) + ] +} diff --git a/integration/example/source/uses-pkg.civet b/integration/example/source/uses-pkg.civet new file mode 100644 index 000000000..79b7031c6 --- /dev/null +++ b/integration/example/source/uses-pkg.civet @@ -0,0 +1,3 @@ +// This file is used in test/integration.civet to test sourcemap source for node_modules +import { add } from 'test-civet-pkg' +console.log add(1, 2) diff --git a/source/unplugin/unplugin.civet b/source/unplugin/unplugin.civet index 2e1d49c43..da2c4971b 100644 --- a/source/unplugin/unplugin.civet +++ b/source/unplugin/unplugin.civet @@ -545,12 +545,21 @@ export const rawPlugin: Parameters>[0] = fsMap.set slashed, compiledTS sourceMaps.set slashed, civetSourceMap + // When id ends with outExt, it was resolved by our resolveId hook as a virtual + // file in a plugin namespace; the bundler uses source paths as-is. When id + // doesn't end with outExt (e.g., node_modules files resolved natively by the + // bundler), the bundler resolves source paths relative to the input file's + // directory, so we make the source path relative to that directory. + srcFileName := if id.endsWith outExt + path.relative outDir, filename + else + path.relative path.dirname(filename), filename jsonSourceMap := sourceMap and if sourceMap assert.equal data.sources[0].replace(/\\/g, '/'), "../source/main.civet" + it "should have correct source map source for civet files in node_modules", -> + // Create a minimal test package fixture in node_modules + pkgDir := "integration/example/node_modules/test-civet-pkg" + fs.mkdirSync pkgDir, {recursive: true} + fs.writeFileSync `${pkgDir}/package.json`, + JSON.stringify {name: "test-civet-pkg", version: "1.0.0", main: "index.civet"} + fs.writeFileSync `${pkgDir}/index.civet`, + "export add := (a: number, b: number): number -> a + b\n" + + await execCmd 'bash -c "(cd integration/example && ../../dist/civet --no-config build-node-modules.civet)"' + data := JSON.parse(fs.readFileSync("integration/example/dist/uses-pkg.js.map", "utf8")) + + pkgSource := data.sources.find (s: string) => s.includes 'test-civet-pkg' + assert.ok pkgSource, "should have a source entry for the test-civet-pkg node_modules file" + // Source path should NOT have a doubled node_modules segment + assert.equal pkgSource.replace(/\\/g, '/'), "../node_modules/test-civet-pkg/index.civet" + it "should load bundled unplugin when localStorage lacks getItem", -> await execCmd '''node -e "globalThis.localStorage = {}; require('./dist/unplugin/unplugin.js')"'''