Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1449 from liaoyinglong/fix/imports
fix: set flags from old node
- Loading branch information
Showing
11 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
test/lib/compiler/hooks/__snapshots__/tsconfig-paths.hook.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`tsconfig paths hooks should remove unused imports 1`] = ` | ||
Map { | ||
"dist/foo.js" => "\\"use strict\\"; | ||
Object.defineProperty(exports, \\"__esModule\\", { value: true }); | ||
exports.Foo = void 0; | ||
class Foo { | ||
} | ||
exports.Foo = Foo; | ||
", | ||
"dist/bar.js" => "\\"use strict\\"; | ||
Object.defineProperty(exports, \\"__esModule\\", { value: true }); | ||
exports.Bar = void 0; | ||
class Bar { | ||
} | ||
exports.Bar = Bar; | ||
", | ||
"dist/main.js" => "\\"use strict\\"; | ||
Object.defineProperty(exports, \\"__esModule\\", { value: true }); | ||
", | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { TypeA } from 'src/type-a'; | ||
import type { TypeB } from 'src/type-b'; | ||
import { TypeC } from './type-c'; | ||
import type { TypeD } from './type-d'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type TypeA = number; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type TypeB = number; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type TypeC = number; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type TypeD = number; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export class Bar {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export class Foo {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import { Foo } from './foo'; | ||
import { Bar } from 'src/bar'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import * as path from 'path'; | ||
import * as ts from 'typescript'; | ||
import { tsconfigPathsBeforeHookFactory } from '../../../../lib/compiler/hooks/tsconfig-paths.hook'; | ||
|
||
function createSpec(baseUrl: string, fileNames: string[]) { | ||
const options: ts.CompilerOptions = { | ||
baseUrl, | ||
outDir: path.join(baseUrl, 'dist'), | ||
target: ts.ScriptTarget.ESNext, | ||
module: ts.ModuleKind.CommonJS, | ||
}; | ||
|
||
const program = ts.createProgram({ | ||
rootNames: fileNames.map((name) => path.join(baseUrl, name)), | ||
options, | ||
}); | ||
const output = new Map<string, string>(); | ||
program.emit( | ||
undefined, | ||
(fileName, data) => { | ||
output.set(path.relative(baseUrl, fileName), data); | ||
}, | ||
undefined, | ||
undefined, | ||
{ | ||
before: [tsconfigPathsBeforeHookFactory(options)], | ||
}, | ||
); | ||
return output; | ||
} | ||
|
||
describe('tsconfig paths hooks', () => { | ||
it('should remove type imports', async () => { | ||
const output = createSpec(path.join(__dirname, './fixtures/type-imports'), [ | ||
'src/main.ts', | ||
'src/type-a.ts', | ||
'src/type-b.ts', | ||
'src/type-c.ts', | ||
'src/type-d.ts', | ||
]); | ||
output.forEach((value) => { | ||
expect(value).toEqual( | ||
`"use strict";\nObject.defineProperty(exports, "__esModule", { value: true });\n`, | ||
); | ||
}); | ||
}); | ||
|
||
it('should remove unused imports', async () => { | ||
const output = createSpec( | ||
path.join(__dirname, './fixtures/unused-imports'), | ||
['src/main.ts', 'src/foo.ts', 'src/bar.ts'], | ||
); | ||
expect(output).toMatchSnapshot(); | ||
}); | ||
}); |