diff --git a/crates/swc/tests/fixture/deno/paths/ext/002/output/index.js b/crates/swc/tests/fixture/deno/paths/ext/002/output/index.js index 507538549ede..1d68a591e3a7 100644 --- a/crates/swc/tests/fixture/deno/paths/ext/002/output/index.js +++ b/crates/swc/tests/fixture/deno/paths/ext/002/output/index.js @@ -1,2 +1,2 @@ -import styles from "./foo.ts/index"; +import styles from "./foo.ts/index.js"; console.log(styles); diff --git a/crates/swc/tests/fixture/deno/paths/issue-2126/output/src/index.ts b/crates/swc/tests/fixture/deno/paths/issue-2126/output/src/index.ts index 758fa00b7554..da767706d2a2 100644 --- a/crates/swc/tests/fixture/deno/paths/issue-2126/output/src/index.ts +++ b/crates/swc/tests/fixture/deno/paths/issue-2126/output/src/index.ts @@ -2,5 +2,5 @@ Object.defineProperty(exports, "__esModule", { value: true }); -const _a = require("../packages/a/src/index"); +const _a = require("../packages/a/src"); console.log(`${(0, _a.displayA)()}`); diff --git a/crates/swc/tests/fixture/deno/paths/issue-2724/output/src/index.ts b/crates/swc/tests/fixture/deno/paths/issue-2724/output/src/index.ts index b2dee6df697d..e44f75ab8257 100644 --- a/crates/swc/tests/fixture/deno/paths/issue-2724/output/src/index.ts +++ b/crates/swc/tests/fixture/deno/paths/issue-2724/output/src/index.ts @@ -4,6 +4,6 @@ Object.defineProperty(exports, "__esModule", { }); const _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard"); (async function() { - const { displayA } = await Promise.resolve().then(()=>/*#__PURE__*/ _interop_require_wildcard._(require("../packages/a/src/index"))); + const { displayA } = await Promise.resolve().then(()=>/*#__PURE__*/ _interop_require_wildcard._(require("../packages/a/src"))); console.log(displayA()); })(); diff --git a/crates/swc/tests/fixture/deno/paths/issue-2844/input/src/core/utilFile.ts b/crates/swc/tests/fixture/deno/paths/issue-2844/input/src/core/utilFile.ts new file mode 100644 index 000000000000..e405565d6f4b --- /dev/null +++ b/crates/swc/tests/fixture/deno/paths/issue-2844/input/src/core/utilFile.ts @@ -0,0 +1 @@ +export function test() {} diff --git a/crates/swc/tests/fixture/deno/paths/issue-2844/input/src/core/utils/index.ts b/crates/swc/tests/fixture/deno/paths/issue-2844/input/src/core/utils/index.ts new file mode 100644 index 000000000000..74dab2b98800 --- /dev/null +++ b/crates/swc/tests/fixture/deno/paths/issue-2844/input/src/core/utils/index.ts @@ -0,0 +1,3 @@ +import { test } from "core/utils/utilFile"; +test(); +export function utilFn() {} diff --git a/crates/swc/tests/fixture/deno/paths/issue-2844/output/src/core/utilFile.ts b/crates/swc/tests/fixture/deno/paths/issue-2844/output/src/core/utilFile.ts new file mode 100644 index 000000000000..acc0f7b3776f --- /dev/null +++ b/crates/swc/tests/fixture/deno/paths/issue-2844/output/src/core/utilFile.ts @@ -0,0 +1,11 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { + value: true +}); +Object.defineProperty(exports, "test", { + enumerable: true, + get: function() { + return test; + } +}); +function test() {} diff --git a/crates/swc/tests/fixture/deno/paths/issue-2844/output/src/core/utils/index.ts b/crates/swc/tests/fixture/deno/paths/issue-2844/output/src/core/utils/index.ts new file mode 100644 index 000000000000..9dc1510486a6 --- /dev/null +++ b/crates/swc/tests/fixture/deno/paths/issue-2844/output/src/core/utils/index.ts @@ -0,0 +1,13 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { + value: true +}); +Object.defineProperty(exports, "utilFn", { + enumerable: true, + get: function() { + return utilFn; + } +}); +const _utilFile = require("./utilFile"); +(0, _utilFile.test)(); +function utilFn() {} diff --git a/crates/swc/tests/fixture/deno/paths/issue-3447/input/.swcrc b/crates/swc/tests/fixture/deno/paths/issue-3447/input/.swcrc index eafe016a9ca3..d3f9e8256cfd 100644 --- a/crates/swc/tests/fixture/deno/paths/issue-3447/input/.swcrc +++ b/crates/swc/tests/fixture/deno/paths/issue-3447/input/.swcrc @@ -16,8 +16,12 @@ } }, "paths": { - "@moduleA/*": ["modules/01-moduleA/*"], - "@moduleB/*": ["modules/03-moduleB/*"] + "@moduleA/*": [ + "modules/01-moduleA/*" + ], + "@moduleB/*": [ + "modules/02-moduleB/*" + ] } }, "module": { @@ -28,4 +32,4 @@ "noInterop": false, "ignoreDynamic": true } -} +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/deno/paths/issue-3447/output/src/index.ts b/crates/swc/tests/fixture/deno/paths/issue-3447/output/src/index.ts index 15e2d82f6423..9c7dcd7e5a1d 100644 --- a/crates/swc/tests/fixture/deno/paths/issue-3447/output/src/index.ts +++ b/crates/swc/tests/fixture/deno/paths/issue-3447/output/src/index.ts @@ -1,3 +1,3 @@ import { sum } from "./modules/01-moduleA/utils"; -export { multiply } from "./modules/03-moduleB/utils"; +export { multiply } from "./modules/02-moduleB/utils"; console.log(sum(1, 2)); diff --git a/crates/swc/tests/fixture/issues-3xxx/3547/1/input/src/lib/structures/route/index.js b/crates/swc/tests/fixture/issues-3xxx/3547/1/input/src/lib/structures/route/index.js new file mode 100644 index 000000000000..04514102e600 --- /dev/null +++ b/crates/swc/tests/fixture/issues-3xxx/3547/1/input/src/lib/structures/route/index.js @@ -0,0 +1 @@ +export default 4; \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-3xxx/3547/1/output/index.js b/crates/swc/tests/fixture/issues-3xxx/3547/1/output/index.js index 645392b5af79..c0c453c2eba3 100644 --- a/crates/swc/tests/fixture/issues-3xxx/3547/1/output/index.js +++ b/crates/swc/tests/fixture/issues-3xxx/3547/1/output/index.js @@ -1,2 +1,2 @@ -import { NekoRoute } from "./src/lib/structures/route"; +import { NekoRoute } from "./src/lib/structures/route/index.js"; console.log(NekoRoute); diff --git a/crates/swc/tests/fixture/issues-3xxx/3547/1/output/src/lib/structures/route/index.js b/crates/swc/tests/fixture/issues-3xxx/3547/1/output/src/lib/structures/route/index.js new file mode 100644 index 000000000000..456ffaafacd1 --- /dev/null +++ b/crates/swc/tests/fixture/issues-3xxx/3547/1/output/src/lib/structures/route/index.js @@ -0,0 +1 @@ +export default 4; diff --git a/crates/swc/tests/fixture/issues-5xxx/5214/1/output/input1.js b/crates/swc/tests/fixture/issues-5xxx/5214/1/output/input1.js index bb6c4dec6fa9..b8705cb5fcb5 100644 --- a/crates/swc/tests/fixture/issues-5xxx/5214/1/output/input1.js +++ b/crates/swc/tests/fixture/issues-5xxx/5214/1/output/input1.js @@ -1,2 +1,2 @@ -import styles from "./src/foo.bar.baz.js"; +import styles from "./src/foo.bar.baz"; console.log(styles); diff --git a/crates/swc/tests/fixture/issues-7xxx/7417/output/src/lib/foo.ts b/crates/swc/tests/fixture/issues-7xxx/7417/output/src/lib/foo.ts index 2d1c5d3da714..2ec72f7e4bf0 100644 --- a/crates/swc/tests/fixture/issues-7xxx/7417/output/src/lib/foo.ts +++ b/crates/swc/tests/fixture/issues-7xxx/7417/output/src/lib/foo.ts @@ -9,7 +9,7 @@ Object.defineProperty(exports, "default", { } }); const _interop_require_default = require("@swc/helpers/_/_interop_require_default"); -const _ = /*#__PURE__*/ _interop_require_default._(require("./index")); +const _ = /*#__PURE__*/ _interop_require_default._(require(".")); function bar() { console.log(_.default); } diff --git a/crates/swc/tests/fixture/jsc-paths/7603/1/input/.swcrc b/crates/swc/tests/fixture/jsc-paths/7603/1/input/.swcrc new file mode 100644 index 000000000000..aa0d9f13a1f9 --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/1/input/.swcrc @@ -0,0 +1,17 @@ +{ + "$schema": "https://json.schemastore.org/swcrc", + "jsc": { + "parser": { + "syntax": "typescript", + "decorators": true, + "dynamicImport": true + }, + "baseUrl": "./src", + "paths": { + "*": [ + "*" + ] + } + }, + "minify": false +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/jsc-paths/7603/1/input/src/app.module.ts b/crates/swc/tests/fixture/jsc-paths/7603/1/input/src/app.module.ts new file mode 100644 index 000000000000..91ac8b84e178 --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/1/input/src/app.module.ts @@ -0,0 +1,9 @@ +import { Module } from '@nestjs/common'; +import { UsersModule } from 'users/users.module'; + +@Module({ + imports: [UsersModule], + controllers: [], + providers: [], +}) +export class AppModule {} diff --git a/crates/swc/tests/fixture/jsc-paths/7603/1/input/src/main.ts b/crates/swc/tests/fixture/jsc-paths/7603/1/input/src/main.ts new file mode 100644 index 000000000000..6bf7fb0590b9 --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/1/input/src/main.ts @@ -0,0 +1,8 @@ +import { NestFactory } from '@nestjs/core'; +import { AppModule } from 'app.module'; + +async function bootstrap() { + const app = await NestFactory.create(AppModule); + await app.listen(5002); +} +bootstrap(); diff --git a/crates/swc/tests/fixture/jsc-paths/7603/1/input/src/users/dto/create-user.dto.ts b/crates/swc/tests/fixture/jsc-paths/7603/1/input/src/users/dto/create-user.dto.ts new file mode 100644 index 000000000000..0311be1384d0 --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/1/input/src/users/dto/create-user.dto.ts @@ -0,0 +1 @@ +export class CreateUserDto {} diff --git a/crates/swc/tests/fixture/jsc-paths/7603/1/input/src/users/dto/update-user.dto.ts b/crates/swc/tests/fixture/jsc-paths/7603/1/input/src/users/dto/update-user.dto.ts new file mode 100644 index 000000000000..d71f3117a90a --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/1/input/src/users/dto/update-user.dto.ts @@ -0,0 +1,4 @@ +import { PartialType } from '@nestjs/mapped-types'; +import { CreateUserDto } from 'users/dto/create-user.dto'; + +export class UpdateUserDto extends PartialType(CreateUserDto) {} diff --git a/crates/swc/tests/fixture/jsc-paths/7603/1/input/src/users/entities/user.entity.ts b/crates/swc/tests/fixture/jsc-paths/7603/1/input/src/users/entities/user.entity.ts new file mode 100644 index 000000000000..4f82c14571c4 --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/1/input/src/users/entities/user.entity.ts @@ -0,0 +1 @@ +export class User {} diff --git a/crates/swc/tests/fixture/jsc-paths/7603/1/input/src/users/users.controller.spec.ts b/crates/swc/tests/fixture/jsc-paths/7603/1/input/src/users/users.controller.spec.ts new file mode 100644 index 000000000000..5ba7e6c3256f --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/1/input/src/users/users.controller.spec.ts @@ -0,0 +1,20 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { UsersController } from 'users/users.controller'; +import { UsersService } from 'users/users.service'; + +describe('UsersController', () => { + let controller: UsersController; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + controllers: [UsersController], + providers: [UsersService], + }).compile(); + + controller = module.get(UsersController); + }); + + it('should be defined', () => { + expect(controller).toBeDefined(); + }); +}); diff --git a/crates/swc/tests/fixture/jsc-paths/7603/1/input/src/users/users.controller.ts b/crates/swc/tests/fixture/jsc-paths/7603/1/input/src/users/users.controller.ts new file mode 100644 index 000000000000..bf9e648ca148 --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/1/input/src/users/users.controller.ts @@ -0,0 +1,34 @@ +import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common'; +import { UsersService } from 'users/users.service'; +import { CreateUserDto } from 'users/dto/create-user.dto'; +import { UpdateUserDto } from 'users/dto/update-user.dto'; + +@Controller('users') +export class UsersController { + constructor(private readonly usersService: UsersService) {} + + @Post() + create(@Body() createUserDto: CreateUserDto) { + return this.usersService.create(createUserDto); + } + + @Get() + findAll() { + return this.usersService.findAll(); + } + + @Get(':id') + findOne(@Param('id') id: string) { + return this.usersService.findOne(+id); + } + + @Patch(':id') + update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) { + return this.usersService.update(+id, updateUserDto); + } + + @Delete(':id') + remove(@Param('id') id: string) { + return this.usersService.remove(+id); + } +} diff --git a/crates/swc/tests/fixture/jsc-paths/7603/1/input/src/users/users.module.ts b/crates/swc/tests/fixture/jsc-paths/7603/1/input/src/users/users.module.ts new file mode 100644 index 000000000000..b0cd1fc260cc --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/1/input/src/users/users.module.ts @@ -0,0 +1,9 @@ +import { Module } from '@nestjs/common'; +import { UsersService } from 'users/users.service'; +import { UsersController } from 'users/users.controller'; + +@Module({ + controllers: [UsersController], + providers: [UsersService], +}) +export class UsersModule { } diff --git a/crates/swc/tests/fixture/jsc-paths/7603/1/input/src/users/users.service.spec.ts b/crates/swc/tests/fixture/jsc-paths/7603/1/input/src/users/users.service.spec.ts new file mode 100644 index 000000000000..b54562b6dcf2 --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/1/input/src/users/users.service.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { UsersService } from 'users/users.service'; + +describe('UsersService', () => { + let service: UsersService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [UsersService], + }).compile(); + + service = module.get(UsersService); + }); + + it('should be defined', () => { + expect(service).toBeDefined(); + }); +}); diff --git a/crates/swc/tests/fixture/jsc-paths/7603/1/input/src/users/users.service.ts b/crates/swc/tests/fixture/jsc-paths/7603/1/input/src/users/users.service.ts new file mode 100644 index 000000000000..83b4a981727f --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/1/input/src/users/users.service.ts @@ -0,0 +1,26 @@ +import { Injectable } from '@nestjs/common'; +import { CreateUserDto } from 'users/dto/create-user.dto'; +import { UpdateUserDto } from 'users/dto/update-user.dto'; + +@Injectable() +export class UsersService { + create(createUserDto: CreateUserDto) { + return 'This action adds a new user'; + } + + findAll() { + return `This action returns all users`; + } + + findOne(id: number) { + return `This action returns a #${id} user`; + } + + update(id: number, updateUserDto: UpdateUserDto) { + return `This action updates a #${id} user`; + } + + remove(id: number) { + return `This action removes a #${id} user`; + } +} diff --git a/crates/swc/tests/fixture/jsc-paths/7603/1/output/src/app.module.ts b/crates/swc/tests/fixture/jsc-paths/7603/1/output/src/app.module.ts new file mode 100644 index 000000000000..1a0f89078ed0 --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/1/output/src/app.module.ts @@ -0,0 +1,17 @@ +import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check"; +import { _ as _ts_decorate } from "@swc/helpers/_/_ts_decorate"; +import { Module } from "@nestjs/common"; +import { UsersModule } from "./users/users.module"; +export var AppModule = function AppModule() { + "use strict"; + _class_call_check(this, AppModule); +}; +AppModule = _ts_decorate([ + Module({ + imports: [ + UsersModule + ], + controllers: [], + providers: [] + }) +], AppModule); diff --git a/crates/swc/tests/fixture/jsc-paths/7603/1/output/src/main.ts b/crates/swc/tests/fixture/jsc-paths/7603/1/output/src/main.ts new file mode 100644 index 000000000000..f979afaddb54 --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/1/output/src/main.ts @@ -0,0 +1,34 @@ +import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator"; +import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator"; +import { NestFactory } from "@nestjs/core"; +import { AppModule } from "./app.module"; +function bootstrap() { + return _bootstrap.apply(this, arguments); +} +function _bootstrap() { + _bootstrap = _async_to_generator(function() { + var app; + return _ts_generator(this, function(_state) { + switch(_state.label){ + case 0: + return [ + 4, + NestFactory.create(AppModule) + ]; + case 1: + app = _state.sent(); + return [ + 4, + app.listen(5002) + ]; + case 2: + _state.sent(); + return [ + 2 + ]; + } + }); + }); + return _bootstrap.apply(this, arguments); +} +bootstrap(); diff --git a/crates/swc/tests/fixture/jsc-paths/7603/1/output/src/users/dto/create-user.dto.ts b/crates/swc/tests/fixture/jsc-paths/7603/1/output/src/users/dto/create-user.dto.ts new file mode 100644 index 000000000000..6260cebb7bba --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/1/output/src/users/dto/create-user.dto.ts @@ -0,0 +1,5 @@ +import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check"; +export var CreateUserDto = function CreateUserDto() { + "use strict"; + _class_call_check(this, CreateUserDto); +}; diff --git a/crates/swc/tests/fixture/jsc-paths/7603/1/output/src/users/dto/update-user.dto.ts b/crates/swc/tests/fixture/jsc-paths/7603/1/output/src/users/dto/update-user.dto.ts new file mode 100644 index 000000000000..56d561199ff2 --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/1/output/src/users/dto/update-user.dto.ts @@ -0,0 +1,15 @@ +import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check"; +import { _ as _inherits } from "@swc/helpers/_/_inherits"; +import { _ as _create_super } from "@swc/helpers/_/_create_super"; +import { PartialType } from "@nestjs/mapped-types"; +import { CreateUserDto } from "./create-user.dto"; +export var UpdateUserDto = /*#__PURE__*/ function(_PartialType) { + "use strict"; + _inherits(UpdateUserDto, _PartialType); + var _super = _create_super(UpdateUserDto); + function UpdateUserDto() { + _class_call_check(this, UpdateUserDto); + return _super.apply(this, arguments); + } + return UpdateUserDto; +}(PartialType(CreateUserDto)); diff --git a/crates/swc/tests/fixture/jsc-paths/7603/1/output/src/users/entities/user.entity.ts b/crates/swc/tests/fixture/jsc-paths/7603/1/output/src/users/entities/user.entity.ts new file mode 100644 index 000000000000..2bff55d67e63 --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/1/output/src/users/entities/user.entity.ts @@ -0,0 +1,5 @@ +import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check"; +export var User = function User() { + "use strict"; + _class_call_check(this, User); +}; diff --git a/crates/swc/tests/fixture/jsc-paths/7603/1/output/src/users/users.controller.spec.ts b/crates/swc/tests/fixture/jsc-paths/7603/1/output/src/users/users.controller.spec.ts new file mode 100644 index 000000000000..03e3d1b39a57 --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/1/output/src/users/users.controller.spec.ts @@ -0,0 +1,36 @@ +import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator"; +import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator"; +import { Test } from "@nestjs/testing"; +import { UsersController } from "./users.controller"; +import { UsersService } from "./users.service"; +describe("UsersController", function() { + var controller; + beforeEach(/*#__PURE__*/ _async_to_generator(function() { + var module; + return _ts_generator(this, function(_state) { + switch(_state.label){ + case 0: + return [ + 4, + Test.createTestingModule({ + controllers: [ + UsersController + ], + providers: [ + UsersService + ] + }).compile() + ]; + case 1: + module = _state.sent(); + controller = module.get(UsersController); + return [ + 2 + ]; + } + }); + })); + it("should be defined", function() { + expect(controller).toBeDefined(); + }); +}); diff --git a/crates/swc/tests/fixture/jsc-paths/7603/1/output/src/users/users.controller.ts b/crates/swc/tests/fixture/jsc-paths/7603/1/output/src/users/users.controller.ts new file mode 100644 index 000000000000..2089e981a46e --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/1/output/src/users/users.controller.ts @@ -0,0 +1,70 @@ +import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check"; +import { _ as _create_class } from "@swc/helpers/_/_create_class"; +import { _ as _define_property } from "@swc/helpers/_/_define_property"; +import { _ as _ts_decorate } from "@swc/helpers/_/_ts_decorate"; +import { _ as _ts_param } from "@swc/helpers/_/_ts_param"; +import { Controller, Get, Post, Body, Patch, Param, Delete } from "@nestjs/common"; +export var UsersController = /*#__PURE__*/ function() { + "use strict"; + function UsersController(usersService) { + _class_call_check(this, UsersController); + _define_property(this, "usersService", void 0); + this.usersService = usersService; + } + _create_class(UsersController, [ + { + key: "create", + value: function create(createUserDto) { + return this.usersService.create(createUserDto); + } + }, + { + key: "findAll", + value: function findAll() { + return this.usersService.findAll(); + } + }, + { + key: "findOne", + value: function findOne(id) { + return this.usersService.findOne(+id); + } + }, + { + key: "update", + value: function update(id, updateUserDto) { + return this.usersService.update(+id, updateUserDto); + } + }, + { + key: "remove", + value: function remove(id) { + return this.usersService.remove(+id); + } + } + ]); + return UsersController; +}(); +_ts_decorate([ + Post(), + _ts_param(0, Body()) +], UsersController.prototype, "create", null); +_ts_decorate([ + Get() +], UsersController.prototype, "findAll", null); +_ts_decorate([ + Get(":id"), + _ts_param(0, Param("id")) +], UsersController.prototype, "findOne", null); +_ts_decorate([ + Patch(":id"), + _ts_param(0, Param("id")), + _ts_param(1, Body()) +], UsersController.prototype, "update", null); +_ts_decorate([ + Delete(":id"), + _ts_param(0, Param("id")) +], UsersController.prototype, "remove", null); +UsersController = _ts_decorate([ + Controller("users") +], UsersController); diff --git a/crates/swc/tests/fixture/jsc-paths/7603/1/output/src/users/users.module.ts b/crates/swc/tests/fixture/jsc-paths/7603/1/output/src/users/users.module.ts new file mode 100644 index 000000000000..0f322fcc7310 --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/1/output/src/users/users.module.ts @@ -0,0 +1,19 @@ +import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check"; +import { _ as _ts_decorate } from "@swc/helpers/_/_ts_decorate"; +import { Module } from "@nestjs/common"; +import { UsersService } from "./users.service"; +import { UsersController } from "./users.controller"; +export var UsersModule = function UsersModule() { + "use strict"; + _class_call_check(this, UsersModule); +}; +UsersModule = _ts_decorate([ + Module({ + controllers: [ + UsersController + ], + providers: [ + UsersService + ] + }) +], UsersModule); diff --git a/crates/swc/tests/fixture/jsc-paths/7603/1/output/src/users/users.service.spec.ts b/crates/swc/tests/fixture/jsc-paths/7603/1/output/src/users/users.service.spec.ts new file mode 100644 index 000000000000..bec7a4dfdf18 --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/1/output/src/users/users.service.spec.ts @@ -0,0 +1,32 @@ +import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator"; +import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator"; +import { Test } from "@nestjs/testing"; +import { UsersService } from "./users.service"; +describe("UsersService", function() { + var service; + beforeEach(/*#__PURE__*/ _async_to_generator(function() { + var module; + return _ts_generator(this, function(_state) { + switch(_state.label){ + case 0: + return [ + 4, + Test.createTestingModule({ + providers: [ + UsersService + ] + }).compile() + ]; + case 1: + module = _state.sent(); + service = module.get(UsersService); + return [ + 2 + ]; + } + }); + })); + it("should be defined", function() { + expect(service).toBeDefined(); + }); +}); diff --git a/crates/swc/tests/fixture/jsc-paths/7603/1/output/src/users/users.service.ts b/crates/swc/tests/fixture/jsc-paths/7603/1/output/src/users/users.service.ts new file mode 100644 index 000000000000..3611a4103291 --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/1/output/src/users/users.service.ts @@ -0,0 +1,46 @@ +import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check"; +import { _ as _create_class } from "@swc/helpers/_/_create_class"; +import { _ as _ts_decorate } from "@swc/helpers/_/_ts_decorate"; +import { Injectable } from "@nestjs/common"; +export var UsersService = /*#__PURE__*/ function() { + "use strict"; + function UsersService() { + _class_call_check(this, UsersService); + } + _create_class(UsersService, [ + { + key: "create", + value: function create(createUserDto) { + return "This action adds a new user"; + } + }, + { + key: "findAll", + value: function findAll() { + return "This action returns all users"; + } + }, + { + key: "findOne", + value: function findOne(id) { + return "This action returns a #".concat(id, " user"); + } + }, + { + key: "update", + value: function update(id, updateUserDto) { + return "This action updates a #".concat(id, " user"); + } + }, + { + key: "remove", + value: function remove(id) { + return "This action removes a #".concat(id, " user"); + } + } + ]); + return UsersService; +}(); +UsersService = _ts_decorate([ + Injectable() +], UsersService); diff --git a/crates/swc/tests/fixture/jsc-paths/7603/2/input/.swcrc b/crates/swc/tests/fixture/jsc-paths/7603/2/input/.swcrc new file mode 100644 index 000000000000..deaef5d790bf --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/2/input/.swcrc @@ -0,0 +1,18 @@ +{ + "$schema": "https://json.schemastore.org/swcrc", + "jsc": { + "parser": { + "syntax": "typescript", + "decorators": true, + "dynamicImport": true + }, + "baseUrl": "./src", + "paths": { + "*": [ + "*", + "src/*" + ] + } + }, + "minify": false +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/jsc-paths/7603/2/input/src/app.module.ts b/crates/swc/tests/fixture/jsc-paths/7603/2/input/src/app.module.ts new file mode 100644 index 000000000000..91ac8b84e178 --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/2/input/src/app.module.ts @@ -0,0 +1,9 @@ +import { Module } from '@nestjs/common'; +import { UsersModule } from 'users/users.module'; + +@Module({ + imports: [UsersModule], + controllers: [], + providers: [], +}) +export class AppModule {} diff --git a/crates/swc/tests/fixture/jsc-paths/7603/2/input/src/main.ts b/crates/swc/tests/fixture/jsc-paths/7603/2/input/src/main.ts new file mode 100644 index 000000000000..6bf7fb0590b9 --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/2/input/src/main.ts @@ -0,0 +1,8 @@ +import { NestFactory } from '@nestjs/core'; +import { AppModule } from 'app.module'; + +async function bootstrap() { + const app = await NestFactory.create(AppModule); + await app.listen(5002); +} +bootstrap(); diff --git a/crates/swc/tests/fixture/jsc-paths/7603/2/input/src/users/dto/create-user.dto.ts b/crates/swc/tests/fixture/jsc-paths/7603/2/input/src/users/dto/create-user.dto.ts new file mode 100644 index 000000000000..0311be1384d0 --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/2/input/src/users/dto/create-user.dto.ts @@ -0,0 +1 @@ +export class CreateUserDto {} diff --git a/crates/swc/tests/fixture/jsc-paths/7603/2/input/src/users/dto/update-user.dto.ts b/crates/swc/tests/fixture/jsc-paths/7603/2/input/src/users/dto/update-user.dto.ts new file mode 100644 index 000000000000..d71f3117a90a --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/2/input/src/users/dto/update-user.dto.ts @@ -0,0 +1,4 @@ +import { PartialType } from '@nestjs/mapped-types'; +import { CreateUserDto } from 'users/dto/create-user.dto'; + +export class UpdateUserDto extends PartialType(CreateUserDto) {} diff --git a/crates/swc/tests/fixture/jsc-paths/7603/2/input/src/users/entities/user.entity.ts b/crates/swc/tests/fixture/jsc-paths/7603/2/input/src/users/entities/user.entity.ts new file mode 100644 index 000000000000..4f82c14571c4 --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/2/input/src/users/entities/user.entity.ts @@ -0,0 +1 @@ +export class User {} diff --git a/crates/swc/tests/fixture/jsc-paths/7603/2/input/src/users/users.controller.spec.ts b/crates/swc/tests/fixture/jsc-paths/7603/2/input/src/users/users.controller.spec.ts new file mode 100644 index 000000000000..5ba7e6c3256f --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/2/input/src/users/users.controller.spec.ts @@ -0,0 +1,20 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { UsersController } from 'users/users.controller'; +import { UsersService } from 'users/users.service'; + +describe('UsersController', () => { + let controller: UsersController; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + controllers: [UsersController], + providers: [UsersService], + }).compile(); + + controller = module.get(UsersController); + }); + + it('should be defined', () => { + expect(controller).toBeDefined(); + }); +}); diff --git a/crates/swc/tests/fixture/jsc-paths/7603/2/input/src/users/users.controller.ts b/crates/swc/tests/fixture/jsc-paths/7603/2/input/src/users/users.controller.ts new file mode 100644 index 000000000000..bf9e648ca148 --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/2/input/src/users/users.controller.ts @@ -0,0 +1,34 @@ +import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common'; +import { UsersService } from 'users/users.service'; +import { CreateUserDto } from 'users/dto/create-user.dto'; +import { UpdateUserDto } from 'users/dto/update-user.dto'; + +@Controller('users') +export class UsersController { + constructor(private readonly usersService: UsersService) {} + + @Post() + create(@Body() createUserDto: CreateUserDto) { + return this.usersService.create(createUserDto); + } + + @Get() + findAll() { + return this.usersService.findAll(); + } + + @Get(':id') + findOne(@Param('id') id: string) { + return this.usersService.findOne(+id); + } + + @Patch(':id') + update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) { + return this.usersService.update(+id, updateUserDto); + } + + @Delete(':id') + remove(@Param('id') id: string) { + return this.usersService.remove(+id); + } +} diff --git a/crates/swc/tests/fixture/jsc-paths/7603/2/input/src/users/users.module.ts b/crates/swc/tests/fixture/jsc-paths/7603/2/input/src/users/users.module.ts new file mode 100644 index 000000000000..b0cd1fc260cc --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/2/input/src/users/users.module.ts @@ -0,0 +1,9 @@ +import { Module } from '@nestjs/common'; +import { UsersService } from 'users/users.service'; +import { UsersController } from 'users/users.controller'; + +@Module({ + controllers: [UsersController], + providers: [UsersService], +}) +export class UsersModule { } diff --git a/crates/swc/tests/fixture/jsc-paths/7603/2/input/src/users/users.service.spec.ts b/crates/swc/tests/fixture/jsc-paths/7603/2/input/src/users/users.service.spec.ts new file mode 100644 index 000000000000..b54562b6dcf2 --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/2/input/src/users/users.service.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { UsersService } from 'users/users.service'; + +describe('UsersService', () => { + let service: UsersService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [UsersService], + }).compile(); + + service = module.get(UsersService); + }); + + it('should be defined', () => { + expect(service).toBeDefined(); + }); +}); diff --git a/crates/swc/tests/fixture/jsc-paths/7603/2/input/src/users/users.service.ts b/crates/swc/tests/fixture/jsc-paths/7603/2/input/src/users/users.service.ts new file mode 100644 index 000000000000..83b4a981727f --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/2/input/src/users/users.service.ts @@ -0,0 +1,26 @@ +import { Injectable } from '@nestjs/common'; +import { CreateUserDto } from 'users/dto/create-user.dto'; +import { UpdateUserDto } from 'users/dto/update-user.dto'; + +@Injectable() +export class UsersService { + create(createUserDto: CreateUserDto) { + return 'This action adds a new user'; + } + + findAll() { + return `This action returns all users`; + } + + findOne(id: number) { + return `This action returns a #${id} user`; + } + + update(id: number, updateUserDto: UpdateUserDto) { + return `This action updates a #${id} user`; + } + + remove(id: number) { + return `This action removes a #${id} user`; + } +} diff --git a/crates/swc/tests/fixture/jsc-paths/7603/2/output/src/app.module.ts b/crates/swc/tests/fixture/jsc-paths/7603/2/output/src/app.module.ts new file mode 100644 index 000000000000..1a0f89078ed0 --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/2/output/src/app.module.ts @@ -0,0 +1,17 @@ +import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check"; +import { _ as _ts_decorate } from "@swc/helpers/_/_ts_decorate"; +import { Module } from "@nestjs/common"; +import { UsersModule } from "./users/users.module"; +export var AppModule = function AppModule() { + "use strict"; + _class_call_check(this, AppModule); +}; +AppModule = _ts_decorate([ + Module({ + imports: [ + UsersModule + ], + controllers: [], + providers: [] + }) +], AppModule); diff --git a/crates/swc/tests/fixture/jsc-paths/7603/2/output/src/main.ts b/crates/swc/tests/fixture/jsc-paths/7603/2/output/src/main.ts new file mode 100644 index 000000000000..f979afaddb54 --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/2/output/src/main.ts @@ -0,0 +1,34 @@ +import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator"; +import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator"; +import { NestFactory } from "@nestjs/core"; +import { AppModule } from "./app.module"; +function bootstrap() { + return _bootstrap.apply(this, arguments); +} +function _bootstrap() { + _bootstrap = _async_to_generator(function() { + var app; + return _ts_generator(this, function(_state) { + switch(_state.label){ + case 0: + return [ + 4, + NestFactory.create(AppModule) + ]; + case 1: + app = _state.sent(); + return [ + 4, + app.listen(5002) + ]; + case 2: + _state.sent(); + return [ + 2 + ]; + } + }); + }); + return _bootstrap.apply(this, arguments); +} +bootstrap(); diff --git a/crates/swc/tests/fixture/jsc-paths/7603/2/output/src/users/dto/create-user.dto.ts b/crates/swc/tests/fixture/jsc-paths/7603/2/output/src/users/dto/create-user.dto.ts new file mode 100644 index 000000000000..6260cebb7bba --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/2/output/src/users/dto/create-user.dto.ts @@ -0,0 +1,5 @@ +import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check"; +export var CreateUserDto = function CreateUserDto() { + "use strict"; + _class_call_check(this, CreateUserDto); +}; diff --git a/crates/swc/tests/fixture/jsc-paths/7603/2/output/src/users/dto/update-user.dto.ts b/crates/swc/tests/fixture/jsc-paths/7603/2/output/src/users/dto/update-user.dto.ts new file mode 100644 index 000000000000..56d561199ff2 --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/2/output/src/users/dto/update-user.dto.ts @@ -0,0 +1,15 @@ +import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check"; +import { _ as _inherits } from "@swc/helpers/_/_inherits"; +import { _ as _create_super } from "@swc/helpers/_/_create_super"; +import { PartialType } from "@nestjs/mapped-types"; +import { CreateUserDto } from "./create-user.dto"; +export var UpdateUserDto = /*#__PURE__*/ function(_PartialType) { + "use strict"; + _inherits(UpdateUserDto, _PartialType); + var _super = _create_super(UpdateUserDto); + function UpdateUserDto() { + _class_call_check(this, UpdateUserDto); + return _super.apply(this, arguments); + } + return UpdateUserDto; +}(PartialType(CreateUserDto)); diff --git a/crates/swc/tests/fixture/jsc-paths/7603/2/output/src/users/entities/user.entity.ts b/crates/swc/tests/fixture/jsc-paths/7603/2/output/src/users/entities/user.entity.ts new file mode 100644 index 000000000000..2bff55d67e63 --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/2/output/src/users/entities/user.entity.ts @@ -0,0 +1,5 @@ +import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check"; +export var User = function User() { + "use strict"; + _class_call_check(this, User); +}; diff --git a/crates/swc/tests/fixture/jsc-paths/7603/2/output/src/users/users.controller.spec.ts b/crates/swc/tests/fixture/jsc-paths/7603/2/output/src/users/users.controller.spec.ts new file mode 100644 index 000000000000..03e3d1b39a57 --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/2/output/src/users/users.controller.spec.ts @@ -0,0 +1,36 @@ +import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator"; +import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator"; +import { Test } from "@nestjs/testing"; +import { UsersController } from "./users.controller"; +import { UsersService } from "./users.service"; +describe("UsersController", function() { + var controller; + beforeEach(/*#__PURE__*/ _async_to_generator(function() { + var module; + return _ts_generator(this, function(_state) { + switch(_state.label){ + case 0: + return [ + 4, + Test.createTestingModule({ + controllers: [ + UsersController + ], + providers: [ + UsersService + ] + }).compile() + ]; + case 1: + module = _state.sent(); + controller = module.get(UsersController); + return [ + 2 + ]; + } + }); + })); + it("should be defined", function() { + expect(controller).toBeDefined(); + }); +}); diff --git a/crates/swc/tests/fixture/jsc-paths/7603/2/output/src/users/users.controller.ts b/crates/swc/tests/fixture/jsc-paths/7603/2/output/src/users/users.controller.ts new file mode 100644 index 000000000000..2089e981a46e --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/2/output/src/users/users.controller.ts @@ -0,0 +1,70 @@ +import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check"; +import { _ as _create_class } from "@swc/helpers/_/_create_class"; +import { _ as _define_property } from "@swc/helpers/_/_define_property"; +import { _ as _ts_decorate } from "@swc/helpers/_/_ts_decorate"; +import { _ as _ts_param } from "@swc/helpers/_/_ts_param"; +import { Controller, Get, Post, Body, Patch, Param, Delete } from "@nestjs/common"; +export var UsersController = /*#__PURE__*/ function() { + "use strict"; + function UsersController(usersService) { + _class_call_check(this, UsersController); + _define_property(this, "usersService", void 0); + this.usersService = usersService; + } + _create_class(UsersController, [ + { + key: "create", + value: function create(createUserDto) { + return this.usersService.create(createUserDto); + } + }, + { + key: "findAll", + value: function findAll() { + return this.usersService.findAll(); + } + }, + { + key: "findOne", + value: function findOne(id) { + return this.usersService.findOne(+id); + } + }, + { + key: "update", + value: function update(id, updateUserDto) { + return this.usersService.update(+id, updateUserDto); + } + }, + { + key: "remove", + value: function remove(id) { + return this.usersService.remove(+id); + } + } + ]); + return UsersController; +}(); +_ts_decorate([ + Post(), + _ts_param(0, Body()) +], UsersController.prototype, "create", null); +_ts_decorate([ + Get() +], UsersController.prototype, "findAll", null); +_ts_decorate([ + Get(":id"), + _ts_param(0, Param("id")) +], UsersController.prototype, "findOne", null); +_ts_decorate([ + Patch(":id"), + _ts_param(0, Param("id")), + _ts_param(1, Body()) +], UsersController.prototype, "update", null); +_ts_decorate([ + Delete(":id"), + _ts_param(0, Param("id")) +], UsersController.prototype, "remove", null); +UsersController = _ts_decorate([ + Controller("users") +], UsersController); diff --git a/crates/swc/tests/fixture/jsc-paths/7603/2/output/src/users/users.module.ts b/crates/swc/tests/fixture/jsc-paths/7603/2/output/src/users/users.module.ts new file mode 100644 index 000000000000..0f322fcc7310 --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/2/output/src/users/users.module.ts @@ -0,0 +1,19 @@ +import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check"; +import { _ as _ts_decorate } from "@swc/helpers/_/_ts_decorate"; +import { Module } from "@nestjs/common"; +import { UsersService } from "./users.service"; +import { UsersController } from "./users.controller"; +export var UsersModule = function UsersModule() { + "use strict"; + _class_call_check(this, UsersModule); +}; +UsersModule = _ts_decorate([ + Module({ + controllers: [ + UsersController + ], + providers: [ + UsersService + ] + }) +], UsersModule); diff --git a/crates/swc/tests/fixture/jsc-paths/7603/2/output/src/users/users.service.spec.ts b/crates/swc/tests/fixture/jsc-paths/7603/2/output/src/users/users.service.spec.ts new file mode 100644 index 000000000000..bec7a4dfdf18 --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/2/output/src/users/users.service.spec.ts @@ -0,0 +1,32 @@ +import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator"; +import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator"; +import { Test } from "@nestjs/testing"; +import { UsersService } from "./users.service"; +describe("UsersService", function() { + var service; + beforeEach(/*#__PURE__*/ _async_to_generator(function() { + var module; + return _ts_generator(this, function(_state) { + switch(_state.label){ + case 0: + return [ + 4, + Test.createTestingModule({ + providers: [ + UsersService + ] + }).compile() + ]; + case 1: + module = _state.sent(); + service = module.get(UsersService); + return [ + 2 + ]; + } + }); + })); + it("should be defined", function() { + expect(service).toBeDefined(); + }); +}); diff --git a/crates/swc/tests/fixture/jsc-paths/7603/2/output/src/users/users.service.ts b/crates/swc/tests/fixture/jsc-paths/7603/2/output/src/users/users.service.ts new file mode 100644 index 000000000000..3611a4103291 --- /dev/null +++ b/crates/swc/tests/fixture/jsc-paths/7603/2/output/src/users/users.service.ts @@ -0,0 +1,46 @@ +import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check"; +import { _ as _create_class } from "@swc/helpers/_/_create_class"; +import { _ as _ts_decorate } from "@swc/helpers/_/_ts_decorate"; +import { Injectable } from "@nestjs/common"; +export var UsersService = /*#__PURE__*/ function() { + "use strict"; + function UsersService() { + _class_call_check(this, UsersService); + } + _create_class(UsersService, [ + { + key: "create", + value: function create(createUserDto) { + return "This action adds a new user"; + } + }, + { + key: "findAll", + value: function findAll() { + return "This action returns all users"; + } + }, + { + key: "findOne", + value: function findOne(id) { + return "This action returns a #".concat(id, " user"); + } + }, + { + key: "update", + value: function update(id, updateUserDto) { + return "This action updates a #".concat(id, " user"); + } + }, + { + key: "remove", + value: function remove(id) { + return "This action removes a #".concat(id, " user"); + } + } + ]); + return UsersService; +}(); +UsersService = _ts_decorate([ + Injectable() +], UsersService); diff --git a/crates/swc_ecma_loader/src/resolvers/node.rs b/crates/swc_ecma_loader/src/resolvers/node.rs index 94a3468f519e..ed9554a30a82 100644 --- a/crates/swc_ecma_loader/src/resolvers/node.rs +++ b/crates/swc_ecma_loader/src/resolvers/node.rs @@ -394,7 +394,7 @@ impl NodeModulesResolver { impl Resolve for NodeModulesResolver { fn resolve(&self, base: &FileName, target: &str) -> Result { debug!( - "Resolve {} from {:#?} for {:#?}", + "Resolving {} from {:#?} for {:#?}", target, base, self.target_env ); diff --git a/crates/swc_ecma_loader/src/resolvers/tsc.rs b/crates/swc_ecma_loader/src/resolvers/tsc.rs index 995afaf87a88..bf2f59369363 100644 --- a/crates/swc_ecma_loader/src/resolvers/tsc.rs +++ b/crates/swc_ecma_loader/src/resolvers/tsc.rs @@ -1,8 +1,8 @@ -use std::path::{Component, PathBuf}; +use std::path::{Component, Path, PathBuf}; use anyhow::{bail, Context, Error}; use swc_common::FileName; -use tracing::{debug, info, trace, Level}; +use tracing::{debug, info, trace, warn, Level}; use crate::resolve::Resolve; @@ -99,6 +99,35 @@ where paths, } } + + fn invoke_inner_resolver( + &self, + base: &FileName, + module_specifier: &str, + ) -> Result { + let res = self.inner.resolve(base, module_specifier).with_context(|| { + format!( + "failed to resolve `{module_specifier}` from `{base}` using inner \ + resolver\nbase_url={}", + self.base_url_filename + ) + }); + + match res { + Ok(resolved) => { + info!( + "Resolved `{}` as `{}` from `{}`", + module_specifier, resolved, base + ); + Ok(resolved) + } + + Err(err) => { + warn!("{:?}", err); + Err(err) + } + } + } } impl Resolve for TsConfigResolver @@ -110,7 +139,7 @@ where Some( tracing::span!( Level::ERROR, - "tsc.resolve", + "TsConfigResolver::resolve", base_url = tracing::field::display(self.base_url.display()), base = tracing::field::display(base), src = tracing::field::display(module_specifier), @@ -127,30 +156,29 @@ where || module_specifier.starts_with("../")) { return self - .inner - .resolve(base, module_specifier) + .invoke_inner_resolver(base, module_specifier) .context("not processed by tsc resolver because it's relative import"); } - if cfg!(debug_assertions) { - debug!("non-relative import"); - } - if let FileName::Real(v) = base { if v.components().any(|c| match c { Component::Normal(v) => v == "node_modules", _ => false, }) { - return self.inner.resolve(base, module_specifier).context( + return self.invoke_inner_resolver(base, module_specifier).context( "not processed by tsc resolver because base module is in node_modules", ); } } + info!("Checking `jsc.paths`"); + // https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping for (from, to) in &self.paths { match from { Pattern::Wildcard { prefix } => { + debug!("Checking `{}` in `jsc.paths`", prefix); + let extra = module_specifier.strip_prefix(prefix); let extra = match extra { Some(v) => v, @@ -163,35 +191,53 @@ where }; if cfg!(debug_assertions) { - trace!("extra = {}", extra); + debug!("Extra: `{}`", extra); } let mut errors = vec![]; for target in to { let mut replaced = target.replace('*', extra); - let rel = format!("./{}", replaced); - let res = self.inner.resolve(base, &rel).with_context(|| { - format!( - "failed to resolve `{}`, which is expanded from `{}`", - replaced, module_specifier + let _tracing = if cfg!(debug_assertions) { + Some( + tracing::span!( + Level::ERROR, + "TsConfigResolver::resolve::jsc.paths", + replaced = tracing::field::display(&replaced), + ) + .entered(), ) - }); + } else { + None + }; + + let relative = format!("./{}", replaced); + + let res = self + .invoke_inner_resolver(base, module_specifier) + .or_else(|_| { + self.invoke_inner_resolver(&self.base_url_filename, &relative) + }) + .or_else(|_| { + self.invoke_inner_resolver(&self.base_url_filename, &replaced) + }); errors.push(match res { - Ok(v) => return Ok(v), + Ok(resolved) => return Ok(resolved), Err(err) => err, }); if cfg!(target_os = "windows") { - if replaced.starts_with("./") { - replaced = replaced[2..].to_string(); - } replaced = replaced.replace('/', "\\"); } if to.len() == 1 { - return Ok(FileName::Real(self.base_url.join(replaced))); + info!( + "Using `{}` for `{}` because the length of the jsc.paths entry is \ + 1", + replaced, module_specifier + ); + return Ok(FileName::Real(replaced.into())); } } @@ -204,33 +250,44 @@ where } Pattern::Exact(from) => { // Should be exactly matched - if module_specifier == from { + if module_specifier != from { + continue; + } + + let tp = Path::new(&to[0]); + if tp.is_absolute() { + return Ok(FileName::Real(tp.into())); + } + + if self.base_url_filename == *base { + // Prevent infinite loop + let replaced = self.base_url.join(&to[0]); if replaced.exists() { return Ok(FileName::Real(replaced)); } return self - .inner - .resolve(base, &format!("./{}", &to[0])) + .invoke_inner_resolver(base, &format!("./{}", &to[0])) .with_context(|| { format!( "tried to resolve `{}` because `{}` was exactly matched", to[0], from ) }); + } else { + return self + .resolve(&self.base_url_filename, &format!("./{}", &to[0])) + .context("failed to resolve using jsc.baseUrl as base"); } } } } - if let Ok(v) = self - .inner - .resolve(&self.base_url_filename, module_specifier) - { + if let Ok(v) = self.invoke_inner_resolver(&self.base_url_filename, module_specifier) { return Ok(v); } - self.inner.resolve(base, module_specifier) + self.invoke_inner_resolver(base, module_specifier) } } diff --git a/crates/swc_ecma_loader/tests/tsc_resolver.rs b/crates/swc_ecma_loader/tests/tsc_resolver.rs index 220563e77e11..5ff4cd0c68e0 100644 --- a/crates/swc_ecma_loader/tests/tsc_resolver.rs +++ b/crates/swc_ecma_loader/tests/tsc_resolver.rs @@ -38,14 +38,8 @@ fn exact() { } { - let err = r - .resolve(&FileName::Anon, "unrelated") + r.resolve(&FileName::Anon, "unrelated") .expect_err("should not touch error"); - - assert!( - err.source().is_none(), - "should not touch error if src is not related" - ); } } diff --git a/crates/swc_ecma_transforms_module/src/path.rs b/crates/swc_ecma_transforms_module/src/path.rs index 7f10d7e52740..57507cee7840 100644 --- a/crates/swc_ecma_transforms_module/src/path.rs +++ b/crates/swc_ecma_transforms_module/src/path.rs @@ -14,7 +14,7 @@ use swc_common::{FileName, Mark, Span, DUMMY_SP}; use swc_ecma_ast::*; use swc_ecma_loader::resolve::Resolve; use swc_ecma_utils::{quote_ident, ExprFactory}; -use tracing::{debug, trace, warn, Level}; +use tracing::{debug, info, warn, Level}; pub(crate) enum Resolver { Real { @@ -124,34 +124,6 @@ where R: Resolve, { fn resolve_import(&self, base: &FileName, module_specifier: &str) -> Result { - fn to_specifier(target_path: &str, orig_ext: Option<&str>) -> JsWord { - let mut p = PathBuf::from(target_path); - - if cfg!(debug_assertions) { - trace!("to_specifier({target_path}): orig_ext={:?}", orig_ext); - } - - if let Some(orig_ext) = orig_ext { - let use_orig = if let Some(ext) = p.extension() { - ext == "ts" || ext == "tsx" - } else { - false - }; - - if use_orig { - if matches!(orig_ext, "js" | "mjs" | "cjs" | "jsx") { - p.set_extension(orig_ext); - } else { - p.set_extension(""); - } - } - } else { - p.set_extension(""); - } - - p.display().to_string().into() - } - let _tracing = if cfg!(debug_assertions) { Some( tracing::span!( @@ -166,17 +138,7 @@ where None }; - if cfg!(debug_assertions) { - debug!("invoking resolver"); - } - - let orig_ext = module_specifier.split('/').last().and_then(|s| { - if s.contains('.') { - s.split('.').last() - } else { - None - } - }); + let orig_filename = module_specifier.split('/').last(); let target = self.resolver.resolve(base, module_specifier); let target = match target { @@ -187,9 +149,18 @@ where } }; + info!("Resolved to {}", target); + let mut target = match target { - FileName::Real(v) => v, - FileName::Custom(s) => return Ok(to_specifier(&s, orig_ext)), + FileName::Real(v) => { + // @nestjs/common should be preserved as a whole + if v.starts_with(".") || v.starts_with("..") || v.is_absolute() { + v + } else { + return Ok(to_specifier(v, orig_filename)); + } + } + FileName::Custom(s) => return Ok(to_specifier(s.into(), orig_filename)), _ => { unreachable!( "Node path provider does not support using `{:?}` as a target file name", @@ -219,6 +190,12 @@ where target = absolute_path(self.base_dir.as_deref(), &target)?; } + debug!( + "Comparing values (after normalizing absoluteness)\nbase={}\ntarget={}", + base.display(), + target.display() + ); + let rel_path = diff_paths( &target, match base.parent() { @@ -229,9 +206,11 @@ where let rel_path = match rel_path { Some(v) => v, - None => return Ok(to_specifier(&target.display().to_string(), orig_ext)), + None => return Ok(to_specifier(target, orig_filename)), }; + debug!("Relative path: {}", rel_path.display()); + { // Check for `node_modules`. @@ -256,11 +235,8 @@ where } else { Cow::Owned(format!("./{}", s)) }; - if cfg!(target_os = "windows") { - Ok(to_specifier(&s.replace('\\', "/"), orig_ext)) - } else { - Ok(to_specifier(&s, orig_ext)) - } + + Ok(to_specifier(s.into_owned().into(), orig_filename)) } } @@ -294,3 +270,55 @@ fn absolute_path(base_dir: Option<&Path>, path: &Path) -> io::Result { Ok(absolute_path) } + +fn to_specifier(mut target_path: PathBuf, orig_filename: Option<&str>) -> JsWord { + debug!( + "Creating a specifier for `{}` with original filename `{:?}`", + target_path.display(), + orig_filename + ); + + if let Some(orig_filename) = orig_filename { + let is_resolved_as_index = if let Some(stem) = target_path.file_stem() { + stem == "index" + } else { + false + }; + + let is_resolved_as_ts = if let Some(ext) = target_path.extension() { + ext == "ts" || ext == "tsx" + } else { + false + }; + + let is_exact = if let Some(filename) = target_path.file_name() { + filename == orig_filename + } else { + false + }; + + if !is_resolved_as_index && !is_exact { + target_path.set_file_name(orig_filename); + } else if is_resolved_as_ts && is_exact { + if let Some(ext) = Path::new(orig_filename).extension() { + target_path.set_extension(ext); + } else { + target_path.set_extension("js"); + } + } else if is_resolved_as_ts && is_resolved_as_index { + if orig_filename == "index" { + target_path.set_extension(""); + } else { + target_path.pop(); + } + } + } else { + target_path.set_extension(""); + } + + if cfg!(target_os = "windows") { + target_path.display().to_string().replace('\\', "/").into() + } else { + target_path.display().to_string().into() + } +} diff --git a/crates/swc_ecma_transforms_module/tests/fixture-manual/issue-4730/output/index.js b/crates/swc_ecma_transforms_module/tests/fixture-manual/issue-4730/output/index.js index ec142a77c1d7..adeea09e9a98 100644 --- a/crates/swc_ecma_transforms_module/tests/fixture-manual/issue-4730/output/index.js +++ b/crates/swc_ecma_transforms_module/tests/fixture-manual/issue-4730/output/index.js @@ -1,6 +1,6 @@ -import { displayB } from "../packages/b/src/index"; +import { displayB } from "../packages/b/src"; async function display() { - const displayA = await import("../packages/a/src/index").then((c)=>c.displayA); + const displayA = await import("../packages/a/src").then((c)=>c.displayA); console.log(displayA()); console.log(displayB()); } diff --git a/crates/swc_ecma_transforms_module/tests/paths/issue-4607/input/src/utils/shared/foo/boo.ts b/crates/swc_ecma_transforms_module/tests/paths/issue-4607/input/src/utils/shared/foo/boo.ts new file mode 100644 index 000000000000..3deda8047e15 --- /dev/null +++ b/crates/swc_ecma_transforms_module/tests/paths/issue-4607/input/src/utils/shared/foo/boo.ts @@ -0,0 +1 @@ +export { } \ No newline at end of file diff --git a/crates/swc_ecma_transforms_module/tests/paths/issue-6159/input/src/a.js b/crates/swc_ecma_transforms_module/tests/paths/issue-6159/input/src/a.js new file mode 100644 index 000000000000..3deda8047e15 --- /dev/null +++ b/crates/swc_ecma_transforms_module/tests/paths/issue-6159/input/src/a.js @@ -0,0 +1 @@ +export { } \ No newline at end of file diff --git a/crates/swc_ecma_transforms_module/tests/paths/issue-7417/output/index.ts b/crates/swc_ecma_transforms_module/tests/paths/issue-7417/output/index.ts index 656d040c8500..d2b5aa4bb414 100644 --- a/crates/swc_ecma_transforms_module/tests/paths/issue-7417/output/index.ts +++ b/crates/swc_ecma_transforms_module/tests/paths/issue-7417/output/index.ts @@ -1,4 +1,4 @@ -import o from "./index"; +import o from "."; export default function bar() { console.log(o); } diff --git a/node-swc/__tests__/transform/issue_4730_test.mjs b/node-swc/__tests__/transform/issue_4730_test.mjs index f2886191d561..2839fe3dc768 100644 --- a/node-swc/__tests__/transform/issue_4730_test.mjs +++ b/node-swc/__tests__/transform/issue_4730_test.mjs @@ -21,7 +21,7 @@ it("should work", async () => { dynamicImport: true, }, target: "es2020", - baseUrl: resolve('.'), + baseUrl: resolve("."), paths: { "@print/a": [join(dir, "./packages/a/src/index.ts")], "@print/b": [join(dir, "./packages/b/src/index.ts")], @@ -37,10 +37,10 @@ it("should work", async () => { Object.defineProperty(exports, \\"__esModule\\", { value: true }); - const _interop_require_wildcard = require(\\"@swc/helpers/_/_interop_require_wildcard\\"); - const _b = require(\\"../packages/b/src/index\\"); + const _interop_require_wildcard = require(\\"node_modules/@swc/helpers/esm/_interop_require_wildcard\\"); + const _b = require(\\"../packages/b/src\\"); async function display() { - const displayA = await Promise.resolve().then(()=>/*#__PURE__*/ _interop_require_wildcard._(require(\\"../packages/a/src/index\\"))).then((c)=>c.displayA); + const displayA = await Promise.resolve().then(()=>/*#__PURE__*/ _interop_require_wildcard._(require(\\"../packages/a/src\\"))).then((c)=>c.displayA); console.log(displayA()); console.log((0, _b.displayB)()); }