2

I am using webpack with vue and lottie to play some SVG animations.

when using "@/assets/animations/images/img_3.png" straight in a component, I can see the image is loaded as an encoded img src - meaning the @ resolves to my 'src' directory.

enter image description here

But, when importing a JSON file (a bodymovin JSON animation file) and using the exact same "@/assets/animations/images/img_3.png" the images are loaded in without the path being resolved:

enter image description here

My webpack confirguration is as follows:

'use strict'
const path = require('path')
const utils = require('./utils')
const config = require('../config')
const vueLoaderConfig = require('./vue-loader.conf')

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

const createLintingRule = () => ({
  test: /\.(js|vue)$/,
  loader: 'eslint-loader',
  enforce: 'pre',
  include: [resolve('src'), resolve('test')],
  options: {
    formatter: require('eslint-friendly-formatter'),
    emitWarning: !config.dev.showEslintErrorsInOverlay
  }
})

module.exports = {
  context: path.resolve(__dirname, '../'),
  entry: {
    app: './src/main.js'
  },
  output: {
    path: config.build.assetsRoot,
    filename: '[name].js',
    publicPath: process.env.NODE_ENV === 'production'
      ? config.build.assetsPublicPath
      : config.dev.assetsPublicPath
  },
  resolve: {
    extensions: ['.js', '.vue', '.json'],
    alias: {
      'vue$': 'vue/dist/vue.esm.js',
        '@': resolve('src'),
    }
  },
  module: {
    rules: [
      ...(config.dev.useEslint ? [createLintingRule()] : []),
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: vueLoaderConfig
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        include: [resolve('src'), resolve('test'), resolve('node_modules/webpack-dev-server/client')]
      },
      {
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: utils.assetsPath('img/[name].[hash:7].[ext]')
        }
      },
      {
        test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: utils.assetsPath('media/[name].[hash:7].[ext]')
        }
      },
      {
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
        }
      }
    ]
  },
  node: {
    // prevent webpack from injecting useless setImmediate polyfill because Vue
    // source contains it (although only uses it if it's native).
    setImmediate: false,
    // prevent webpack from injecting mocks to Node native modules
    // that does not make sense for the client
    dgram: 'empty',
    fs: 'empty',
    net: 'empty',
    tls: 'empty',
    child_process: 'empty'
  }
}

I have even tried using relative paths but no luck, the images are never loaded in as an encoded src.

My Directory structure:

  • src
    • assets
      • animations
      • images
        • XXX.png
    • components
      • animated_svg
        • test_animation.vue
user3195250
  • 127
  • 2
  • 13
  • did you find a solution? I'm having the exact same problem – goleon Nov 07 '18 at 13:14
  • Same problem here... – realnot Mar 07 '19 at 19:22
  • I know this loader im about to suggest has nothing to do with Lottie, however the issue you are trying to fix is very simple to write a loader for.. so pretty much any similar loader would work in your case, one that comes on top of my mind now is this one: https://github.com/mientjan/gltf-webpack-loader which essentially finds all URLs inside a JSON file, and `required` it using the webpack module resolution, so this loader would certainly work for you even thou it has nothing to do with lottie :D – Rafael Milewski Mar 11 '20 at 17:44

0 Answers0