使用 Plop 修改现有文件?
Modifying an existing file with Plop?
我正在使用 plop 为基于 React 的组件库生成组件。我有一个充满用于创建新组件目录的 Handlebars 模板的目录,并且一切正常。我还有这个 index.js
文件作为我的捆绑器的入口点。它只是充满了大量的进口和出口。它看起来像这样:
import { Badge } from './components/Badge';
import { Button, ButtonMemo } from './components/Button';
import { Column } from './components/Column';
import { ErrorBoundary } from './components/ErrorBoundary';
import { FormBasicInfo } from './components/FormBasicInfo';
import { FormLogin } from './components/FormLogin';
import { FormSignup } from './components/FormSignUp';
import { Icon } from './components/Icon';
import { Input } from './components/Input';
import { Link } from './components/Link';
import { Loader } from './components/Loader';
import { Logo } from './components/Logo';
import { Row } from './components/Row';
import { Select } from './components/Select';
import { SimpleMenu } from './components/SimpleMenu';
import { Slideshow } from './components/Slideshow';
import { Spinner } from './components/Spinner';
export {
Badge,
Button,
Column,
ErrorBoundary,
FormBasicInfo,
FormLogin,
FormSignup,
Icon,
Input,
Link,
Loader,
Logo,
Row,
Select,
SimpleMenu,
Slideshow,
Spinner,
};
生成新组件时,我也希望将新组件添加到这个入口点文件中。这需要在顶部块添加一个导入行,在底部添加一个导出行。理想情况下,它仍会按字母顺序排序,但这不是硬性要求。
这可能吗?
这就是我最终实现它的方式:
在我的 index.js
文件(我要修改的文件)中,我在应该插入的地方添加了两条注释:
…
import { PointAccountCard } from './components/PointAccountCard';
import { Card } from './components/Card';
import { SettingsPanelFooter } from './components/SettingsPanelFooter';
// COMPONENT IMPORTS
export {
…
PointAccountCard,
Card,
SettingsPanelFooter,
// COMPONENT EXPORTS
};
然后在我的 plopfile.js
配置中,我将这两个步骤添加到我的 actions
配置中:
module.exports = function (plop) {
plop.setGenerator('component', {
actions: [
…
{
path: '../../src/index.js',
pattern: /(\/\/ COMPONENT IMPORTS)/g,
template: 'import { {{name}} } from \'./components/{{name}}\';\n',
type: 'modify',
},
{
path: '../../src/index.js',
pattern: /(\/\/ COMPONENT EXPORTS)/g,
template: '\t{{name}},\n',
type: 'modify',
},
],
description: 'New React Component',
prompts: [
…
],
})
};
所有新组件都会自动添加到 index.js
文件中。
我正在使用 plop 为基于 React 的组件库生成组件。我有一个充满用于创建新组件目录的 Handlebars 模板的目录,并且一切正常。我还有这个 index.js
文件作为我的捆绑器的入口点。它只是充满了大量的进口和出口。它看起来像这样:
import { Badge } from './components/Badge';
import { Button, ButtonMemo } from './components/Button';
import { Column } from './components/Column';
import { ErrorBoundary } from './components/ErrorBoundary';
import { FormBasicInfo } from './components/FormBasicInfo';
import { FormLogin } from './components/FormLogin';
import { FormSignup } from './components/FormSignUp';
import { Icon } from './components/Icon';
import { Input } from './components/Input';
import { Link } from './components/Link';
import { Loader } from './components/Loader';
import { Logo } from './components/Logo';
import { Row } from './components/Row';
import { Select } from './components/Select';
import { SimpleMenu } from './components/SimpleMenu';
import { Slideshow } from './components/Slideshow';
import { Spinner } from './components/Spinner';
export {
Badge,
Button,
Column,
ErrorBoundary,
FormBasicInfo,
FormLogin,
FormSignup,
Icon,
Input,
Link,
Loader,
Logo,
Row,
Select,
SimpleMenu,
Slideshow,
Spinner,
};
生成新组件时,我也希望将新组件添加到这个入口点文件中。这需要在顶部块添加一个导入行,在底部添加一个导出行。理想情况下,它仍会按字母顺序排序,但这不是硬性要求。
这可能吗?
这就是我最终实现它的方式:
在我的 index.js
文件(我要修改的文件)中,我在应该插入的地方添加了两条注释:
…
import { PointAccountCard } from './components/PointAccountCard';
import { Card } from './components/Card';
import { SettingsPanelFooter } from './components/SettingsPanelFooter';
// COMPONENT IMPORTS
export {
…
PointAccountCard,
Card,
SettingsPanelFooter,
// COMPONENT EXPORTS
};
然后在我的 plopfile.js
配置中,我将这两个步骤添加到我的 actions
配置中:
module.exports = function (plop) {
plop.setGenerator('component', {
actions: [
…
{
path: '../../src/index.js',
pattern: /(\/\/ COMPONENT IMPORTS)/g,
template: 'import { {{name}} } from \'./components/{{name}}\';\n',
type: 'modify',
},
{
path: '../../src/index.js',
pattern: /(\/\/ COMPONENT EXPORTS)/g,
template: '\t{{name}},\n',
type: 'modify',
},
],
description: 'New React Component',
prompts: [
…
],
})
};
所有新组件都会自动添加到 index.js
文件中。