Skip to content

vue-cli3 & html-webpack-plugin 4.x How to inject cdn dynamically? #1333

Closed
@DevinWell1ee

Description

@DevinWell1ee

Expected behaviour

in html, Can inject cdn dynamically through htmlWebpackPlugin.options

Current behaviour

it not work

Config

Copy the minimal webpack.config.js to produce this issue:

module.exports = {
  chainWebpack: config => {
     config
      .plugin('html')
      .use(HtmlWebpackPlugin)
      .tap(options => {
        options.cdn = ['production', 'stage'].includes(process.env.NODE_ENV) ? cdn['build'] : cdn['dev']
        return options
      })
  }
}
<% for (var i in htmlWebpackPlugin.options.cdn&&htmlWebpackPlugin.options.cdn.js) { %>
    <script src="<%= htmlWebpackPlugin.options.cdn.js[i] %>"></script>
    <% } %>

i don't know if delete preload caused it .
i delete reload, because htmlWebpackPlugin4.x seems to be in conflict with preload

Activity

jantimon

jantimon commented on Mar 24, 2020

@jantimon
Owner

Can you also change the webpack publicPath to be that cdn?

That would be picked up by the html-webpack-plugin automatically.

lijason1121

lijason1121 commented on May 18, 2020

@lijason1121

make sure your value of "publicPath" property in "output" is your cdn address.

  output: {
    path: __dirname + '/dist',
    filename:[name].[contenthash:8].bundle.js',
    chunkFilename: [name].[contenthash:8].chunk.js', 
    publicPath: process.env.NODE_ENV === 'production' ?
      `your/cdn/address` :
      ''
  },
stale

stale commented on Dec 12, 2020

@stale

This issue had no activity for at least half a year. It's subject to automatic issue closing if there is no activity in the next 15 days.

gary-dgc

gary-dgc commented on Oct 26, 2022

@gary-dgc

see you [.use(HtmlWebpackPlugin)], guess you get error:
[ Error: Cannot call .tap() on a plugin that has not yet been defined. Call plugin('html').use(<Plugin>) first.].
if you configured multi-pages in vue.config.js, the [plugin('html')] not exist in config, coz it's replaced with the multi-pages settings, to identify the correct html plugin name, you need to run command to list all the plugins:

npx vue-cli-service inspect --plugins
[
  'vue-loader',
  'define',
  'case-sensitive-paths',
  'friendly-errors',
  'html-index',
  'html-demo',
  'preload-index',
  'prefetch-index',
  'preload-oeditor',
  'prefetch-oeditor',
  'copy',
  'ThemeColorReplacer',
  'IgnorePlugin'
]

Now you can use following to modify the plugin options

      config.plugin('html-index')
        .tap(args => {
          args[0].cdn = assetsCDN
          return args
        })
      config.plugin('html-demo')
        .tap(args => {
          args[0].cdn = assetsCDN
          return args
        })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @lijason1121@jantimon@gary-dgc@DevinWell1ee

        Issue actions

          vue-cli3 & html-webpack-plugin 4.x How to inject cdn dynamically? · Issue #1333 · jantimon/html-webpack-plugin