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
Javascript 如何使用webpack4正确拆分常见依赖项_Javascript_Webpack_Webpack 4 - Fatal编程技术网

Javascript 如何使用webpack4正确拆分常见依赖项

Javascript 如何使用webpack4正确拆分常见依赖项,javascript,webpack,webpack-4,Javascript,Webpack,Webpack 4,我在配置webpack4以正确绑定共享依赖项时遇到困难 我的申请有两页(第1页和第2页)。两者都需要bootstrap,jquery以及名为core的定制JavaScript应用程序 第2页需要相同但也是一个名为myapp的自定义JavaScript应用程序,以及lodash 由于我的core应用程序将包含在所有页面中,因此我希望将jquery和bootstrap放在同一个捆绑包中 由于lodash仅对运行my app的页面是必需的,因此我希望将该依赖项包含在my app捆绑包中 因此,我将我的

我在配置webpack4以正确绑定共享依赖项时遇到困难

我的申请有两页(第1页和第2页)。两者都需要
bootstrap
jquery
以及名为
core
的定制JavaScript应用程序

第2页需要相同但也是一个名为
myapp
的自定义JavaScript应用程序,以及
lodash

由于我的
core
应用程序将包含在所有页面中,因此我希望将
jquery
bootstrap
放在同一个捆绑包中

由于
lodash
仅对运行
my app
的页面是必需的,因此我希望将该依赖项包含在
my app
捆绑包中

因此,我将我的应用程序设置为:

webpack.config.js

const path = require('path');
const webpack = require('webpack');


module.exports = {
  entry: {
    'core': './src/core/index.js',
    'my-app': './src/my-app/index.js'
  },
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
  resolve: {
    alias: {
      jquery: 'jquery/src/jquery',
    }
  },
  plugins: [
    new webpack.ProvidePlugin({
      $: "jquery",
      jQuery: "jquery"
    }),
  ],
  mode: 'development',
}
page1.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Page1</title>

    <script src="dist/core.bundle.js"></script>
  </head>
  <body>
    <h1>Page1</h1>
    <span id="test"></span>
  </body>
  <script>
    $(document).ready(function() {
      $('#test').text('jQuery Works!');
    });
  </script>
</html>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Page1</title>

    <script src="dist/core.bundle.js"></script>
    <script src="dist/my-app.bundle.js"></script>

  </head>
  <body>
    <h1>Page2</h1>
    <span id="test"></span>
  </body>
  <script>
    $(document).ready(function() {
      $('#test').text('jQuery Works!');
    });
  </script>
</html>

第1页
第1页
$(文档).ready(函数(){
$('#test').text('jqueryworks!');
});
page2.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Page1</title>

    <script src="dist/core.bundle.js"></script>
  </head>
  <body>
    <h1>Page1</h1>
    <span id="test"></span>
  </body>
  <script>
    $(document).ready(function() {
      $('#test').text('jQuery Works!');
    });
  </script>
</html>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Page1</title>

    <script src="dist/core.bundle.js"></script>
    <script src="dist/my-app.bundle.js"></script>

  </head>
  <body>
    <h1>Page2</h1>
    <span id="test"></span>
  </body>
  <script>
    $(document).ready(function() {
      $('#test').text('jQuery Works!');
    });
  </script>
</html>

第1页
第2页
$(文档).ready(函数(){
$('#test').text('jqueryworks!');
});
(完整项目:)

当我运行
npxwebpack
时,它会创建
core.bundle.js
myapp.bundle.js
,但是这两个都包括
jquery


是否可以将所有“全局”依赖项放在
core.bundle.js
中?

这里只需要记住一件事,在webpack 4中,您不需要将供应商脚本作为条目添加到webpack.config中,只需将真正的条目脚本添加到应用程序中。 WP将使用默认设置为您的应用程序创建优化的捆绑输出

您必须将供应商缓存组添加到配置中,以便将
jQuery、Lodash、Bootstrap、Popper
提取到单独的包中:

 optimization: {
    splitChunks: {
      cacheGroups: {       
        vendor: {
          test: /node_modules/,
          name: "vendor",
          chunks: "all", 
          enforce: true
        }
      }
    }
  },

这里需要记住的一件事是,使用webpack 4,您不会将供应商脚本作为条目添加到webpack.config中,而是将真正的条目脚本添加到应用程序中。 WP将使用默认设置为您的应用程序创建优化的捆绑输出

您必须将供应商缓存组添加到配置中,以便将
jQuery、Lodash、Bootstrap、Popper
提取到单独的包中:

 optimization: {
    splitChunks: {
      cacheGroups: {       
        vendor: {
          test: /node_modules/,
          name: "vendor",
          chunks: "all", 
          enforce: true
        }
      }
    }
  },

查看供应商/清单优化的公共区块查看供应商/清单优化的公共区块