svg-sprite-loader icon indicating copy to clipboard operation
svg-sprite-loader copied to clipboard

spriteModule path resolution

Open ryan-codingintrigue opened this issue 6 years ago • 5 comments

Do you want to request a feature, report a bug or ask a question? Bug

What is the current behavior? spriteModule does not resolve if including SVG from another NPM package

What is the expected behavior? spriteModule should resolve to the root directory as per the README

If the current behavior is a bug, please provide the steps to reproduce, at least part of webpack config with loader configuration and piece of your code.

webpack.config.js

use: [{
    loader: "svg-sprite-loader",
    options: {
        // Does not work
        spriteModule: "./src/path/to/my-loader.js"
        // Works on MacOS, but not on Windows
        spriteModule: "/src/path/to/my-loader.js"
        // Tried this, but doesn't work on Windows either
        spriteModule: path.normalize("/src/path/to/my-loader.js")
    }
}]

code.js

import sprite from "custom-module/dist/icon.svg";

The error returned is:

Module not found: Error: Can't resolve '../../src/path/to/my-loader.js' in 'C:\Work\node_modules\custom-module\dist'

Please tell us about your environment:

  • Node.js version: 8.12.0
  • webpack version: 4.16.1
  • svg-sprite-loader version: 4.1.1
  • OS type & version: MacOS High Sierra / Windows 10

ryan-codingintrigue avatar Sep 21 '18 14:09 ryan-codingintrigue

Could you please create repo with minimal setup to demonstrate a problem (package.json, webpack config, SVG image and piece of your code). If you don't want to create a repository - you can create a gist with multiple files.

kisenka avatar Sep 27 '18 06:09 kisenka

@kisenka Sure thing! https://github.com/ryan-codingintrigue/svg-loader-308-repro

npm run repro should output the following:

ERROR in ../another-module/dist/felix.svg Module not found: Error: Can't resolve './custom-loader.js' in '/path/to/svg-loader-repro/another-module/dist' @ ../another-module/dist/felix.svg 2:0-40 9:13-19 @ ./index.js

The issue only appears when the SVG is imported from a module, i.e.:

import "another-module/dist/felix.svg";

This works fine:

import "./felix.svg";

ryan-codingintrigue avatar Sep 27 '18 12:09 ryan-codingintrigue

I'm having the same issue. @ryan-codingintrigue did you find a solution?

frimoldi avatar Apr 05 '19 12:04 frimoldi

Ok, look:

First of all, I had found __webpack_public_path__ and then I saw many rows, that contain the next:

node_modules/svg-sprite-loader/lib/runtime-generator.js

image

I think that the problem in this:

image

...But I don't know how can I help to fix it. I think ...(symbol.request.file) returns absolute path: C:/Users/...

(Sry, if I have any grammar mistakes, I'm not native english speaker)

cherrynik avatar Aug 12 '19 11:08 cherrynik

I ever had the same trouble. My solution is:

// the path is base on svg file's location
spriteModule: '../../svg-sprite.config.js'
//"[email protected]"

It works well on both wins and linux.

zhanzizhen avatar Nov 19 '19 02:11 zhanzizhen