From 27b47d0122cc87c4b93bfe6bca357b9f50b408bc Mon Sep 17 00:00:00 2001 From: Finn Scott Date: Thu, 26 May 2022 23:00:18 +0100 Subject: [PATCH 1/4] fix: force the use of posix path seperators for ts module graphs When resolving the name of the main module for use in TS module graphs, ensure we force the usage of posix paths as this is what `typescript` uses and returns. This fixes an issue when running on Windows, where an assert would be thrown as a result of two identical paths using different path seperators being compared for equality. --- .../transformers/typescript-types/src/TSTypesTransformer.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/transformers/typescript-types/src/TSTypesTransformer.js b/packages/transformers/typescript-types/src/TSTypesTransformer.js index 19f3c3747b5..35c7afc438a 100644 --- a/packages/transformers/typescript-types/src/TSTypesTransformer.js +++ b/packages/transformers/typescript-types/src/TSTypesTransformer.js @@ -53,7 +53,9 @@ export default (new Transformer({ let mainModuleName = path .relative(program.getCommonSourceDirectory(), asset.filePath) - .slice(0, -path.extname(asset.filePath).length); + .slice(0, -path.extname(asset.filePath).length) + .split(path.sep) + .join(path.posix.sep); let moduleGraph = new TSModuleGraph(mainModuleName); let emitResult = program.emit(undefined, undefined, undefined, true, { From b1c6474f34cbd4eb2dd88ab0f7f504f09af53f65 Mon Sep 17 00:00:00 2001 From: Finn Scott Date: Sat, 28 May 2022 11:42:19 +0100 Subject: [PATCH 2/4] chore: updated to use the existing method --- packages/transformers/typescript-types/package.json | 1 + .../typescript-types/src/TSTypesTransformer.js | 11 ++++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/transformers/typescript-types/package.json b/packages/transformers/typescript-types/package.json index 86ef0125087..fb25bb9e6e3 100644 --- a/packages/transformers/typescript-types/package.json +++ b/packages/transformers/typescript-types/package.json @@ -24,6 +24,7 @@ "@parcel/plugin": "2.6.0", "@parcel/source-map": "^2.0.0", "@parcel/ts-utils": "2.6.0", + "@parcel/utils": "2.6.0", "nullthrows": "^1.1.1" }, "devDependencies": { diff --git a/packages/transformers/typescript-types/src/TSTypesTransformer.js b/packages/transformers/typescript-types/src/TSTypesTransformer.js index 35c7afc438a..80473191db4 100644 --- a/packages/transformers/typescript-types/src/TSTypesTransformer.js +++ b/packages/transformers/typescript-types/src/TSTypesTransformer.js @@ -8,6 +8,7 @@ import type {CompilerOptions} from 'typescript'; import ts from 'typescript'; import {CompilerHost, loadTSConfig} from '@parcel/ts-utils'; +import {normalizeSeparators} from '@parcel/utils'; import {escapeMarkdown} from '@parcel/diagnostic'; import {TSModuleGraph} from './TSModuleGraph'; import nullthrows from 'nullthrows'; @@ -51,11 +52,11 @@ export default (new Transformer({ } } - let mainModuleName = path - .relative(program.getCommonSourceDirectory(), asset.filePath) - .slice(0, -path.extname(asset.filePath).length) - .split(path.sep) - .join(path.posix.sep); + let mainModuleName = normalizeSeparators( + path + .relative(program.getCommonSourceDirectory(), asset.filePath) + .slice(0, -path.extname(asset.filePath).length), + ); let moduleGraph = new TSModuleGraph(mainModuleName); let emitResult = program.emit(undefined, undefined, undefined, true, { From 49fc3118116f278325c1943e5a69f7854cd94964 Mon Sep 17 00:00:00 2001 From: Finn Scott Date: Sat, 28 May 2022 11:43:46 +0100 Subject: [PATCH 3/4] test: added tests for verifying types are generated correctly on windows when using tsconfig paths --- .../ts-types/windows-paths/expected.d.ts | 18 +++++++++++++++ .../ts-types/windows-paths/index.ts | 1 + .../ts-types/windows-paths/package.json | 6 +++++ .../ts-types/windows-paths/tsconfig.json | 9 ++++++++ .../ts-types/windows-paths/yarn.lock | 0 .../core/integration-tests/test/ts-types.js | 22 +++++++++++++++++++ 6 files changed, 56 insertions(+) create mode 100644 packages/core/integration-tests/test/integration/ts-types/windows-paths/expected.d.ts create mode 100644 packages/core/integration-tests/test/integration/ts-types/windows-paths/index.ts create mode 100644 packages/core/integration-tests/test/integration/ts-types/windows-paths/package.json create mode 100644 packages/core/integration-tests/test/integration/ts-types/windows-paths/tsconfig.json create mode 100644 packages/core/integration-tests/test/integration/ts-types/windows-paths/yarn.lock diff --git a/packages/core/integration-tests/test/integration/ts-types/windows-paths/expected.d.ts b/packages/core/integration-tests/test/integration/ts-types/windows-paths/expected.d.ts new file mode 100644 index 00000000000..a1dcb1c62bc --- /dev/null +++ b/packages/core/integration-tests/test/integration/ts-types/windows-paths/expected.d.ts @@ -0,0 +1,18 @@ +type Params = { + hello: number; +}; +interface Hello { + yo: string; +} +export class Test { + test(hello: Hello): string; +} +export function test(params: Params): number; +export function foo(): number; +export var x: number; +export var hi: number; +export declare module mod { + function bar(): number; +} + +//# sourceMappingURL=types.d.ts.map diff --git a/packages/core/integration-tests/test/integration/ts-types/windows-paths/index.ts b/packages/core/integration-tests/test/integration/ts-types/windows-paths/index.ts new file mode 100644 index 00000000000..11aece60c49 --- /dev/null +++ b/packages/core/integration-tests/test/integration/ts-types/windows-paths/index.ts @@ -0,0 +1 @@ +export * from './lib/index'; diff --git a/packages/core/integration-tests/test/integration/ts-types/windows-paths/package.json b/packages/core/integration-tests/test/integration/ts-types/windows-paths/package.json new file mode 100644 index 00000000000..e7879334bc0 --- /dev/null +++ b/packages/core/integration-tests/test/integration/ts-types/windows-paths/package.json @@ -0,0 +1,6 @@ +{ + "name": "ts-types-main", + "private": true, + "main": "dist/main.js", + "types": "dist/types.d.ts" +} \ No newline at end of file diff --git a/packages/core/integration-tests/test/integration/ts-types/windows-paths/tsconfig.json b/packages/core/integration-tests/test/integration/ts-types/windows-paths/tsconfig.json new file mode 100644 index 00000000000..68dcab57b6b --- /dev/null +++ b/packages/core/integration-tests/test/integration/ts-types/windows-paths/tsconfig.json @@ -0,0 +1,9 @@ +{ + "compilerOptions": { + "removeComments": true, + "baseUrl": ".", + "paths": { + "@parcel/*": ["../../*"] + } + } +} diff --git a/packages/core/integration-tests/test/integration/ts-types/windows-paths/yarn.lock b/packages/core/integration-tests/test/integration/ts-types/windows-paths/yarn.lock new file mode 100644 index 00000000000..e69de29bb2d diff --git a/packages/core/integration-tests/test/ts-types.js b/packages/core/integration-tests/test/ts-types.js index 8a8e02324d0..957aafae66b 100644 --- a/packages/core/integration-tests/test/ts-types.js +++ b/packages/core/integration-tests/test/ts-types.js @@ -388,4 +388,26 @@ describe('typescript types', function () { ); assert.equal(dist, expected); }); + + it('should handle a tsconfig file with paths on windows', async function () { + await bundle( + path.join(__dirname, '/integration/ts-types/windows-paths/index.ts'), + ); + + let dist = ( + await outputFS.readFile( + path.join( + __dirname, + '/integration/ts-types/windows-paths/dist/types.d.ts', + ), + 'utf8', + ) + ).replace(/\r\n/g, '\n'); + + let expected = await inputFS.readFile( + path.join(__dirname, '/integration/ts-types/windows-paths/expected.d.ts'), + 'utf8', + ); + assert.equal(dist, expected); + }); }); From 040b2e6479d52fe72faad14c76ceead83a7c11f1 Mon Sep 17 00:00:00 2001 From: Finn Scott Date: Tue, 31 May 2022 10:25:37 +0100 Subject: [PATCH 4/4] chore: renamed `lib` directory to `src` as `lib` is gitignored --- .../ts-types/windows-paths/index.ts | 2 +- .../ts-types/windows-paths/src/index.ts | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 packages/core/integration-tests/test/integration/ts-types/windows-paths/src/index.ts diff --git a/packages/core/integration-tests/test/integration/ts-types/windows-paths/index.ts b/packages/core/integration-tests/test/integration/ts-types/windows-paths/index.ts index 11aece60c49..cba18435457 100644 --- a/packages/core/integration-tests/test/integration/ts-types/windows-paths/index.ts +++ b/packages/core/integration-tests/test/integration/ts-types/windows-paths/index.ts @@ -1 +1 @@ -export * from './lib/index'; +export * from './src/index'; diff --git a/packages/core/integration-tests/test/integration/ts-types/windows-paths/src/index.ts b/packages/core/integration-tests/test/integration/ts-types/windows-paths/src/index.ts new file mode 100644 index 00000000000..ba2131c36da --- /dev/null +++ b/packages/core/integration-tests/test/integration/ts-types/windows-paths/src/index.ts @@ -0,0 +1,31 @@ +type Params = { + hello: number; +}; + +interface Hello { + yo: string; +} + +export class Test { + test(hello: Hello) { + return hello.yo; + } +} + +export function test(params: Params) { + return params.hello; +} + +export function foo() { + return 2; +} + +var x = 2; +var p = x + 2, q = 3; +export {p as hi, x}; + +export module mod { + export function bar() { + return 2; + } +}