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

Preact typescript pragma fix #15564

Merged
merged 11 commits into from
Jul 27, 2022
Merged
5 changes: 5 additions & 0 deletions code/examples/preact-kitchen-sink/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ module.exports = {
settings: {
react: {
pragma: 'h',
pragmaFrag: 'Fragment',
},
},
rules: {
'react/react-in-jsx-scope': 'off',
'react/prop-types': 'off',
},
};
2 changes: 1 addition & 1 deletion code/examples/preact-kitchen-sink/.storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const mainConfig: StorybookConfig = {
webpackFinal: (config) => {
const rules = config.module?.rules || [];
rules.push({
test: [/\.stories\.js$/],
test: [/\.stories\.(js|ts|jsx|tsx)$/],
use: [require.resolve('@storybook/source-loader')],
include: [path.resolve(__dirname, '../src')],
enforce: 'pre',
Expand Down
6 changes: 1 addition & 5 deletions code/examples/preact-kitchen-sink/src/Button.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
/** @jsx h */
/* eslint-disable react/prop-types */

import { h } from 'preact';

/**
* The button component will render a clickable button
*/

const Button = ({ children, href, ...props }) => {
const TagName = href ? 'a' : 'button';

Expand Down
1 change: 0 additions & 1 deletion code/examples/preact-kitchen-sink/src/React.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/** @jsx React.createElement */
import React from 'react';
import PropTypes from 'prop-types';

Expand Down
8 changes: 0 additions & 8 deletions code/examples/preact-kitchen-sink/src/Welcome.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/** @jsx h */

import { h } from 'preact';

const Main = (props) => (
<article
{...props}
Expand All @@ -14,7 +10,6 @@ const Main = (props) => (
/>
);

// eslint-disable-next-line react/prop-types
const Title = ({ children, ...props }) => <h1 {...props}>{children}</h1>;

const Note = (props) => (
Expand All @@ -41,7 +36,6 @@ const InlineCode = (props) => (
/>
);

// eslint-disable-next-line react/prop-types
const Link = ({ children, href, ...props }) => (
<a
href={href}
Expand All @@ -57,7 +51,6 @@ const Link = ({ children, href, ...props }) => (
</a>
);

// eslint-disable-next-line react/prop-types
const NavButton = ({ children, ...props }) => (
<button
{...props}
Expand All @@ -80,7 +73,6 @@ const NavButton = ({ children, ...props }) => (
</button>
);

/* eslint-disable-next-line react/prop-types */
const Welcome = ({ showApp }) => (
<Main>
<Title>Welcome to storybook for Preact</Title>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
/** @jsx h */

import { h } from 'preact';
import { useState } from 'preact/hooks';

import { action, actions } from '@storybook/addon-actions';
import Button from '../Button';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/** @jsx h */

import { h } from 'preact';

import Button from '../Button';

export default {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
/** @jsx h */

import { h } from 'preact';

import { linkTo } from '@storybook/addon-links';

import Button from '../Button';

export default {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
/** @jsx h */

import { h } from 'preact';
import { action } from '@storybook/addon-actions';

import Button from '../Button';

export default {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/** @jsx h */
import { h } from 'preact';
import { ReactFunctionalComponent, ReactClassComponent } from '../React';

export default {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Storyshots Pragma test Default 1`] = `"<div>This component should render OK.</div>"`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const TestComponent = () => <div>This component should render OK.</div>;

export default TestComponent;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import TestComponent from './no-pragma';

export default {
title: 'Pragma test',
};

export const Default = () => <TestComponent />;
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
/** @jsx h */

import { h } from 'preact';
import { linkTo } from '@storybook/addon-links';

import Welcome from '../Welcome';

export default {
Expand Down
8 changes: 8 additions & 0 deletions code/examples/preact-kitchen-sink/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"jsx": "react-jsx",
"jsxFactory": "h",
"jsxFragmentFactory": "Fragment",
Comment on lines +4 to +5
Copy link
Member

Choose a reason for hiding this comment

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

This seems to give me error/warnings when I open the file:
Screen Shot 2022-07-27 at 11 31 24

"jsxImportSource": "preact"
}
}
20 changes: 19 additions & 1 deletion code/presets/preact-webpack/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,30 @@ export const babelDefault: StorybookConfig['babelDefault'] = (config) => {
return {
...config,
plugins: [
[
require.resolve('@babel/plugin-transform-react-jsx'),
{ importSource: 'preact', runtime: 'automatic' },
],
...(config.plugins || []),
[require.resolve('@babel/plugin-transform-react-jsx'), { pragma: 'h' }, 'preset'],
],
};
};

export const webpackFinal: StorybookConfig['webpackFinal'] = (config) => {
const rules = config.module?.rules || [];
const tsxRule = rules.find((rule) => (rule.test as RegExp).test?.('main.tsx'));
tsxRule.use = (tsxRule.use as any).map((entry: any) => {
let newPlugins = entry.options.plugins;
if (entry.loader?.includes('babel-loader')) {
newPlugins = (entry.options as any).plugins.map((plugin: any) => {
if (plugin[0]?.includes?.('@babel/plugin-transform-react-jsx')) {
return [plugin[0], { importSource: 'preact', runtime: 'automatic' }];
}
return plugin;
});
}
return { ...entry, options: { ...entry.options, plugins: newPlugins } };
});
return {
...config,
resolve: {
Expand All @@ -23,6 +40,7 @@ export const webpackFinal: StorybookConfig['webpackFinal'] = (config) => {
react: path.dirname(require.resolve('preact/compat/package.json')),
'react-dom/test-utils': path.dirname(require.resolve('preact/test-utils/package.json')),
'react-dom': path.dirname(require.resolve('preact/compat/package.json')),
'react/jsx-runtime': path.dirname(require.resolve('preact/jsx-runtime/package.json')),
},
},
};
Expand Down
25 changes: 25 additions & 0 deletions scripts/.babelrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,31 @@ module.exports = {
test: withTests,
},
},
{
test: './examples/preact-kitchen-sink',
presets: [
[
'@babel/preset-env',
{
shippedProposals: true,
useBuiltIns: 'usage',
corejs: '3',
targets,
modules,
},
],
['@babel/preset-typescript'],
[
'@babel/preset-react',
{
importSource: 'preact',
runtime: 'automatic',
},
],
'@babel/preset-flow',
],
env: { test: withTests },
},
{
test: './lib',
presets: [
Expand Down