Skip to content

It should actually do a full reload when it's required, not just reporting a need #94

@nskazki

Description

@nskazki

I think it should actually do a full reload when it's required, not just reporting a need because it's really simple to miss the report among other HMR/app's messages. If making it a default behaviour is too much, an option would suffice.

console.warn(
'Something went wrong during Vue component hot-reload. Full reload required.'
)
}

console.warn(
'Root or manually mounted instance modified. Full reload required.'
)

To anyone who also think so, you can use this snippet until the option is introduce or the default behaviour is changed:

// webpack.config.js
const VueReload = require('../plugins/vue-reload')

module.exports = {
  plugins: [new VueReload()]
}

// vue-reload.js
module.exports = class VueReloadPlugin {
  apply(compiler) {
    compiler.hooks.compilation.tap('VueReloadPlugin', compilation => {
      compilation.hooks.processAssets.tap({ name: 'VueReloadPlugin', stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONS  }, () => {
        const patched = []

        for (const chunk of compilation.chunks) {
          const anyVueModules = chunk.getModules().some((module) => /\.vue/.test(module.resource))
          if (!anyVueModules) {
            continue
          }

          for (const file of chunk.files) {
            const isJSFile = /\.js/.test(file)
            if (!isJSFile) {
              continue
            }

            compilation.updateAsset(file, (input) => {
              // the \\n part is necessary in case the source code is turned into an eval string
              const re = /(console\.warn\([\s\\n]*['"](?:Something went wrong during Vue component hot-reload.|Root or manually mounted instance modified.) Full reload required.['"][\s\\n]*\))/g
              const matches = Array.from(input.source().matchAll(re))
              if (matches.length === 0) {
                return input
              }

              const output = new ReplaceSource(input)
              for (const match of matches) {
                const position = match.index + match[1].length
                output.insert(position, ';window.location.reload();')
              }

              patched.push(file)
              return output
            })
          }
        }

        if (patched.length !== 0) {
          console.info('vue-reload-plugin: patched', patched.join(', '))
        }
      })
    })
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions