Skip to content

Commit

Permalink
refactor: Switch to automatic runtime JSX transform (#1725)
Browse files Browse the repository at this point in the history
* refactor: Switch over to automatic runtime transform for JSX

* test: Removing `h` imports from test suite

* docs: Adding changeset
  • Loading branch information
rschristian committed Dec 20, 2022
1 parent 9a478af commit 2a52a28
Show file tree
Hide file tree
Showing 26 changed files with 34 additions and 53 deletions.
7 changes: 7 additions & 0 deletions .changeset/metal-roses-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'preact-cli': major
---

Changes the JSX transform from 'classic' to the newer 'automatic'

Users will no longer need to add `import { h } from 'preact'` in their components; it will be done automatically for them.
6 changes: 3 additions & 3 deletions jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
"noEmit": true,
"resolveJsonModule": true,
"allowSyntheticDefaultImports": true,
"jsx": "react",
"jsxFactory": "h",
"jsxFragmentFactory": "Fragment"
"jsx": "preserve",
"jsxFactory": "preact.h",
"jsxFragmentFactory": "preact.Fragment"
},
"include": ["packages/**/*"]
}
6 changes: 2 additions & 4 deletions packages/cli/src/lib/babel-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,14 @@ module.exports = function (env) {
require.resolve('babel-plugin-macros'),
[
require.resolve('@babel/plugin-transform-react-jsx'),
{ pragma: 'h', pragmaFrag: 'Fragment' },
{ runtime: 'automatic', importSource: 'preact' },
],
].filter(Boolean),
overrides: [
// Transforms to apply only to first-party code:
{
exclude: '**/node_modules/**',
presets: [
[require.resolve('@babel/preset-typescript'), { jsxPragma: 'h' }],
],
presets: [require.resolve('@babel/preset-typescript')],
plugins: [
!isProd && refresh && require.resolve('react-refresh/babel'),
].filter(Boolean),
Expand Down
4 changes: 0 additions & 4 deletions packages/cli/src/lib/webpack/webpack-base-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,6 @@ module.exports = function createBaseConfig(env) {
}
)
),
new webpack.ProvidePlugin({
h: ['preact', 'h'],
Fragment: ['preact', 'Fragment'],
}),
// Fix for https://github.com/webpack-contrib/mini-css-extract-plugin/issues/151
new RemoveEmptyScriptsPlugin(),
new MiniCssExtractPlugin({
Expand Down
28 changes: 14 additions & 14 deletions packages/cli/tests/images/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ exports.default = {
'assets/favicon.ico': 15086,

'ssr-build/ssr-bundle.dba70.css': 1281,
'ssr-build/ssr-bundle.dba70.css.map': 2081,
'ssr-build/ssr-bundle.dba70.css.map': 2069,
'ssr-build/ssr-bundle.js': 26049,
'ssr-build/ssr-bundle.js.map': 59971,
'ssr-build/ssr-bundle.js.map': 63276,
'ssr-build/asset-manifest.json': 82,

'bundle.12615.js': 21560,
'bundle.12615.js.map': 85822,
'bundle.12615.legacy.js': 22549,
'bundle.12615.legacy.js.map': 106841,
'bundle.79d07.js': 21560,
'bundle.79d07.js.map': 85822,
'bundle.79d07.legacy.js': 22549,
'bundle.79d07.legacy.js.map': 106841,
'bundle.354c3.css': 945,
'bundle.354c3.css.map': 1758,

Expand All @@ -31,17 +31,17 @@ exports.default = {
'push-manifest.json': 450,
'asset-manifest.json': 943,

'route-home.chunk.f910e.js': 307,
'route-home.chunk.f910e.js.map': 1516,
'route-home.chunk.f910e.legacy.js': 347,
'route-home.chunk.f910e.legacy.js.map': 1770,
'route-home.chunk.dd4a3.js': 347,
'route-home.chunk.dd4a3.js.map': 1848,
'route-home.chunk.dd4a3.legacy.js': 388,
'route-home.chunk.dd4a3.legacy.js.map': 2136,
'route-home.chunk.6eaee.css': 112,
'route-home.chunk.6eaee.css.map': 224,

'route-profile.chunk.ef912.js': 3106,
'route-profile.chunk.ef912.js.map': 12220,
'route-profile.chunk.ef912.legacy.js': 3243,
'route-profile.chunk.ef912.legacy.js.map': 15558,
'route-profile.chunk.0d30e.js': 3198,
'route-profile.chunk.0d30e.js.map': 12743,
'route-profile.chunk.0d30e.legacy.js': 3335,
'route-profile.chunk.0d30e.legacy.js.map': 16137,
'route-profile.chunk.0af3e.css': 118,
'route-profile.chunk.0af3e.css.map': 231,
};
Expand Down
2 changes: 0 additions & 2 deletions packages/cli/tests/subjects/css-inline/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { h } from 'preact';

import './style.css';

export default () => <h1>Test CSS inlining</h1>;
2 changes: 0 additions & 2 deletions packages/cli/tests/subjects/css-modules/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { h } from 'preact';

import './style.css';
import styles from './style.module.css';

Expand Down
2 changes: 0 additions & 2 deletions packages/cli/tests/subjects/css-sass/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { h } from 'preact';

import './style.sass';
import './style.scss';
import sassStyles from './style.module.sass';
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/tests/subjects/custom-babelrc/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { h, Component } from 'preact';
import { Component } from 'preact';

const delay = t => new Promise(r => setTimeout(r, t));

Expand Down
2 changes: 0 additions & 2 deletions packages/cli/tests/subjects/custom-template/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import { h } from 'preact';

export default () => <h2>This is an app with custom template</h2>;
2 changes: 0 additions & 2 deletions packages/cli/tests/subjects/custom-webpack/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import { h } from 'preact';

export default () => <h2>This is an app with custom webpack config</h2>;
2 changes: 0 additions & 2 deletions packages/cli/tests/subjects/location-patch/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { h } from 'preact';

export default () => {
if (
location.protocol === undefined ||
Expand Down
1 change: 0 additions & 1 deletion packages/cli/tests/subjects/minimal/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { h } from 'preact';
import './style.css';

export default () => <h1>Minimal App</h1>;
2 changes: 1 addition & 1 deletion packages/cli/tests/subjects/multiple-config-files/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { h, Component } from 'preact';
import { Component } from 'preact';
import { Router } from 'preact-router';
import Home from './routes/home';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { h, Component } from 'preact';
import { Component } from 'preact';
import { Router } from 'preact-router';
import { Provider } from '@preact/prerender-data-provider';
import Home from './routes/home';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { h, Component } from 'preact';
import { Component } from 'preact';
import './custom.css';
import { PreRenderDataSource } from '@preact/prerender-data-provider';
class Custom extends Component {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/tests/subjects/multiple-prerendering/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { h, Component } from 'preact';
import { Component } from 'preact';
import { Router } from 'preact-router';
import Home from './routes/home';
import Route66 from './routes/route66';
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/tests/subjects/preload-chunks/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { h, Component } from 'preact';
import { Component } from 'preact';
import { Router } from 'preact-router';
import Home from './routes/home';
import Route66 from './routes/route66';
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/tests/subjects/preload-chunks/routes/base.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { h, Component } from 'preact';
import { Component } from 'preact';

export default class Base extends Component {
state = {
Expand Down
1 change: 0 additions & 1 deletion packages/cli/tests/subjects/preload-chunks/routes/home.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { h } from 'preact';
import Base from './base';
import './home.css';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { h } from 'preact';
import Base from './base';
import './route66.css';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { h } from 'preact';
import Base from './base';
import './route89.css';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { h, Component } from 'preact';
import { Component } from 'preact';

export default class App extends Component {
render() {
Expand Down
1 change: 0 additions & 1 deletion packages/cli/tests/subjects/proxy/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { h } from 'preact';
import { useEffect, useState } from 'preact/hooks';

export default () => {
Expand Down
1 change: 0 additions & 1 deletion packages/cli/tests/subjects/public-path/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { h } from 'preact';
import './style.css';

export default () => <h1>Public path test</h1>;
2 changes: 0 additions & 2 deletions packages/cli/tests/subjects/static-root/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { h } from 'preact';

export default () => {
return <h1>Static test</h1>;
};

0 comments on commit 2a52a28

Please sign in to comment.