webpack入门

使用前先安装nodejs

1.打包一个单独的js文件

新建一个hello.js文件

/**
 * Created by kongwc on 2017/8/17.
 */

function hello() {
    alert("hello,Mr.Kong");
}

打包使用命令  webpack hello.js hello.bundle.js

hello.bundle.js是打包后的文件名,可以随意取

执行命令后,会生成webpack hello.js hello.bundle.js

2.打包引用的文件

若引用css文件,需要安装install css-loader 和style-loader,由于国外的服务比较慢,所以建议使用国内阿里的npm服务器,命令为

npm install css-loader style-loader --registry=http://registry.npm.taobao.org

为避免每次输入,可以修改配置

npm configset registry http://registry.npm.taobao.org

hello.js改为

/**
 * Created by kongwc on 2017/8/17.
 */
require('./world');
require('style-loader!css-loader!./cssTest.css')
function hello() {
    alert("hello,Mr.Kong");
}

hello();
cssTest.css为

html,body{
    background-color: aquamarine;
}

为了查看效果,可以新建一个html文件,引入打包后的hello.bundle.js

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="./hello.bundle.js"></script>
</head>
<body>

</body>
</html>
为方便引入css文件,可以使用webpack参数

webpack hello.js hello.bundle.js --module-bind 'css=style-loader!css-loader!'

3,为防止每次修改文件都要输入打包命令,可以使用 --watch

webpack hello.js hello.bundle.js --module-bind 'css=style-loader!css-loader!' --watch



{
  "name": "vuejs",
  "version": "1.0.0",
  "description": "vue_webpack",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
    "webpack" : "webpack --watch"
  },
  "author": "kongwc",
  "license": "ISC"
}

npm run webpack






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

裂魂人1214

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值