Skip to content

swc stripping non-file extensions from imports when using paths + commonjs #2269

@stewartmcgown

Description

@stewartmcgown

Describe the bug
Compiling a NestJS project with swc leads to some imports having their trailing filenames cut if the filename uses periods. As far as I can this is only happening when using the paths property and the commonjs module type. Using paths in combination with ES modules does not result in this error. I can also replicate this when using umd modules.

For example:

import { CookieNames, setAuthCookies } from '~interceptors/cookie.interceptor';

Compiles to:

var _cookieInterceptor = require("../../interceptors/cookie");

Leading to a predictable crash when attempting to run:

internal/modules/cjs/loader.js:892
  throw err;
  ^

Error: Cannot find module '../../interceptors/cookie'
Require stack:
- /home/stewart/Documents/ecoeats/server/dist/guards/gql/gql.guard.js
- /home/stewart/Documents/ecoeats/server/dist/main.js

When inspecting the compiled JS from the main.ts file, the entrypoint in my application and therefore never included via the paths aliases in my code, the require import seems to be correct:

var _sentryInterceptor = require("./interceptors/sentry.interceptor");

Config

{
    "jsc": {
        "parser": {
            "syntax": "typescript",
            "decorators": true
        },
        "transform": {
            "legacyDecorator": true,
            "decoratorMetadata": true
        },
        "baseUrl": "./src",
        "target": "es2015",
        "paths": {
            "~exceptions/*": [
                "exceptions/*"
            ],
            "~modules/*": [
                "modules/*"
            ],
            "~utils/*": [
                "utils/*"
            ],
            "~models/*": [
                "models/*"
            ],
            "~config/*": [
                "config/*"
            ],
            "~decorators/*": [
                "decorators/*"
            ],
            "~interceptors/*": [
                "interceptors/*"
            ],
            "~guards/*": [
                "guards/*"
            ],
            "~crypto/*": [
                "crypto/*"
            ]
        }
    },
    "module": {
        "type": "commonjs",
        "strict": false,
        "strictMode": true,
        "lazy": false,
        "noInterop": false
    },
    "sourceMaps": true
}

Expected behavior
Filenames with periods not to be treated as files unless they match a very specific set of extensions.

Version
The version of @swc/core: 1.2.88

p.s. Your project rocks 😍

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions