Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use with webpack alias #16

Open
Hulkmaster opened this issue Nov 18, 2019 · 2 comments
Open

Cannot use with webpack alias #16

Hulkmaster opened this issue Nov 18, 2019 · 2 comments

Comments

@Hulkmaster
Copy link

When i write rule like
background-image: svg("~@images/table.svg", (path: (fill: $primary_1)));

i get error
ENOENT: no such file or directory, open '/var/www/~@images/table.svg'

should add somethink like

'getPath($filePath)': function (filePath, done) {
                  /**
                   * That loader helper function will resolve webpack aliased imports into absolute strings
                   * getPath("~@Images/icon.svg") -> /var/www/app/static/img/icon.svg
                   *
                   * Sass import might include webpack alias (like ~@Images/)
                   * These aliased imports always start with '~', so if we see one - we modify it into valid webpack alias
                   */
                  const filePathValue = filePath.getValue();
                  const resultPath = filePathValue.indexOf('~') !== -1 ? filePathValue.substr(1) : filePathValue;
                  loaderContext.resolve('', resultPath, (error, result) => {
                    if (error) {
                      throw error;
                    }
                    // eslint-disable-next-line global-require
                    const SassString = require('node-sass').types.String;
                    done(SassString(result));
                  });
                },
@Hulkmaster
Copy link
Author

Hulkmaster commented Nov 18, 2019

thats the hack i have for now

const sassInlineSvg = require('sass-inline-svg')('/', { optimize: true });
// ...
{
            loader: 'sass-loader',
            options: {
              functions: (loaderContext) => ({
                svg(filePath, options, done) {
                  /**
                   * That loader helper function will resolve webpack aliased imports into absolute strings
                   * svg("~@images/icon.svg") -> /var/www/app/static/img/icon.svg
                   *
                   * Sass import might include webpack alias (like ~@images/)
                   * These aliased imports always start with '~', so if we see one - we modify it into valid webpack alias
                   */
                  const filePathValue = filePath.getValue();
                  const resultPath = filePathValue.indexOf('~') !== -1 ? filePathValue.substr(1) : filePathValue;
                  loaderContext.resolve('', resultPath, (error, result) => {
                    if (error) {
                      throw error;
                    }
                    // eslint-disable-next-line global-require
                    const SassString = require('node-sass').types.String;
                    done(sassInlineSvg(SassString(result), options));
                  });
                },
              }),
            },
          },

@SergProduction
Copy link

Thanks.
Сould you put it in the readme.
I tried for a long time to set up your module in webpack until I stumbled upon this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants