Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions integration/example/build-node-modules.civet
Original file line number Diff line number Diff line change
@@ -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({})
]
}
3 changes: 3 additions & 0 deletions integration/example/source/uses-pkg.civet
Original file line number Diff line number Diff line change
@@ -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)
11 changes: 10 additions & 1 deletion source/unplugin/unplugin.civet
Original file line number Diff line number Diff line change
Expand Up @@ -545,12 +545,21 @@ export const rawPlugin: Parameters<typeof createUnplugin<PluginOptions>>[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 <? "string"
JSON.parse(sourceMap)
else
sourceMap.json
path.relative outDir, filename
srcFileName
path.relative outDir, id

transformed: TransformResult .=
Expand Down
17 changes: 17 additions & 0 deletions test/integration.civet
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ describe "integration", ->

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')"'''

Expand Down