当前位置: 首页 > 知识库问答 >
问题:

javascript - vue项目部分图片在windows下引用不到,linux下正常是怎么回事?

劳研
2024-07-16

vue项目做了个皮肤切换之后,在windows系统打包后运行会出现某些图片引用不到,我看了路径是完全正常的,但是在linux系统打包后运行完全正常。

下面这两张图片存储在一个文件夹,一张可以加载,一张加载不出来,也没有任何报错。
正常加载的
image.png

不正常加载的
image.png

以下是配置文件代码

'use strict'
const path = require('path')
const defaultSettings = require('./src/settings.js')
const webpack = require('webpack')
var HardSourceWebpackPlugin = require('hard-source-webpack-plugin')
// const MiniCssExtractPlugin = require('mini-css-extract-plugin')
// 不要使用自己安装的mini-css-extract-plugin,会报错,使用vue内置的,可能是因为版本不一致的原因
const MiniCssExtractPlugin = require('./node_modules/@vue/cli-service/node_modules/mini-css-extract-plugin')
const htmlWebpackInjectAttributesPlugin = require('html-webpack-inject-attributes-plugin')
const HtmlWebpackExcludeAssetsPlugin = require('html-webpack-exclude-assets-plugin')

// npx vue-cli-service inspect --mode=production >> webpack.config.js  打印生产配置
// npx vue-cli-service inspect --mode=development >> webpack.config.dev.js  打印开发配置

function resolve(dir) {
  return path.join(__dirname, dir)
}

function setPublicPath() {
  return (process.env.ENV === 'production' ? './' : '/udap-web/')
}

function setOutPutDir() {
  if (process.env.ENV === 'staging' || process.env.ENV === 'development') {
    return 'udap-web'
  }
  return 'udap'
}

const themeReg = /^(.*)((red|blue)\..+\.css)$/
const themeRegJS = /^(.*)((red|blue).*\.js)$/
const themeLinkJudge = (tagName, attributes) => {
  return (
    tagName === 'link' && attributes.href && themeReg.test(attributes.href)
  )
}

const name = defaultSettings.title || '' // page title

// If your port is set to 80,
// use administrator privileges to execute the command line.
// For example, Mac: sudo npm run
// You can change the port by the following method:
// port = 9527 npm run dev OR npm run dev --port = 9527
const port = process.env.port || process.env.npm_config_port || 9527 // dev port

// All configuration item explanations can be find in https://cli.vuejs.org/config/
module.exports = {
  /**
   * You will need to set publicPath if you plan to deploy your site under a sub path,
   * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
   * then publicPath should be set to "/bar/".
   * In most cases please use '/' !!!
   * Detail: https://cli.vuejs.org/config/#publicpath
   */
  publicPath: setPublicPath(),
  outputDir: setOutPutDir(),
  assetsDir: 'udap-web-assets',
  lintOnSave: process.env.NODE_ENV === 'development',
  productionSourceMap: false,
  devServer: {
    port: port,
    disableHostCheck: true,
    proxy: {
      '/mock-api': {
        target: '/',
        changeOrigin: true
      },
      '/api/udap_web/websocket': {
        target: 'ws://192.168.31.xxxx:3210',
        changeOrigin: true,
        pathRewrite: {
          '^/api': ''
        }
      },
      '/api': {
        target: 'http://192.168.31.xxxx:3210', // 公司服务器
        changeOrigin: true
        // pathRewrite: {
        //  '^/api': ''
        // }
      },
      '/cogones-api': {
        target: 'http://192.168.31.xxxx:9300',
        changeOrigin: true,
        pathRewrite: {
          '^/cogones-api': ''
        }
      }
    }
  },
  configureWebpack: {
    devtool: 'source-map',
    // provide the app's title in webpack's name field, so that
    // it can be accessed in index.html to inject the correct title.
    name: name,
    resolve: {
      alias: {
        '@': resolve('src')
      }
    },
    plugins: [
      new HardSourceWebpackPlugin(),
      new webpack.ProvidePlugin({ _: 'lodash' })
    ],
    performance: {
      hints: false
    },
    entry: {
      app: path.resolve(__dirname, 'src/main.js'),
      blue: path.resolve(__dirname, 'src/theme/blue/index.js'),
      red: path.resolve(__dirname, 'src/theme/red/index.js')
    }
  },
  chainWebpack(config) {
    // it can improve the speed of the first screen, it is recommended to turn on preload
    // it can improve the speed of the first screen, it is recommended to turn on preload
    config.plugin('preload').tap(() => [
      {
        rel: 'preload',
        // to ignore runtime.js
        // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
        fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
        include: 'initial'
      }
    ])

    // when there are many pages, it will cause too many meaningless requests
    config.plugins.delete('prefetch')

    // set svg-sprite-loader
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/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
                }
              }
            })
          // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
          config.optimization.runtimeChunk('single')
        }
      )

    /** 皮肤配置  start */
    // MiniCssExtractPlugin可以给不同入口的独立打包css文件
    // dev环境下vue使用的不是MiniCssExtractPlugin来提取css生成css文件,而是使用vue-style-loader来生成style标签,
    // 因为我们需要生成文件,所以这里即使dev环境下也使用MiniCssExtractPlugin

    config.when(process.env.NODE_ENV === 'development', config => {
      config
        .plugin('extract-css')
        .use(MiniCssExtractPlugin, [
          {
            filename: '[name].[contenthash:8].css',
            chunkFilename: '[name].[contenthash:8].css'
          }
        ])
        .end()
    })

    // 让normal这个module不去匹配我们的主题文件  /^((?!light\.scss|default\.scss).)*$/
    config.module
      .rule('scss')
      .oneOf('normal')
      .test((p1) => {
        const pat = /^(?!.*\\theme\\).*\.scss$/
        const result = pat.test(p1)
        return result
      })
      .end()

    // 定义自己的module来使用我们自己的loader .*(light|default)\.scss$
    const theme = config.module
      .rule('scss')
      .oneOf('ownTheme')
      .test(/^(?=.*\\theme\\).*\.scss$/)
    // 清除vue-cli写好的loader,改用自己写的
    theme.uses.clear()

    theme
      .use(MiniCssExtractPlugin.loader)
      .loader(MiniCssExtractPlugin.loader)
      .end()
      .use('css-loader')
      .loader('css-loader')
      .end()
      .use('postcss-loader')
      .loader('postcss-loader')
      .end()
      .use('sass-loader')
      .loader('sass-loader')
      .end()

    // 增加自定义标签属性
    config
      .plugin('html')
      .tap((args) => {
        args[0].attributes = {
          'data-theme': function(tag) {
            const { attributes, tagName } = tag
            if (themeLinkJudge(tagName, attributes)) {
              return 'true'
            }
            return false
          },
          rel: function(tag) {
            const { attributes, tagName } = tag
            if (themeLinkJudge(tagName, attributes)) {
              if (attributes.href.indexOf('blue') > -1) {
                return 'stylesheet'
              }
              return 'alternate stylesheet'
            } else if (tagName === 'link') {
              return 'stylesheet'
            }
            return false
          },
          title: function(tag) {
            const { attributes, tagName } = tag
            if (themeLinkJudge(tagName, attributes)) {
              if (attributes.href.indexOf('red') > -1) {
                return 'red'
              } else if (attributes.href.indexOf('blue') > -1) {
                return 'blue'
              }
              return false
            }
            return false
          }
        }

        args[0].excludeAssets = [themeRegJS]
        return args
      })
      .end()

    // HtmlWebpackPlugin生成HTML文件时,这个插件能给生成的标签注入自己需要的属性
    config
      .plugin('htmlInjectAttribute')
      .after('html')
      .use(htmlWebpackInjectAttributesPlugin)
      .end()

    // 移除多入口生成的无用script标签和JS文件
    config
      .plugin('ExcludeAssets')
      .after('htmlInjectAttribute')
      .use(HtmlWebpackExcludeAssetsPlugin)
      .end()

    /** 皮肤配置 end */
  }
}

共有1个答案

黄和怡
2024-07-16

问题解答

这个问题可能是由于Windows和Linux对大小写敏感性的不同所导致的。Windows系统对文件路径大小写不敏感,而Linux系统则是敏感的。

当你在Windows下引用图片时,可能由于路径大小写的不一致(例如:bVddhjIbVddhjZ),但实际上文件系统中只有一个正确的大小写文件名存在,Windows系统能够正确加载是因为它不区分大小写。然而,在Linux系统中,由于它区分大小写,所以如果你的引用路径的大小写与文件系统中的实际文件名大小写不匹配,那么图片将无法被正确加载。

解决方案

  1. 检查文件名的大小写:确保你在Vue项目中引用图片的路径与文件系统中实际文件名的大小写完全一致。
  2. 避免大小写不一致:在编写代码和命名文件时,尽量保持大小写的一致性,以避免跨平台时出现此类问题。
  3. 使用工具:你可以使用文本编辑器或IDE的搜索和替换功能,在项目中查找并替换所有可能的大小写不一致的路径引用。
  4. 检查构建脚本:确保你的构建脚本(如Webpack配置)没有在处理文件路径时引入大小写不一致的情况。
  5. 测试:在更改后,分别在Windows和Linux系统上进行测试,以确保问题已得到解决。

注意:在Vue项目中,通常建议将资源(如图片)放在public目录下,并通过绝对路径(以/开头)来引用它们,这样可以避免在构建时由于路径处理导致的问题。如果你将资源放在src/assets或其他由Webpack处理的目录中,那么你需要确保路径的大小写与文件系统中的实际文件名一致。

 类似资料:
  • Scrapy提供了一个 item pipeline ,来下载属于某个特定项目的图片,比如,当你抓取产品时,也想把它们的图片下载到本地。 这条管道,被称作图片管道,在 ImagesPipeline 类中实现,提供了一个方便并具有额外特性的方法,来下载并本地存储图片: 将所有下载的图片转换成通用的格式(JPG)和模式(RGB) 避免重新下载最近已经下载过的图片 缩略图生成 检测图像的宽/高,确保它们满

  • 现在有一个需求是需要动态的创建vue文件,并写入内容放到指定目录下

  • 问: 我在用workerman-chat 开发了一个简单的即时通讯工具,不过是用的windows版本 现在要把它放到linux环境下面该怎么操作呢? 答: 两种方法: 1、下载linux版本的workerman-chat,然后把你的Applications下的文件替换上去即可。 2、把Workerman目录换成Linux版本的Workerman即可。 第2种方法适用于所有windows版本移植到L

  • rank ▲ ✰ vote url 8 919 442 608 url 怎么在windows下安装pip? 怎么在windows下安装pip? Python3.4+ 好消息,Python3.4已经自带Pip.这是所有Python发行版中最好的特性了.方便了所有人使用公共库.新手再也不用操心安装额外库的繁琐步骤了.它自带的包管理器中加入了 Ruby, Nodejs, Haskell, Perl, G

  • 问题内容: 现在我有一个,我想将其另存为PNG。我可以使用所有那些花哨的复杂文件系统API来做到这一点,但我真的不喜欢它们。 我知道上面是否有带有属性的链接: 如果用户单击该文件,它将下载文件。因此我想到了这个: 但是,它似乎不起作用。它是否必须由用户操作触发?否则为什么它不起作用? 问题答案: 问题在于jQuery不会触发元素的本机事件,因此不会发生导航(的正常行为),因此您需要手动执行操作。对

  • 1.手机版 1)对话中下载、转发:长按对话中的文件/图片-转发/下载   2)文件库里下载、转发:我-文件-找到要操作的文件/图片-转发/下载   2.电脑版 1)对话中下载、转发:对话中的文件/图片-下载/转发   下载后的文件可以点击打开或打开文件夹 2)文件库里下载、转发:上方文件按钮-找到要操作的文件/图片-下载/转发

  • 除了阅读github中的代码之外,是否有关于signalr.redis包如何工作的白皮书类型的文档?具体地说,我想知道它为Redis添加了哪些键、更新/删除策略等。当查看Redis内部时,我只看到以下调用中指定的一个键(即“signalr.Redis.sample”): 这把钥匙好像是Redis的柜台。我假设正在创建其他键并迅速删除,以方便连接到Redis的每个应用服务器之间的消息。

  • 本文向大家介绍linux 下部署nodejs项目(两种方式),包括了linux 下部署nodejs项目(两种方式)的使用技巧和注意事项,需要的朋友参考一下 之前在linux下部署过几次NodeJS,也有些经验,最近也在Q群里有朋友问Node在linux下部署问题,于是总结一下,主要介绍两种不同的部署方式,二进制文件直接解压部署和手动编译安装,一来巩固自己的知识,二来希望能对新手有所帮助。  关于在