我正在使用nuxt.js项目,需要在每次webpack构建时运行一个shell脚本来处理更改的文件。
因此,我正在使用Webpack Hooks。
我创建了自己的Webpack插件。
这个脚本是为了在组件目录中的每个文件夹中创建命名导出,例如。
但是当我运行我的应用程序并更改任何文件时,服务器会提示已对components/atoms/index.js进行更改,然后进行新的构建,因此它会无限制地构建。
有人能帮我打破这个循环吗?
因为只要更改一个文件,就生成新的index.js而不是生成无限构建。
提前感谢。
因此,我正在使用Webpack Hooks。
我创建了自己的Webpack插件。
/plugins/NamedExports.js
const pluginName = 'NamedExports'
const { exec } = require('child_process')
class NamedExports {
apply(compiler) {
compiler.hooks.beforeCompile.tap(pluginName, (params, callback) => {
exec('sh plugins/shell.sh', (err, stdout, stderr) => {
console.log(stdout)
console.log(stderr)
})
})
}
}
export default NamedExports
plugins/shell.js
parameters=$(ls components)
for item in ${parameters[*]}
do
ls components/$item/ | grep -v index.js | sed 's#^\([^.]*\).*$#export { default as \1 } from "./&"#' > components/$item/index.js
done
echo "worked"
这个脚本是为了在组件目录中的每个文件夹中创建命名导出,例如。
components/atoms/ButtonStyled.vue
components/atoms/BoxStyled.vue
然后它会生成 components/atoms/index.js
export { default as ButtonStyled } from "./ButtonStyled.vue"
export { default as BoxStyled } from "./BoxStyled.vue"
我在nuxt.config.nuxt
或webpack.config.js
中注册了我的插件。
import NamedExports from './plugins/NamedExports.js'
export default {
// ... other config here ...
build: {
plugins: [
// ... other plugins here ...
new NamedExports()
],
}
}
但是当我运行我的应用程序并更改任何文件时,服务器会提示已对components/atoms/index.js进行更改,然后进行新的构建,因此它会无限制地构建。
有人能帮我打破这个循环吗?
因为只要更改一个文件,就生成新的index.js而不是生成无限构建。
提前感谢。