1.项目场景是有两个静态资源目录,一个用于开发,一个用于发布,上线多个版本,打包多个版本后,也要部署到同一个服务器的同一个端口下.
根据我自己的摸索,我搞出来了下面的配置,自感觉还蛮好用的
先看我的config.js 用来控制所有的静态资源目录和打包上线路径,
module.exports = {
dev: {
template: {
title: '开发的时候的系统名字',
header: false,
footer: false,
publicPath: '/',
publicStaticPath: '../',
assetsPath: 'assets_All/'
}
},
build: {
template: {
title: '我的友好系统',
header:true,
footer:false,
publicPath: '/hahaha/',
publicStaticPath: '/hahaha/',
assetsPath: 'assets/'
}
}
}
再来看一下我的webpack.config.js 用到了上面的配置,但是基本上一般什么都不用动哦~~~
const TerserWebpackPlugin = require('terser-webpack-plugin')
const terserPlugin = new TerserWebpackPlugin({
parallel: 4,
extractComments: true,
terserOptions: {
compress: {
warnings: false,
drop_console: true,
drop_debugger: true,
pure_funcs: ['console.log'],
},
},
})
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const isDev = process.env.NODE_ENV.trim() === 'development'
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const path = require('path')
const config = require('./public/config')[isDev ? 'dev' : 'build']
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.[hash:6].js',
publicPath: config.template.publicPath
},
mode: isDev ? 'development' : 'production',
module: {
rules: [
{
test: /\.jsx?$/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
plugins: [
[
'@babel/plugin-transform-runtime',
{
corejs: 3,
},
],
],
},
},
exclude: /node_modules|jsm|models/,
},
{
test: /\.(le|c)ss$/,
use: [
'style-loader',
'css-loader',
{
loader: 'postcss-loader',
options: {
plugins: function () {
return [
require('autoprefixer')({
overrideBrowserslist: ['>0.25%', 'not dead'],
}),
]
},
},
},
'less-loader',
],
exclude: /node_modules/,
},
{
test: /\.(png|jpg|gif|jpeg|webp|svg|eot|gltf|ttf|woff|woff2)$/,
use: [
{
loader: 'url-loader',
options: {
limit: 10240,
esModule: false,
name: '[name]_[hash:0].[ext]',
outputPath: config.template.assetsPath,
},
},
],
exclude: /node_modules/,
},
],
},
optimization: {
minimizer: [
terserPlugin,
],
},
plugins: [
new HtmlWebpackPlugin({
template: './public/index.html',
filename: 'index.html',
config: config.template,
minify: {
removeAttributeQuotes: false,
collapseWhitespace: false,
},
hash: true,
}),
new CleanWebpackPlugin(),
new CopyWebpackPlugin({
patterns: [
{
from: 'src/'+ config.template.assetsPath,
to: config.template.assetsPath,
},
],
}),
],
devServer: {
port: '8080',
quiet: false,
inline: true,
stats: 'errors-only',
overlay: false,
clientLogLevel: 'silent',
compress: true,
},
devtool: 'cheap-module-eval-source-map',
}
.babelrc
{
"presets": ["@babel/preset-env"],
"plugins": [
[
"@babel/plugin-transform-runtime",
{
"corejs": 3
}
]
]
}
我的package.json
{
"name": "webpack_test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "set NODE_ENV=production&& webpack",
"dev": "set NODE_ENV=development&& webpack-dev-server --host 10.251.134.102",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.8.6",
"@babel/plugin-transform-runtime": "^7.8.3",
"@babel/preset-env": "^7.8.6",
"autoprefixer": "^9.7.4",
"babel-loader": "^8.0.6",
"clean-webpack-plugin": "^3.0.0",
"css-loader": "^3.4.2",
"file-loader": "^5.1.0",
"html-webpack-plugin": "^3.2.0",
"less": "^3.11.1",
"less-loader": "^5.0.0",
"postcss-loader": "^3.0.0",
"style-loader": "^1.1.3",
"terser-webpack-plugin": "^4.2.3",
"url-loader": "^3.0.0",
"webpack": "^4.41.6",
"webpack-cli": "^3.3.11",
"webpack-dev-server": "^3.10.3"
},
"dependencies": {
"@babel/runtime": "^7.8.4",
"@babel/runtime-corejs3": "^7.8.4",
"copy-webpack-plugin": "^6.0.2",
"html2canvas": "^1.0.0-rc.7",
"tween": "^0.9.0"
}
}
接下来我来放几个代码的使用方式吧~~
import './index.less'
import './js/indexTest_2.js'
console.log('项目已启动')
先来看一下less里的图片引用方式
src\index.less
@color: red;
body {
background: #020929;
.panel {
border: 0;
width: 400px;
font-size: 30px;
line-height: 31px;
text-indent: 20px;
}
}
我们再来看一下js的图片引用方式
const isDev = process.env.NODE_ENV.trim() === 'development'
const config = require('../../public/config')[isDev ? 'dev' : 'build']
const staticUrl = config.template.publicStaticPath
export { staticUrl }
再来看一下src\js\indexV1_2.js
import { staticUrl } from './tool'
var planeTexture = new THREE.TextureLoader().load(
staticUrl + 'assets/img/equipment/floor.png'
)
好了,这个就是大概的使用方法了~~ 目前已经试过多次,打包运行什么的没得毛病哦,我觉得还挺好用
希望路过的大佬指点一下有没有更好的办法~~~
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
2020-04-16 JS实现excel数据透析,形成关系图
2020-04-16 使用ng-zorro图标库出现the icon redo-o does not exist or is not registered.