【问题标题】:Prevent file from generating new build - Webpack防止文件生成新版本 - Webpack
【发布时间】:2019-11-17 08:15:52
【问题描述】:

我有一个带有 nuxt.js / Vue 的应用程序。

我创建了一个 Webpack 插件,以便随着每个文件的更改,在某个目录中生成一个 index.js。

问题是当index.js 生成时,Webpack 将其识别为新的更改并再次构建,因此它停留在那个无限循环中......

为了检测变化,我使用webpack hooks

compiler.hooks.beforeCompile.tapAsync('MyPlugin', (params, callback) => {
  // script to generate an index.js in a given directory
});

如何防止index.js 触发新构建?

更新问题以便更好地理解

我正在开发一个使用 vue.js 制作的应用程序 | nuxt.js 和这个组件结构

├── components
│   ├── quarks
│   │   └── ...
│   ├── bosons
│   │   └── GridLayout.vue
│   │   └── ...
│   ├── atoms
│   │   └── ButtonStyle.vue
│   │   └── InputStyle.vue
│   │   └── ...
│   ├── molecules
│   │   └── ...
│   ├── organisms
│   │   └── ...
│   ├── templates
│   │   └── ...
└─────

我需要进行命名和分组导入,如下所示:

import { ButtonStyle, InputStyle } from '@/components/atoms/'

但要解决这个问题,我需要在每个文件夹中都有一个 index.js,逐个导出组件,例如

├── components
│   ├── atoms
│   │   └── ButtonStyle.vue
│   │   └── InputStyle.vue
│   │   └── index.js
└─────

在index.js

export { default as ButtonStyled } from './ButtonStyled.vue'
export { default as InputStyle } from './InputStyle.vue'

但是手动完成这项工作可能是一项非常令人厌烦的任务。每次创建、删除、重命名组件时,都必须更新各自文件夹的 index.js。

所以我开始开发解决方案

在nuxt.config.js

import NamedExports from './plugins/NamedExports.js'

export default {
  // ... other config here ...
  build: {
    plugins: [
      new NamedExports()
    ],
  }
}

在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.sh

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

但每当插件创建index.js 时,就会触发新的构建

【问题讨论】:

    标签: javascript node.js webpack nuxt.js


    【解决方案1】:

    我使用一个只运行一次的钩子,并使用chokidar 来监听组件目录中的变化

    compiler.hooks.entryOption.tap('MyPlugin', (context, entry) => {
      // generates index.js
      // Watch a directory with chokidar 
    });
    

    我把它变成了一个图书馆,也许它可以帮助其他人

    Weback Plugin - named-exports

    【讨论】:

      【解决方案2】:

      您是否已将新文件/目录添加到 WebPacks 排除列表中?如果没有,那么 watchOptions.ignore 属性可能正是您要寻找的: https://webpack.js.org/configuration/watch/

      希望对你有帮助

      【讨论】:

      • 我已经尝试过这个选项,我可以让 webpack 停止观察index.js,但这会导致另一个问题。当我的脚本 updatesindex.js 的内容时,webpack 将无法识别这些更改,并且将就好像它没有更新一样。
      • 嗯...也许我当时误解了这个问题。为了让我们能够正确理解并尝试更好地回答让我问。您是说希望 WebPack 检测文件的内容更改,但如果文件被替换(即创建一个具有相同名称和大小的新文件)则忽略该文件?
      • 这正是我所需要的。 index.js 文件进行命名导出,并且可以随着新组件/模块的创建或删除而更新,因此内容对于应用程序的运行非常重要。但 index.js 是动态生成的,因此无法触发新结构。
      • 不幸的是,我不熟悉可以为您提供开箱即用的行为的监视选项。也许其他人有一些想法。但是,如果您愿意分享更多信息,例如您的插件何时被触发,我们也许可以从另一个向量中解决这个问题。根据我所学到的一点点,听起来这主要是开发时的问题?在任何情况下,我们都可以通过时间和安排更新来解决这个问题。或者也许利用您的索引文件作为生成文件的链接并排除这些文件。
      • 我更新了我的问题,以便更好地理解我的问题,如果你能读到,那就开心
      猜你喜欢
      • 2020-12-11
      • 1970-01-01
      • 1970-01-01
      • 2011-02-15
      • 1970-01-01
      • 2020-09-10
      • 1970-01-01
      • 1970-01-01
      • 2014-08-09
      相关资源
      最近更新 更多