From 89ac2246b2e716bbfe6dab62edca92157a2eae7d Mon Sep 17 00:00:00 2001 From: nukeop <12746779+nukeop@users.noreply.github.com> Date: Wed, 25 Oct 2023 00:03:44 +0200 Subject: [PATCH] Fix plugin transpiler --- packages/core/jest.config.ts | 4 +++- packages/core/src/plugins/plugins.test.ts | 7 ++++--- packages/core/src/plugins/transform.ts | 24 +++++++++++------------ packages/core/test/jest-setup.ts | 17 ++++++++++++++++ packages/core/tsconfig.json | 2 +- 5 files changed, 36 insertions(+), 18 deletions(-) create mode 100644 packages/core/test/jest-setup.ts diff --git a/packages/core/jest.config.ts b/packages/core/jest.config.ts index b115d824ba..48a1896a05 100644 --- a/packages/core/jest.config.ts +++ b/packages/core/jest.config.ts @@ -4,5 +4,7 @@ export default async () => ({ }, moduleNameMapper: { '^react$': '/../../node_modules/react' - } + }, + testEnvironment: 'node', + setupFilesAfterEnv: ['./test/jest-setup.ts'] }); diff --git a/packages/core/src/plugins/plugins.test.ts b/packages/core/src/plugins/plugins.test.ts index 1ce0d542d0..b7eb17c45d 100644 --- a/packages/core/src/plugins/plugins.test.ts +++ b/packages/core/src/plugins/plugins.test.ts @@ -13,8 +13,8 @@ describe('Plugin tests', () => { }); it('transform some code', async () => { - const result: any = await transformSource('const f = (a,b) => a+b; const test = f(2,3); export default test;'); - expect(eval(result.code)).toEqual(5); + const result = await transformSource('const f = (a,b) => a+b; const test = f(2,3); export default test;'); + expect(result.default).toEqual(5); }); it('throw an error on invalid code', async () => { @@ -22,9 +22,10 @@ describe('Plugin tests', () => { }); it('transform jsx', async () => { + const result: any = await transformSource( 'import React from \'react\'; export const component = () =>
;' ); - expect(() => eval(result.code)).not.toThrow(); + expect(result.component).toBeDefined(); }); }); diff --git a/packages/core/src/plugins/transform.ts b/packages/core/src/plugins/transform.ts index 9df53d0d87..d9236bcfa9 100644 --- a/packages/core/src/plugins/transform.ts +++ b/packages/core/src/plugins/transform.ts @@ -2,18 +2,10 @@ import { createConfigItem, transform, transformFile } from '@babel/core'; import presetEnv from '@babel/preset-env'; import presetReact from '@babel/preset-react'; -interface BabelConfig { - configFile: boolean; - sourceType: string; - presets: any[]; -} - interface TransformResult { - metadata: any; - options: BabelConfig; + [key: string]: any; } -type Cb = (err: Error, result: TransformResult) => void; type Transformer = typeof transform | typeof transformFile; const transformGeneric = (transformer: Transformer) => @@ -23,19 +15,25 @@ const transformGeneric = (transformer: Transformer) => input, { configFile: false, - sourceType: 'unambiguous', + sourceType: 'module', presets: [ createConfigItem([ presetEnv, { - targets: {electron: '4'} + targets: {electron: '12'} } ]), createConfigItem(presetReact) ] }, - (err: Error, result: TransformResult) => { - err ? reject(err) : resolve(result); + (err: Error, result) => { + if (err) { + reject(err); + } else { + const module = { exports: {} }; + new Function('exports', result.code)(module.exports); + resolve(module.exports as TransformResult); + } } ); }); diff --git a/packages/core/test/jest-setup.ts b/packages/core/test/jest-setup.ts new file mode 100644 index 0000000000..7327d77ff4 --- /dev/null +++ b/packages/core/test/jest-setup.ts @@ -0,0 +1,17 @@ +/* eslint-disable node/no-unsupported-features/node-builtins */ +import * as util from 'util'; + +// ref: https://jestjs.io/docs/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom +// ref: https://github.com/jsdom/jsdom/issues/2524 +Object.defineProperty(global, 'TextEncoder', { + writable: true, + value: util.TextEncoder +}); +Object.defineProperty(global, 'TextDecoder', { + writable: true, + value: util.TextDecoder +}); +Object.defineProperty(global, 'require', { + writable: true, + value: require +}); diff --git a/packages/core/tsconfig.json b/packages/core/tsconfig.json index d2e39bdd10..6b7e17ae8e 100644 --- a/packages/core/tsconfig.json +++ b/packages/core/tsconfig.json @@ -18,7 +18,7 @@ "src", "typings", "test" - ], +], "exclude": [ "lib" ]