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

Configure both internal and external loaders in 0.17.x #150

Open
daniluk4000 opened this issue Feb 18, 2021 · 2 comments
Open

Configure both internal and external loaders in 0.17.x #150

daniluk4000 opened this issue Feb 18, 2021 · 2 comments

Comments

@daniluk4000
Copy link

daniluk4000 commented Feb 18, 2021

I will just leave it here for someone looking for usage in 0.17.x. It's almost the same, but without babel

Vue

chainWebpack: (config) => {
    const svgRule = config.module.rule('svg');

    svgRule.uses.clear();

    svgRule
        .oneOf('inline')
        .resourceQuery(/inline/)
        .use('vue-loader')
        .loader('vue-loader')
        .end()
        .use('vue-svg-loader')
        .loader('vue-svg-loader')
        .end()
        .end()
        .oneOf('external')
        .use('file-loader')
        .loader('file-loader')
        .options({
            name: `assets/[name].[hash:8].[ext]`,
        });
},

Tested in Vue 2.x

Nuxt

        extend(config: any) {
            const svgRule = config.module.rules.find((rule: any) => rule.test.test('.svg'));

            svgRule.test = /\.(png|jpe?g|gif|webp)$/i;

            config.module.rules.push({
                test: /\.svg$/,
                oneOf: [
                    {
                        resourceQuery: /inline/,
                        use: [
                            'vue-loader',
                            'vue-svg-loader',
                        ],
                    },
                    // Add original url-loader config to resolve every other svg without inline query
                    svgRule.use[0],
                ],
            });

            return config;
        },
@andreasvirkus
Copy link

andreasvirkus commented Apr 13, 2021

I'm trying to get it to work in the reverse - I want to load the SVG inline by default and only load it externally when the resource query is passed in. So I thought I'd need to switch the rule order, as otherwise oneOf would always match the inline import pattern first:

const svgRule = config.module.rule('svg')
svgRule.uses.clear()

svgRule
  .oneOf('external')
  .resourceQuery('/external/')
  .use('file-loader')
  .loader('file-loader')
  .options({ name: 'assets/[name].[hash:8].[ext]' })
  .end()
  .end()
  .oneOf('inline')
  .use('vue-loader') // or babel-loader on 0.16.0
  .loader('vue-loader')
  .end()
  .use('vue-svg-loader')
  .loader('vue-svg-loader')
  .end()
  .end()

So I could do

import ComponentIcon from './svg/icon.svg' // Can be used as <component-icon />
import IconPath from './svg/icon.svg?external' // Can be used as <img :src="iconPath" />

But i still get back a Vue instance for both imports (with both 0.16.0 & 0.17.*) 🤔 @visualfanatic or @daniluk4000 do you spot anything wrong with my rules?

@anish2690
Copy link

@andreasvirkus try with

 svgRule
    .oneOf('external')
    .resourceQuery(/external/)
    .use('file-loader')
    .loader('file-loader')
    .options({ name: 'assets/[name].[hash:8].[ext]' })
    .end()
    .end()
    .oneOf('inline')
    .use('babel-loader')
    .loader('babel-loader')
    .end()
    .use('vue-svg-loader')
    .loader('vue-svg-loader')
    .end()
    .end()

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

3 participants