swc
swc copied to clipboard
Compiled code imports package.json from outside the output directory
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
@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.
I personally don't believe so (yet), as you said we don't walk through imports but supports single file transform only.
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);
I don't believe tsc-cli provides this feature. Could you provide a example?
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
.
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
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 ofsrc
.
swc . -d dist --copy-files
produces no output in the example I provided.
I encountered the same problem. Is there an adequate solution?
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 See https://swc.rs/docs/usage/cli#--strip-leading-paths