“@babel/cli”: “^7.2.3” 表示babel 7的版本

在此版本中

  1. // webpack.config.js exports对象
  2. exports.module.rules = [
  3. {
  4. test: /\.(js|jsx)$/,
  5. exclude: /(node_modules|bower_components)/,
  6. use: {
  7. loader: 'babel-loader',
  8. },
  9. }
  10. ]
  11. // 然后在配置babel.config.js文件
  12. // 导出预设和插件设置
  13. // 每次执行babel命令时,或通过babel-loader执行babel操作时,
  14. // 都会自动读取根目录的babel.config.js文件的配置
  1. // webpack.common.js
  2. const path = require('path');
  3. const HtmlWebpackPlugin = require('html-webpack-plugin'); // 自动生成页面
  4. const CleanWebpackPlugin = require('clean-webpack-plugin'); // 自动清理,清理dist旧文件
  5. const webpack = require('webpack');
  6. module.exports = {
  7. optimization: {
  8. runtimeChunk: 'single',
  9. splitChunks: {
  10. cacheGroups: {
  11. vendor: {
  12. test: /[\\/]node_modules[\\/]/,
  13. name: 'vendors',
  14. chunks: 'all'
  15. }
  16. }
  17. }
  18. },
  19. entry: {
  20. app: './src/index.jsx',
  21. },
  22. plugins: [
  23. new CleanWebpackPlugin(['dist']),
  24. new HtmlWebpackPlugin({
  25. template: path.resolve(__dirname, 'src/index.html'),
  26. filename: 'index.html',
  27. }),
  28. new webpack.HotModuleReplacementPlugin(),
  29. new webpack.HashedModuleIdsPlugin()
  30. ],
  31. output: {
  32. filename: '[name].[hash].js',
  33. // chunkFilename: '[name].[contenthash].js',
  34. path: path.resolve(__dirname, 'dist'),
  35. publicPath: '/'
  36. },
  37. module: {
  38. rules: [
  39. {
  40. test: /\.(js|jsx)$/,
  41. exclude: /(node_modules|bower_components)/,
  42. use: {
  43. loader: 'babel-loader',
  44. },
  45. },
  46. {
  47. test: /\.css$/,
  48. use: [
  49. 'style-loader',
  50. 'css-loader'
  51. ]
  52. },
  53. {
  54. test: /\.(png|svg|jpg|gif)$/,
  55. use: [
  56. 'file-loader'
  57. ]
  58. },
  59. {
  60. test: /\.(woff|woff2|eot|ttf|otf)$/,
  61. use: [
  62. 'file-loader'
  63. ]
  64. },
  65. {
  66. test: /\.(csv|tsv)$/,
  67. use: [
  68. 'csv-loader'
  69. ]
  70. },
  71. {
  72. test: /\.xml$/,
  73. use: [
  74. 'xml-loader'
  75. ]
  76. }
  77. ],
  78. }
  79. };
  80. // webpack.dev.js
  81. const merge = require('webpack-merge');
  82. const common = require('./webpack.common.js');
  83. module.exports = merge(common, {
  84. mode: 'development',
  85. devtool: 'inline-source-map',
  86. devServer: {
  87. contentBase: './dist',
  88. hot: true
  89. }
  90. });
  91. // webpack.prod.js
  92. const merge = require('webpack-merge');
  93. const common = require('./webpack.common.js');
  94. module.exports = merge(common, {
  95. mode: 'production',
  96. });
  1. {
  2. "name": "webpackDemo",
  3. "version": "1.0.0",
  4. "description": "",
  5. "private": true,
  6. "main": "index.js",
  7. "scripts": {
  8. "test": "echo \"Error: no test specified\" && exit 1",
  9. "watch": "webpack --watch",
  10. "build": "webpack --config webpack.prod.js",
  11. "buildDev": "webpack --config webpack.dev.js",
  12. "start": "webpack-dev-server --open --config webpack.dev.js",
  13. "server": "node server.js"
  14. },
  15. "keywords": [],
  16. "author": "",
  17. "license": "ISC",
  18. "devDependencies": {
  19. "@babel/cli": "^7.2.3",
  20. "@babel/core": "^7.2.2",
  21. "@babel/plugin-proposal-class-properties": "^7.2.3",
  22. "@babel/plugin-proposal-object-rest-spread": "^7.2.0",
  23. "@babel/plugin-transform-proto-to-assign": "^7.2.0",
  24. "@babel/plugin-transform-regenerator": "^7.0.0",
  25. "@babel/plugin-transform-runtime": "^7.2.0",
  26. "@babel/preset-env": "^7.2.3",
  27. "@babel/preset-react": "^7.0.0",
  28. "babel-loader": "^8.0.4",
  29. "babel-plugin-add-module-exports": "^1.0.0",
  30. "babel-plugin-import": "^1.11.0",
  31. "babel-plugin-syntax-dynamic-import": "^6.18.0",
  32. "clean-webpack-plugin": "^1.0.0",
  33. "css-loader": "^2.0.0",
  34. "csv-loader": "^3.0.2",
  35. "express": "^4.16.4",
  36. "file-loader": "^2.0.0",
  37. "html-webpack-plugin": "^3.2.0",
  38. "style-loader": "^0.23.1",
  39. "webpack": "^4.27.1",
  40. "webpack-cli": "^3.1.2",
  41. "webpack-dev-middleware": "^3.4.0",
  42. "webpack-dev-server": "^3.1.10",
  43. "webpack-merge": "^4.1.5",
  44. "xml-loader": "^1.2.1"
  45. },
  46. "dependencies": {
  47. "@babel/polyfill": "^7.2.3",
  48. "lodash": "^4.17.11",
  49. "react": "^16.7.0",
  50. "react-dom": "^16.7.0",
  51. "react-loadable": "^5.5.0",
  52. "react-redux": "^6.0.0",
  53. "react-router-dom": "^4.3.1",
  54. "redux": "^4.0.1",
  55. "redux-saga": "^0.16.2",
  56. "webpack-hot-middleware": "^2.24.3"
  57. }
  58. }
  1. const presets = [
  2. [
  3. '@babel/preset-env',
  4. {
  5. modules: false,
  6. loose: true,
  7. targets: {
  8. edge: '10',
  9. firefox: '50',
  10. chrome: '52',
  11. safari: '8',
  12. },
  13. useBuiltIns: 'usage',
  14. },
  15. ],
  16. [
  17. '@babel/preset-react'
  18. ],
  19. ];
  20. const plugins = [
  21. ["syntax-dynamic-import"],
  22. ['babel-plugin-import', {
  23. 'libraryName': 'antd',
  24. 'libraryDirectory': 'es',
  25. 'style': 'css'
  26. }],
  27. [
  28. '@babel/plugin-transform-runtime',
  29. {
  30. 'corejs': false,
  31. 'helpers': false,
  32. 'regenerator': true,
  33. 'useESModules': false
  34. }
  35. ],
  36. ['@babel/plugin-transform-regenerator'],
  37. ['@babel/plugin-proposal-object-rest-spread'],
  38. ['@babel/plugin-proposal-class-properties'],
  39. ];
  40. module.exports = { presets, plugins };