vue-loader Documentation

repository·main·Indexed 26 days ago

https://github.com/vuejs/vue-loader

A webpack loader for processing Vue Single-File Components (SFCs). It allows template, script, and style blocks to be processed by specialized loaders while maintaining a unified structure. Requires the VueLoaderPlugin to function, which clones existing webpack rules to handle the different blocks within .vue files.

Tokens
1.4K
Snippets
2
Records
11
Agent score
90%

What's inside vue-loader

  1. Understand Single-File Components (SFCs) in vue-loader

    main

    vue-loader allows you to author Vue components using the Single-File Component (SFC) format, which encapsulates <template>, <script>, and <style> in a single .vue file.

    Key features include:

    • Using different webpack loaders for different blocks (e.g., Sass for <style>, Pug for <template>).
    • Support for custom blocks with custom loader chains.
    • Treating static assets in <style> and <template> as module dependencies.
    • Scoped CSS simulation.
    • State-preserving hot-reloading.
    <template>
      <div class="example">{{ msg }}</div>
    </template>
    
    <script>
    export default {
      data() {
        return {
          msg: 'Hello world!',
        }
      },
    }
    </script>
    
    <style>
    .example {
      color: red;
    }
    </style>
  2. Configure vue-loader options for v16+

    main

    Versions 16 and above support several specific options to customize SFC processing:

    • reactivityTransform: (boolean) Enables Vue Reactivity Transform for SFCs.
    • customElement: (boolean | RegExp) Enables custom elements mode. In this mode, <style> tags are inlined as strings under the component's styles option. When used with defineCustomElement from Vue core, these styles are injected into the shadow root.
      • Default: /\.ce\.vue$/ (matches files ending in .ce.vue).
      • Setting to true processes all .vue files in custom element mode.
    • enableTsInTemplate: (boolean, 16.8+) Allows TypeScript expressions in templates when the <script> block has lang="ts". Defaults to true.
      • Note: If using ts-loader, this may cause issues with hot-reloading templates in isolation. You can set this to false to avoid the issue, or switch to esbuild-loader for faster transpilation without the hot-reload penalty.
  3. Configure VueLoaderPlugin requirements

    main

    For VueLoaderPlugin to work correctly, your Webpack configuration must satisfy the following requirements:

    1. Root-level rule for .vue files: There must be at least one rule at the top level of your module.rules array that matches .vue or .vue.html files.
    2. Include vue-loader: The rule matching .vue files must include vue-loader in its use array.
    3. No oneOf support: The rule matching .vue files cannot use the oneOf property; it must be a standard rule structure.
  4. Integrate Vue with Webpack using VueLoaderPlugin

    main
    To use .vue Single File Components (SFCs) in a Webpack project, you must instantiate and apply the VueLoaderPlugin. The plugin automatically configures your Webpack rules to handle the different blocks within a .vue file (like <template>, <script>, and <style>) by cloning your existing rules and applying them to the appropriate language blocks.
  5. Configure VueLoaderOptions

    main

    The VueLoaderOptions interface defines the configuration available to the vue-loader. Use these options to customize how SFCs (Single File Components) are processed, including template compilation, script parsing, and custom element behavior.

    interface VueLoaderOptions {
      babelParserPlugins?: SFCScriptCompileOptions['babelParserPlugins']
      transformAssetUrls?: SFCTemplateCompileOptions['transformAssetUrls']
      compiler?: TemplateCompiler | string
      compilerOptions?: CompilerOptions
      /** @deprecated */
      reactivityTransform?: boolean
      /** @experimental */
      propsDestructure?: boolean
      /** @experimental */
      defineModel?: boolean
      customElement?: boolean | RegExp
      hotReload?: boolean
      exposeFilename?: boolean
      appendExtension?: boolean
      enableTsInTemplate?: boolean
      experimentalInlineMatchResource?: boolean
      isServerBuild?: boolean
    }
  6. Error: vue-loader not found in rules

    main

    If the plugin finds a rule for .vue files but that rule does not actually use vue-loader, it will throw the following error:

    [VueLoaderPlugin Error] No matching use for vue-loader is found. Make sure the rule matching .vue files include vue-loader in its use.

  7. Error: vue rules with oneOf not supported

    main

    The VueLoaderPlugin currently does not support rules that use the oneOf property to match .vue files. If such a rule is detected, it will throw:

    [VueLoaderPlugin Error] vue-loader currently does not support vue rules with oneOf.

  8. Error: No matching rule for .vue files

    main

    If the plugin cannot find a rule that matches .vue or .vue.html files in your Webpack configuration, it will throw the following error:

    [VueLoaderPlugin Error] No matching rule for .vue files found. Make sure there is at least one root-level rule that matches .vue or .vue.html files.