-
Notifications
You must be signed in to change notification settings - Fork 71
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
Add basic HMR support for Svelte 3 (and svelte-native) #107
Closed
Closed
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
45f83ef
remove hard dep on svelte
rixo 23fa354
style: remove trailing space
rixo eda88c6
extract makeHot from index.js
rixo 5b568c8
add support for HMR with svelte 3
rixo dcf722e
Merge remote-tracking branch 'upstream/master' into hmr
rixo 9dd5eec
add support for HMR with svelte-native
rixo 21e0276
update native support
halfnelson 733852f
Merge pull request #3 from halfnelson/hmr-native-fix
rixo b2c1d29
remove global polution, now that it isn't needed anymore
rixo 81cfdd2
style: remove trailing space
rixo 189a246
refactor: extract svelte resolution to its own module
rixo 74636fb
resolve svelte from application entrypoint - fixes #109
rixo 0089526
add support for overriding svelte location by env variable
rixo e106329
Merge branch 'fix-link'
rixo 7787618
refactor: move makeHot to resolveSvelte (because it depends on svelte…
rixo 75bc4a3
better debug name for file with multiple extensions
rixo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,29 @@ | ||
const posixify = require('./posixify'); | ||
|
||
const hotApi = require.resolve('./hot-api.js'); | ||
|
||
function makeHot(id, code, hotOptions) { | ||
const options = JSON.stringify(hotOptions); | ||
const replacement = ` | ||
if (module.hot) { | ||
const { configure, register, reload } = require('${posixify(hotApi)}'); | ||
|
||
module.hot.accept(); | ||
|
||
if (!module.hot.data) { | ||
// initial load | ||
configure(${options}); | ||
$2 = register(${id}, $2); | ||
} else { | ||
// hot update | ||
$2 = reload(${id}, $2); | ||
} | ||
} | ||
|
||
export default $2; | ||
`; | ||
|
||
return code.replace(/(export default ([^;]*));/, replacement); | ||
} | ||
|
||
module.exports = makeHot; |
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,3 @@ | ||
module.exports = function posixify(file) { | ||
return file.replace(/[/\\]/g, '/'); | ||
}; |
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,39 @@ | ||||||||
const path = require('path'); | ||||||||
|
||||||||
const resolveSvelte = () => { | ||||||||
const { SVELTE } = process.env; | ||||||||
|
||||||||
const absolute = (name, base) => | ||||||||
path.isAbsolute(name) ? name : path.join(base, name); | ||||||||
|
||||||||
if (SVELTE) { | ||||||||
return { | ||||||||
req: require, | ||||||||
base: absolute(SVELTE, process.cwd()), | ||||||||
}; | ||||||||
} else { | ||||||||
return { | ||||||||
req: require.main.require.bind(require.main), | ||||||||
base: 'svelte', | ||||||||
}; | ||||||||
} | ||||||||
}; | ||||||||
|
||||||||
const { req, base } = resolveSvelte(); | ||||||||
|
||||||||
const { version } = req(`${base}/package.json`); | ||||||||
|
||||||||
const major_version = +version[0]; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
const { compile, preprocess } = | ||||||||
major_version >= 3 ? req(`${base}/compiler`) : req(`${base}`); | ||||||||
|
||||||||
const makeHot = | ||||||||
major_version >= 3 ? require('./svelte3/make-hot') : require('./make-hot'); | ||||||||
|
||||||||
module.exports = { | ||||||||
major_version, | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
compile, | ||||||||
preprocess, | ||||||||
makeHot, | ||||||||
}; |
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,63 @@ | ||
/* global Symbol */ | ||
|
||
// This module monkey patches Page#showModal in order to be able to | ||
// access from the HMR proxy data passed to `showModal` in svelte-native. | ||
// | ||
// Data are stored in a opaque prop accessible with `getModalData`. | ||
// | ||
// It also switches the `closeCallback` option with a custom brewed one | ||
// in order to give the proxy control over when its own instance will be | ||
// destroyed. | ||
// | ||
// Obviously this method suffer from extreme coupling with the target code | ||
// in svelte-native. So it would be wise to recheck compatibility on SN | ||
// version upgrades. | ||
// | ||
// Relevant code is there (last checked version): | ||
// | ||
// https://github.com/halfnelson/svelte-native/blob/08702e6b178644f43052f6ec0a789a51e800d21b/src/dom/svelte/StyleElement.ts | ||
// | ||
|
||
// FIXME should we override ViewBase#showModal instead? | ||
import { Page } from 'tns-core-modules/ui/page'; | ||
|
||
const prop = | ||
typeof Symbol !== 'undefined' | ||
? Symbol('hmr_svelte_native_modal') | ||
: '___HMR_SVELTE_NATIVE_MODAL___'; | ||
|
||
const sup = Page.prototype.showModal; | ||
|
||
let patched = false; | ||
|
||
export const patchShowModal = () => { | ||
// guard: already patched | ||
if (patched) return; | ||
patched = true; | ||
|
||
Page.prototype.showModal = function(modalView, options) { | ||
const modalData = { | ||
originalOptions: options, | ||
closeCallback: options.closeCallback, | ||
}; | ||
|
||
modalView[prop] = modalData; | ||
|
||
// Proxies to a function that can be swapped on the fly by HMR proxy. | ||
// | ||
// The default is still to call the original closeCallback from svelte | ||
// navtive, which will destroy the modal view & component. This way, if | ||
// no HMR happens on the modal content, normal behaviour is preserved | ||
// without the proxy having any work to do. | ||
// | ||
const closeCallback = (...args) => { | ||
return modalData.closeCallback(...args); | ||
}; | ||
|
||
const temperedOptions = Object.assign({}, options, { closeCallback }); | ||
|
||
return sup.call(this, modalView, temperedOptions); | ||
}; | ||
}; | ||
|
||
export const getModalData = modalView => modalView[prop]; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.