Closed sleepyShen1989 closed 2 years ago
一些webpack的基本配置,方便快速开发demo
npm init -y
npm install webpack webpack-cli --save-dev
npm install css-loader style-loader less-loader postcss-loader postcss-preset-env --save-dev
npm install html-webpack-plugin --save-dev
clean: true
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
npm install copy-webpack-plugin --save-dev
// package.json "scripts": { "build": "webpack --config webpack.config.dev.js", }, "private": true, "devDependencies": { "clean-webpack-plugin": "^4.0.0", "copy-webpack-plugin": "^11.0.0", "css-loader": "^6.7.1", "html-webpack-plugin": "^5.5.0", "less-loader": "^11.1.0", "postcss-loader": "^7.0.1", "postcss-preset-env": "^7.8.2", "style-loader": "^3.3.1", "webpack": "^5.74.0", "webpack-cli": "^4.10.0" } // postcss.config.js module.exports = { plugins: [ require('postcss-preset-env') ] }
// webpack.config.dev.js const path = require('path'); const HTMLWebpackPlugin = require('html-webpack-plugin') const { DefinePlugin } = require('webpack') const CopyWebpackPlugin = require('copy-webpack-plugin') module.exports = { mode: 'development', entry: './src/index.js', devtool: 'eval-source-map', output: { filename: 'js/main_[contenthash:8].js', path: path.resolve(__dirname, 'dist'), clean: true, }, module: { rules: [ { test: /\.css$/i, use: [ 'style-loader', 'css-loader', 'postcss-loader' ], }, { test: /\.less$/i, use: [ 'style-loader', 'css-loader', 'postcss-loader', 'less-loader'], }, { test: /\.(jpe?g|png|gif|svg)$/i, type: 'asset', generator: { filename: 'assets/[name]_[contenthash:8][ext]' }, parser: { dataUrlCondition: { // 单位字节 maxSize: 10*1024 } } }, { test: /\.(eot|ttf|woff2?)$/i, type: 'asset/resource', generator: { filename: 'assets/[name]_[contenthash:8][ext]' }, }, ], }, plugins: [ // 插件执行顺序由插件内部的实现决定 new HTMLWebpackPlugin({ // 按需配置是否设置html模版 template: './public/index.html' }), // 配置模版中的变量 new DefinePlugin({ BASE_URL: "'./'" }), new CopyWebpackPlugin({ patterns: [ { from: 'public', globOptions: { ignore: [ '**/index.html' ] } } ] }) ] };
一些webpack的基本配置,方便快速开发demo
TL,DR
npm init -y
npm init -y
npm install webpack webpack-cli --save-dev
npm install css-loader style-loader less-loader postcss-loader postcss-preset-env --save-dev
npm install html-webpack-plugin --save-dev
clean: true
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
npm install copy-webpack-plugin --save-dev