1. yarn global add webpack@4 webpack-cli@3
  2. npm install 改成yarn add

Live loading

use webpack-dev-server

install

npm install webpack-dev-server —save-dev


Use

use script (Recommend)

  1. "scripts": {
  2. "start": "webpack-dev-server",
  3. },

or
use cli

  1. npx webpack serve

Loader VS Plugin


babel-loader
css-loader

使用scss
install

  1. yarn add sass-loader dart-sass --dev

node sass过时了,优先使用dart sass

Lazy load 懒加载

  1. const button = document.createElement('button')
  2. button.innerText = 'lazy load'
  3. button.onClick = ()=>{
  4. const promise = import('./lazy')
  5. promise.then( (module)=>{
  6. const fn = module.default
  7. fn()
  8. }, ()=>{
  9. console.log('lazy load error')
  10. }
  11. )
  12. }

使用import就可以实现懒加载
关键字import可以像调用函数一样来动态的导入模块。以这种方式调用,将返回一个 promise。

CSS-loader VS Style-loader
css-loader 负责解析css
style-loader 创建style标签,添加到dom