Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify where to inject chunks in html template with vue-cli?

I am using vue-cli generated project and specified pages in vue.config.js. Vue is automatically injecting the JS chunks at the end of the HTML template file. But in I need to specify a different location to place them in that file.

Standard Webpack syntax does not really help (<%= htmlWebpackPlugin ...). It actually injects the chunks there, but then the chunks are again inserted at the end of the page anyway.

Is there a correct way, how specify exact place in HTML file for building with vue-cli, instead of the default end of the file (or before which does not exist in my template)?

Template (IndexTemplate.cshtml)

...

<scripts-section>
    <% for (var chunk in htmlWebpackPlugin.files.chunks) { %>
    <script src="<%= htmlWebpackPlugin.files.chunks[chunk].entry %>"></script><% } %>

</scripts-section>

Generated File (Index.cshtml)

...

<scripts-section>
    <script src="/dist/vue/runtime.js"></script>
    <script src="/dist/vue/sentry.vendor.js"></script>
    <script src="/dist/vue/axios.vendor.js"></script>
    <script src="/dist/vue/vue.vendor.js"></script>
    <script src="/dist/vue/vendors~Search.js"></script>
    <script src="/dist/vue/Search.js"></script>

</scripts-section>

<script type="text/javascript" src="/dist/vue/runtime.js"></script><script type="text/javascript" src="/dist/vue/sentry.vendor.js"></script><script type="text/javascript" src="/dist/vue/axios.vendor.js"></script><script type="text/javascript" src="/dist/vue/vue.vendor.js"></script><script type="text/javascript" src="/dist/vue/vendors~Search.js"></script><script type="text/javascript" src="/dist/vue/Search.js"></script>

As you can see, it gets inserted twice. Having it at the end of the file is not an option here.

vue.config.js

module.exports = {
    publicPath: '/dist/vue/',
    outputDir: './wwwroot/dist/vue/',
    runtimeCompiler: true,
    pages: {
        Search: {
            entry: 'Frontend/vue/Search/main.ts',
            template: 'Views/Mentors/IndexTemplate.cshtml',
            filename: '../../../Views/Mentors/Index.cshtml',
            chunks: ['runtime', 'vue.vendor', 'axios.vendor', 'sentry.vendor', 'Search', 'vendors~Search']
        }
    },
    chainWebpack: config => {
        config.optimization
            .runtimeChunk('single')
            .splitChunks({
                chunks: 'all',
                cacheGroups: {
                    vue: {
                        test: /[\\/]node_modules[\\/](.*vue.*)[\\/]/,
                        name: 'vue.vendor',
                        chunks: 'all',
                    },
                    // ...
                }
            });
    }
}

I expect, that the JS chunks would be injected only in the <scripts-section> element.

like image 609
Jan Pospíšil Avatar asked May 26 '19 00:05

Jan Pospíšil


People also ask

How do I add Vue component to HTML?

The simplest way to get started with Vue is to grab the development version script for it and add it to the head tag of your HTML file. Then you can start Vue code inside the HTML file inside of script tags. And have the Vue code connect up with an existing element on your HTML page.

Does Vue compile to HTML?

All Vue templates are syntactically valid HTML that can be parsed by spec-compliant browsers and HTML parsers. Under the hood, Vue compiles the templates into highly-optimized JavaScript code.

Which Vue Command inspect and modify the config?

You can edit this file directly with your editor of choice to change the saved options. You can also use the vue config command to inspect or modify the global CLI config.


1 Answers

For future reference of others:

In the vue.config.js file, we also have to add the config for html plugin, and set inject to false:

config.plugin("html").tap((args) => {
  args[0].inject = false;
  return args;
});

The complete edited version of the vue.config.js is given below for clarity:

module.exports = {
  publicPath: "/dist/vue/",
  outputDir: "./wwwroot/dist/vue/",
  runtimeCompiler: true,
  pages: {
    Search: {
      entry: "Frontend/vue/Search/main.ts",
      template: "Views/Mentors/IndexTemplate.cshtml",
      filename: "../../../Views/Mentors/Index.cshtml",
      chunks: ["runtime", "vue.vendor", "axios.vendor", "sentry.vendor", "Search", "vendors~Search"],
    },
  },
  chainWebpack: (config) => {
    config.optimization.runtimeChunk("single").splitChunks({
      chunks: "all",
      cacheGroups: {
        vue: {
          test: /[\\/]node_modules[\\/](.*vue.*)[\\/]/,
          name: "vue.vendor",
          chunks: "all",
        },
        // ...
      },
    });
    config.plugin("html").tap((args) => {
      args[0].inject = false;
      return args;
    });
  },
};

For a more detailed example, visit this link.

like image 74
user3980196 Avatar answered Sep 23 '22 00:09

user3980196