vue項目中的public、static及指定不編譯文件問題
vue中的public、static及指定不編譯文件
public 文件夾的糾結(jié)
這些天在自做項目時發(fā)現(xiàn),自做的項目居然沒有 public 文件夾,我另一個項目卻有,一個是 vue@2.6.11,一個是 vue@2.6.14,兩個都是用 vue/cli 搭建,版本如此接近結(jié)果卻不同?。ㄅ茼椖棵罡虬疃疾煌?,一個是用封裝好的 vue-cli-service,一個是 webpack-dev-server)。
后來到網(wǎng)上查了下相關(guān)情況,才知道自做的項目雖然沒有 public 文件夾,但有 static 文件夾,功能都相同,不編譯直接復(fù)制到包里。后來在兩個項目的代碼中找到依據(jù)。
// copy custom static assets
? ? new CopyWebpackPlugin([
? ? ? {
? ? ? ? from: path.resolve(__dirname, '../static'),
? ? ? ? to: config.build.assetsSubDirectory,
? ? ? ? ignore: ['.*']
? ? ? }
? ? ])// copy static assets in public/
? ?const publicDir = api.resolve('public')
? ?if (!isLegacyBundle && fs.existsSync(publicDir)) {
? ? ?webpackConfig
? ? ? ?.plugin('copy')
? ? ? ? ?.use(require('copy-webpack-plugin'), [[{
? ? ? ? ? ?from: publicDir,
? ? ? ? ? ?to: outputDir,
? ? ? ? ? ?toType: 'dir',
? ? ? ? ? ?ignore: publicCopyIgnore
? ? ? ? ?}]])
? ?}自己指定文件不編譯
在查找上面的疑或中,發(fā)現(xiàn)了其實能夠自己指定某些文件為不編譯,直接復(fù)制到打的包里。就是用插件 copy-webpack-plugin 來實現(xiàn),如同上面兩個,都是 vue 項目默認指定的不編譯文件夾。
我的項目是在 build/webpack.prd.conf.js 中修改:
new CopyWebpackPlugin([
? ? ?// 指定 public 文件夾不被編譯 -- 示例
? ? {
? ? ? ? from: path.resolve(__dirname, '../public'),
? ? ? ? to: config.build.assetsSubDirectory,
? ? ? ? ignore: ['.*']
? ? },
? ? // 指定 static 文件夾不會被編譯 -- 默認已加
? ? {
? ? ? ? from: path.resolve(__dirname, '../static'),
? ? ? ? to: config.build.assetsSubDirectory,
? ? ? ? ignore: ['.*']
? ? },
? ? // 指定不被編譯的文件及輸入的文件位置、名稱
? ? {
? ? ? ? from: path.resolve(__dirname, ?'../static/study/images/logo_vue.png'), // 指定文件
? ? ? ? to: path.resolve(config.build.assetsSubDirectory, ?'logo.png'), // 指定輸出位置、名稱
? ? ? ? ignore: ['.*'] ?// 忽略的文件
? ? }
])public、static、指定不編譯的文件跟 assets 的區(qū)別
相同點都是存放一些靜態(tài)資源。
不同點:
- 不編譯的文件:一般是不怎樣變動的文件,比如第三方文件,不會被 webpack 解析,直接復(fù)制到打包的目錄里,需使用絕對路徑來引用。
- assets: 存放相對變動性的資源,會被 webpack 當(dāng)作模塊來解析,最終生成相關(guān)依賴的編譯文件,需要使用相對路徑來引用。
vue項目編譯打包
Vue項目打包命令:npm run build:prod


等編譯后的文件會出現(xiàn)在項目中的“dist”文件夾中,即打包完成!

