[Webpack 2] Grouping vendor files with the Webpack CommonsChunkPlugin

Often, you have dependencies which you rarely change. In these cases, you can leverage the CommonsChunkPlugin to automatically put these modules in a separate bundled file so they can be cached and loaded separately from the rest of your code (leveraging the browser cache much more effectively).

The libaraies like ‘lodash‘, ‘jquery‘ are required alomost all the projects and once download, rarly change any more. So it would be a good idea to spreate those common libaries into a common vendor file.

  entry: {
    app: ‘./js/app.js‘,
    vendor: [‘lodash‘, ‘jquery‘],
  },

So rename the entry, add ‘app‘ and ‘vendor‘ entries.

So the output file canbe named like ‘bundle.app.js‘ and ‘bundle.vendor.js‘:

  output: {
    filename: ‘bundle.[name].js‘,
    path: resolve(__dirname, ‘dist‘),
    pathinfo: true,
  },

We will use webpack build in CommonsChunkPlugin:

  plugins: [
    isTest ? undefined : new webpack.optimize.CommonsChunkPlugin({
      name: ‘vendor‘,
    }),
  ].filter(p => !!p),

Now we can include those two bundle files into index.html:

    <script src="/bundle.vendor.js"></script>
    <script src="/bundle.app.js"></script>
时间: 2024-10-03 22:54:06

[Webpack 2] Grouping vendor files with the Webpack CommonsChunkPlugin的相关文章

[Webpack 2] Maintain sane file sizes with webpack code splitting

As a Single Page Application grows in size, the size of the payload can become a real problem for performance. In this lesson, learn how to leverage code splitting to easily implement lazy loading for your application to load only the code necessary

[Webpack 2] Expose modules to dependencies with Webpack

When you have a dependency that has dependencies on global variables (like jQuery or lodash) or assumes that this is bound to window, you can use the imports-loaderto provide those dependencies explicitly. In case there is one dependency that we use

webpack简单教程(1)--从零开始搭建一个webpack小例子

基于Windows操作系统下搭建webpack的运行环境,这里采用npm环境,node.js官网下载最新版本的, 这里忽略下载和安装,然后直接运行  Node 环境,如下图所示. 检查一下,node版本和npm工具的版本确保是最新的,这里并不是最新版本的, 如果版本都是最新的,那么就可以来直接搭建webpack一个小demo案例. 1.在F盘中创建一个文件夹webpack 2.在文件夹中创建index.html <!DOCTYPE html> <html lang="en&qu

webpack 4.x 解决 webpack-dev-server工具在webpack构建的项目中使用问题

首先将webpack-dev-server安装到项目中 npm install webpack-dev-server -D 这时在powershell中敲 webpack-dev-server 会发现 'webpack-dev-server' 不是内部或外部命令,也不是可运行的程序 或批处理文件. 这是由于该工具只是安装到本地项目中了 纳尼? 难道要全局安装一下吗??? 没必要!!!这时只需在根目录的package.json中"scripts"下中添加 "dev":

React 不暴露webpack配置的情况下,修改webpack配置

需要安装[email protected] 需要babel-plugin-import 扩展react里面的webpack配置,新建config-overrides.js const {injectBabelPlugin} = require('react-app-rewired'); module.exports = function override(config, env){ config = injectBabelPlugin([ 'import', {libraryName: 'ant

[Webpack 2] Import a non-ES6 module with Webpack

When you have a dependency that does not export itself properly, you can use the exports-loader to force it to export the pieces of the file that you need. Install: npm i -D exports-loader Add exports-loader to the module you want: module: { loaders:

[webpack]手写一个mvp版本的webpack

let fs = require('fs'); let path = require('path'); let babylon = require('babylon'); // Babylon 把源码转换为AST let t = require('@babel/types'); // @babel-types 替换节点 let traverse = require('@babel/traverse').default; // @babel-traverse 遍历节点 let generator

gulp &amp; webpack整合

为什么需要前端工程化? 前端工程化的意义在于让前端这个行业由野蛮时代进化为正规军时代,近年来很多相关的工具和概念诞生.好奇心日报在进行前端工程化的过程中,主要的挑战在于解决如下问题:? 如何管理多个项目的前端代码?? 如何同步修改复用代码?? 如何让开发体验更爽? 项目实在太多 之前写过一篇博文 如何管理被多个项目引用的通用项目?,文中提到过好奇心日报的项目偏多(PC/Mobile/App/Pad),要为这么多项目开发前端组件并维护是一个繁琐的工作,并且会有很多冗余的工作. 更好的管理前端代码

有了它们就能更好的理解webpack了

最新内容,请在github阅读.同时,All issue and star welcomed! 1.获取webpack配置的输出配置 compilation.outputOptions 其中该对象有如下内容: { path: 'builds', filename: 'bundle.js', publicPath: 'builds/', chunkFilename: '[id].bundle.js', library: '', hotUpdateFunction: 'webpackHotUpdat