Webpack-TypeError:callbacks [i]不是函数

时间:2019-07-09 11:29:49

标签: javascript node.js webpack

当我捆绑拆分webpack供应商(node_modules)时,我的main.js文件将输出以下消息。当我禁用捆绑拆分时,它可以工作。但是,当我捆绑split并在console.log模块(responseMenu)时,它会抱怨回调。我正在使用webpack捆绑程序加载器创建我的代码拆分捆绑程序。

我的dist文件夹通常如下所示

  • main.js
  • bundle.1.js
  • bundle.2.js
  • bundle.x.js等

enter image description here

下面是我的webpack文件,位于main.js和响应Menu.js的顶部,我的babelrc.js也存在

babelrc.js

  api.cache(true);

  const presets = [['@babel/preset-env',  {
        "targets": {
          "esmodules": true
        }
      }]];
  const plugins = ['@babel/plugin-proposal-export-default-from', '@babel/plugin-transform-runtime'];

  return {
    presets,
    plugins,
  };
};

Webpack配置

const webpack = require('webpack');
const minimatch = require('minimatch');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const StyleLintPlugin = require('stylelint-webpack-plugin');
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const ImageminPlugin = require('imagemin-webpack-plugin').default;
const TerserPlugin = require('terser-webpack-plugin');
const LiveReloadPlugin = require('webpack-livereload-plugin');
const globImporter = require('node-sass-glob-importer');
const CopyPlugin = require('copy-webpack-plugin');
const HappyPack = require('happypack');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const PhpManifestPlugin = require('webpack-php-manifest');
const phpManifest = new PhpManifestPlugin();

const devMode = 'development' === process.env.NODE_ENV;

