tianyunperfect vor 3 Jahren
Commit
5f8baa8c5e
5 geänderte Dateien mit 143 neuen und 0 gelöschten Zeilen
  1. 1 0
      .gitignore
  2. 28 0
      package.json
  3. 1 0
      src/index.js
  4. 9 0
      src/public/index.html
  5. 104 0
      webpack.config.js

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+*.iml

+ 28 - 0
package.json

@@ -0,0 +1,28 @@
+{
+  "name": "app",
+  "version": "1.0.0",
+  "description": "",
+  "main": "index.js",
+  "devDependencies": {
+    "@babel/core": "^7.16.0",
+    "@babel/polyfill": "^7.12.1",
+    "@babel/preset-env": "^7.16.0",
+    "babel-loader": "^8.2.3",
+    "clean-webpack-plugin": "^4.0.0",
+    "css-loader": "^6.5.1",
+    "file-loader": "^6.2.0",
+    "html-webpack-plugin": "^5.5.0",
+    "style-loader": "^3.3.1",
+    "url-loader": "^4.1.1",
+    "webpack": "^5.64.0",
+    "webpack-cli": "^4.9.1",
+    "webpack-dev-server": "^4.5.0"
+  },
+  "scripts": {
+    "dev": "webpack-dev-server --config ./webpack.config.js",
+    "build": "webpack --config ./webpack.config.js"
+  },
+  "keywords": [],
+  "author": "",
+  "license": "ISC"
+}

+ 1 - 0
src/index.js

@@ -0,0 +1 @@
+console.log(123);

+ 9 - 0
src/public/index.html

@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <title>Title</title>
+</head>
+<body>
+</body>
+</html>

+ 104 - 0
webpack.config.js

@@ -0,0 +1,104 @@
+const path = require('path');
+const HtmlWebpackPlugin = require("html-webpack-plugin");
+const {CleanWebpackPlugin} = require("clean-webpack-plugin");
+
+const config = {
+    mode: 'development',
+    entry: ['@babel/polyfill', path.resolve(__dirname, './src/index.js')],
+    output: {
+        filename: '[name].[hash:8].js',
+        path: path.resolve(__dirname, './dist')
+    },
+    plugins: [
+        // 自动把入口 js 插入到 index.html 中
+        new HtmlWebpackPlugin({
+            filename: "index.html",
+            template: path.resolve(__dirname, './src/public/index.html')
+        }),
+        // 清除 dist 的插件
+        new CleanWebpackPlugin()
+    ],
+    module: {
+        rules: [
+            // 转为 es5 语法,兼容更多浏览器
+            {
+                test: /\.js$/,
+                exclude: /node_modules/,
+                use: [
+                    {
+                        loader: 'babel-loader',
+                        options: {
+                            presets: ['@babel/preset-env']
+                        }
+                    }
+                ]
+            },
+            // css 打包
+            {
+                test: /\.css$/,
+                use: ['style-loader', 'css-loader']
+            },
+            // 设置 limit,如果超过限制,使用 file-loader 打包
+            {
+                test: /\.(jpg|png|gif)$/,
+                use: [
+                    {
+                        loader: 'url-loader',
+                        options: {
+                            limit: 1024,
+                            fallback: {
+                                loader: 'file-loader',
+                                options: {
+                                    name: 'img/[name].[hash:8].[ext]'
+                                }
+                            }
+                        }
+                    }
+                ]
+            },
+            {
+                test: /\.(mp4|webm|ogg|mp3|wav)(\?.*)?$/,
+                use: [
+                    {
+                        loader: 'url-loader',
+                        options: {
+                            limit: 1024,
+                            fallback: {
+                                loader: 'file-loader',
+                                options: {
+                                    name: 'media/[name].[hash:8].[ext]'
+                                }
+                            }
+                        }
+                    }
+                ]
+            },
+            {
+                test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
+                use: [
+                    {
+                        loader: 'url-loader',
+                        options: {
+                            limit: 1024,
+                            fallback: {
+                                loader: 'file-loader',
+                                options: {
+                                    name: 'fonts/[name].[hash:8].[ext]'
+                                }
+                            }
+                        }
+                    }
+                ]
+            }
+        ]
+    },
+    devServer: {
+        port: '3000',
+        host: '127.0.0.1',
+        hot: true,
+        // 自动打开 url
+        open: true
+    },
+}
+
+module.exports = config;