Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
nskazki opened this issue Feb 13, 2022 · 0 comments

Comments

@nskazki
Copy link

nskazki commented Feb 13, 2022

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(', '))
        }
      })
    })
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant