1. 创建 demo 目录
mkdir demo
2. 初始化应用
npm init
工程信息
package name : tetris-class-demo
version:
description: tetris game
entry point:
test command:
git repository:
keywords:tetris, game
author: zonehoo
license: MIT
3.在 demo 下 创建 src , webpack 文件夹 .babelrc (编译代码配置) index.html 文件 (入口页面)
mkdir src webpack
touch .babelrc index.html
webpack 文件夹下 创建 build.js (webpack 打包用) develop.js (调试用)
touch webpack/build.js webpack/develop.js
src 文件夹下 创建 index.js(js 入口文件)
touch src/index.js
4. 编写 .babelrc
{
"presets": [
[
"env",
{
"targets": {
"browsers": ["last 2 versions","ie >= 10"]
},
"debug": false
}
]
],
"plugins": [
"transform-class-properties",
"transform-decorators-legacy",
[
"transform-react-jsx",
{
"pragma": "React.createElement"
}
],
[
"transform-runtime",
{
"helpers": true,
"polyfill": true,
"regenerator": true,
"moduleName": "babel-runtime"
}
]
]
}
5. 安装 webpack 和 babel 编译工具 并 添加 webpack 指令
编写 package.json
#package.json
{
"name": "tetris-class-demo",
"version": "1.0.0",
"description": "tetris game",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --config ./webpack/develop.js --hot --inline",
"build": "webpack --color --config ./webpack/build.js"
},
"keywords": [
"tetris",
"game"
],
"author": "zonehoo",
"license": "MIT",
"dependencies": {
"shortid": "^2.2.12",
"webpack-require-http": "^0.4.3"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-loader": "^7.1.5",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-decorators-legacy": "^1.3.5",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",
"css-loader": "^1.0.0",
"html-webpack-plugin": "^3.2.0",
"less": "^3.7.1",
"less-loader": "^4.1.0",
"style-loader": "^0.21.0",
"svg-inline-loader": "^0.8.0",
"webpack": "^3.10.0",
"webpack-bundle-analyzer": "^2.13.1",
"webpack-cli": "^3.0.8",
"webpack-dev-server": "^2.11.2"
}
}
安装拓展包
npm install
6. 编写 webpack/build.js 文件
var path = require('path')
var webpack = require('webpack')
var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
var UglifyJSPlugin = webpack.optimize.UglifyJsPlugin
module.exports = {
entry: path.resolve(__dirname, '../src/index.js'),
output: {
path: path.resolve(__dirname, '../.package'),
filename: 'bundle.min.js'
},
externals: {
react: 'React',
'react-dom': 'ReactDOM'
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: [
'babel-loader'
]
},
{
test: /\.css$/,
exclude: /node_modules/,
loader: 'style-loader!css-loader'
},
{
test: /\.less$/,
exclude: /node_modules/,
loader: 'style-loader!css-loader!less-loader'
},
{
test: /\.svg$/,
loader: 'svg-inline-loader'
}
]
},
plugins: [
new UglifyJSPlugin({
mangle: {
// Skip mangling these
except: ['exports', 'require']
},
sourceMap: true
}),
new BundleAnalyzerPlugin({
analyzerMode: 'static',
reportFilename: 'BundleReportRelease.html',
logLevel: 'info'
})
]
}
8. 编写 webpack/develop.js
var path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
entry: path.resolve(__dirname, '../src/index.js'),
output: {
path: path.resolve(__dirname, './'),
filename: 'index.js',
publicPath: '/'
},
devtool: 'inline-source-map',
externals: {
react: 'React',
'react-dom': 'ReactDOM'
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader'
},
{
test: /\.css$/,
exclude: /node_modules/,
loader: 'style-loader!css-loader'
},
{
test: /\.less$/,
exclude: /node_modules/,
loader: 'style-loader!css-loader!less-loader'
},
{
test: /\.svg$/,
loader: 'svg-inline-loader'
}
]
},
plugins: [
new HtmlWebpackPlugin({
filename: 'index.html',
template: './index.html'
})
],
devServer: {
hot: true,
port: 9000
}
}
9 写首页 index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Demo</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div id='app'></div>
</body>
</html>
10. 引入 MVC
在 src 下 创建 view.js controller.js model.js
touch src/view.js src/controller.js src/model.js
创建 游戏 运行 js tetris.js
touch src/tetris.js
11. 编写 index.js
alert(1);
12. 使用 npm start 并访问 127.0.0.1:9000 弹出1即可
npm start