swc icon indicating copy to clipboard operation
swc copied to clipboard

Compiled code imports package.json from outside the output directory

Open andrucz opened this issue 10 months ago • 10 comments

Describe the bug

package.json is not copied into the output directory even with --copy-files and the compiled code ends up importing it from the input directory.

This issue has been initially reported in the Nest CLI repo: https://github.com/nestjs/nest-cli/issues/2253

Input code

import { version } from '../package.json';
console.log(version);

Config

{
  "jsc": {
    "parser": {
      "syntax": "typescript"
    },
    "target": "es5"
  },
  "sourceMaps": true
}

Playground link

No response

Expected behavior

When resolveJsonModule is enabled in tsconfig.json, tsc copies package.json into the output directory:

dist/
  package.json
  src/
    index.js

Therefore, importing from ../package.json works as expected.

Actual behavior

Compiled code imports package.json from outside the output directory.

Version

1.3.78

Additional context

No response

andrucz avatar Aug 23 '23 13:08 andrucz

@kwonoj @magic-akari @Austaras Do you think we should support this? This looks like a project-mode feature of tsc, but I'm not sure.

Currently we only support single-file mode.

kdy1 avatar Aug 23 '23 15:08 kdy1

I personally don't believe so (yet), as you said we don't walk through imports but supports single file transform only.

kwonoj avatar Aug 23 '23 16:08 kwonoj

Any workarounds would be appreciated.

I've tried adding this path mapping to tsconfig.json:

"paths": {
  "~/*": ["./*"]
}

Changing .swcrc to

{
  "$schema": "https://json.schemastore.org/swcrc",
  "sourceMaps": true,
  "jsc": {
    "parser": {
      "syntax": "typescript"
    },
    "baseUrl": "./",
    "paths": {
      "~/*": ["dist/*"]
    }
  }
}

Changing the code to

import { version } from '~/package.json';
console.log(version);

And the build script to cp package.json dist && swc ./src -d dist --copy-files

And this was the result:

import { version } from "../dist/package.json";
console.log(version);

andrucz avatar Aug 23 '23 17:08 andrucz

I don't believe tsc-cli provides this feature. Could you provide a example?

magic-akari avatar Aug 24 '23 12:08 magic-akari

dist/
  package.json
  src/
    index.js

The output structure is a bit unusual. I think if you want to simulate similar behavior for the SWC, you should make . the input directory for the SWC instead of src.

magic-akari avatar Aug 24 '23 12:08 magic-akari

I don't believe tsc-cli provides this feature. Could you provide a example?

You can use the same example and change the build script to tsc

andrucz avatar Aug 24 '23 12:08 andrucz

dist/
  package.json
  src/
    index.js

The output structure is a bit unusual. I think if you want to simulate similar behavior for the SWC, you should make . the input directory for the SWC instead of src.

swc . -d dist --copy-files produces no output in the example I provided.

andrucz avatar Aug 24 '23 13:08 andrucz

I encountered the same problem. Is there an adequate solution?

NeoKms avatar Jan 29 '24 20:01 NeoKms

Same issue here. Seems like some recent changes caused this to happen. My build outpus now all get thrown into /dist/src/{main.js, ....}

dnlsndr avatar Feb 02 '24 12:02 dnlsndr

@dnlsndr See https://swc.rs/docs/usage/cli#--strip-leading-paths

kdy1 avatar Feb 02 '24 12:02 kdy1