module.exports = {
  resolve: {
    alias: {
      assets: path.resolve(__dirname, 'assets'),
      fonts: path.resolve(__dirname, 'assets/fonts'),
      modernizr$: path.resolve(__dirname, './.modernizrrc')
    }
  },
  mode: process.env.NODE_ENV,
  entry: {
    'js/main': './assets/js/scripts/main.js',
    'css/style': './assets/scss/style.scss'
  },
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: '[name].js',
    chunkFilename: 'js/bundle.[id].js',
    publicPath: 'wp-content/themes/strafe-boilerplate/dist/'
  },
  devtool: devMode ? 'source-map' : 'cheap-eval-source-map',
  performance: {
    maxAssetSize: 1000000
  },
  optimization: {
    minimize: !devMode,
    removeAvailableModules: !devMode,
    removeEmptyChunks: !devMode,
    splitChunks: !devMode ? {
      maxInitialRequests: Infinity,
      minSize: 0,
      chunks: 'all',
      cacheGroups: {
        default: false,
        vendors: false,
        name: 'vendors',
        vendors: {

          test: /node_modules/,
          name(module, chunks, cacheGroupKey) {
            const moduleFileName = module
              .identifier()
              .split('/')
              .reduceRight(item => item);
            const allChunksNames = chunks.map(item => item.name).join('');
            let name = `${cacheGroupKey}-${allChunksNames}-${moduleFileName}`;
            return name;
          }
        },

      }
    } : false,
    minimizer: !devMode ? [
      new TerserPlugin({
        sourceMap: !devMode,
        cache: !devMode,
        parallel: true
      })
    ] : []
  },
  stats: {
    assets: !devMode,
    builtAt: !devMode,
    children: false,
    chunks: false,
    colors: true,
    entrypoints: !devMode,
    env: false,
    errors: !devMode,
    errorDetails: false,
    hash: false,
    maxModules: 20,
    modules: false,
    performance: !devMode,
    publicPath: false,
    reasons: false,
    source: false,
    timings: !devMode,
    version: false,
    warnings: !devMode
  },
  module: {
    rules: [
      !devMode ? 
      {
        test: /\.js$/,
        exclude: /(node_modules|bower_components)/,
        include: path.resolve(__dirname, 'assets/js'),
        use: ['happypack/loader', {
          loader : 'bundle-loader'
        }]
      } : {
         test: /\.js$/,
        exclude: /(node_modules|bower_components)/,
        include: path.resolve(__dirname, 'assets/js'),
        use: ['happypack/loader']
      },
      {
        test: /\.(sa|sc|c)ss$/,
        exclude: /(node_modules|bower_components)/,
        include: path.resolve(__dirname, 'assets/scss'),
        use: [
          MiniCssExtractPlugin.loader,

          {
            loader: 'css-loader',
            options: {
              sourceMap: true,
              url: true
            }
          },

          {
            loader: 'postcss-loader',
            options: {
              sourceMap: true
            }
          },
          {
            loader: 'resolve-url-loader',
            options: {
              engine: 'postcss',
              root: path.resolve(__dirname, 'assets')
            }
          },
          {
            loader: 'sass-loader',
            options: {
              sourceMap: true,
              importer: globImporter(),
              outputPath: 'css',
              debug: true
            }
          }
        ]
      },
      {
        test: /\.(png|jpg|gif)$/,
        include: path.resolve(__dirname, 'assets/img'),
        use: [
          {
            loader: 'file-loader',
            options: {
              outputPath: 'img',
              name: '[folder]/[name].[ext]',
              publicPath: '../img/',
              useRelativePath: true
            }
          }
        ]
      },
      {
        test: /\.(eot|woff|woff2|ttf)([\?]?.*)$/,
        include: path.resolve(__dirname, 'assets/fonts'),
        use: [
          {
            loader: 'file-loader',
            options: {
              outputPath: 'fonts',
              name: '[folder]/[name].[ext]',
              debug: true,
              publicPath: '../fonts/',
              useRelativePath: true
            }
          }
        ]
      },
      {
        test: /\.(svg)$/,
        use: [
          {
            loader: 'file-loader',
            options: {
              outputPath: 'img',
              name: '[folder]/[name].[ext]',
              publicPath: '../img/',
              useRelativePath: true
            }
          },
          {
            loader: 'svgo-loader',
            options: {
              plugins: [
                { removeTitle: false },
                { convertColors: { shorthex: false } },
                { convertPathData: false },
                { removeViewBox: false }
              ]
            }
          }
        ]
      },
      {
        test: /\.modernizrrc.js$/,
        use: ['modernizr-loader']
      },
      {
        test: /\.modernizrrc(\.json)?$/,
        use: ['modernizr-loader', 'json-loader']
      }
    ]
  },
  plugins: [
    new HappyPack({
      loaders: [
        {
           loader: 'babel-loader',
          options: {
            presets: [ '@babel/preset-react' ]
          }
        }
      ]
    }),
    new webpack.ProvidePlugin({
      'Promise': 'bluebird'
    }),
    phpManifest,
    devMode && new FriendlyErrorsPlugin({
        clearConsole: false
    }),
    new CleanWebpackPlugin(['dist'], {
      cleanStaleWebpackAssets: false,
    }),
    new MiniCssExtractPlugin({
      filename: '[name].css',
      chunkFilename: !devMode ? 'css/bundle.[id].css' : false
    }),
    !devMode && new ImageminPlugin({
      test: /\.(jpe?g|png|gif)$/i,
      cacheFolder: './imgcache'
    }),
    devMode && new LiveReloadPlugin(),
    new CopyPlugin([{ from: 'assets/img', to: 'img' }])
  ].filter(Boolean)
};

sensitiveMenu.js


function responsiveMenu() {
  // Responsive Menu

  // Setup Doc, call out functions
  function init() {
    const toggleTrigger = $('.mobile-toggle');
    const menuWrap = $('.menu-responsive');

    menuPositionSetup(menuWrap);
    triggerMenu(toggleTrigger, menuWrap);
    childItemsHandler();
  }

  function menuPositionSetup(menu) {
    let headerHeight = $('.header').outerHeight(true);
    $(menu).css('top', headerHeight);
    $(menu).css({ height: 'calc(100% - ' + headerHeight + 'px)' });
  }

  function triggerMenu(toggle, menu) {
    $(toggle).on('click', function(e) {
      mobileToggle($(this), menu);
    });
  }

  function mobileToggle(trigger, menu) {
    $(menu).toggleClass('is-active');
    $(trigger).toggleClass('is-active');
    $('body').toggleClass('is-overflow-h');
  }

  function childItemsHandler() {
    $('.menu-item-has-children > a').on('click', function(e) {
      if ($(this).hasClass('is-active')) {
        // Go to the link
      } else {
        // Open the sub menu
        $(this).addClass('is-active');
        $(this)
          .parent()
          .find('.sub-menu')
          .slideDown();
        e.preventDefault();
      }
    });
  }
};

export default responsiveMenu

main.js

import * as $ from 'jquery';
import responsiveMenu from './responsiveMenu';

console.log(responsiveMenu)

$(document).ready(function() {
  init();

});


function init() {
  responsiveMenu()
}

0 个答案:

没有答案