{
// …
“scripts”: {
“build”: “webpack --mode=development --config script/webpack.config.js”
},
}
然后根目录终端输入:npm run build
在浏览器中打开 dist
目录下的 index.html
,如果一切正常,你应该能看到以下文本:'React'
index.html
目前放在 dist
目录下,但它是手动创建的,下面会教你如何生成 index.html
而非手动编辑它。
Webpack 核心功能
Babel
$ npm install @babel/cli @babel/core babel-loader @babel/preset-env --save-dev
script/webpack.config.js
module.exports = {
// …
module: {
rules: [
{
test: /.(js|jsx)$/,
loader: “babel-loader”,
exclude: /node_modules/,
},
],
},
};
.babelrc
在根目录下添加 .babelrc
文件:
{
“presets”: [“@babel/preset-env”, “@babel/preset-react”]
}
样式
$ npm install style-loader css-loader less less-loader --save-dev
script/webpack.config.js
module.exports = {
// …
module: {
rules: [
{
test: /.(css|less)$/,
use: [
{
loader: “style-loader”,
},
{
loader: “css-loader”,
options: {
importLoaders: 1,
},
},
{
loader: “less-loader”,
lessOptions: {
javascriptEnabled: true,
},
},
],
},
],
},
};
图片字体
$ npm install file-loader --save-dev
script/webpack.config.js
module.exports = {
// …
module: {
rules: [
{
test: /.(png|svg|jpg|gif|jpeg)$/,
loader: ‘file-loader’
},
{
test: /.(woff|woff2|eot|ttf|otf)$/,
loader: ‘file-loader’
}
],
},
};
HTML
$ npm install html-webpack-plugin --save-dev
script/webpack.config.js
const HtmlWebpackPlugin = require(‘html-webpack-plugin’);
module.exports = {
// …
plugins: {
html: new HtmlWebpackPlugin({
title: ‘tristana’,
template: ‘public/index.html’
}),
}
};
index.html
开发服务
$ npm install webpack-dev-server --save-dev
script/webpack.config.js
const path = require(“path”);
const HtmlWebpackPlugin = require(‘html-webpack-plugin’);
module.exports = {
// …
devServer: {
contentBase: path.resolve(__dirname, “dist”),
hot: true,
historyApiFallback: true,
compress: true,
},
};
package.json
{
// …
“scripts”: {
“start”: “webpack serve --mode=development --config script/webpack.config.js”
},
// …
}
清理 dist
$ npm install clean-webpack-plugin --save-dev
script/webpack.config.js
const { CleanWebpackPlugin } = require(‘clean-webpack-plugin’);
module.exports = {
// …
plugins: {
new CleanWebpackPlugin()
}
};
Tips
由于 webpack
使用的是^5.21.2
版本,在使用该插件时,会提示clean-webpack-plugin: options.output.path not defined. Plugin disabled...
,暂时还未解决。
环境变量
$ npm install cross-env --save-dev
package.json
{
// …
“scripts”: {
“start”: “cross-env ENV_LWD=development webpack serve --mode=development --config script/webpack.config.js”,
“build”: “cross-env ENV_LWD=production webpack --mode=production --config script/webpack.config.js”
},
// …
}
.jsx 文件
安装依赖
$ npm install @babel/preset-react react react-dom --save-dev
.babelrc
{
“presets”: [“@babel/preset-env”, “@babel/preset-react”]
}
src/App.jsx
在 src
目录下,新增 App.jsx
文件:
import React, { Component } from “react”;
class App extends Component {
render() {
return (
Hello, World!
);
}
}
export default App;
src/index.js
import React from “react”;
import ReactDOM from “react-dom”;
import App from “./App.jsx”;
ReactDOM.render(, document.getElementById(“root”));
React Router
安装依赖
$ npm install react-router history --save
src/index.js
import React from “react”;
import ReactDOM from “react-dom”;
import { Router, Route, Link } from “react-router”;
import { createBrowserHistory } from “history”;
import App from “./App.jsx”;
const About = () => {
return <>About</>;
};
ReactDOM.render(
,
document.getElementById(“root”)
);
MobX
安装依赖
$ npm install mobx mobx-react babel-preset-mobx --save
.babelrc
{
“presets”: [“@babel/preset-env”, “@babel/preset-react”, “mobx”]
}
src/store.js
在 src
目录下新建 store.js
import { observable, action, makeObservable } from “mobx”;
class Store {
constructor() {
makeObservable(this);
}
@observable
count = 0;
@action(“add”)
add = () => {
this.count = this.count + 1;
};
@action(“reduce”)
reduce = () => {
this.count = this.count - 1;
};
}
export default new Store();
index.js
import { Provider } from “mobx-react”;
import Store from “./store”;
// …
ReactDOM.render(
,
document.getElementById(“root”)
);
src/App.jsx
import React, { Component } from “react”;
import { observer, inject } from “mobx-react”;
@inject(“store”)
@observer
class App extends Component {
render() {
return (
add
reduce
);
}
}
export default App;
Ant Design
安装依赖
$ npm install antd babel-plugin-import --save
.babelrc
{
// …
“plugins”: [
[
“import”,
{
“libraryName”: “antd”,
“libraryDirectory”: “es”,
“style”: true
}
]
]
}
src/App.jsx
// …
import { DatePicker } from “antd”;
import “antd/dist/antd.css”;
@inject(“store”)
@observer
class App extends Component {
render() {
return (
);
}
}
export default App;
TypeScript
安装依赖
$ npm install typescript @babel/preset-typescript --save-dev
.babelrc
{
“presets”: [
// …
“@babel/preset-typescript”
]
}
tsconfig.json
在根目录下,新增 tsconfig.json
文件:
{
“compilerOptions”: {
“emitDecoratorMetadata”: true,
“experimentalDecorators”: true,
“target”: “ES5”,
“allowSyntheticDefaultImports”: true,
“strict”: true,
“forceConsistentCasingInFileNames”: true,
“allowJs”: true,
“outDir”: “./dist/”,
“esModuleInterop”: true,
“noImplicitAny”: false,
“sourceMap”: true,
“module”: “esnext”,
“moduleResolution”: “node”,
“isolatedModules”: true,
“importHelpers”: true,
“lib”: [“esnext”, “dom”, “dom.iterable”],
“skipLibCheck”: true,
“jsx”: “react”,
“typeRoots”: [“node”, “node_modules/@types”],
“rootDirs”: [“./src”],
“baseUrl”: “./src”
},
“include”: [“./src/**/*”],
“exclude”: [“node_modules”]
}
src/App.jsx
更换文件后缀 App.jsx
-> App.tsx
import React, { Component } from “react”;
import { observer, inject } from “mobx-react”;
import { DatePicker } from “antd”;
import “antd/dist/antd.css”;
@inject(“store”)
@observer
class App extends Component {
props: any;
render() {
return (
add
reduce
);
}
}
export default App;
代码规范
代码校验、代码格式化、Git
提交前校验、Vscode
配置、编译校验
ESLint
安装依赖
$ npm install @typescript-eslint/parser eslint eslint-plugin-standard @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-plugin-promise --save-dev
.eslintrc.js
在根目录下,新增 .eslintrc.js
文件:
module.exports = {
extends: [“eslint:recommended”, “plugin:react/recommended”],
env: {
browser: true,
commonjs: true,
es6: true,
},
globals: {
$: true,
process: true,
__dirname: true,
},
parser: “@typescript-eslint/parser”,
parserOptions: {
ecmaFeatures: {
jsx: true,
modules: true,
},
sourceType: “module”,
ecmaVersion: 6,
},
plugins: [“react”, “standard”, “promise”, “@typescript-eslint”],
settings: {
“import/ignore”: [“node_modules”],
react: {
version: “latest”,
},
},
rules: {
quotes: [2, “single”],
“no-console”: 0,
“no-debugger”: 1,
“no-var”: 1,
semi: [“error”, “always”],
“no-irregular-whitespace”: 0,
“no-trailing-spaces”: 1,
“eol-last”: 0,
“no-unused-vars”: [
1,
{
vars: “all”,
args: “after-used”,
},
],
“no-case-declarations”: 0,
“no-underscore-dangle”: 0,
“no-alert”: 2,
“no-lone-blocks”: 0,
“no-class-assign”: 2,
“no-cond-assign”: 2,
“no-const-assign”: 2,
“no-delete-var”: 2,
“no-dupe-keys”: 2,
“use-isnan”: 2,
“no-duplicate-case”: 2,
“no-dupe-args”: 2,
“no-empty”: 2,
“no-func-assign”: 2,
“no-invalid-this”: 0,
“no-redeclare”: 2,
“no-spaced-func”: 2,
“no-this-before-super”: 0,
“no-undef”: 2,
“no-return-assign”: 0,
“no-script-url”: 2,
“no-use-before-define”: 2,
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
regular-whitespace": 0,
“no-trailing-spaces”: 1,
“eol-last”: 0,
“no-unused-vars”: [
1,
{
vars: “all”,
args: “after-used”,
},
],
“no-case-declarations”: 0,
“no-underscore-dangle”: 0,
“no-alert”: 2,
“no-lone-blocks”: 0,
“no-class-assign”: 2,
“no-cond-assign”: 2,
“no-const-assign”: 2,
“no-delete-var”: 2,
“no-dupe-keys”: 2,
“use-isnan”: 2,
“no-duplicate-case”: 2,
“no-dupe-args”: 2,
“no-empty”: 2,
“no-func-assign”: 2,
“no-invalid-this”: 0,
“no-redeclare”: 2,
“no-spaced-func”: 2,
“no-this-before-super”: 0,
“no-undef”: 2,
“no-return-assign”: 0,
“no-script-url”: 2,
“no-use-before-define”: 2,
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-hEQb6HF1-1715812512824)]
[外链图片转存中…(img-xpa5nFeG-1715812512824)]
[外链图片转存中…(img-UX7p4Ct1-1715812512825)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!