This repository has been archived by the owner on Aug 7, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add initial HMR support for plain JS/TS apps (#645)
- Loading branch information
Showing
13 changed files
with
332 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module.exports.reload = ` | ||
if (module.hot) { | ||
module.hot.accept(); | ||
module.hot.dispose(() => { | ||
setTimeout(() => { | ||
global.__hmrLivesyncBackup(); | ||
}); | ||
}) | ||
} | ||
`; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
const log = console; | ||
const refresh = 'Please refresh the page.'; | ||
const hotOptions = { | ||
ignoreUnaccepted: true, | ||
ignoreDeclined: true, | ||
ignoreErrored: true, | ||
onUnaccepted(data) { | ||
const chain = [].concat(data.chain); | ||
const last = chain[chain.length - 1]; | ||
|
||
if (last === 0) { | ||
chain.pop(); | ||
} | ||
|
||
log.warn(`Ignored an update to unaccepted module ${chain.join(' ➭ ')}`); | ||
}, | ||
onDeclined(data) { | ||
log.warn(`Ignored an update to declined module ${data.chain.join(' ➭ ')}`); | ||
}, | ||
onErrored(data) { | ||
log.warn( | ||
`Ignored an error while updating module ${data.moduleId} <${data.type}>` | ||
); | ||
log.warn(data.error); | ||
}, | ||
}; | ||
|
||
let lastHash; | ||
|
||
function upToDate() { | ||
return lastHash.indexOf(__webpack_hash__) >= 0; | ||
} | ||
|
||
function result(modules, appliedModules) { | ||
const unaccepted = modules.filter( | ||
(moduleId) => appliedModules && appliedModules.indexOf(moduleId) < 0 | ||
); | ||
|
||
if (unaccepted.length > 0) { | ||
let message = 'The following modules could not be updated:'; | ||
|
||
for (const moduleId of unaccepted) { | ||
message += `\n ⦻ ${moduleId}`; | ||
} | ||
log.warn(message); | ||
} | ||
|
||
if (!(appliedModules || []).length) { | ||
console.info('No Modules Updated.'); | ||
} else { | ||
const message = ['The following modules were updated:']; | ||
|
||
for (const moduleId of appliedModules) { | ||
message.push(` ↻ ${moduleId}`); | ||
} | ||
|
||
console.info(message.join('\n')); | ||
|
||
const numberIds = appliedModules.every( | ||
(moduleId) => typeof moduleId === 'number' | ||
); | ||
if (numberIds) { | ||
console.info( | ||
'Please consider using the NamedModulesPlugin for module names.' | ||
); | ||
} | ||
} | ||
} | ||
|
||
function check(options) { | ||
module.hot | ||
.check() | ||
.then((modules) => { | ||
if (!modules) { | ||
log.warn( | ||
`Cannot find update. The server may have been restarted. ${refresh}` | ||
); | ||
return null; | ||
} | ||
|
||
return module.hot | ||
.apply(hotOptions) | ||
.then((appliedModules) => { | ||
if (!upToDate()) { | ||
log.warn("Hashes don't match. Ignoring second update..."); | ||
// check(options); | ||
} | ||
|
||
result(modules, appliedModules); | ||
|
||
if (upToDate()) { | ||
console.info('App is up to date.'); | ||
} | ||
}) | ||
.catch((err) => { | ||
const status = module.hot.status(); | ||
if (['abort', 'fail'].indexOf(status) >= 0) { | ||
log.warn(`Cannot apply update. ${refresh}`); | ||
log.warn(err.stack || err.message); | ||
if (options.reload) { | ||
window.location.reload(); | ||
} | ||
} else { | ||
log.warn(`Update failed: ${err.stack}` || err.message); | ||
} | ||
}); | ||
}) | ||
.catch((err) => { | ||
const status = module.hot.status(); | ||
if (['abort', 'fail'].indexOf(status) >= 0) { | ||
log.warn(`Cannot check for update. ${refresh}`); | ||
log.warn(err.stack || err.message); | ||
} else { | ||
log.warn(`Update check failed: ${err.stack}` || err.message); | ||
} | ||
}); | ||
} | ||
|
||
if (module.hot) { | ||
console.info('Hot Module Replacement Enabled. Waiting for signal.'); | ||
} else { | ||
console.error('Hot Module Replacement is disabled.'); | ||
} | ||
|
||
module.exports = function update(currentHash, options) { | ||
lastHash = currentHash; | ||
if (!upToDate()) { | ||
const status = module.hot.status(); | ||
|
||
if (status === 'idle') { | ||
console.info('Checking for updates to the bundle.'); | ||
check(options); | ||
} else if (['abort', 'fail'].indexOf(status) >= 0) { | ||
log.warn( | ||
`Cannot apply update. A previous update ${status}ed. ${refresh}` | ||
); | ||
} | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,23 @@ | ||
const { runWebpackCompiler } = require("./compiler"); | ||
|
||
module.exports = function ($logger, $liveSyncService, hookArgs) { | ||
if (hookArgs.config) { | ||
const appFilesUpdaterOptions = hookArgs.config.appFilesUpdaterOptions; | ||
if (appFilesUpdaterOptions.bundle) { | ||
const platforms = hookArgs.config.platforms; | ||
return Promise.all(platforms.map(platform => { | ||
const env = hookArgs.config.env || {}; | ||
const config = { | ||
env, | ||
platform, | ||
bundle: appFilesUpdaterOptions.bundle, | ||
release: appFilesUpdaterOptions.release, | ||
watch: true | ||
}; | ||
module.exports = function ($logger, $liveSyncService, $options, hookArgs) { | ||
if (hookArgs.config) { | ||
const appFilesUpdaterOptions = hookArgs.config.appFilesUpdaterOptions; | ||
if (appFilesUpdaterOptions.bundle) { | ||
const platforms = hookArgs.config.platforms; | ||
return Promise.all(platforms.map(platform => { | ||
const env = hookArgs.config.env || {}; | ||
env.hmr = !!$options.hmr; | ||
const config = { | ||
env, | ||
platform, | ||
bundle: appFilesUpdaterOptions.bundle, | ||
release: appFilesUpdaterOptions.release, | ||
watch: true | ||
}; | ||
|
||
return runWebpackCompiler(config, hookArgs.projectData, $logger, $liveSyncService, hookArgs); | ||
})); | ||
} | ||
} | ||
return runWebpackCompiler(config, hookArgs.projectData, $logger, $liveSyncService, hookArgs); | ||
})); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const { reload } = require("./hot-loader-helper"); | ||
|
||
module.exports = function (source) { | ||
return `${source};${reload}`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const { reload } = require("./hot-loader-helper"); | ||
|
||
module.exports = function (source) { | ||
return `${source};${reload}`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const { reload } = require("./hot-loader-helper"); | ||
|
||
module.exports = function (source) { | ||
return `${source};${reload}`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.