jharris4 / html-webpack-tags-plugin

lets you define html tags to inject with html-webpack-plugin
MIT License
255 stars 31 forks source link

webpack@4.5 TypeError: Cannot read property 'tapAsync' of undefined #25

Closed luozhihua closed 6 years ago

luozhihua commented 6 years ago

webpack v4.5

TypeError: Cannot read property 'tapAsync' of undefined
    at onCompilation (/Users/colin/Projects/Outsource/sinostrong/im/client/node_modules/html-webpack-include-assets-plugin/index.js:415:63)
    at SyncHook.eval [as call] (eval at create (/Users/colin/Projects/Outsource/sinostrong/im/client/node_modules/tapable/lib/HookCodeFactory.js:17:12), <anonymous>:15:1)
    at SyncHook.lazyCompileHook [as _call] (/Users/colin/Projects/Outsource/sinostrong/im/client/node_modules/tapable/lib/Hook.js:35:21)
    at Compiler.newCompilation (/Users/colin/Projects/Outsource/sinostrong/im/client/node_modules/webpack/lib/Compiler.js:436:26)
    at hooks.beforeCompile.callAsync.err (/Users/colin/Projects/Outsource/sinostrong/im/client/node_modules/webpack/lib/Compiler.js:472:29)
    at _err0 (eval at create (/Users/colin/Projects/Outsource/sinostrong/im/client/node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:11:1)
    at compiler.hooks.beforeCompile.tapAsync (/Users/colin/Projects/Outsource/sinostrong/im/client/node_modules/webpack/lib/DllReferencePlugin.js:51:13)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/colin/Projects/Outsource/sinostrong/im/client/node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:7:1)
    at AsyncSeriesHook.lazyCompileHook [as _callAsync] (/Users/colin/Projects/Outsource/sinostrong/im/client/node_modules/tapable/lib/Hook.js:35:21)
    at Compiler.compile (/Users/colin/Projects/Outsource/sinostrong/im/client/node_modules/webpack/lib/Compiler.js:467:28)
    at compiler.hooks.watchRun.callAsync.err (/Users/colin/Projects/Outsource/sinostrong/im/client/node_modules/webpack/lib/Watching.js:77:18)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/colin/Projects/Outsource/sinostrong/im/client/node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:33:1)
    at AsyncSeriesHook.lazyCompileHook [as _callAsync] (/Users/colin/Projects/Outsource/sinostrong/im/client/node_modules/tapable/lib/Hook.js:35:21)
    at Watching._go (/Users/colin/Projects/Outsource/sinostrong/im/client/node_modules/webpack/lib/Watching.js:40:32)
    at Watching.compiler.readRecords.err (/Users/colin/Projects/Outsource/sinostrong/im/client/node_modules/webpack/lib/Watching.js:32:9)
    at Compiler.readRecords (/Users/colin/Projects/Outsource/sinostrong/im/client/node_modules/webpack/lib/Compiler.js:340:11)

    // Webpack 4+
    if (compilation.hooks) {

      //   Error throws out at here:
      compilation.hooks.htmlWebpackPluginBeforeHtmlGeneration.tapAsync(
        'htmlWebpackIncludeAssetsPlugin',
        onBeforeHtmlGeneration,
      );
      compilation.hooks.htmlWebpackPluginAlterAssetTags.tapAsync(
        'htmlWebpackIncludeAssetsPlugin',
        onAlterAssetTag,
      );
    } else {
      // Webpack 3
      compilation.plugin('html-webpack-plugin-before-html-generation', onBeforeHtmlGeneration);
      compilation.plugin('html-webpack-plugin-alter-asset-tags', onAlterAssetTag);
    }
luozhihua commented 6 years ago

I moved html-webpack-include-assets-plugin after html-webpack-plugin was works now.

yoasia commented 6 years ago

Hi :) I have same problem. In which file did you moved this line?

gairon commented 6 years ago

@yoasia just place using of HtmlWebpackPlugin above usgin HtmlWebpackIncludeAssetsPlugin in your config.plugins.

