nestjs/nest-cli

Ability to override default swc cliOptions when using SWC compiler

pauliusg opened this issue · 2 comments

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe it

In my project I have to import package.json file:

import { version } from 'package.json';

When using SWC compiler, I get this error:

> nest start --watch

>  SWC  Running...
Successfully compiled: 18 files with swc (41.56ms)
Watching for file changes.
Error: Cannot find module 'package.json'
Require stack:
- c:\app\Grainger\workstation-auth\api\dist\main.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)
    at Function.Module._load (node:internal/modules/cjs/loader:922:27)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at require (node:internal/modules/cjs/helpers:121:18)
    at Object.<anonymous> (c:\app\Grainger\workstation-auth\api\dist\main.js:5:22)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Function.Module._load (node:internal/modules/cjs/loader:960:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)

This is because SWC is not copying json files to dist folder.
To copy those files --copy-files cli option has to be used: https://swc.rs/docs/usage/cli#--copy-files--d

Also, I have tried this configuration in nest-cli.json file:

{
  "$schema": "https://json.schemastore.org/nest-cli",
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "builder": "swc",
    "options": {
      "swcrcPath": ".swcrc",
      "copyFiles": true
    },
    "deleteOutDir": true
  }
}

But it still does not work.

Describe the solution you'd like

I see that NestJS swc compiler gets default with false:

We need an option to override default cliOptions:

cliOptions: {
outDir: tsOptions?.outDir ? convertPath(tsOptions.outDir) : 'dist',
filenames: [configuration?.sourceRoot ?? 'src'],
sync: false,
extensions: ['.js', '.ts'],
copyFiles: false,
includeDotfiles: false,
quiet: false,
watch: false,
...builderOptions,
},

Or at least copyFiles cli option.

Teachability, documentation, adoption, migration strategy

No response

What is the motivation / use case for changing the behavior?

Described above.

Probably this not a NestJS issue because if I try compile directly with SWC:

npx swc ./src -d dist --config-file .swcrc --extensions .ts,.json --copy-files

json file is still not copied to dist folder...

My .swcrc file:

{
    "$schema": "https://json.schemastore.org/swcrc",
    "jsc": {
        "parser": {
            "syntax": "typescript",
            "decorators": true,
            "dynamicImport": true
        },
        "target": "esnext",
        "baseUrl": "./src"
    },
    "module": {"type": "commonjs"},
    "sourceMaps": true,
}

Closing as this is a SWC issue. When I compile with TSC, I get this dist folder structure:
image
However with npx swc ./src -d dist --config-file .swcrc --extensions .ts,.json --copy-files
package.json is not copied and I get this dist folder structure (src folder content):
image