2

If there is a custom block in vue file. E.g.

<router3333>
    path: /:category/:segment
</router3333>

It will be compiled and work fine with vue loader configured as the only one in the .use clause https://i.imgur.com/BQxmrcF.png

If i add another loader into use clause that do exactly nothing for example whole loader

module.exports = function (source) { return source }

Compilation will fail with error

Module parse failed: Unexpected token (24:17)
File was processed with these loaders:
 * ./my-loader.js
 * ./node_modules/vue-loader/lib/index.js
You may need an additional loader to handle the result of these loaders.
|
|
> path: /:category/:segment

The output of vue-loader in both cases is same

import { render, staticRenderFns } from "./xxxc.vue?vue&type=template&id=0067048f&"                                                                                                       01:13:55
import script from "./xxxc.vue?vue&type=script&lang=ts&"
export * from "./xxxc.vue?vue&type=script&lang=ts&"


/* normalize component */
import normalizer from "!../../node_modules/vue-loader/lib/runtime/componentNormalizer.js"
var component = normalizer(
  script,
  render,
  staticRenderFns,
  false,
  null,
  null,
  null

)

/* custom blocks */
import block0 from "./xxxc.vue?vue&type=custom&index=0&blockType=router3333"
if (typeof block0 === 'function') block0(component)

export default component.exports

So just adding an loader that do nothing in use clause make custom block fail. Whats happening here and how to avoid it ?

Here is repro https://codesandbox.io/s/codesandbox-nuxt-4dqmy If in nuxt.config set vuetify.treeShake: true - it will do what is described here, e.g. add another loader to use clause and it will cause error. The loader code itself doesn't matter as it happen with empty loader too.

7
  • Could you provide the related webpack config section? Commented Jul 15, 2019 at 8:58
  • @MaxSinev not using webpack directly, but rather via nuxt. And the relevant change to add new loader is done via api. vueRule.use.unshift({ loader: require.resolve('./loader'), options: this.options }) Commented Jul 15, 2019 at 9:19
  • @MaxSinev i had added a repro link into issue Commented Jul 15, 2019 at 11:42
  • The error pops up whether you add the loader or not. The repro does not contain any my-loader. Commented Jul 15, 2019 at 16:42
  • @MunimMunna yes, the error popups whether i add new loader to use clause or no - no error. Disable treeShake and there wont be error. Thats the exact point. The repro contains vuetify-loader added, but the loader content isnt important at all, it could be replaced with empty loader e.g. my-loader with exact same result like i said . Commented Jul 15, 2019 at 17:08

1 Answer 1

1
+100

Whats happening here ...?

The answer is pretty simple - because you have another one loader for vue files(that's why even vuetify-loader fails your build), and it can be proved with vue-loader source code function which is responsible for this. It just checks number of loaders and decides to add import statement for a custom block or just pass empty string [code block].

... and how to avoid it ?

Vue-loader has docs section for the custom blocks functionality. You can just create a new rule with blockType query param to process your custom router3333 block code:

rules: [
  // ...
  {
    resourceQuery: /blockType=router3333/,
    loader: require.resolve('./my-loader')
  }
  // ...
]

So as you can see you should use your block tag as type in the code above.

And if you don't want to do anything for now with your custom block content just return an empty string from your loader function.

Vue-loader Custom Blocks

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks :) That code piece from vue-loader look exactly what i was looking for, i didnt digged into vue loader code to find out it (
@Aldarund And there was also the same internal issue with cache-loader for vue files and they added kind of exception for that case(in the function from my link in the answer) github.com/vuejs/vue-loader/issues/1394 :)
yes, i noticed it in code. Not very robust solution to hardcode ignored loaders like this :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.