swc-project / swc-node

Faster ts-node without typecheck
MIT License
1.69k stars 69 forks source link

Sourcemaps not working with @swc-node/register when SWCRC=true #746

Open pauliusg opened 6 months ago

pauliusg commented 6 months ago

I have made minimal reproducible example project only with one source file and empty .swcrc file. When SWCRC is not set, sourcemap lines are good. But when I set SWCRC=true sourcemap lines are wrong.

Attaching minimal example project: swc-maps-issue.zip

Run without SWCRC: npm run maps-good Run with SWCRC: npm run maps-bad

My console output:

C:\Temp\swc-maps-issue>npm run maps-good

> maps-good
> cross-env SWCRC= node --enable-source-maps -r @swc-node/register src/index-dev.ts

c:\Temp\swc-maps-issue\src\index-dev.ts:8
    throw new Error('Test @swc-node/register');
          ^

Error: Test @swc-node/register
    at C:\Temp\swc-maps-issue\src\index-dev.ts:8:11
    at Object.<anonymous> (C:\Temp\swc-maps-issue\src\index-dev.ts:12:1)
    at Module._compile (node:internal/modules/cjs/loader:1356:14)
    at Module._compile (C:\Temp\swc-maps-issue\node_modules\pirates\lib\index.js:117:24)
    at Module._extensions..js (node:internal/modules/cjs/loader:1414:10)
    at Object.newLoader [as .ts] (C:\Temp\swc-maps-issue\node_modules\pirates\lib\index.js:121:7)
    at Module.load (node:internal/modules/cjs/loader:1197:32)
    at Function.Module._load (node:internal/modules/cjs/loader:1013:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:128:12)
    at node:internal/main/run_main_module:28:49

Node.js v18.19.0

C:\Temp\swc-maps-issue>npm run maps-bad

> maps-bad
> cross-env SWCRC=true node --enable-source-maps -r @swc-node/register src/index-dev.ts

c:\Temp\swc-maps-issue\src\index-dev.ts:8
    throw new Error('Test @swc-node/register');
          ^

Error: Test @swc-node/register
    at C:\Temp\swc-maps-issue\src\index-dev.ts:130:15
    at step (C:\Temp\swc-maps-issue\src\index-dev.ts:108:23)
    at Object.next (C:\Temp\swc-maps-issue\src\index-dev.ts:49:20)
    at asyncGeneratorStep (C:\Temp\swc-maps-issue\src\index-dev.ts:3:28)
    at _next (C:\Temp\swc-maps-issue\src\index-dev.ts:21:17)
    at C:\Temp\swc-maps-issue\src\index-dev.ts:26:13
    at new Promise (<anonymous>)
    at C:\Temp\swc-maps-issue\src\index-dev.ts:18:16
    at Object.<anonymous> (C:\Temp\swc-maps-issue\src\index-dev.ts:133:3)
    at Module._compile (node:internal/modules/cjs/loader:1356:14)

Node.js v18.19.0

package.json:

{
    "name": "bad-sourcemaps-with-SWCRC-example",
    "description": "",
    "private": true,
    "engines": {
        "node": ">=18.19.x <19"
    },
    "scripts": {
        "maps-good": "cross-env SWCRC= node --enable-source-maps -r @swc-node/register src/index-dev.ts",
        "maps-bad": "cross-env SWCRC=true node --enable-source-maps -r @swc-node/register src/index-dev.ts"
    },
    "devDependencies": {
        "@swc-node/register": "^1.6.8",
        "@swc/cli": "^0.1.63",
        "@swc/core": "^1.3.102",
        "cross-env": "^7.0.3",
        "typescript": "^5.1.3"
    }
}

tsconfig.json:

{
    "compilerOptions": {
        "module": "commonjs",
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true,
        "noEmit": true,
        "noImplicitAny": false,
        "moduleResolution": "node",
        "resolveJsonModule": true,
        "skipLibCheck": true,
        "strictBindCallApply": true,
        "sourceMap": true,
        "strict": true,
        "outDir": "dist",
        "baseUrl": "./src"
    },
    "include": ["**/*"]
}

.swcrc:

{
    "$schema": "https://json.schemastore.org/swcrc",
}

index-dev.ts:

// Just a comment

(async () => {
    // Just a comment
    // Just a comment

    throw new Error('Test @swc-node/register');

    // Just a comment
})();
dsyddall commented 5 months ago

Possibly a duplicate of https://github.com/swc-project/swc-node/issues/741?

The issue was fixed in https://github.com/swc-project/swc-node/pull/742, but the changes have not yet been released.

pauliusg commented 4 months ago

I can confirm that issue swill exists with @swc-node/register@1.8.0. This release includes @dsyddall mentioned fix https://github.com/swc-project/swc-node/pull/742.

Updated package.json:

{
    "name": "bad-sourcemaps-with-SWCRC-example",
    "description": "",
    "private": true,
    "engines": {
        "node": ">=18.19.x <19"
    },
    "scripts": {
        "maps-good": "cross-env SWCRC= node --enable-source-maps -r @swc-node/register src/index-dev.ts",
        "maps-bad": "cross-env SWCRC=true node --enable-source-maps -r @swc-node/register src/index-dev.ts"
    },
    "devDependencies": {
        "@swc-node/register": "^1.8.0",
        "@swc/cli": "^0.3.10",
        "@swc/core": "^1.4.2",
        "cross-env": "^7.0.3",
        "typescript": "^5.1.3"
    }
}

Attaching updated (with new swc-node version) minimal example project: swc-maps-issue-updated.zip

pauliusg commented 4 months ago

Another update, I can achieve good source maps with some additional configuration in .swcrc file. Source maps are bad, when .swcrc file is empty:

{
    "$schema": "https://json.schemastore.org/swcrc",
}

Source maps are good, when I add this configuration:

{
    "$schema": "https://json.schemastore.org/swcrc",
    "jsc": {
        "target": "esnext"
    },
    "sourceMaps": true
}

Probably we still can consider that issue is not fixed as it should behave the same either SWCRC file is not used or using empty SWCRC file.