swc
swc copied to clipboard
`cli`: Pathing under --out-dir has extra segments
Describe the bug
When the input directory has multiple segments, swc appears to strip only the first. Remaining segments appear in the output tree, making it more nested than expected.
Example:
alexeagle@system76-pc:~/repros/swc-paths$ mkdir -p some/path/to/dir
alexeagle@system76-pc:~/repros/swc-paths$ cat > some/path/to/dir/file.js
const a = 1
alexeagle@system76-pc:~/repros/swc-paths$ npx swc some/path/to/dir --out-dir=some/other/path
Successfully compiled: 1 file with swc (8.46ms)
alexeagle@system76-pc:~/repros/swc-paths$ ls -R some/other/path
some/other/path:
path
some/other/path/path:
to
some/other/path/path/to:
dir
some/other/path/path/to/dir:
file.js
I don't think we expected the repeated path/path part of that resulting output directory.
Note, under bazel this results in output directories like bazel-bin/examples/directory/minify/k8-fastbuild/bin/examples/directory/split_app/file1.js
Input code
No response
Config
No response
Playground link
No response
Expected behavior
I would have expected that example to write output to some/other/path/file.js
Version
@swc/cli: 0.1.53 @swc/core: 1.2.119
Additional context
No response
I also encountered the same problem
Can I work on this, @kdy1?
Of course, thank you!
Just ran into this as well. The expected output is consistent with how Babel handles the same command but the extra directories are definitely being generated unexpectedly. @ruiconti @kdy1 has there been any headway on this one? i feel like this is a pretty large issue and causes extra code to have to be written to re-copy all the output into the expected dir and remove the extra generated dirs manually.
I think I have the same issue.
I'm building a component library for one of my projects and I have the component files under ./src/components. When running npx swc ./src/components -d ./dist I expect all files to be compiled and placed in the destination folder as tsc would do, instead I get an extra components folder inside the output dist folder.
As a temporary fix I'm compiling everything in the root directory and then renaming the output components directory to dist. See build:js below
// package.json
{
... stuff ...
"sripts": {
... other stuff ...
"build": "yarn clean && yarn build:js && yarn build:types",
"build:js": "npx swc ./src/components -d ./ && mv ./components ./dist",
"build:types": "tsc --emitDeclarationOnly --declaration --outDir ./dist",
"clean": "rm -rf ./dist"
},
... more stuff ...
}
build:types is there because, as far as I know, swc doesn't have an option for generating type declaration files.
We are using this logic to create a workaround at the moment:
const firstSegmentIndex = srcPath.indexOf('/');
if (firstSegmentIndex !== -1) {
// TODO remove this when https://github.com/swc-project/swc/issues/3028 is fixed
destPath = destPath.slice(0, -srcPath.length + firstSegmentIndex);
}
Which changes this:
srcPath: 'mylib/nested/other-nested'
destPath: 'dist/mylib/nested/other-nested'
to
srcPath: 'mylib/nested/other-nested'
destPath: 'dist/mylib'
swc src -d dist is transpiling from ./src to ./dist/src instead of ./dist, this wasn't an issue with older versions
Fine:
@swc/cli: ^0.1.65
@swc/core: ^1.3.105
Extra segments:
@swc/cli: ^0.3.2
@swc/core: ^1.3.106
@Dinhero21 Please see https://github.com/swc-project/cli/issues/281