博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
webpack笔记一 起步
阅读量:5058 次
发布时间:2019-06-12

本文共 4524 字,大约阅读时间需要 15 分钟。

webpack笔记一 起步

安装

对于大多数项目,我们建议本地安装(--save-dev)。这可以在引入突破式变更(breaking change)版本时,更容易分别升级项目。

起步

初始化项目

mkdir webpack-project && cd webpack-projectnpm initnpm install webpack webpack-cli --save-dev

可能遇到npm ERR! Maximum call stack size exceeded的错误,尝试升级npm,然后执行npm cache clean --force即可。

我们还需要调整 package.json 文件,以便确保我们安装包是 private(私有的),并且移除 main 入口。这可以防止意外发布你的代码。

{    "name": "webpack-demo",    "version": "1.0.0",    "description": "",    "private": true,    "scripts": {        "test": "echo \"Error: no test specified\" && exit 1"    },    "keywords": [],    "author": "",    "license": "ISC",    "devDependencies": {        "webpack": "^4.30.0",        "webpack-cli": "^3.3.1"    }}

项目结构:

webpack-demo   |- package.json   |- /dist     |- index.html   |- /src     |- index.js

安装示例用的包,比如lodash

npm install --save lodash

src/index.js

import _ from 'lodash';function component() {    let element = document.createElement('div');    element.innerHTML = _.join(['Hello', 'webpack'], ' ');    return element;}document.body.appendChild(component());

dist/index.html

    
Webpack Demo

然后再命令行执行npx webapck

C:\Users\Jehorn\Work\learn\webpack-demo>npx webpackHash: 090d6ac02451c0b4b043Version: webpack 4.30.0Time: 3030msBuilt at: 2019-04-23 17:51:44  Asset      Size  Chunks             Chunk Namesmain.js  70.4 KiB       0  [emitted]  mainEntrypoint main = main.js[1] ./src/index.js 232 bytes {0} [built][2] (webpack)/buildin/global.js 472 bytes {0} [built][3] (webpack)/buildin/module.js 497 bytes {0} [built]    + 1 hidden moduleWARNING in configurationThe 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment.You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/concepts/mode/

浏览器打开index.html可以看到显示“Hello webpack”。

模块

注意,webpack 不会更改代码中除importexport语句以外的部分。如果使用其它ES6特性,需要添加babel之类的转译器(transpiler)。

使用配置文件 webpack.config.js

在项目根目录下增加webpack.config.js文件。

const path = require('path');module.exports = {    entry: './src/index.js',    output: {        filename: 'main.js',        path: path.resolve(__dirname, 'dist')    }};

执行打包:

如果webpack.config.js存在,则webpack命令将默认选择使用它。这里使用--config选项表示可以传递任何名称的配置文件。对于需要拆分成多个文件的复杂配置是非常有用的。

C:\Users\Jehorn\Work\learn\webpack-demo>npx webpack --config webpack.config.jsHash: d660fe3445f5b4b2318aVersion: webpack 4.30.0Time: 373msBuilt at: 2019-04-23 18:01:39  Asset      Size  Chunks             Chunk Namesmain.js  70.4 KiB       0  [emitted]  mainEntrypoint main = main.js[1] ./src/index.js 232 bytes {0} [built][2] (webpack)/buildin/global.js 472 bytes {0} [built][3] (webpack)/buildin/module.js 497 bytes {0} [built]    + 1 hidden moduleWARNING in configurationThe 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment.You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/concepts/mode/

使用 npm scripts

配置package.json文件,即可使用npm run build命令来打包程序:

{    "name": "webpack-demo",    "version": "1.0.0",    "description": "",    "private": true,    "scripts": {        "test": "echo \"Error: no test specified\" && exit 1",        "build": "webpack"    },    "keywords": [],    "author": "",    "license": "ISC",    "devDependencies": {        "webpack": "^4.30.0",        "webpack-cli": "^3.3.1"    },    "dependencies": {        "lodash": "^4.17.11"    }}

通过在npm run build命令和你的参数之间添加两个中横线,可以将自定义参数传递给webpack。

C:\Users\Jehorn\Work\learn\webpack-demo>npm run build> webpack-demo@1.0.0 build C:\Users\Jehorn\Work\learn\webpack-demo> webpackHash: d660fe3445f5b4b2318aVersion: webpack 4.30.0Time: 386msBuilt at: 2019-04-23 18:10:58  Asset      Size  Chunks             Chunk Namesmain.js  70.4 KiB       0  [emitted]  mainEntrypoint main = main.js[1] ./src/index.js 232 bytes {0} [built][2] (webpack)/buildin/global.js 472 bytes {0} [built][3] (webpack)/buildin/module.js 497 bytes {0} [built]    + 1 hidden moduleWARNING in configurationThe 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment.You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/concepts/mode/

The end... Last updated by: Jehorn, April 23, 2019, 6:14 PM

转载于:https://www.cnblogs.com/jehorn/p/10758043.html

你可能感兴趣的文章
jdk1.8 api 下载
查看>>
svn 图标不显示
查看>>
getElement的几中属性介绍
查看>>
iOS 使用Quartz 2D画虚线 【转】
查看>>
平面最接近点对
查看>>
HTML列表,表格与媒体元素
查看>>
PHP、Java、Python、C、C++ 这几种编程语言都各有什么特点或优点?
查看>>
感谢青春
查看>>
Jquery Uploadify4.2 falsh 实现上传
查看>>
雨林木风 GHOST_XP SP3 快速装机版YN12.08
查看>>
linux基础-命令
查看>>
java对象的深浅克隆
查看>>
Hadoop流程---从tpch到hive
查看>>
数据结构3——浅谈zkw线段树
查看>>
Introduction to my galaxy engine 2: Depth of field
查看>>
V2019 Super DSP3 Odometer Correction Vehicle List
查看>>
Python 3.X 练习集100题 05
查看>>
今时不同往日:VS2010十大绝技让VS6叹服
查看>>
设计器 和后台代码的转换 快捷键
查看>>
在线视频播放软件
查看>>