支持部分IE

.browserslistrc

  1. [production]
  2. > 1% 支持全世界大于百分之一的浏览器
  3. ie 9 支持IE9
  4. [modern]
  5. last 1 chrome version 支持最新的chrome版本
  6. last 1 firefox version 支持最新的firefox
  7. [ssr]
  8. node 12 一般不用 跑在node

babel-loader打包JS

https://webpack.js.org/loaders/babel-loader/

  • 安装babel依赖
    • yarn add babel-loader @babel/core @babel/preset-env webpack --dev
  • 配置webpack.config.js
    1. module.exports={
    2. mode:'production',
    3. module: {
    4. rules: [
    5. {
    6. test: /\.jsx?$/,
    7. exclude: /node_modules/,
    8. use: {
    9. loader: 'babel-loader',
    10. options: {
    11. presets: [
    12. ['@babel/preset-env']
    13. ]
    14. }
    15. }
    16. }
    17. ]
    18. }
    19. }

    babel-loader打包JSX

    https://babeljs.io/docs/en/babel-preset-react
    λ yarn add @babel/preset-react --dev
    1. module.exports={
    2. mode:'production',
    3. module: {
    4. rules: [
    5. {
    6. test: /\.jsx?$/,
    7. exclude: /node_modules/,
    8. use: {
    9. loader: 'babel-loader',
    10. options: {
    11. presets: [
    12. ['@babel/preset-env'],
    13. ['@babel/preset-react']
    14. ]
    15. }
    16. }
    17. }
    18. ]
    19. }
    20. }

    webpack配置ESLint插件

    保证IDE的ESlint
    /.eslintrc.js
    1. module.exports = {
    2. extends: ['react-app'],
    3. rules: {
    4. 'react/jsx-uses-react': [2], //0 不报错 2 报错 1警告
    5. // 提示要在 JSX 文件里手动引入 React
    6. 'react/react-in-jsx-scope': [2]
    7. }
    8. }
    保证Webpack的ESlint
    λ yarn add eslint-webpack-plugin --dev
    1. const ESLintPlugin = require('eslint-webpack-plugin')
    2. module.exports={
    3. mode:'production',
    4. plugins:[new ESLintPlugin({
    5. extensions:['.js','.jsx'] //不加 .jsx 就不检查jsx文件
    6. })],
    7. module: {
    8. rules: [
    9. {
    10. test: /\.jsx?$/,
    11. exclude: /node_modules/,
    12. use: {
    13. loader: 'babel-loader',
    14. options: {
    15. presets: [
    16. ['@babel/preset-env'],
    17. ['@babel/preset-react',{runtime:'classic'}]
    18. ]
    19. }
    20. }
    21. }
    22. ]
    23. }
    24. }

    babel-loader打包TypeScript

    λ yarn add @babel/preset-typescript --dev
    1. const ESLintPlugin = require('eslint-webpack-plugin')
    2. module.exports={
    3. mode:'production',
    4. plugins:[new ESLintPlugin({
    5. extensions:['.js','.jsx'] //不加 .jsx 就不检查jsx文件
    6. })],
    7. module: {
    8. rules: [
    9. {
    10. test: /\.[jt]sx?$/,
    11. exclude: /node_modules/,
    12. use: {
    13. loader: 'babel-loader',
    14. options: {
    15. presets: [
    16. ['@babel/preset-env'],
    17. ['@babel/preset-react',{runtime:'classic'}],
    18. ['@babel/preset-typescript']
    19. ]
    20. }
    21. }
    22. }
    23. ]
    24. }
    25. }

    ESLint支持TypeScript

    λ yarn add eslint-config-airbnb-typescript --dev
    /webpack.config.js
    1. ...
    2. module.exports={
    3. ...
    4. plugins:[new ESLintPlugin({
    5. extensions:['.js','.jsx','.ts','tsx'] //不加 .jsx 就不检查jsx文件
    6. })],
    7. ....
    8. }
    /.eslintrc.js
    1. module.exports = {
    2. extends: ['react-app'],
    3. rules: {
    4. 'react/jsx-uses-react': [2],
    5. // 提示要在 JSX 文件里手动引入 React
    6. 'react/react-in-jsx-scope': [2]
    7. },
    8. overrides: [{
    9. files: ['*.ts', '*.tsx'],
    10. parserOptions: {
    11. project: './tsconfig.json',
    12. },
    13. extends: ['airbnb-typescript'],
    14. rules: {
    15. '@typescript-eslint/object-curly-spacing': [0],
    16. 'import/prefer-default-export': [0]
    17. }
    18. }]
    19. }
    /tsconfig.json 这个文件要有

babel-loader打包TSX

λ npx tsc --init
/tsconfig.json 改动

  1. ...
  2. "jsx":"react",
  3. "strict":false,
  4. "noImplicitAny":true
  5. ...

JS和TS支持@alias

JS

webpack.config.js

  1. const ESLintPlugin = require('eslint-webpack-plugin')
  2. const path = require('path')
  3. module.exports={
  4. mode:'production',
  5. plugins:[new ESLintPlugin({
  6. extensions:['.js','.jsx'] //不加 .jsx 就不检查jsx文件
  7. })],
  8. resolve:{
  9. alias:{
  10. '@':path.join(__dirname,'./src/')
  11. }
  12. },
  13. module: {
  14. rules: [
  15. {
  16. test: /\.[jt]sx?$/,
  17. exclude: /node_modules/,
  18. use: {
  19. loader: 'babel-loader',
  20. options: {
  21. presets: [
  22. ['@babel/preset-env'],
  23. ['@babel/preset-react',{runtime:'classic'}],
  24. ['@babel/preset-typescript']
  25. ]
  26. }
  27. }
  28. }
  29. ]
  30. }
  31. }

TS

/tscongfig.json

  1. ...
  2. "baseUrl":".",
  3. "paths":{
  4. "@/*":["src/*"]
  5. }
  6. ...