Skip to content
This repository has been archived by the owner on Mar 7, 2024. It is now read-only.

Commit

Permalink
feat: 支持 React 17 (#1527)
Browse files Browse the repository at this point in the history
Co-authored-by: 谢仁洪 <xierenhong@renrenche.com>
  • Loading branch information
xierenyuan and 谢仁洪 committed Apr 14, 2021
1 parent db0e398 commit 7844089
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 3 deletions.
3 changes: 3 additions & 0 deletions docs/guide/config/babel.md
Expand Up @@ -30,6 +30,9 @@ module.exports = {
[
'remax',
{
react: {
runtime: 'automatic',
},
typescript: {
allowNamespaces: true,
},
Expand Down
7 changes: 7 additions & 0 deletions packages/babel-preset-remax/README.md
Expand Up @@ -8,6 +8,10 @@ Babel preset for remax app.

## Options

### react

configure react preset. https://babeljs.io/docs/en/babel-preset-react

### typescript

configure typescript preset. https://babeljs.io/docs/en/babel-preset-typescript
Expand All @@ -30,6 +34,9 @@ configure react preset throwIfNamespace option. https://babeljs.io/docs/en/babel
[
'remax',
{
react: {
runtime: 'classic',
},
typescript: {
allowNamespaces: true,
},
Expand Down
12 changes: 12 additions & 0 deletions packages/babel-preset-remax/jest.config.js
@@ -0,0 +1,12 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['/esm/', '/cjs/'],
testRegex: '.*\\.test\\.tsx?$',
coveragePathIgnorePatterns: ['/src/__tests__/'],
globals: {
'ts-jest': {
isolatedModules: true,
},
},
};
3 changes: 2 additions & 1 deletion packages/babel-preset-remax/package.json
Expand Up @@ -10,7 +10,8 @@
"scripts": {
"clean": "rimraf lib tsconfig.tsbuildinfo",
"prebuild": "npm run clean",
"build": "tsc"
"build": "tsc",
"test": "jest"
},
"bugs": {
"url": "https://github.com/remaxjs/remax/issues"
Expand Down
43 changes: 43 additions & 0 deletions packages/babel-preset-remax/src/index.test.ts
@@ -0,0 +1,43 @@
import { transformSync } from '@babel/core';
import preset from './index';

test('react default', () => {
const code = transformSync(
`
import React from 'react'
import { View } from '@remax/one'
function Demo() {
return <View>demo</View>
}
`.trim(),
{
filename: 'file.tsx',
babelrc: false,
presets: [preset],
}
)!.code;

expect(code).toMatch(`/*#__PURE__*/_react.default.createElement(_one.View, null, "demo")`);
});

test('react options', () => {
const code = transformSync(
`
import React from 'react'
import { View } from '@remax/one'
function Demo() {
return <View>demo</View>
}
`.trim(),
{
filename: 'file.tsx',
babelrc: false,
presets: [[preset, { react: { development: true } }]],
}
)!.code;

expect(code).toMatch(`return /*#__PURE__*/_react.default.createElement(_one.View, {`);
expect(code).toMatch(`__self: this,`);
});
7 changes: 5 additions & 2 deletions packages/babel-preset-remax/src/index.ts
@@ -1,7 +1,7 @@
import { declare } from '@babel/helper-plugin-utils';

interface PresetOption {
react?: boolean;
react?: boolean | { [key: string]: any };
typescript?: any;
decorators?: any;
'class-properties'?: any;
Expand Down Expand Up @@ -32,7 +32,10 @@ function preset(api: any, presetOption: PresetOption) {
}

if (react) {
presets.push([require.resolve('@babel/preset-react'), { throwIfNamespace }]);
const defaultReactOpt = { throwIfNamespace };
const reactOpts = typeof react === 'boolean' ? defaultReactOpt : Object.assign(defaultReactOpt, react);

presets.push([require.resolve('@babel/preset-react'), reactOpts]);
}

return {
Expand Down

1 comment on commit 7844089

@vercel
Copy link

@vercel vercel bot commented on 7844089 Apr 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.