常見問題
dist文件夾下的東西就是需要部署的項目。
但是遇到問題:index.html頁面出現(xiàn)一片空白,右鍵檢查network發(fā)現(xiàn)一堆錯誤。
解決
沒有修改config配置文件,如果直接打包,系統(tǒng)默認的module.exports是’/’(根目錄),而不是’./’(當(dāng)前目錄),從而導(dǎo)致路徑不對,頁面加載不出來,需要自己在項目的根目錄下手動建一個配置文件并添上以下代碼,然后在重新打包一次就可以了。
'use strict'
const path = require('path')
function resolve(dir) {
return path.join(__dirname, dir)
}
const name = process.env.VUE_APP_TITLE || '管理系統(tǒng)' // 網(wǎng)頁標題
const port = process.env.port || process.env.npm_config_port || 8091 // 測試端口8091 正式端口 9094
// vue.config.js 配置說明
//官方vue.config.js 參考文檔 https://cli.vuejs.org/zh/config/#css-loaderoptions
// 這里只列一部分,具體配置參考文檔
module.exports = {
// 部署生產(chǎn)環(huán)境和開發(fā)環(huán)境下的URL。
// 默認情況下,Vue CLI 會假設(shè)你的應(yīng)用是被部署在一個域名的根路徑上
// 例如 https://www.ruoyi.vip/。如果應(yīng)用被部署在一個子路徑上,你就需要用這個選項指定這個子路徑。例如,如果你的應(yīng)用被部署在 https://www.ruoyi.vip/admin/,則設(shè)置 baseUrl 為 /admin/。
publicPath: process.env.NODE_ENV === "production" ? "/" : "/",
// 在npm run build 或 yarn build 時 ,生成文件的目錄名稱(要和baseUrl的生產(chǎn)環(huán)境路徑一致)(默認dist)
outputDir: 'dist',
// 用于放置生成的靜態(tài)資源 (js、css、img、fonts) 的;(項目打包之后,靜態(tài)資源會放在這個文件夾下)
assetsDir: 'static',
// 是否開啟eslint保存檢測,有效值:ture | false | 'error'
lintOnSave: process.env.NODE_ENV === 'development',
// 如果你不需要生產(chǎn)環(huán)境的 source map,可以將其設(shè)置為 false 以加速生產(chǎn)環(huán)境構(gòu)建。
productionSourceMap: false,
// webpack-dev-server 相關(guān)配置
devServer: {
host: '0.0.0.0',
port: port,
open: true,
proxy: {
[process.env.VUE_APP_BASE_API]: {
target: `http://39.104.54.20:8090`, //
//target: `http://cqtcs.sdmaen.com:8090`, //測試庫
//target: `http://39.104.54.20:8090`, //新測試庫
//target: `http://192.168.124.22:8090/`, //l
//target: `http://192.168.1.101:8090`, //正式庫
changeOrigin: true,
pathRewrite: {
['^' + process.env.VUE_APP_BASE_API]: ''
}
}
},
disableHostCheck: true
},
configureWebpack: {
name: name,
resolve: {
alias: {
'@': resolve('src')
}
}
},
chainWebpack(config) {
config.plugins.delete('preload') // TODO: need test
config.plugins.delete('prefetch') // TODO: need test
// set svg-sprite-loader
config.module
.rule('svg')
.exclude.add(resolve('src/assets/icons'))
.end()
config.module
.rule('icons')
.test(/\.svg$/)
.include.add(resolve('src/assets/icons'))
.end()
.use('svg-sprite-loader')
.loader('svg-sprite-loader')
.options({
symbolId: 'icon-[name]'
})
.end()
config
.when(process.env.NODE_ENV !== 'development',
config => {
config
.plugin('ScriptExtHtmlWebpackPlugin')
.after('html')
.use('script-ext-html-webpack-plugin', [{
// `runtime` must same as runtimeChunk name. default is `runtime`
inline: /runtime\..*\.js$/
}])
.end()
config
.optimization.splitChunks({
chunks: 'all',
cacheGroups: {
libs: {
name: 'chunk-libs',
test: /[\\/]node_modules[\\/]/,
priority: 10,
chunks: 'initial' // only package third parties that are initially dependent
},
elementUI: {
name: 'chunk-elementUI', // split elementUI into a single package
priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
},
commons: {
name: 'chunk-commons',
test: resolve('src/components'), // can customize your rules
minChunks: 3, // minimum common number
priority: 5,
reuseExistingChunk: true
}
}
})
config.optimization.runtimeChunk('single'),
{
from: path.resolve(__dirname, './public/robots.txt'), //防爬蟲文件
to: './' //到根目錄下
}
}
)
}
}總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue3 setup點擊跳轉(zhuǎn)頁面的實現(xiàn)示例
本文主要介紹了vue3 setup點擊跳轉(zhuǎn)頁面的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-10-10
vue中router.beforeEach()的簡單用法舉例
router.beforeEach()一般用來做一些進入頁面的限制,比如沒有登錄,就不能進入某些頁面,只有登錄了之后才有權(quán)限查看某些頁面,下面這篇文章主要給大家介紹了關(guān)于vue中router.beforeEach()的簡單用法舉例,需要的朋友可以參考下2023-01-01

