Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Webpack 网页包供应商LIB未拆分为单独的捆绑包_Webpack_Code Splitting_Webpack Plugin_Dllplugin - Fatal编程技术网

Webpack 网页包供应商LIB未拆分为单独的捆绑包

Webpack 网页包供应商LIB未拆分为单独的捆绑包,webpack,code-splitting,webpack-plugin,dllplugin,Webpack,Code Splitting,Webpack Plugin,Dllplugin,我正在尝试使用(v0.4.2)分离我的供应商和应用程序包。此软件包是一个顶级插件,位于webpack和的上方,用于在index.html中自动排序导入 这个插件应该做的是分离供应商库和应用程序代码。我可以用fromWebpack实现这一点,但这样每次重新编译时都会重新生成包。这比一次生成供应商包的效率要低,并且只有在库发生更改时才重新编译它 问题 我让这个插件“工作”(它正在输出一个vendor.bundle.js)。这里唯一的问题是,当我用(v2.13.1)检查app.bundle.js时。

我正在尝试使用(
v0.4.2
)分离我的供应商和应用程序包。此软件包是一个顶级插件,位于webpack和的上方,用于在
index.html
中自动排序导入

这个插件应该做的是分离供应商库和应用程序代码。我可以用fromWebpack实现这一点,但这样每次重新编译时都会重新生成包。这比一次生成供应商包的效率要低,并且只有在库发生更改时才重新编译它


问题

我让这个插件“工作”(它正在输出一个
vendor.bundle.js
)。这里唯一的问题是,当我用(
v2.13.1
)检查
app.bundle.js
时。我可以看到,
vendor.bundle.js
中的所有node_模块也被加载到这个bundle中,因此插件无法正常工作


版本

const merge = require('webpack-merge');
const webpack = require('webpack');
const path = require('path');
const HardSourceWebpackPlugin = require('hard-source-webpack-plugin');

const common = require('./webpack.common.js');

module.exports = merge(common, {
  mode: 'development',
  devtool: 'eval-source-map',
  devServer: {
    hot: true,
    contentBase: path.resolve(__dirname, 'build'),
    historyApiFallback: true, // Allow refreshing of the page
  },
  plugins: [
    // Enable hot reloading
    new webpack.HotModuleReplacementPlugin(),

    // Enable caching
    new HardSourceWebpackPlugin({
      cacheDirectory: '.cache/hard-source/[confighash]',
      configHash: function(webpackConfig) {
        return require('node-object-hash')({ sort: false }).hash(webpackConfig);
      },
      environmentHash: {
        root: process.cwd(),
        directories: [],
        files: ['package-lock.json'],
      },
      info: {
        mode: 'none',
        level: 'warn',
      },
    }),
  ],
});
const merge = require('webpack-merge');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const CleanWebpackPlugin = require('clean-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

const common = require('./webpack.common.js');

module.exports = merge(common, {
  mode: 'production',
  plugins: [
    // Clean the build folders
    new CleanWebpackPlugin(['build'], {
      root: process.cwd(), // Otherwise the 'webpack' folder is used
    }),

    //Make vendor bundle size visible
    new BundleAnalyzerPlugin({
      analyzerMode: 'static',
      openAnalyzer: false,
      reportFilename: 'stats/prod-report.html',
    }),
  ],
  optimization: {
    splitChunks: {
      chunks: 'all',
      cacheGroups: {
        vendor: {
          name: 'vendor',
          chunks: 'all',
          test: /[\\/]node_modules[\\/]/,
        },
      },
    },
    minimizer: [
      // Optimize minimization
      new UglifyJsPlugin({
        cache: true,
        parallel: true,
        uglifyOptions: {
          ecma: 6,
          mangle: true,
          toplevel: true,
        },
      }),
    ],
  },
});
我正在使用:

  • 网页包
    v4.11.0
  • 巴别塔加载器
    v7.1.4
  • 巴别塔核心
    v6.26.3
  • autodll网页包插件
    v0.4.2

项目结构

我的项目具有以下文档结构:

App
-- build //Here are the output bundles located
-- node_modules
-- public
  -- index.html
-- src // App code
-- webpack
  -- webpack.common.js
  -- webpack.dev.js
  -- webpack.prod.js
-- package.json 

我的webpack.common.js(此代码与开发和生产版本共享)

根据
autodll网页包插件的定义,应该使用上下文键来分隔。所以我认为这就是问题所在


文档描述您应该引用
webpack.config.js
所在的文件夹,但我有3个,我需要引用哪一个?我的文件夹名为
webpack
,而不是您在文档中看到的
config
这里也正确吗

所以最后我没有让DLL设置正常工作。在阅读了更多的内容后,我意识到,该网页的创建者建议改用,因为webpack将来可能会用作默认选项

在阅读了更多内容之后,我还意识到不建议在生产环境中使用DLL插件,因为您必须重新编译它(dev build添加了一些东西)。因此,您应该使用
硬源代码webpack插件
进行开发构建和生产

我让这两个人很容易地工作:

Webpack.dev.js

const merge = require('webpack-merge');
const webpack = require('webpack');
const path = require('path');
const HardSourceWebpackPlugin = require('hard-source-webpack-plugin');

const common = require('./webpack.common.js');

module.exports = merge(common, {
  mode: 'development',
  devtool: 'eval-source-map',
  devServer: {
    hot: true,
    contentBase: path.resolve(__dirname, 'build'),
    historyApiFallback: true, // Allow refreshing of the page
  },
  plugins: [
    // Enable hot reloading
    new webpack.HotModuleReplacementPlugin(),

    // Enable caching
    new HardSourceWebpackPlugin({
      cacheDirectory: '.cache/hard-source/[confighash]',
      configHash: function(webpackConfig) {
        return require('node-object-hash')({ sort: false }).hash(webpackConfig);
      },
      environmentHash: {
        root: process.cwd(),
        directories: [],
        files: ['package-lock.json'],
      },
      info: {
        mode: 'none',
        level: 'warn',
      },
    }),
  ],
});
const merge = require('webpack-merge');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const CleanWebpackPlugin = require('clean-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

const common = require('./webpack.common.js');

module.exports = merge(common, {
  mode: 'production',
  plugins: [
    // Clean the build folders
    new CleanWebpackPlugin(['build'], {
      root: process.cwd(), // Otherwise the 'webpack' folder is used
    }),

    //Make vendor bundle size visible
    new BundleAnalyzerPlugin({
      analyzerMode: 'static',
      openAnalyzer: false,
      reportFilename: 'stats/prod-report.html',
    }),
  ],
  optimization: {
    splitChunks: {
      chunks: 'all',
      cacheGroups: {
        vendor: {
          name: 'vendor',
          chunks: 'all',
          test: /[\\/]node_modules[\\/]/,
        },
      },
    },
    minimizer: [
      // Optimize minimization
      new UglifyJsPlugin({
        cache: true,
        parallel: true,
        uglifyOptions: {
          ecma: 6,
          mangle: true,
          toplevel: true,
        },
      }),
    ],
  },
});
webpack.prod.js

const merge = require('webpack-merge');
const webpack = require('webpack');
const path = require('path');
const HardSourceWebpackPlugin = require('hard-source-webpack-plugin');

const common = require('./webpack.common.js');

module.exports = merge(common, {
  mode: 'development',
  devtool: 'eval-source-map',
  devServer: {
    hot: true,
    contentBase: path.resolve(__dirname, 'build'),
    historyApiFallback: true, // Allow refreshing of the page
  },
  plugins: [
    // Enable hot reloading
    new webpack.HotModuleReplacementPlugin(),

    // Enable caching
    new HardSourceWebpackPlugin({
      cacheDirectory: '.cache/hard-source/[confighash]',
      configHash: function(webpackConfig) {
        return require('node-object-hash')({ sort: false }).hash(webpackConfig);
      },
      environmentHash: {
        root: process.cwd(),
        directories: [],
        files: ['package-lock.json'],
      },
      info: {
        mode: 'none',
        level: 'warn',
      },
    }),
  ],
});
const merge = require('webpack-merge');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const CleanWebpackPlugin = require('clean-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

const common = require('./webpack.common.js');

module.exports = merge(common, {
  mode: 'production',
  plugins: [
    // Clean the build folders
    new CleanWebpackPlugin(['build'], {
      root: process.cwd(), // Otherwise the 'webpack' folder is used
    }),

    //Make vendor bundle size visible
    new BundleAnalyzerPlugin({
      analyzerMode: 'static',
      openAnalyzer: false,
      reportFilename: 'stats/prod-report.html',
    }),
  ],
  optimization: {
    splitChunks: {
      chunks: 'all',
      cacheGroups: {
        vendor: {
          name: 'vendor',
          chunks: 'all',
          test: /[\\/]node_modules[\\/]/,
        },
      },
    },
    minimizer: [
      // Optimize minimization
      new UglifyJsPlugin({
        cache: true,
        parallel: true,
        uglifyOptions: {
          ecma: 6,
          mangle: true,
          toplevel: true,
        },
      }),
    ],
  },
});
我希望这对任何人都有帮助