Bring the awesome "pug in typescript" to the Webpack, and more.
- Tik Tok
- Installation
- Configuration
- Options
- Basic Process
- React-native, Expo
- Caveats
- Sample Project
- Lisense
Try using pug inside TypeScript.
yarn add webpack-preprocessor-pug-tsx -D
or
npm install webpack-preprocessor-pug-tsx -D
module.exports = {
// ...
module: {
rules: [
{
test: /\.(ts|tsx)$/,
use: [
'babel-loader',
{
loader: 'webpack-preprocessor-pug-tsx',
options: {},
},
],
},
],
},
};
type:
string[]
default:
['jsx', 'React']
Variable that must be included among imported libs.
type:
{[key: string]: string}
default:
{'jsx': '/** @jsx jsx */ jsx'}
When you need to transform the variable declared in includes.
// In webpack config...
{
loader: 'webpack-preprocessor-pug-tsx',
options: {
replace: {
jsx: '/** @jsx jsx */ jsx',
},
},
},
> type: string[]
>
> default: ['pug`', 'css`', ' `[^;,]', '\\(`']
Specifies the starting string of the element containing the backtick. Expressed as a regular expression string.
- pug` is the starting string of pug.
- css` is the starting string for emotion css.
- `[^;] is the starting string for template strings.
Edit the document as follows:
import { jsx, css } from '@emotion/core';
import Button from './my/Button';
import ButtonGroup from './my/ButtonGroup';
...
return pug`
div(css=[fullscreen, darkLayer])
div(css=[fullscreen, whiteBoxWrapper])
div(css=whiteBoxWrapper)
h3 Confirm
p 정말로 삭제하시 겠습니까?
ButtonGroup
Button cancel
Button ok
`;
...
The following code...
/** @jsx jsx */ jsx;
Button;
ButtonGroup;
import { jsx, css } from '@emotion/core';
import Button from './my/Button';
import ButtonGroup from './my/ButtonGroup';
...
return pug`
div(css=[fullscreen, darkLayer])
div(css=[fullscreen, whiteBoxWrapper])
div(css=whiteBoxWrapper)
h3 Confirm
p 정말로 삭제하시 겠습니까?
ButtonGroup
Button cancel
Button ok
`;
...
metro.config.js
const { getDefaultConfig } = require('metro-config');
const { setOptions } = require('webpack-preprocessor-pug-tsx');
setOptions({
start: ['gql`'],
});
module.exports = (async () => {
const {
resolver: { sourceExts },
} = await getDefaultConfig();
return {
transformer: {
babelTransformerPath: require.resolve('webpack-preprocessor-pug-tsx'),
},
resolver: {
sourceExts: [...sourceExts, 'tsx'],
},
};
})();
The following code may not work as expected:
const Button = styled.button`
color: turquoise;
`;
render pug`
Button This my button component.
`;
So, you need to add the following to the start of options.
{
loader: 'webpack-preprocessor-pug-tsx',
options: {
start: ['button`'],
},
},
The following code is added automatically.
before
import { jsx, css } from '@emotion/core';
after
/** @jsx jsx */ jsx;
import { jsx, css } from '@emotion/core';
git clone [email protected]:pruge/storybook-tutorial.git
yarn
yarn storybook
MIT License