Webpack模块加载程序多次解析文件

gxwragnw  于 2022-11-13  发布在  Webpack
关注(0)|答案(1)|浏览(331)

我目前正在使用Webpack(带有Laravel Mix)和ifdef-loader模块进行条件编译,但当我引入html-webpack-plugin在每次编译前自动清理输出文件夹时(如此处所述),我就遇到了下面的问题。
无法真正理解这是怎么回事,有人能帮忙吗?

网页包.混合. js

mix
    .webpackConfig(webpack => {
        return {
            module: {
                rules: [
                    {
                        use: [{
                            loader: "ifdef-loader",
                            options: ifdef_loader_opts,
                        }]
                    }
                ],
            },
            plugins: [
                new HtmlWebpackPlugin({
                    title: 'Output Management',
                }),
            ],
            output: {
                path: path.resolve(__dirname, 'public'),
                clean: true,
            }
        };
    })
    .setPublicPath('public')

    // other assets management

错误

ERROR in   Error: Child compilation failed:
  Module parse failed: Unexpected token (1:0)
  File was processed with these loaders:
   * ./node_modules/html-webpack-plugin/lib/loader.js
   * ./node_modules/ifdef-loader/ifdef-loader.js
  You may need an additional loader to handle the result of these loaders.
  > <!DOCTYPE html>
  | <html>
  |   <head>
  ModuleParseError: Module parse failed: Unexpected token (1:0)
  File was processed with these loaders:
   * ./node_modules/html-webpack-plugin/lib/loader.js
   * ./node_modules/ifdef-loader/ifdef-loader.js
  You may need an additional loader to handle the result of these loaders.
  > <!DOCTYPE html>
  | <html>
  |   <head>
      at handleParseError (/home/node/app/node_modules/webpack/lib/NormalModule.js:976:19)
      at /home/node/app/node_modules/webpack/lib/NormalModule.js:1095:5
      at processResult (/home/node/app/node_modules/webpack/lib/NormalModule.js:800:11)
      at /home/node/app/node_modules/webpack/lib/NormalModule.js:860:5
      at /home/node/app/node_modules/loader-runner/lib/LoaderRunner.js:407:3
      at iterateNormalLoaders (/home/node/app/node_modules/loader-runner/lib/LoaderRunner.js:233:10)
      at iterateNormalLoaders (/home/node/app/node_modules/loader-runner/lib/LoaderRunner.js:240:10)
      at /home/node/app/node_modules/loader-runner/lib/LoaderRunner.js:255:3
      at runSyncOrAsync (/home/node/app/node_modules/loader-runner/lib/LoaderRunner.js:143:11)
      at iterateNormalLoaders (/home/node/app/node_modules/loader-runner/lib/LoaderRunner.js:251:2)
  
  - NormalModule.js:976 handleParseError
    [app]/[webpack]/lib/NormalModule.js:976:19
  
  - NormalModule.js:1095 
    [app]/[webpack]/lib/NormalModule.js:1095:5
  
  - NormalModule.js:800 processResult
    [app]/[webpack]/lib/NormalModule.js:800:11
  
  - NormalModule.js:860 
    [app]/[webpack]/lib/NormalModule.js:860:5
  
  - LoaderRunner.js:407 
    [app]/[loader-runner]/lib/LoaderRunner.js:407:3
  
  - LoaderRunner.js:233 iterateNormalLoaders
    [app]/[loader-runner]/lib/LoaderRunner.js:233:10
  
  - LoaderRunner.js:240 iterateNormalLoaders
    [app]/[loader-runner]/lib/LoaderRunner.js:240:10
  
  - LoaderRunner.js:255 
    [app]/[loader-runner]/lib/LoaderRunner.js:255:3
  
  - LoaderRunner.js:143 runSyncOrAsync
    [app]/[loader-runner]/lib/LoaderRunner.js:143:11
  
  - LoaderRunner.js:251 iterateNormalLoaders
    [app]/[loader-runner]/lib/LoaderRunner.js:251:2
  
  - child-compiler.js:169 
    [app]/[html-webpack-plugin]/lib/child-compiler.js:169:18
  
  - Compiler.js:551 finalCallback
    [app]/[webpack]/lib/Compiler.js:551:5
  
  - Compiler.js:577 
    [app]/[webpack]/lib/Compiler.js:577:11
  
  - Compiler.js:1196 
    [app]/[webpack]/lib/Compiler.js:1196:17
  
  
  - task_queues:96 processTicksAndRejections
    node:internal/process/task_queues:96:5
  

1 ERROR in child compilations (Use 'stats.children: true' resp. '--stats-children' for more details)
webpack compiled with 2 errors
ni65a41a

ni65a41a1#

好的,我设法解决了我的问题,只是限制ifdef-loader必须解析的文件:因为我只在javascript文件中编写条件代码,所以我只是在它的规则中添加了一个测试,以确保只考虑*.js文件。

module: {
        rules: [
            {
                test: /\.js$/,    // <- added test for .js files
                use: [{
                    loader: "ifdef-loader",
                    options: ifdef_loader_opts,
                }]
            }
        ],
    },

相关问题