fix(runner): import export assignment from ts (#19559)

This commit is contained in:
Han Yeong-woo 2022-12-20 07:41:29 +09:00 committed by GitHub
parent 467d9f37fc
commit 00ffd74727
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 2 deletions

View File

@ -14,7 +14,9 @@
* limitations under the License.
*/
import type { BabelFileResult } from '@babel/core';
import type { BabelFileResult, NodePath, PluginObj } from '@babel/core';
import type { TSExportAssignment } from '@babel/types';
import type { TemplateBuilder } from '@babel/template';
import * as babel from '@babel/core';
export { codeFrameColumns } from '@babel/code-frame';
@ -39,7 +41,22 @@ export function babelTransform(filename: string, isTypeScript: boolean, isModule
[require('@babel/plugin-syntax-optional-catch-binding')],
[require('@babel/plugin-syntax-async-generators')],
[require('@babel/plugin-syntax-object-rest-spread')],
[require('@babel/plugin-proposal-export-namespace-from')]
[require('@babel/plugin-proposal-export-namespace-from')],
[
// From https://github.com/G-Rath/babel-plugin-replace-ts-export-assignment/blob/8dfdca32c8aa428574b0cae341444fc5822f2dc6/src/index.ts
(
{ template }: { template: TemplateBuilder<TSExportAssignment> }
): PluginObj => ({
name: 'replace-ts-export-assignment',
visitor: {
TSExportAssignment(path: NodePath<TSExportAssignment>) {
path.replaceWith(template('module.exports = ASSIGNMENT;')({
ASSIGNMENT: path.node.expression
}));
}
}
})
]
);
}

View File

@ -507,3 +507,20 @@ test('should resolve .js import to .ts file in non-ESM mode', async ({ runInline
expect(result.passed).toBe(1);
expect(result.exitCode).toBe(0);
});
test('should import export assignment from ts', async ({ runInlineTest }) => {
const result = await runInlineTest({
'a.test.ts': `
const { test } = pwt;
import number from './utils.js';
test('pass', () => {
expect(number).toBe(1);
});
`,
'utils.ts': `
export = 1;
`
});
expect(result.passed).toBe(1);
expect(result.exitCode).toBe(0);
});