为了账号安全,请及时绑定邮箱和手机立即绑定

webpack建立一个前端项目

标签:
Html5 webpack

首先执行npm init:

然后一路回车,生成package.json

npm install -g cnpm --registry=https://registry.npm.taobao.org

sudo cnpm install webpack webpack-cli --save-dev

安装好webpack和webpack -cli

然后在项目根目录下新建src,在src底下建立index.js

在项目根目录下新建文件webpack.dev.config.js

module.exports = {
    entry: './src/index.js',
    output: {
        path: __dirname,
        filename: './release/bundle.js'  // release 会自动创建
    },
}

然后在package.json的scripts下增加:

"dev":"webpack --config ./webpack.dev.config.js --node development"

这时候package.json是

{
  "name": "fontend2",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev":"webpack --config ./webpack.dev.config.js --node development"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "webpack": "^4.29.6",
    "webpack-cli": "^3.3.0"
  }
}

然后在项目根目录下运行npm run dev:

https://img1.sycdn.imooc.com//5c9721e80001e61411440602.jpg

https://img1.sycdn.imooc.com//5c9721fb0001427c15240590.jpg

在项目的根目录生成release文件夹表示运行成功

如果想在浏览器中看到效果:

执行:

sudo cnpm install webpack-dev-server html-webpack-plugin --save-dev

然后在根目录下新建index.html:

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>慕课网-nick老师的前端设计模式</title>
</head>
<body>
    慕课网-nick老师的前端设计模式
</body>
</html>

修改webpack.dev.config.js的配置:

const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
    entry: './src/index.js',
    output: {
        path: __dirname,
        filename: './release/bundle.js'  // release 会自动创建
    },
    plugins:[
    new HtmlWebpackPlugin({
    template:'./index.html'
    })
    ],
    devServer:{
    contentBase: path.join(__dirname,'./release'),
    open:true,
    port:9000
    }
}

然后修改package.json的配置:

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "webpack-dev-server --config ./webpack.dev.config.js --mode development"
  },

在根目录下执行npm run dev:

https://img1.sycdn.imooc.com//5c972b290001ded011460568.jpg

就可以自动打开浏览器看到内容了

然后安装babel

cnpm install babel-core babel-loader babel-polyfill babel-preset-es2015 babel-preset-latest --save-dev

在安装运行过程中发现一个问题:

https://img1.sycdn.imooc.com//5c97330f00017af611280586.jpg

这个babel-loader版本不对,建议安装7.x的版本,所以我运行了

cnpm i babel-loader@7.1.5 -D

然后在项目根目录下建立.babelsrc

{
"presets":["es2015","lastest"],
"plugins":[]
}

然后在webpack.dev.config.js那里配置babel:

 module:{
    rules:[{
    test:/\.js?$/,
    exclude:/(node_modules)/,
    loader:'babel-loader'
    }
    ]
    },

然后在index.js那里我们引入ex6语法:

class Person{
constructor(name) {
this.name = name
}
getName() {
return this.name
}
}
let p = new Person("nick老师")
alert(p.getName())

然后在项目根目录下运行npm run dev:

项目成功运行起来了

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
全栈工程师
手记
粉丝
6508
获赞与收藏
303

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 1
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消