从无到有搭建前端项目

1.github创建项目,clone到本地

git clone "xxxxxxxx"
cd my_project

2.npm 初始化项目(入口文件写 src/index.js)

npm init 

在这里插入图片描述

3.创建几个文件夹

mkdir src build test doc example

4.新建 src/index.js 文件

alert('hello world')

5.安装webpack和babel插件

npm i babel-core babel-loader babel-polyfill babel-preset-es2015 babel-preset-latest webpack webpack-cli --save-dev

查看 package.json

{
  "name": "my_project",
  "version": "0.0.1",
  "description": "README",
  "main": "src/index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "xxxxxxxx/my_project.git"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.4",
    "babel-polyfill": "^6.26.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-latest": "^6.24.1",
    "webpack": "^4.7.0",
    "webpack-cli": "^3.1.2"
  }
}

6.新建 .babelrc 文件

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

7. 新建 webpack.config.js 文件,配置webpack

module.exports = {
    entry: "./src/index.js",
    output: {
        path: __dirname,
        filename: "./build/bundle.js"
    },
    module: {
        rules: [{
            test: /\.js?$/,
            exclude: /(node_modules)/,
            loader: "babel-loader"
        }]
    }
}

8. package.json 添加build脚本

"build": "webpack"

9. 装http-server服务

npm install http-server -g

10.package.json 添加启动服务命令

"start": "http-server -p 8080"

11.example文件夹中创建 index.html 文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>come</title>
</head>
<body>
    <p>hello, wo lai le</p>
    
    <script src="../release/bundle.js"></script>
</body>
</html>

12. 运行http服务

npm run start

13.浏览器查看运行结果

访问 http://localhost:8080/example/index.html 查看结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

叨槿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值