Static / Fixed filenames for generated vue-cli builds
04 Jul 18 20:00
2 minutes read

As you may know, the release of vue-cli 3 is getting close (currently at RC3 status).

I really like the streamlined way of starting projects and having a good baseline for development (especially when trying to setup a good starting point in house, that has documentation and it’s actively developed).

However the default setup isn’t friendly with legacy projects because vue-cli implicitly adds a hash to the generated filenames. That’s great if you’re starting a new project/SPA because it’s like a built in cache buster but doesn’t help if you’re trying to integrate it with your favourite c#/php/python/ruby etc application.

In order to change this let’s quickly look over the following config

let assetsDir = "assets";

module.exports = {
  assetsDir: assetsDir,

  configureWebpack: {
    output: {
      filename: assetsDir + "/[name].js",
      chunkFilename: assetsDir + "/[name].js"
    }
  },

  chainWebpack: config => {
    if (config.plugins.has("extract-css")) {
      const extractCSSPlugin = config.plugin("extract-css");
      extractCSSPlugin &&
        extractCSSPlugin.tap(() => [
          {
            filename: assetsDir + "/[name].css",
            chunkFilename: assetsDir + "/[name].css"
          }
        ]);
    }

    config.plugins
      .delete("html")
      .delete("prefetch")
      .delete("preload");
  }
};

Since assetsDir isn’t applied to custom filenames, we do a small workaround defining a variable, and using that for our generated filenames.

We’re then setting the javascript and the css filenames using their respective options and deleting the html plugin that generates the index.html file with it’s ‘dependencies’ prefetch and preload.

Now you are free to use npm run build --modern and setup Modern Mode

An update:

The official docs reference this setup now in a cleaner way

« Back to posts