[webpack] ch7. Libraries Codes Splitting

Libraries Codes Splitting

  1. npm init -y으로 package.json 생성
  2. npm명령어로 loader&plugin 설치

Bundling libraries

  1. npm init -y로 package.json 생성
  2. 아래 명령어를 통해 라이브러리 설치
    npm install webpack --save-dev
    npm install mement lodash --save
    npm install webpack-manifest-plugin --save-dev
    
  3. index.html 추가 ```html <!DOCTYPE html>
Libraries Code Splitting

Libraries Code Splitting

not yet loaded

not yet loaded

4. `app/index.js` 추가
```javascript
var moment = require('moment');
var _ = require('lodash');
var ele = document.querySelectorAll('p');

document.addEventListener("DOMContentLoaded", function(event) {
  ele[0].innerText = moment().format();
  ele[1].innerText = _.drop([1, 2, 3], 0);
});
  1. webpack.config.js 추가 ```javascript var webpack = require(‘webpack’); var path = require(‘path’);

module.exports = { entry: { main: ‘./app/index.js’, vendor: [ ‘moment’, ‘lodash’ ] }, output: { filename: ‘[name].js’, path: path.resolve(__dirname, ‘dist’) } }

- optional
```javascript
// 1 (webpack4부터 없어짐)
plugins: [
  new webpack.optimize.CommonsChunkPlugin({
    name: 'vendor' // Specify the common bundle's name.
  }),
]

// 2 (webpack4부터 없어짐)
plugins: [
  new webpack.optimize.CommonsChunkPlugin({
    names: ['vendor', 'manifest'] // Extract the webpack bootstrap logic into manifest.js
  }),
]

// 3
new ManifestPlugin({
  fileName: 'manifest.json',
  basePath: './dist/'
})

// CommonChunkPlugin기능을 쓰려면 아래와 같은 코드를 Plugin이 아닌 module에 삽입해야함
optimization: {
  splitChunks: {
    cacheGroups: {
      vendor: {
        chunks: 'initial',
        name: 'vendor',
        enforce: true,
      },
    },
  },
  runtimeChunk: {
    name: 'manifest',
  }
},