zenoven commented 6 years ago

same issue, but the solution move HtmlWebpackIncludeAssetsPlugin after HTMLWebpackPlugin is not working for me,

I have multiple HtmlWebpackIncludeAssetsPlugin instances

const webpack = require('webpack')
const CleanWebpackPlugin = require('clean-webpack-plugin')
const path = require('path')
const HTMLWebpackPlugin = require("html-webpack-plugin")
const glob = require('glob')
const HappyPack = require('happypack')
const os = require('os')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
const HtmlWebpackIncludeAssetsPlugin = require('html-webpack-include-assets-plugin')

const root = path.join(__dirname, '../')
const srcPath = path.join(root, 'client')
const distPath = path.join(root, 'dist/client')
const env = process.env.NODE_ENV || 'development'
// webpack 4 mode只有 production/development/none 其他取值会报错
const mode = env === 'production' ? 'production' : 'development'
const entry = {}
const plugins = []
const happyThreadPool = HappyPack.ThreadPool({ size: os.cpus().length });

glob.sync('views/**/*.js', {cwd: srcPath})
  .forEach( (filePath) => {
    let chunk = filePath.slice(0 , path.extname(filePath).length * -1)
    entry[chunk] = [`./${chunk}`]
  } )

glob.sync('views/**/*.html', {cwd: srcPath})
  .forEach( (filePath) => {
    let chunk = filePath.slice(0, path.extname(filePath).length * -1)

    plugins.push(new HTMLWebpackPlugin({
      filename: filePath,
      template: filePath,
      minify: false,
      inject: false,
      chunks: entry[chunk] ? [ chunk ] : []
    }))
  })

plugins.push(new HtmlWebpackIncludeAssetsPlugin({
  assets: [
    {
      path: 'client/',
      globPath: distPath,
      glob: 'vendors*.js'
    }
  ],
  append: false
}),)

const config = {
  mode: mode,
  entry: entry,
  context: srcPath,
  devtool: 'cheap-module-source-map',
  output: {
    ...
  },
  module: {
    rules: [
      ...
    ]
  },
  plugins: [
    new HappyPack({
      id: 'js',
       ...
    }),
    new HappyPack({
      id: 'style',
      ...
    }),
    new webpack.HotModuleReplacementPlugin(),
    new ExtractTextPlugin({
      allChunks: true,
      filename: '[name].css'
    }),
  ].concat(plugins),

  resolve: {
    modules: [
      srcPath,
      'node_modules'
    ],
    extensions: ['.js', '.jsx', '.json', '.less']
  },

  optimization: {
    splitChunks: {
      chunks: 'all',
      minSize: 30000,
      maxSize: 0,
      minChunks: 2,
      maxAsyncRequests: 5,
      maxInitialRequests: 3,
      automaticNameDelimiter: '-',
      name: true,
      cacheGroups: {
        plugins: {
          test: /[\\/]node_modules[\\/]/,
          priority: -10
        },
        default: {
          minChunks: 2,
          priority: -20,
          reuseExistingChunk: true
        }
      }
    }
  }
}

module.exports = config
viatoswelz commented 5 years ago

I moved html-webpack-include-assets-plugin after html-webpack-plugin was works now.

same problem here. and changing order did not solve the problem in my case

eytienne commented 5 years ago

@viatoswelz did you solve the problem ?

FarahZaqout commented 5 years ago

This is an issue with any two plugins not interacting properly with each other.

Make sure there are no typos in your import/require statements and then that you call all your plugins in the right order. Then it should work fine.

JohnAllen commented 5 years ago

What is the right order?

kmvan commented 5 years ago

"html-webpack-plugin": "^3.2.0", This version all OK, but not @next

jharris4 commented 5 years ago

The full test suite for this plugin is run and passes with both versions of html-webpack-plugin so if you’re having issues it’s likely something to do with your webpack config.

You might want to read the section in the README.md about plugin ordering.

jahooma commented 5 years ago

For what it's worth, I had this error, and solved it by (what else) deleting my node_modules folder, package.json & running npm install.