Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: default react runtime automatic #13

Merged
merged 16 commits into from
Mar 29, 2022
5 changes: 5 additions & 0 deletions .changeset/five-drinks-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'craco-swc': minor
---

Enable react automatic runtime, you don't need to import React anymore!
2 changes: 1 addition & 1 deletion examples/with-javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.3",
"private": true,
"dependencies": {
"@craco/craco": "6.4.2",
"@craco/craco": "6.4.3",
"@testing-library/jest-dom": "^5.15.1",
"@testing-library/react": "^11.2.7",
"@testing-library/user-event": "^12.8.3",
Expand Down
1 change: 0 additions & 1 deletion examples/with-javascript/src/App.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import React from 'react';
import logo from './logo.svg';
import './App.css';

Expand Down
1 change: 0 additions & 1 deletion examples/with-javascript/src/App.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import App from './App';

Expand Down
4 changes: 2 additions & 2 deletions examples/with-typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@craco/craco": "6.4.2",
"@craco/craco": "6.4.3",
"@testing-library/jest-dom": "^5.15.1",
"@testing-library/react": "^11.2.7",
"@testing-library/user-event": "^12.8.3",
Expand All @@ -15,7 +15,7 @@
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "4.0.3",
"typescript": "^4.5.2",
"typescript": "^4.6.3",
"web-vitals": "^1.1.2"
},
"scripts": {
Expand Down
1 change: 0 additions & 1 deletion examples/with-typescript/src/App.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import App from './App';

Expand Down
1 change: 0 additions & 1 deletion examples/with-typescript/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import React from 'react';
import logo from './logo.svg';
import './App.css';

Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
]
},
"devDependencies": {
"@changesets/changelog-github": "0.4.2",
"@changesets/cli": "2.18.1",
"@changesets/changelog-github": "0.4.4",
"@changesets/cli": "2.22.0",
"husky": "7.0.4",
"lint-staged": "12.1.2",
"prettier": "2.5.0"
"lint-staged": "12.3.7",
"prettier": "2.6.1"
}
}
8 changes: 4 additions & 4 deletions packages/craco-swc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
"react-scripts": "^3.3.0 || ^4.0.0"
},
"dependencies": {
"@swc/core": "^1.2.114",
"@swc/helpers": "^0.2.14",
"@swc/jest": "^0.2.11",
"@swc/core": "^1.2.161",
"@swc/helpers": "^0.3.8",
"@swc/jest": "^0.2.20",
"swc-loader": "^0.1.15"
},
"devDependencies": {
"@craco/craco": "6.4.2",
"@craco/craco": "6.4.3",
pradel marked this conversation as resolved.
Show resolved Hide resolved
"react-scripts": "4.0.3"
}
}
23 changes: 17 additions & 6 deletions packages/craco-swc/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ module.exports = {
undefined
: {
jsc: {
externalHelpers: true,
target: 'es2015',
externalHelpers: true,
transform: {
react: {
runtime: 'automatic',
},
},
parser: useTypeScript
? {
syntax: 'typescript',
Expand All @@ -55,27 +60,33 @@ module.exports = {
/**
* To process the js/ts files we replace the babel-loader with the swc jest loader
*/
overrideJestConfig: ({
jestConfig,
pluginOptions,
context: { env, paths, resolve, rootDir },
}) => {
overrideJestConfig: ({ jestConfig, pluginOptions, context: { paths } }) => {
const useTypeScript = fs.existsSync(paths.appTsConfig);

// Replace babel transform with swc
const key = Object.keys(jestConfig.transform)[0];
// TODO find a way to pass options directly to the plugin without having to use a .swcrc
jestConfig.transform[key] = [
require.resolve('@swc/jest', {
sourceMaps: true,
jsc: {
target: 'es2021',
externalHelpers: true,
transform: {
react: {
runtime: 'automatic',
},
},
parser: useTypeScript
? {
syntax: 'typescript',
tsx: true,
dynamicImport: true,
}
: {
syntax: 'ecmascript',
jsx: true,
dynamicImport: true,
},
},
}),
Expand Down
Loading