简体   繁体   English

Webpack较少加载程序缺少生产构建的基本路径

[英]Webpack less-loader Missing Base Path on Production Build

I've got a vue-js app that uses webpack. 我有一个使用webpack的vue-js应用程序。 Everything works fine in development and test environments, but I'm having trouble getting it to build for production. 一切在开发和测试环境中都可以正常工作,但是我很难将其构建用于生产。

I've got background images in the LESS files. LESS文件中有背景图像。 The image files are in /static . 图像文件位于/static (I'm not sure whether that's kosher or if they should be in side src/assets .) (我不确定这是犹太洁食还是应该放在侧面src/assets 。)

At any rate, when the LESS has something like this: 无论如何,当LESS具有以下内容时:

 background-url: url("/static/img/foobar/my-image.svg") 

Then the compilted CSS will have the same url 然后,已编译的CSS将具有相同的url

 background-url: url("/static/img/foobar/my-image.svg") 

When the browser loaders, it can't find that imgate file. 当浏览器加载程序时,找不到该imgate文件。 The browser is attempting to find the file here: 浏览器正在尝试在此处查找文件:

file:///static/img/foobar/my-image.svg

Can anyone recommend a way to prepend the absolute path when the app builds for production? 任何人都可以推荐一种方法来构建应用程序以投入生产时的绝对路径吗?

Do you have your static assets outside of our project directory in /static ? 您的静态资产是否在/static项目目录之外? Otherwise I don't get why your browser is trying to request it from file:///static/img/foobar/my-image.svg 否则我不明白为什么您的浏览器试图从file:///static/img/foobar/my-image.svg请求它

anyway, your static assets should be part of your repo/project. 无论如何,您的静态资产应该是您的回购/项目的一部分。 They do not need to be in src directory, a /static folder within the root of your project is just fine. 它们不需要位于src目录中,项目根目录中的/static文件夹就可以了。

when you compile your application - let's say into a dist folder - you should copy the images also in that dist folder. 编译应用程序时-假设您将其复制到dist文件夹中-您还应该将图像复制到该dist文件夹中。 In my app I use the copy-webpack-plugin for that task (I have my images in ./public/assets/img/.. and I reference them as /assets/img/.. ) 在我的应用程序中,我使用copy-webpack-plugin来完成该任务(我的图像位于./public/assets/img/..并且将它们引用为/assets/img/..

    const CopyWebpackPlugin = require('copy-webpack-plugin');
    ...

    plugins: [
    ...
    /** copy all files from the public folder to the compilation root */
    new CopyWebpackPlugin([{
        from: 'public',
        to  : ''
    }]),
    ...

also you should make sure that you have the file-loader in place for your static assets. 您还应确保已为静态资产安装了文件加载器。 I use it like so: 我这样使用它:

  {
       test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)(\??\#?v=[.0-9]+)?$/,
       use : 'file-loader?name=assets/[name].[hash].[ext]'
  }

I hope this will help you to resolve your problem. 希望这可以帮助您解决问题。

I ended up moving the images into assets/img and then updating my webpack config to specify the directory using publicPath . 我最终将图像移动到assets/img ,然后更新了webpack配置以使用publicPath指定目录。 Here's what it ended up looking like: 最终结果如下所示:

      {
    test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
    loader: 'file-loader',
    include: [resolve('src')],
    options: {
      limit: 10000,
      name: '/[name].[hash:7].[ext]',
      publicPath: path.resolve(__dirname, 'app')
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM