Skip to content

Commit

Permalink
Fix plugin transpiler
Browse files Browse the repository at this point in the history
  • Loading branch information
nukeop committed Oct 24, 2023
1 parent 20996b1 commit 89ac224
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 18 deletions.
4 changes: 3 additions & 1 deletion packages/core/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ export default async () => ({
},
moduleNameMapper: {
'^react$': '<rootDir>/../../node_modules/react'
}
},
testEnvironment: 'node',
setupFilesAfterEnv: ['./test/jest-setup.ts']
});
7 changes: 4 additions & 3 deletions packages/core/src/plugins/plugins.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ 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 () => {
expect(transformSource('()asd')).rejects.toThrow();
});

it('transform jsx', async () => {

const result: any = await transformSource(
'import React from \'react\'; export const component = () => <div />;'
);
expect(() => eval(result.code)).not.toThrow();
expect(result.component).toBeDefined();
});
});
24 changes: 11 additions & 13 deletions packages/core/src/plugins/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand All @@ -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);
}
}
);
});
Expand Down
17 changes: 17 additions & 0 deletions packages/core/test/jest-setup.ts
Original file line number Diff line number Diff line change
@@ -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
});
2 changes: 1 addition & 1 deletion packages/core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"src",
"typings",
"test"
],
],
"exclude": [
"lib"
]
Expand Down

0 comments on commit 89ac224

Please sign in to comment.