feat: docker compose maybe

This commit is contained in:
2023-11-13 16:10:04 -05:00
parent 180b261e40
commit b625ccd8d6
8031 changed files with 2182966 additions and 0 deletions

13
node_modules/svelte-hmr/LICENSE generated vendored Normal file
View File

@ -0,0 +1,13 @@
Copyright 2019 - 2020, rixo and the svelte-hmr contributors.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

223
node_modules/svelte-hmr/README.md generated vendored Normal file
View File

@ -0,0 +1,223 @@
# svelte-hmr
HMR commons for Svelte 3.
This packages provides shared dependencies for implementing Svelte HMR in any bundler plugins.
If you want to _use_ HMR in your Svelte project, what you need is a HMR enabled plugin for your bundler (e.g. Rollup or Webpack). You'll find a list of available tools at the end of this doc.
On the other hand, if you are really developing a plugin... Sorry, no docs for now! Drop me a line, I'd be happy to help!
## Features
- update Svelte components in place
- preservation of component state, including local state (i.e. `let` vars in your components)
- inject CSS instead of doing a full replace when only the component's CSS has changed, with compatible HMR APIs (`rollup-plugin-hot`, Nollup, and Snowpack for now)
## Options
Those are the HMR options that are implemented by `svelte-hmr` itself, and so should be supported by any plugin listed bellow (especially if they include a link pointing to this section). How to pass those options is specific to each plugins, so refer to their specific docs on this point.
#### noReload
Type: `bool`<br>
Default: `false`
By default, `svelte-hmr` will trigger a full browser reload when it detects an error that will prevent subsequent HMR updates to be applied correctly. Set this to `true` to prevent automatic reloads. Note that Svelte Native does _not_ execute in a browser, and so this option has no effect there.
#### noPreserveState
**Deprecated: removed and default changed from version 0.12. Use `preserveLocalState` instead.**
#### preserveLocalState
Type: `bool`<br>
Default: `false`
Enable [preservation of local state](#preservation-of-local-state) for all variables in all components.
#### noPreserveStateKey
Type: `string`<br>
Default: `'@hmr:reset'` (also accepts legacy `'@!hmr'`)
Force disable preservation of local state for this component.
This flag has priority over all other settings of state preservation. If it is present, all the state of the component will be reset on the next update, regardless of the value of all the other state preservation settings.
```svelte
<!-- @hmr:reset -->
<script>
'@hmr:reset'
// @hmr:reset
</script>
```
#### preserveAllLocalStateKey
Type: `string`<br>
Default: `'@hmr:keep-all'`
Force preservation of all local variables of this component.
```svelte
<!-- @hmr:keep-all -->
<script>
'@hmr:keep-all'
// @hmr:keep-all
</script>
```
#### preserveLocalStateKey
Type: `string`<br>
Default: `'@hmr:keep'`
Force preservation of a given local variable in this component.
```svelte
<script>
// @hmr:keep
let x = 0
let y = 0 // @hmr:keep
x = 1 // @hmr:keep
</script>
```
#### optimistic
Type: `bool`<br>
Default: `false`
When `false`, runtime errors during component init (i.e. when your `<script>` code is run) are considered fatal to HMR (hence worthy of a full reload if `noReload` option is not set). When `true`, `svelte-hmr` will try to render the next version of the component in the place of the one that has crashed instead of programming a full reload.
## What's HMR, by the way?
> **NOTE** To avoid repetition, the following text only mentions HMR in the context of browsers, but it can also be used in other platforms. For example `svelte-hmr` is also used in Svelte Native.
HMR stands for Hot Module Replacement. It is a tool that is used during development to replace only the parts that have changed in a _running_ application, without the need to reload the whole browser page.
It's nice because it shortens your feedback loop (you don't lose the current state of the page you're working it with each code change), and it feels like magic! :sparkles:
Well, since you're reading this, let me tell you a little more about HMR. Magic is actually not such a good think in software development, so if we can demystify HMR a bit, it will probably benefits you when it comes to answer setup questions or, generally, get the most out of your HMR experience.
So... There are multiple layers to HMR. The first one is the technical capacity to actually replace a JS _module_ (think ES module -- in practice a JS file) at runtime. This capacity is provided (or not) by your bundler or dev server (e.g. Webpack, Parcel, Vite, Snowpack...). A notable absent in the HMR capable bundlers is Rollup, but a HMR plugin exists, as well as a Rollup-compatible super fast dev bundler and server, Nollup (see links below).
The HMR capacity essentially revolves around watching your file system and sending events to the browser when this happens (and, of course, doing all the other bundler stuff, but they're not interesting for this discussion).
In the browser, we need something to receive and process those events. And so there is a HMR runtime (i.e. some JS code) that is injected in the browser when you enable HMR. This runtime exposes a HMR (or hot) API. The hot API differs with each bundler, even if there is some level of standardization. For example, `rollup-plugin-hot`'s API looks like this:
```js
if (import.meta.hot) {
import.meta.hot.dispose(data => {
// do cleanup
})
import.meta.hot.accept(() => {
// adjust side effects
})
}
```
Why do we need a hot API?
Because just replacing a whole chunk of JS code doesn't really gets you what you want. Let's think about it: what does replacing a JS module really means? You can't unrun the previously executed module. So that's merely running the new version of the module.
And that's about all what your HMR server will give you. But it's not enough. If your module was a helper function, new calls will go to the new version of the function, but the result of calling the previous version are still visible on the page. Same with a UI component: newly created components will be fresh, but existing components on the page are still from the previous versions...
That's why we need this hot API. We need to provide HMR handlers, that is HMR specific code to properly apply the effects of the code change in the app, and cleanup the effects of the previous module versions.
Actually, what should be done to reflect a code update is hyper specific to the content of the module. Typically, you'd need specific HMR handlers for each one of your modules (i.e. files). For most kind of contents, this cannot be automatized. Oh, and did I tell you that HMR handlers are pretty tedious things to write?
Oops. Does this makes HMR essentially worthless? No! Because, fortunately, there is one kind of content for which we know how to do automatic HMR handlers. Those are UI components. When a HMR update hits a component, we can destroy all existing instances of the component and replace them with new instances created with the updated version of the component. Bim. Well, there's also a part of dark magic involved because we need to recreate the new instances in the same state as the previous instance we're destroying. We need to pass the right props, transfer inner state, local state, event listeners, etc.
This kind of automatic HMR handlers for Svelte components -- and associated dark magic -- is what `svelte-hmr` provides! (For the sake of completeness, it also provides some Node utils to help bundlers apply the code transform needed to inject those HMR handlers into a Svelte component.)
Cool! So you can have HMR with a lot of your modules without having to write HMR handlers yourself, that's good news. And there are more. HMR APIs implements a _bubbling mechanism_. In HMR linguo, when a module has some HMR handlers, it is said to be "accepted". When a HMR update hits a module that is not accepted, this will not immediately translate into a full reload of the page. Instead, the HMR engine will check all the modules that imports the unaccepted module and, if they are all accepted, then it will refresh all those parent modules instead (refresh means running HMR handlers, here). If some of the parents are themselves unaccepted, the HMR update will continue to bubble up toward the app's entry point, stopping at accepted module on each branch. If an update hits the entry point (and the entry point itself is not accepted), then only will it command a full reload.
It makes sense: all those modules boast of being able to apply a code change to themselves so, if we refresh all of them, we should theoretically also see the updated effect of their dependencies, no? Well, unfortunately, it depends... If your updated module is exporting values or pure functions, it will work perfectly. If your module uses `setInterval` and there isn't a HMR handler to dispose that, the `setInterval` function will keep running (and each HMR update will probably add a new one). It all depends on how stateful or how much side effects your module has. But in many cases it can work and, in those cases, you'll also get HMR for free (since all HMR updates will probably be stopped by Svelte components). For the remaining cases... Well, you're probably left with [learning how to write HMR handlers](https://github.com/rixo/rollup-plugin-hot#api), or accept to manually reload the browser.
Here you are. This is what HMR is, and how it works.
I think the most important take away is that the component that is affected by a change will be recreated. Its state will be preserved but all its children will be recreated too, and their state not preserved. This is a necessity because the elements / child components structure of a component can be entirely different after the file has been modified (and we don't really have the technical capacity to track children).
Now, the best way to see what it can do for you is probably to checkout the template bellow and get your hands at it! (Add 500 components and try Nollup, you should love the speed!)
### Preservation of local state
**From version 0.12** this behaviour has been deemed too confusing and hard to anticipate, so preservation of state is now disabled by default, and some escape hatches to preserve the state of some given variables have been added.
Local state can be preserved by Svelte HMR, that is any state that Svelte itself tracks as reactive (basically any root scope `let` vars, exported or not).
This means that in code like this:
```svelte
<script>
let x = 1
x++ // x is now 2
</script>
<p>{x}</p>
```
If you replace `let x = 1` by `let x = 10` and save, the previous value of `x` will be preserved. That is, `x` will be 2 and not 10. The restoration of previous state happens _after_ the init code of the component has run, so the value will not be 11 either, despite the `x++` that is still here.
If you want this behaviour for all the state of all your components, you can enable it by setting the `preserveLocalState` option to `true`.
If you then want to disable it for just one particular file, or just temporarily, you can turn it off by adding a `// @hmr:reset` comment somewhere in your component.
On the contrary, if you keep the default `preserveLocalState` to `false`, you can enable preservation of all the local state of a given component by adding the following comment: `// @hmr:keep-all`. You can also preserve only the state of some specific variables, by annotating them with: `// @hmr:keep`.
For example:
```svelte
<script>
let x = 0 // @hmr:keep
// or:
// @hmr:keep
let y = 1,
z = 2
</script>
```
## Svelte HMR tools
### Vite
The [official Svelte plugin for Vite][vite-plugin-svelte] has HMR support.
### Webpack
The [official loader for Webpack][svelte-loader] now has HMR support.
### Rollup
Rollup does not natively support HMR, so we recommend using Vite. However, if you'd like to add HMR support to Rollup, the best way to get started is to refer to [svelte-template-hot], which demonstrates usage of both [rollup-plugin-hot] and [rollup-plugin-svelte-hot].
### Svelte Native
The official [Svelte Native template][svelte-native-template] already includes HMR support.
## License
[ISC](LICENSE)
[vite-plugin-svelte]: https://www.npmjs.com/package/@sveltejs/vite-plugin-svelte
[svelte-loader]: https://github.com/sveltejs/svelte-loader
[rollup-plugin-hot]: https://github.com/rixo/rollup-plugin-hot
[rollup-plugin-svelte-hot]: https://github.com/rixo/rollup-plugin-svelte-hot
[rollup-plugin-svelte]: https://github.com/rollup/rollup-plugin-svelte
[svelte-template-hot]: https://github.com/rixo/svelte-template-hot
[svelte-template]: https://github.com/sveltejs/template
[svelte-native-template]: https://github.com/halfnelson/svelte-native-template
[svelte-loader-hot]: https://github.com/rixo/svelte-loader-hot
[svelte-template-webpack-hot]: https://github.com/rixo/svelte-template-webpack-hot

12
node_modules/svelte-hmr/index.js generated vendored Normal file
View File

@ -0,0 +1,12 @@
const createMakeHotFactory = require('./lib/make-hot.js')
const { resolve } = require('path')
const { name, version } = require('./package.json')
const resolveAbsoluteImport = target => resolve(__dirname, target)
const createMakeHot = createMakeHotFactory({
pkg: { name, version },
resolveAbsoluteImport,
})
module.exports = { createMakeHot }

18
node_modules/svelte-hmr/lib/css-only.js generated vendored Normal file
View File

@ -0,0 +1,18 @@
/**
* Injects a `{}*` CSS rule to force Svelte compiler to scope every elements.
*/
export const injectScopeEverythingCssRule = (parse, code) => {
const { css } = parse(code)
if (!css) return code
const {
content: { end },
} = css
return code.slice(0, end) + '*{}' + code.slice(end)
}
export const normalizeJsCode = code => {
// Svelte now adds locations in dev mode, code locations can change when
// CSS change, but we're unaffected (not real behaviour changes)
code = code.replace(/\badd_location\s*\([^)]*\)\s*;?/g, '')
return code
}

497
node_modules/svelte-hmr/lib/make-hot.js generated vendored Normal file
View File

@ -0,0 +1,497 @@
const globalName = '___SVELTE_HMR_HOT_API'
const globalAdapterName = '___SVELTE_HMR_HOT_API_PROXY_ADAPTER'
const defaultHotOptions = {
// preserve all local state
preserveLocalState: false,
// escape hatchs from preservation of local state
//
// disable preservation of state for this component
noPreserveStateKey: ['@hmr:reset', '@!hmr'],
// enable preservation of state for all variables in this component
preserveAllLocalStateKey: '@hmr:keep-all',
// enable preservation of state for a given variable (must be inline or
// above the target variable or variables; can be repeated)
preserveLocalStateKey: '@hmr:keep',
// don't reload on fatal error
noReload: false,
// try to recover after runtime errors during component init
// defaults to false because some runtime errors are fatal and require a full reload
optimistic: false,
// auto accept modules of components that have named exports (i.e. exports
// from context="module")
acceptNamedExports: true,
// auto accept modules of components have accessors (either accessors compile
// option, or <svelte:option accessors={true} />) -- this means that if you
// set accessors compile option globally, you must also set this option to
// true, or no component will be hot reloaded (but there are a lot of edge
// cases that HMR can't support correctly with accessors)
acceptAccessors: true,
// only inject CSS instead of recreating components when only CSS changes
injectCss: false,
// to mitigate FOUC between dispose (remove stylesheet) and accept
cssEjectDelay: 100,
// Svelte Native mode
native: false,
// name of the adapter import binding
importAdapterName: globalAdapterName,
// disable runtime error overlay
noOverlay: false,
// set to true on systems that supports them, to avoid useless caught /
// managed (but still...) exceptions, by using Svelte's current_component
// instead of get_current_component
allowLiveBinding: false,
// use acceptExports instead of accept, to fix support of context:module
partialAccept: false,
}
const defaultHotApi = 'hot-api-esm.js'
const json = JSON.stringify
const posixify = file => file.replace(/[/\\]/g, '/')
const renderApplyHmr = ({
id,
cssId,
nonCssHash,
hotOptions: {
injectCss,
partialAccept,
_accept = partialAccept ? "acceptExports(['default'])" : 'accept()',
},
hotOptionsJson,
hotApiImport,
adapterImport,
importAdapterName,
meta,
acceptable,
preserveLocalState,
emitCss,
imports = [
`import * as ${globalName} from ${JSON.stringify(hotApiImport)}`,
`import { adapter as ${importAdapterName} } from ${JSON.stringify(adapterImport)}`,
],
}) =>
// this silly formatting keeps all original characters in their position,
// thus saving us from having to provide a sourcemap
//
// NOTE the `if (false) accept()` line is for tools that wants to see the
// accept call in the actual accepted module to enable HMR (Vite and, I
// believe, Snowpack 3)
//
`${imports.join(';')};${`
if (${meta} && ${meta}.hot) {
${acceptable ? `if (false) ${meta}.hot.${_accept};` : ''};
$2 = ${globalName}.applyHmr({
m: ${meta},
id: ${json(id)},
hotOptions: ${hotOptionsJson},
Component: $2,
ProxyAdapter: ${importAdapterName},
acceptable: ${json(acceptable)},
preserveLocalState: ${json(preserveLocalState)},
${cssId ? `cssId: ${json(cssId)},` : ''}
${nonCssHash ? `nonCssHash: ${json(nonCssHash)},` : ''}
emitCss: ${json(emitCss)},
});
}
`
.split('\n')
.map(x => x.trim())
.filter(Boolean)
.join(' ')}
export default $2;
${
// NOTE when doing CSS only voodoo, we have to inject the stylesheet as soon
// as the component is loaded because Svelte normally do that when a component
// is instantiated, but we might already have instances in the large when a
// component is loaded with HMR
cssId && injectCss && !emitCss
? `
if (typeof add_css !== 'undefined' && !document.getElementById(${json(
cssId
)})) add_css();`
: ``
}
`
// replace from last occurrence of the splitter
const replaceLast = (string, splitter, regex, replacer) => {
const lastIndex = string.lastIndexOf(splitter)
return (
string.slice(0, lastIndex) +
string.slice(lastIndex).replace(regex, replacer)
)
}
// https://github.com/darkskyapp/string-hash/blob/master/index.js
// (via https://github.com/sveltejs/svelte/blob/91d758e35b2b2154512ddd11e6b6d9d65708a99e/src/compiler/compile/utils/hash.ts#L2)
const stringHashcode = str => {
let hash = 5381
let i = str.length
while (i--) hash = ((hash << 5) - hash) ^ str.charCodeAt(i)
return (hash >>> 0).toString(36)
}
const normalizeJsCode = (code, cssHash) =>
code
// ignore css hashes in the code (that have changed, necessarily)
.replace(new RegExp('\\b' + cssHash + '\\b', 'g'), '')
// Svelte now adds locations in dev mode, code locations can change when
// CSS change, but we're unaffected (not real behaviour changes)
.replace(/\badd_location\s*\([^)]*\)\s*;?/g, '')
const parseCssId = (code, compileOptions = {}, parseHash, originalCode) => {
// the regex matching is very pretty conservative 'cause I don't want to
// match something else by error... I'm probably make it more lax if I have
// to fix it 3 times in a single week ¯\_(ツ)_/¯
let match = /^function add_css\(\) \{[\s\S]*?^}/m.exec(code)
if (!match) {
// guard: injectCss is off, no need to compute hashes
if (!parseHash) return {}
// guard: compile.css is true, so we should have found the add_css function,
// something unexpected is unraveling here, fall back to caution
if (compileOptions.css) return {}
// trying to get CSS id the same way as Svelte does it
match = /<style[^>]*>([\s\S]*)<\/\s*style\s*>/.exec(originalCode)
const cssHash = match && match[1] ? stringHashcode(match[1]) : null
if (!cssHash) return {}
return {
cssId: `svelte-${cssHash}-style`,
nonCssHash: stringHashcode(normalizeJsCode(code, cssHash)),
}
}
const codeExceptCSS =
code.slice(0, match.index) + code.slice(match.index + match[0].length)
match = /\bstyle\.id\s*=\s*(['"])([^'"]*)\1/.exec(match[0])
const cssId = match ? match[2] : null
if (!parseHash || !cssId) return { cssId }
const cssHash = cssId.split('-')[1]
const nonCssHash = stringHashcode(normalizeJsCode(codeExceptCSS, cssHash))
return { cssId, nonCssHash }
}
const isNamedExport = v => v.export_name && v.module
const isProp = v => v.export_name && !v.module
const resolvePackageImport = (name = 'svelte-hmr', version = '') => {
const versionSpec = version ? '@' + version : ''
return target => `${name}${versionSpec}/${target}`
}
const createMakeHot = ({ resolveAbsoluteImport, pkg = {} }) => ({
// NOTE hotOptions can be customized by end user through plugin options, while
// options passed to this function can only customized by the plugin implementer
//
// meta can be 'import.meta' or 'module'
// const createMakeHot = (hotApi = defaultHotApi, options) => {
walk,
meta = 'import.meta',
hotApi: customHotApi = '',
adapter: customAdapter = '',
// use absolute file paths to import runtime deps of svelte-hmr
// - pnpm needs absolute paths because svelte-hmr, being a transitive dep via
// the bundler plugin, is not directly importable from user's project
// (see https://github.com/rixo/svelte-hmr/issues/11)
// - Snowpack source=remote needs a version number, otherwise it will try to
// load import, resulting in a possible version mismatch between the code
// transform and the runtime
// (see: https://github.com/rixo/svelte-hmr/issues/27#issuecomment-800142127)
absoluteImports = true,
versionNonAbsoluteImports = true,
hotOptions: hotOptionsArg = {},
}) => {
const hotOptions = { ...defaultHotOptions, ...hotOptionsArg }
const hotOptionsJson = JSON.stringify(hotOptions)
// NOTE Native adapter cannot be required in code (as opposed to this
// generated code) because it requires modules from NativeScript's code that
// are not resolvable for non-native users (and those missing modules would
// prevent webpack from building).
//
// careful with relative paths
// (see https://github.com/rixo/svelte-hmr/issues/11)
const defaultAdapter = hotOptions.native
? 'svelte-native/proxy-adapter-native.js'
: 'proxy-adapter-dom.js'
const resolveImport = absoluteImports
? resolveAbsoluteImport
: resolvePackageImport(pkg.name, versionNonAbsoluteImports && pkg.version)
const adapterImport = posixify(
customAdapter || resolveImport('runtime/' + defaultAdapter)
)
const hotApiImport = posixify(
customHotApi || resolveImport('runtime/' + defaultHotApi)
)
const resolvePreserveLocalStateKey = ({
preserveLocalStateKey,
compiled,
}) => {
const containsKey = comments =>
comments &&
comments.some(({ value }) => value.includes(preserveLocalStateKey))
const variables = new Set()
const addReference = node => {
if (!node.name) {
// eslint-disable-next-line no-console
console.warn('Incorrect identifier for preserveLocalStateKey')
}
variables.add(node.name)
}
const processNodes = targets => targets.forEach(processNode)
const processNode = node => {
switch (node.type) {
case 'Identifier':
variables.add(node.name)
return true
case 'UpdateExpression':
addReference(node.argument)
return true
case 'VariableDeclarator':
addReference(node.id)
return true
case 'AssignmentExpression':
processNode(node.left)
return true
case 'ExpressionStatement':
processNode(node.expression)
return true
case 'VariableDeclaration':
processNodes(node.declarations)
return true
case 'SequenceExpression': // ++, --
processNodes(node.expressions)
return true
}
return false
}
const stack = []
if (compiled.ast.instance) {
walk(compiled.ast.instance, {
leave() {
stack.shift()
},
enter(node) {
stack.unshift(node)
if (
containsKey(node.leadingComments) ||
containsKey(node.trailingComments)
) {
stack
.slice(0, 3)
.reverse()
.some(processNode)
}
},
})
}
return [...variables]
}
const resolvePreserveLocalState = ({
hotOptions,
originalCode,
compiled,
}) => {
const {
preserveLocalState,
noPreserveStateKey,
preserveLocalStateKey,
preserveAllLocalStateKey,
} = hotOptions
if (originalCode) {
const hasKey = key => {
const test = k => originalCode.indexOf(k) !== -1
return Array.isArray(key) ? key.some(test) : test(key)
}
// noPreserveStateKey
if (noPreserveStateKey && hasKey(noPreserveStateKey)) {
return false
}
// preserveAllLocalStateKey
if (preserveAllLocalStateKey && hasKey(preserveAllLocalStateKey)) {
return true
}
// preserveLocalStateKey
if (preserveLocalStateKey && hasKey(preserveLocalStateKey)) {
// returns an array of variable names to preserve
return resolvePreserveLocalStateKey({ preserveLocalStateKey, compiled })
}
}
// preserveLocalState
if (preserveLocalState) {
return true
}
return false
}
const hasAccessorsOption = compiled => {
if (!compiled.ast || !compiled.ast.html) return
let accessors = false
walk(compiled.ast.html, {
enter(node) {
if (accessors) return
if (node.type !== 'Options') return
if (!node.attributes) return
accessors = node.attributes.some(
({ name, value }) => name === 'accessors' && value
)
},
})
return accessors
}
const isAcceptable = (hotOptions, compileOptions, compiled) => {
if (!compiled || !compileOptions) {
// this should never happen, since it's the bundler plugins that control
// what version of svelte-hmr they embark, and they should be kept up to
// date with our API
//
// eslint-disable-next-line no-console
console.warn(
'WARNING Your bundler plugin is outdated for this version of svelte-hmr'
)
return true
}
const { vars } = compiled
// if the module has named exports (in context="module"), then we can't
// auto accept the component, since all the consumers need to be aware of
// the change (e.g. rerender with the new exports value)
if (!hotOptions.acceptNamedExports && vars.some(isNamedExport)) {
return false
}
// ...same for accessors: if accessible props change, then we need to
// rerender/rerun all the consumers to reflect the change
//
// NOTE we can still accept components with no props, since they won't
// have accessors... this is actually all we can safely accept in this case
//
if (
!hotOptions.acceptAccessors &&
// we test is we have props first, because searching for the
// <svelte:options /> tag in the AST is probably the most expensive here
vars.some(isProp) &&
(compileOptions.accessors || hasAccessorsOption(compiled))
) {
return false
}
return true
}
const parseMakeHotArgs = args => {
// case: named args (object)
if (args.length === 1) return args[0]
// case: legacy (positional)
const [
id,
compiledCode,
hotOptions,
compiled,
originalCode,
compileOptions,
] = args
return {
id,
compiledCode,
hotOptions,
compiled,
originalCode,
compileOptions,
}
}
function makeHot(...args) {
const {
id,
compiledCode,
compiled,
originalCode,
compileOptions,
} = parseMakeHotArgs(args)
const { importAdapterName, injectCss } = hotOptions
const emitCss =
compileOptions &&
(compileOptions.css === false || compileOptions.css === 'external')
const preserveLocalState = resolvePreserveLocalState({
hotOptions,
originalCode,
compiled,
})
const replacement = renderApplyHmr({
id,
// adds cssId & nonCssHash
...((injectCss || !emitCss) &&
parseCssId(compiledCode, compileOptions, injectCss, originalCode)),
hotOptions,
hotOptionsJson,
preserveLocalState,
hotApiImport,
adapterImport,
importAdapterName,
meta,
acceptable: isAcceptable(hotOptions, compileOptions, compiled),
// CSS is handled outside of Svelte: don't tamper with it!
emitCss,
})
// NOTE export default can appear in strings in use code
// see: https://github.com/rixo/svelte-hmr/issues/34
return replaceLast(
compiledCode,
'export default',
/(\n?export default ([^;]*);)/,
(match, $1, $2) => replacement.replace(/\$2/g, () => $2)
)
}
// rollup-plugin-svelte-hot needs hotApi path (for tests)
// makeHot.hotApi = hotApi
Object.defineProperty(makeHot, 'hotApi', {
get() {
// TODO makeHot.hotApi has been lost in 0.12 => 0.13... still needed?
debugger // eslint-disable-line no-debugger
return undefined
},
})
return makeHot
}
module.exports = createMakeHot

44
node_modules/svelte-hmr/package.json generated vendored Normal file
View File

@ -0,0 +1,44 @@
{
"name": "svelte-hmr",
"version": "0.15.3",
"description": "Bundler agnostic HMR utils for Svelte 3",
"main": "index.js",
"author": "rixo <rixo@rixo.fr>",
"license": "ISC",
"homepage": "https://github.com/sveltejs/svelte-hmr",
"bugs": {
"url": "https://github.com/sveltejs/svelte-hmr/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/sveltejs/svelte-hmr",
"directory": "packages/svelte-hmr"
},
"files": [
"index.js",
"lib",
"runtime"
],
"engines": {
"node": "^12.20 || ^14.13.1 || >= 16"
},
"peerDependencies": {
"svelte": "^3.19.0 || ^4.0.0"
},
"devDependencies": {
"dotenv": "^10.0.0",
"prettier": "^1.19.1",
"svelte": "^3.59.2",
"tap-mocha-reporter": "^5.0.3",
"zoar": "^0.3.0",
"zorax": "^0.0.14"
},
"scripts": {
"lint": "eslint '**/*.{js,cjs,mjs}'",
"lint:fix": "pnpm run lint --fix",
"format": "prettier '**/*.{js,cjs,mjs}' --check",
"format:fix": "pnpm run format --write",
"test:fancy": "zoar --pipe 'tap-mocha-reporter spec'",
"test": "zoar --exit"
}
}

7
node_modules/svelte-hmr/runtime/hot-api-esm.js generated vendored Normal file
View File

@ -0,0 +1,7 @@
import { makeApplyHmr } from '../runtime/index.js'
export const applyHmr = makeApplyHmr(args =>
Object.assign({}, args, {
hot: args.m.hot,
})
)

172
node_modules/svelte-hmr/runtime/hot-api.js generated vendored Normal file
View File

@ -0,0 +1,172 @@
/* eslint-env browser */
import { createProxy, hasFatalError } from './proxy.js'
const logPrefix = '[HMR:Svelte]'
// eslint-disable-next-line no-console
const log = (...args) => console.log(logPrefix, ...args)
const domReload = () => {
// eslint-disable-next-line no-undef
const win = typeof window !== 'undefined' && window
if (win && win.location && win.location.reload) {
log('Reload')
win.location.reload()
} else {
log('Full reload required')
}
}
const replaceCss = (previousId, newId) => {
if (typeof document === 'undefined') return false
if (!previousId) return false
if (!newId) return false
// svelte-xxx-style => svelte-xxx
const previousClass = previousId.slice(0, -6)
const newClass = newId.slice(0, -6)
// eslint-disable-next-line no-undef
document.querySelectorAll('.' + previousClass).forEach(el => {
el.classList.remove(previousClass)
el.classList.add(newClass)
})
return true
}
const removeStylesheet = cssId => {
if (cssId == null) return
if (typeof document === 'undefined') return
// eslint-disable-next-line no-undef
const el = document.getElementById(cssId)
if (el) el.remove()
return
}
const defaultArgs = {
reload: domReload,
}
export const makeApplyHmr = transformArgs => args => {
const allArgs = transformArgs({ ...defaultArgs, ...args })
return applyHmr(allArgs)
}
let needsReload = false
function applyHmr(args) {
const {
id,
cssId,
nonCssHash,
reload = domReload,
// normalized hot API (must conform to rollup-plugin-hot)
hot,
hotOptions,
Component,
acceptable, // some types of components are impossible to HMR correctly
preserveLocalState,
ProxyAdapter,
emitCss,
} = args
const existing = hot.data && hot.data.record
const canAccept = acceptable && (!existing || existing.current.canAccept)
const r =
existing ||
createProxy({
Adapter: ProxyAdapter,
id,
Component,
hotOptions,
canAccept,
preserveLocalState,
})
const cssOnly =
hotOptions.injectCss &&
existing &&
nonCssHash &&
existing.current.nonCssHash === nonCssHash
r.update({
Component,
hotOptions,
canAccept,
nonCssHash,
cssId,
previousCssId: r.current.cssId,
cssOnly,
preserveLocalState,
})
hot.dispose(data => {
// handle previous fatal errors
if (needsReload || hasFatalError()) {
if (hotOptions && hotOptions.noReload) {
log('Full reload required')
} else {
reload()
}
}
// 2020-09-21 Snowpack master doesn't pass data as arg to dispose handler
data = data || hot.data
data.record = r
if (!emitCss && cssId && r.current.cssId !== cssId) {
if (hotOptions.cssEjectDelay) {
setTimeout(() => removeStylesheet(cssId), hotOptions.cssEjectDelay)
} else {
removeStylesheet(cssId)
}
}
})
if (canAccept) {
hot.accept(async arg => {
const { bubbled } = arg || {}
// NOTE Snowpack registers accept handlers only once, so we can NOT rely
// on the surrounding scope variables -- they're not the last version!
const { cssId: newCssId, previousCssId } = r.current
const cssChanged = newCssId !== previousCssId
// ensure old style sheet has been removed by now
if (!emitCss && cssChanged) removeStylesheet(previousCssId)
// guard: css only change
if (
// NOTE bubbled is provided only by rollup-plugin-hot, and we
// can't safely assume a CSS only change without it... this means we
// can't support CSS only injection with Nollup or Webpack currently
bubbled === false && // WARNING check false, not falsy!
r.current.cssOnly &&
(!cssChanged || replaceCss(previousCssId, newCssId))
) {
return
}
const success = await r.reload()
if (hasFatalError() || (!success && !hotOptions.optimistic)) {
needsReload = true
}
})
}
// well, endgame... we won't be able to render next updates, even successful,
// if we don't have proxies in svelte's tree
//
// since we won't return the proxy and the app will expect a svelte component,
// it's gonna crash... so it's best to report the real cause
//
// full reload required
//
const proxyOk = r && r.proxy
if (!proxyOk) {
throw new Error(`Failed to create HMR proxy for Svelte component ${id}`)
}
return r.proxy
}

1
node_modules/svelte-hmr/runtime/index.js generated vendored Normal file
View File

@ -0,0 +1 @@
export { makeApplyHmr } from './hot-api.js'

133
node_modules/svelte-hmr/runtime/overlay.js generated vendored Normal file
View File

@ -0,0 +1,133 @@
/* eslint-env browser */
const removeElement = el => el && el.parentNode && el.parentNode.removeChild(el)
const ErrorOverlay = () => {
let errors = []
let compileError = null
const errorsTitle = 'Failed to init component'
const compileErrorTitle = 'Failed to compile'
const style = {
section: `
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
padding: 32px;
background: rgba(0, 0, 0, .85);
font-family: Menlo, Consolas, monospace;
font-size: large;
color: rgb(232, 232, 232);
overflow: auto;
z-index: 2147483647;
`,
h1: `
margin-top: 0;
color: #E36049;
font-size: large;
font-weight: normal;
`,
h2: `
margin: 32px 0 0;
font-size: large;
font-weight: normal;
`,
pre: ``,
}
const createOverlay = () => {
const h1 = document.createElement('h1')
h1.style = style.h1
const section = document.createElement('section')
section.appendChild(h1)
section.style = style.section
const body = document.createElement('div')
section.appendChild(body)
return { h1, el: section, body }
}
const setTitle = title => {
overlay.h1.textContent = title
}
const show = () => {
const { el } = overlay
if (!el.parentNode) {
const target = document.body
target.appendChild(overlay.el)
}
}
const hide = () => {
const { el } = overlay
if (el.parentNode) {
overlay.el.remove()
}
}
const update = () => {
if (compileError) {
overlay.body.innerHTML = ''
setTitle(compileErrorTitle)
const errorEl = renderError(compileError)
overlay.body.appendChild(errorEl)
show()
} else if (errors.length > 0) {
overlay.body.innerHTML = ''
setTitle(errorsTitle)
errors.forEach(({ title, message }) => {
const errorEl = renderError(message, title)
overlay.body.appendChild(errorEl)
})
show()
} else {
hide()
}
}
const renderError = (message, title) => {
const div = document.createElement('div')
if (title) {
const h2 = document.createElement('h2')
h2.textContent = title
h2.style = style.h2
div.appendChild(h2)
}
const pre = document.createElement('pre')
pre.textContent = message
div.appendChild(pre)
return div
}
const addError = (error, title) => {
const message = (error && error.stack) || error
errors.push({ title, message })
update()
}
const clearErrors = () => {
errors.forEach(({ element }) => {
removeElement(element)
})
errors = []
update()
}
const setCompileError = message => {
compileError = message
update()
}
const overlay = createOverlay()
return {
addError,
clearErrors,
setCompileError,
}
}
export default ErrorOverlay

105
node_modules/svelte-hmr/runtime/proxy-adapter-dom.js generated vendored Normal file
View File

@ -0,0 +1,105 @@
/* global window, document */
import * as svelteInternal from 'svelte/internal'
// NOTE from 3.38.3 (or so), insert was carrying the hydration logic, that must
// be used because DOM elements are reused more (and so insertion points are not
// necessarily added in order); then in 3.40 the logic was moved to
// insert_hydration, which is the one we must use for HMR
const svelteInsert = svelteInternal.insert_hydration || svelteInternal.insert
if (!svelteInsert) {
throw new Error(
'failed to find insert_hydration and insert in svelte/internal'
)
}
import ErrorOverlay from './overlay.js'
const removeElement = el => el && el.parentNode && el.parentNode.removeChild(el)
export const adapter = class ProxyAdapterDom {
constructor(instance) {
this.instance = instance
this.insertionPoint = null
this.afterMount = this.afterMount.bind(this)
this.rerender = this.rerender.bind(this)
this._noOverlay = !!instance.hotOptions.noOverlay
}
// NOTE overlay is only created before being actually shown to help test
// runner (it won't have to account for error overlay when running assertions
// about the contents of the rendered page)
static getErrorOverlay(noCreate = false) {
if (!noCreate && !this.errorOverlay) {
this.errorOverlay = ErrorOverlay()
}
return this.errorOverlay
}
// TODO this is probably unused now: remove in next breaking release
static renderCompileError(message) {
const noCreate = !message
const overlay = this.getErrorOverlay(noCreate)
if (!overlay) return
overlay.setCompileError(message)
}
dispose() {
// Component is being destroyed, detaching is not optional in Svelte3's
// component API, so we can dispose of the insertion point in every case.
if (this.insertionPoint) {
removeElement(this.insertionPoint)
this.insertionPoint = null
}
this.clearError()
}
// NOTE afterMount CAN be called multiple times (e.g. keyed list)
afterMount(target, anchor) {
const {
instance: { debugName },
} = this
if (!this.insertionPoint) {
this.insertionPoint = document.createComment(debugName)
}
svelteInsert(target, this.insertionPoint, anchor)
}
rerender() {
this.clearError()
const {
instance: { refreshComponent },
insertionPoint,
} = this
if (!insertionPoint) {
throw new Error('Cannot rerender: missing insertion point')
}
refreshComponent(insertionPoint.parentNode, insertionPoint)
}
renderError(err) {
if (this._noOverlay) return
const {
instance: { debugName },
} = this
const title = debugName || err.moduleName || 'Error'
this.constructor.getErrorOverlay().addError(err, title)
}
clearError() {
if (this._noOverlay) return
const overlay = this.constructor.getErrorOverlay(true)
if (!overlay) return
overlay.clearErrors()
}
}
// TODO this is probably unused now: remove in next breaking release
if (typeof window !== 'undefined') {
window.__SVELTE_HMR_ADAPTER = adapter
}
// mitigate situation with Snowpack remote source pulling latest of runtime,
// but using previous version of the Node code transform in the plugin
// see: https://github.com/rixo/svelte-hmr/issues/27
export default adapter

427
node_modules/svelte-hmr/runtime/proxy.js generated vendored Normal file
View File

@ -0,0 +1,427 @@
/* eslint-env browser */
/**
* The HMR proxy is a component-like object whose task is to sit in the
* component tree in place of the proxied component, and rerender each
* successive versions of said component.
*/
import { createProxiedComponent } from './svelte-hooks.js'
const handledMethods = ['constructor', '$destroy']
const forwardedMethods = ['$set', '$on']
const logError = (msg, err) => {
// eslint-disable-next-line no-console
console.error('[HMR][Svelte]', msg)
if (err) {
// NOTE avoid too much wrapping around user errors
// eslint-disable-next-line no-console
console.error(err)
}
}
const posixify = file => file.replace(/[/\\]/g, '/')
const getBaseName = id =>
id
.split('/')
.pop()
.split('.')
.slice(0, -1)
.join('.')
const capitalize = str => str[0].toUpperCase() + str.slice(1)
const getFriendlyName = id => capitalize(getBaseName(posixify(id)))
const getDebugName = id => `<${getFriendlyName(id)}>`
const relayCalls = (getTarget, names, dest = {}) => {
for (const key of names) {
dest[key] = function(...args) {
const target = getTarget()
if (!target) {
return
}
return target[key] && target[key].call(this, ...args)
}
}
return dest
}
const isInternal = key => key !== '$$' && key.slice(0, 2) === '$$'
// This is intented as a somewhat generic / prospective fix to the situation
// that arised with the introduction of $$set in Svelte 3.24.1 -- trying to
// avoid giving full knowledge (like its name) of this implementation detail
// to the proxy. The $$set method can be present or not on the component, and
// its presence impacts the behaviour (but with HMR it will be tested if it is
// present _on the proxy_). So the idea here is to expose exactly the same $$
// props as the current version of the component and, for those that are
// functions, proxy the calls to the current component.
const relayInternalMethods = (proxy, cmp) => {
// delete any previously added $$ prop
Object.keys(proxy)
.filter(isInternal)
.forEach(key => {
delete proxy[key]
})
// guard: no component
if (!cmp) return
// proxy current $$ props to the actual component
Object.keys(cmp)
.filter(isInternal)
.forEach(key => {
Object.defineProperty(proxy, key, {
configurable: true,
get() {
const value = cmp[key]
if (typeof value !== 'function') return value
return (
value &&
function(...args) {
return value.apply(this, args)
}
)
},
})
})
}
// proxy custom methods
const copyComponentProperties = (proxy, cmp, previous) => {
if (previous) {
previous.forEach(prop => {
delete proxy[prop]
})
}
const props = Object.getOwnPropertyNames(Object.getPrototypeOf(cmp))
const wrappedProps = props.filter(prop => {
if (!handledMethods.includes(prop) && !forwardedMethods.includes(prop)) {
Object.defineProperty(proxy, prop, {
configurable: true,
get() {
return cmp[prop]
},
set(value) {
// we're changing it on the real component first to see what it
// gives... if it throws an error, we want to throw the same error in
// order to most closely follow non-hmr behaviour.
cmp[prop] = value
},
})
return true
}
})
return wrappedProps
}
// everything in the constructor!
//
// so we don't polute the component class with new members
//
class ProxyComponent {
constructor(
{
Adapter,
id,
debugName,
current, // { Component, hotOptions: { preserveLocalState, ... } }
register,
},
options // { target, anchor, ... }
) {
let cmp
let disposed = false
let lastError = null
const setComponent = _cmp => {
cmp = _cmp
relayInternalMethods(this, cmp)
}
const getComponent = () => cmp
const destroyComponent = () => {
// destroyComponent is tolerant (don't crash on no cmp) because it
// is possible that reload/rerender is called after a previous
// createComponent has failed (hence we have a proxy, but no cmp)
if (cmp) {
cmp.$destroy()
setComponent(null)
}
}
const refreshComponent = (target, anchor, conservativeDestroy) => {
if (lastError) {
lastError = null
adapter.rerender()
} else {
try {
const replaceOptions = {
target,
anchor,
preserveLocalState: current.preserveLocalState,
}
if (conservativeDestroy) {
replaceOptions.conservativeDestroy = true
}
cmp.$replace(current.Component, replaceOptions)
} catch (err) {
setError(err, target, anchor)
if (
!current.hotOptions.optimistic ||
// non acceptable components (that is components that have to defer
// to their parent for rerender -- e.g. accessors, named exports)
// are most tricky, and they havent been considered when most of the
// code has been written... as a result, they are especially tricky
// to deal with, it's better to consider any error with them to be
// fatal to avoid odities
!current.canAccept ||
(err && err.hmrFatal)
) {
throw err
} else {
// const errString = String((err && err.stack) || err)
logError(`Error during component init: ${debugName}`, err)
}
}
}
}
const setError = err => {
lastError = err
adapter.renderError(err)
}
const instance = {
hotOptions: current.hotOptions,
proxy: this,
id,
debugName,
refreshComponent,
}
const adapter = new Adapter(instance)
const { afterMount, rerender } = adapter
// $destroy is not called when a child component is disposed, so we
// need to hook from fragment.
const onDestroy = () => {
// NOTE do NOT call $destroy on the cmp from here; the cmp is already
// dead, this would not work
if (!disposed) {
disposed = true
adapter.dispose()
unregister()
}
}
// ---- register proxy instance ----
const unregister = register(rerender)
// ---- augmented methods ----
this.$destroy = () => {
destroyComponent()
onDestroy()
}
// ---- forwarded methods ----
relayCalls(getComponent, forwardedMethods, this)
// ---- create & mount target component instance ---
try {
let lastProperties
createProxiedComponent(current.Component, options, {
allowLiveBinding: current.hotOptions.allowLiveBinding,
onDestroy,
onMount: afterMount,
onInstance: comp => {
setComponent(comp)
// WARNING the proxy MUST use the same $$ object as its component
// instance, because a lot of wiring happens during component
// initialisation... lots of references to $$ and $$.fragment have
// already been distributed around when the component constructor
// returns, before we have a chance to wrap them (and so we can't
// wrap them no more, because existing references would become
// invalid)
this.$$ = comp.$$
lastProperties = copyComponentProperties(this, comp, lastProperties)
},
})
} catch (err) {
const { target, anchor } = options
setError(err, target, anchor)
throw err
}
}
}
const syncStatics = (component, proxy, previousKeys) => {
// remove previously copied keys
if (previousKeys) {
for (const key of previousKeys) {
delete proxy[key]
}
}
// forward static properties and methods
const keys = []
for (const key in component) {
keys.push(key)
proxy[key] = component[key]
}
return keys
}
const globalListeners = {}
const onGlobal = (event, fn) => {
event = event.toLowerCase()
if (!globalListeners[event]) globalListeners[event] = []
globalListeners[event].push(fn)
}
const fireGlobal = (event, ...args) => {
const listeners = globalListeners[event]
if (!listeners) return
for (const fn of listeners) {
fn(...args)
}
}
const fireBeforeUpdate = () => fireGlobal('beforeupdate')
const fireAfterUpdate = () => fireGlobal('afterupdate')
if (typeof window !== 'undefined') {
window.__SVELTE_HMR = {
on: onGlobal,
}
window.dispatchEvent(new CustomEvent('svelte-hmr:ready'))
}
let fatalError = false
export const hasFatalError = () => fatalError
/**
* Creates a HMR proxy and its associated `reload` function that pushes a new
* version to all existing instances of the component.
*/
export function createProxy({
Adapter,
id,
Component,
hotOptions,
canAccept,
preserveLocalState,
}) {
const debugName = getDebugName(id)
const instances = []
// current object will be updated, proxy instances will keep a ref
const current = {
Component,
hotOptions,
canAccept,
preserveLocalState,
}
const name = `Proxy${debugName}`
// this trick gives the dynamic name Proxy<MyComponent> to the concrete
// proxy class... unfortunately, this doesn't shows in dev tools, but
// it stills allow to inspect cmp.constructor.name to confirm an instance
// is a proxy
const proxy = {
[name]: class extends ProxyComponent {
constructor(options) {
try {
super(
{
Adapter,
id,
debugName,
current,
register: rerender => {
instances.push(rerender)
const unregister = () => {
const i = instances.indexOf(rerender)
instances.splice(i, 1)
}
return unregister
},
},
options
)
} catch (err) {
// If we fail to create a proxy instance, any instance, that means
// that we won't be able to fix this instance when it is updated.
// Recovering to normal state will be impossible. HMR's dead.
//
// Fatal error will trigger a full reload on next update (reloading
// right now is kinda pointless since buggy code still exists).
//
// NOTE Only report first error to avoid too much polution -- following
// errors are probably caused by the first one, or they will show up
// in turn when the first one is fixed ¯\_(ツ)_/¯
//
if (!fatalError) {
fatalError = true
logError(
`Unrecoverable HMR error in ${debugName}: ` +
`next update will trigger a full reload`
)
}
throw err
}
}
},
}[name]
// initialize static members
let previousStatics = syncStatics(current.Component, proxy)
const update = newState => Object.assign(current, newState)
// reload all existing instances of this component
const reload = () => {
fireBeforeUpdate()
// copy statics before doing anything because a static prop/method
// could be used somewhere in the create/render call
previousStatics = syncStatics(current.Component, proxy, previousStatics)
const errors = []
instances.forEach(rerender => {
try {
rerender()
} catch (err) {
logError(`Failed to rerender ${debugName}`, err)
errors.push(err)
}
})
if (errors.length > 0) {
return false
}
fireAfterUpdate()
return true
}
const hasFatalError = () => fatalError
return { id, proxy, update, reload, hasFatalError, current }
}

347
node_modules/svelte-hmr/runtime/svelte-hooks.js generated vendored Normal file
View File

@ -0,0 +1,347 @@
/**
* Emulates forthcoming HMR hooks in Svelte.
*
* All references to private component state ($$) are now isolated in this
* module.
*/
import {
current_component,
get_current_component,
set_current_component,
} from 'svelte/internal'
const captureState = cmp => {
// sanity check: propper behaviour here is to crash noisily so that
// user knows that they're looking at something broken
if (!cmp) {
throw new Error('Missing component')
}
if (!cmp.$$) {
throw new Error('Invalid component')
}
const {
$$: { callbacks, bound, ctx, props },
} = cmp
const state = cmp.$capture_state()
// capturing current value of props (or we'll recreate the component with the
// initial prop values, that may have changed -- and would not be reflected in
// options.props)
const hmr_props_values = {}
Object.keys(cmp.$$.props).forEach(prop => {
hmr_props_values[prop] = ctx[props[prop]]
})
return {
ctx,
props,
callbacks,
bound,
state,
hmr_props_values,
}
}
// remapping all existing bindings (including hmr_future_foo ones) to the
// new version's props indexes, and refresh them with the new value from
// context
const restoreBound = (cmp, restore) => {
// reverse prop:ctxIndex in $$.props to ctxIndex:prop
//
// ctxIndex can be either a regular index in $$.ctx or a hmr_future_ prop
//
const propsByIndex = {}
for (const [name, i] of Object.entries(restore.props)) {
propsByIndex[i] = name
}
// NOTE $$.bound cannot change in the HMR lifetime of a component, because
// if bindings changes, that means the parent component has changed,
// which means the child (current) component will be wholly recreated
for (const [oldIndex, updateBinding] of Object.entries(restore.bound)) {
// can be either regular prop, or future_hmr_ prop
const propName = propsByIndex[oldIndex]
// this should never happen if remembering of future props is enabled...
// in any case, there's nothing we can do about it if we have lost prop
// name knowledge at this point
if (propName == null) continue
// NOTE $$.props[propName] also propagates knowledge of a possible
// future prop to the new $$.props (via $$.props being a Proxy)
const newIndex = cmp.$$.props[propName]
cmp.$$.bound[newIndex] = updateBinding
// NOTE if the prop doesn't exist or doesn't exist anymore in the new
// version of the component, clearing the binding is the expected
// behaviour (since that's what would happen in non HMR code)
const newValue = cmp.$$.ctx[newIndex]
updateBinding(newValue)
}
}
// restoreState
//
// It is too late to restore context at this point because component instance
// function has already been called (and so context has already been read).
// Instead, we rely on setting current_component to the same value it has when
// the component was first rendered -- which fix support for context, and is
// also generally more respectful of normal operation.
//
const restoreState = (cmp, restore) => {
if (!restore) return
if (restore.callbacks) {
cmp.$$.callbacks = restore.callbacks
}
if (restore.bound) {
restoreBound(cmp, restore)
}
// props, props.$$slots are restored at component creation (works
// better -- well, at all actually)
}
const get_current_component_safe = () => {
// NOTE relying on dynamic bindings (current_component) makes us dependent on
// bundler config (and apparently it does not work in demo-svelte-nollup)
try {
// unfortunately, unlike current_component, get_current_component() can
// crash in the normal path (when there is really no parent)
return get_current_component()
} catch (err) {
// ... so we need to consider that this error means that there is no parent
//
// that makes us tightly coupled to the error message but, at least, we
// won't mute an unexpected error, which is quite a horrible thing to do
if (err.message === 'Function called outside component initialization') {
// who knows...
return current_component
} else {
throw err
}
}
}
export const createProxiedComponent = (
Component,
initialOptions,
{ allowLiveBinding, onInstance, onMount, onDestroy }
) => {
let cmp
let options = initialOptions
const isCurrent = _cmp => cmp === _cmp
const assignOptions = (target, anchor, restore, preserveLocalState) => {
const props = Object.assign({}, options.props)
// Filtering props to avoid "unexpected prop" warning
// NOTE this is based on props present in initial options, but it should
// always works, because props that are passed from the parent can't
// change without a code change to the parent itself -- hence, the
// child component will be fully recreated, and initial options should
// always represent props that are currnetly passed by the parent
if (options.props && restore.hmr_props_values) {
for (const prop of Object.keys(options.props)) {
if (restore.hmr_props_values.hasOwnProperty(prop)) {
props[prop] = restore.hmr_props_values[prop]
}
}
}
if (preserveLocalState && restore.state) {
if (Array.isArray(preserveLocalState)) {
// form ['a', 'b'] => preserve only 'a' and 'b'
props.$$inject = {}
for (const key of preserveLocalState) {
props.$$inject[key] = restore.state[key]
}
} else {
props.$$inject = restore.state
}
} else {
delete props.$$inject
}
options = Object.assign({}, initialOptions, {
target,
anchor,
props,
hydrate: false,
})
}
// Preserving knowledge of "future props" -- very hackish version (maybe
// there should be an option to opt out of this)
//
// The use case is bind:something where something doesn't exist yet in the
// target component, but comes to exist later, after a HMR update.
//
// If Svelte can't map a prop in the current version of the component, it
// will just completely discard it:
// https://github.com/sveltejs/svelte/blob/1632bca34e4803d6b0e0b0abd652ab5968181860/src/runtime/internal/Component.ts#L46
//
const rememberFutureProps = cmp => {
if (typeof Proxy === 'undefined') return
cmp.$$.props = new Proxy(cmp.$$.props, {
get(target, name) {
if (target[name] === undefined) {
target[name] = 'hmr_future_' + name
}
return target[name]
},
set(target, name, value) {
target[name] = value
},
})
}
const instrument = targetCmp => {
const createComponent = (Component, restore, previousCmp) => {
set_current_component(parentComponent || previousCmp)
const comp = new Component(options)
// NOTE must be instrumented before restoreState, because restoring
// bindings relies on hacked $$.props
instrument(comp)
restoreState(comp, restore)
return comp
}
rememberFutureProps(targetCmp)
targetCmp.$$.on_hmr = []
// `conservative: true` means we want to be sure that the new component has
// actually been successfuly created before destroying the old instance.
// This could be useful for preventing runtime errors in component init to
// bring down the whole HMR. Unfortunately the implementation bellow is
// broken (FIXME), but that remains an interesting target for when HMR hooks
// will actually land in Svelte itself.
//
// The goal would be to render an error inplace in case of error, to avoid
// losing the navigation stack (especially annoying in native, that is not
// based on URL navigation, so we lose the current page on each error).
//
targetCmp.$replace = (
Component,
{
target = options.target,
anchor = options.anchor,
preserveLocalState,
conservative = false,
}
) => {
const restore = captureState(targetCmp)
assignOptions(
target || options.target,
anchor,
restore,
preserveLocalState
)
const callbacks = cmp ? cmp.$$.on_hmr : []
const afterCallbacks = callbacks.map(fn => fn(cmp)).filter(Boolean)
const previous = cmp
if (conservative) {
try {
const next = createComponent(Component, restore, previous)
// prevents on_destroy from firing on non-final cmp instance
cmp = null
previous.$destroy()
cmp = next
} catch (err) {
cmp = previous
throw err
}
} else {
// prevents on_destroy from firing on non-final cmp instance
cmp = null
if (previous) {
// previous can be null if last constructor has crashed
previous.$destroy()
}
cmp = createComponent(Component, restore, cmp)
}
cmp.$$.hmr_cmp = cmp
for (const fn of afterCallbacks) {
fn(cmp)
}
cmp.$$.on_hmr = callbacks
return cmp
}
// NOTE onMount must provide target & anchor (for us to be able to determinate
// actual DOM insertion point)
//
// And also, to support keyed list, it needs to be called each time the
// component is moved (same as $$.fragment.m)
if (onMount) {
const m = targetCmp.$$.fragment.m
targetCmp.$$.fragment.m = (...args) => {
const result = m(...args)
onMount(...args)
return result
}
}
// NOTE onDestroy must be called even if the call doesn't pass through the
// component's $destroy method (that we can hook onto by ourselves, since
// it's public API) -- this happens a lot in svelte's internals, that
// manipulates cmp.$$.fragment directly, often binding to fragment.d,
// for example
if (onDestroy) {
targetCmp.$$.on_destroy.push(() => {
if (isCurrent(targetCmp)) {
onDestroy()
}
})
}
if (onInstance) {
onInstance(targetCmp)
}
// Svelte 3 creates and mount components from their constructor if
// options.target is present.
//
// This means that at this point, the component's `fragment.c` and,
// most notably, `fragment.m` will already have been called _from inside
// createComponent_. That is: before we have a chance to hook on it.
//
// Proxy's constructor
// -> createComponent
// -> component constructor
// -> component.$$.fragment.c(...) (or l, if hydrate:true)
// -> component.$$.fragment.m(...)
//
// -> you are here <-
//
if (onMount) {
const { target, anchor } = options
if (target) {
onMount(target, anchor)
}
}
}
const parentComponent = allowLiveBinding
? current_component
: get_current_component_safe()
cmp = new Component(options)
cmp.$$.hmr_cmp = cmp
instrument(cmp)
return cmp
}

View File

@ -0,0 +1,62 @@
// 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/48fdc97d2eb4d3958cfcb4ff6cf5755a220829eb/src/dom/navigation.ts#L132
//
// FIXME should we override ViewBase#showModal instead?
// eslint-disable-next-line import/no-unresolved
import { Page } from '@nativescript/core'
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 tamperedOptions = Object.assign({}, options, { closeCallback })
return sup.call(this, modalView, tamperedOptions)
}
}
export const getModalData = modalView => modalView[prop]

View File

@ -0,0 +1,341 @@
/* global document */
import { adapter as ProxyAdapterDom } from '../proxy-adapter-dom'
import { patchShowModal, getModalData } from './patch-page-show-modal'
patchShowModal()
// Svelte Native support
// =====================
//
// Rerendering Svelte Native page proves challenging...
//
// In NativeScript, pages are the top level component. They are normally
// introduced into NativeScript's runtime by its `navigate` function. This
// is how Svelte Natives handles it: it renders the Page component to a
// dummy fragment, and "navigate" to the page element thus created.
//
// As long as modifications only impact child components of the page, then
// we can keep the existing page and replace its content for HMR.
//
// However, if the page component itself is modified (including its system
// title bar), things get hairy...
//
// Apparently, the sole way of introducing a new page in a NS application is
// to navigate to it (no way to just replace it in its parent "element", for
// example). This is how it is done in NS's own "core" HMR.
//
// NOTE The last paragraph has not really been confirmed with NS6.
//
// Unfortunately the API they're using to do that is not public... Its various
// parts remain exposed though (but documented as private), so this exploratory
// work now relies on it. It might be fragile...
//
// The problem is that there is no public API that can navigate to a page and
// replace (like location.replace) the current history entry. Actually there
// is an active issue at NS asking for that. Incidentally, members of
// NativeScript-Vue have commented on the issue to weight in for it -- they
// probably face some similar challenge.
//
// https://github.com/NativeScript/NativeScript/issues/6283
const getNavTransition = ({ transition }) => {
if (typeof transition === 'string') {
transition = { name: transition }
}
return transition ? { animated: true, transition } : { animated: false }
}
// copied from TNS FrameBase.replacePage
//
// it is not public but there is a comment in there indicating it is for
// HMR (probably their own core HMR though)
//
// NOTE this "worked" in TNS 5, but not anymore in TNS 6: updated version bellow
//
// eslint-disable-next-line no-unused-vars
const replacePage_tns5 = (frame, newPageElement, hotOptions) => {
const currentBackstackEntry = frame._currentEntry
frame.navigationType = 2
frame.performNavigation({
isBackNavigation: false,
entry: {
resolvedPage: newPageElement.nativeView,
//
// entry: currentBackstackEntry.entry,
entry: Object.assign(
currentBackstackEntry.entry,
getNavTransition(hotOptions)
),
navDepth: currentBackstackEntry.navDepth,
fragmentTag: currentBackstackEntry.fragmentTag,
frameId: currentBackstackEntry.frameId,
},
})
}
// Updated for TNS v6
//
// https://github.com/NativeScript/NativeScript/blob/6.1.1/tns-core-modules/ui/frame/frame-common.ts#L656
const replacePage = (frame, newPageElement) => {
const currentBackstackEntry = frame._currentEntry
const newPage = newPageElement.nativeView
const newBackstackEntry = {
entry: currentBackstackEntry.entry,
resolvedPage: newPage,
navDepth: currentBackstackEntry.navDepth,
fragmentTag: currentBackstackEntry.fragmentTag,
frameId: currentBackstackEntry.frameId,
}
const navigationContext = {
entry: newBackstackEntry,
isBackNavigation: false,
navigationType: 2 /* NavigationType replace */,
}
frame._navigationQueue.push(navigationContext)
frame._processNextNavigationEntry()
}
export const adapter = class ProxyAdapterNative extends ProxyAdapterDom {
constructor(instance) {
super(instance)
this.nativePageElement = null
this.originalNativeView = null
this.navigatedFromHandler = null
this.relayNativeNavigatedFrom = this.relayNativeNavigatedFrom.bind(this)
}
dispose() {
super.dispose()
this.releaseNativePageElement()
}
releaseNativePageElement() {
if (this.nativePageElement) {
// native cleaning will happen when navigating back from the page
this.nativePageElement = null
}
}
// svelte-native uses navigateFrom event + e.isBackNavigation to know
// when to $destroy the component -- but we don't want our proxy instance
// destroyed when we renavigate to the same page for navigation purposes!
interceptPageNavigation(pageElement) {
const originalNativeView = pageElement.nativeView
const { on } = originalNativeView
const ownOn = originalNativeView.hasOwnProperty('on')
// tricks svelte-native into giving us its handler
originalNativeView.on = function(type, handler) {
if (type === 'navigatedFrom') {
this.navigatedFromHandler = handler
if (ownOn) {
originalNativeView.on = on
} else {
delete originalNativeView.on
}
} else {
//some other handler wireup, we will just pass it on.
if (on) {
on(type, handler)
}
}
}
}
afterMount(target, anchor) {
// nativePageElement needs to be updated each time (only for page
// components, native component that are not pages follow normal flow)
//
// TODO quid of components that are initially a page, but then have the
// <page> tag removed while running? or the opposite?
//
// insertionPoint needs to be updated _only when the target changes_ --
// i.e. when the component is mount, i.e. (in svelte3) when the component
// is _created_, and svelte3 doesn't allow it to move afterward -- that
// is, insertionPoint only needs to be created once when the component is
// first mounted.
//
// TODO is it really true that components' elements cannot move in the
// DOM? what about keyed list?
//
const isNativePage =
(target.tagName === 'fragment' || target.tagName === 'frame') &&
target.firstChild &&
target.firstChild.tagName == 'page'
if (isNativePage) {
const nativePageElement = target.firstChild
this.interceptPageNavigation(nativePageElement)
this.nativePageElement = nativePageElement
} else {
// try to protect against components changing from page to no-page
// or vice versa -- see DEBUG 1 above. NOT TESTED so prolly not working
this.nativePageElement = null
super.afterMount(target, anchor)
}
}
rerender() {
const { nativePageElement } = this
if (nativePageElement) {
this.rerenderNative()
} else {
super.rerender()
}
}
rerenderNative() {
const { nativePageElement: oldPageElement } = this
const nativeView = oldPageElement.nativeView
const frame = nativeView.frame
if (frame) {
return this.rerenderPage(frame, nativeView)
}
const modalParent = nativeView._modalParent // FIXME private API
if (modalParent) {
return this.rerenderModal(modalParent, nativeView)
}
// wtf? hopefully a race condition with a destroyed component, so
// we have nothing more to do here
//
// for once, it happens when hot reloading dev deps, like this file
//
}
rerenderPage(frame, previousPageView) {
const isCurrentPage = frame.currentPage === previousPageView
if (isCurrentPage) {
const {
instance: { hotOptions },
} = this
const newPageElement = this.createPage()
if (!newPageElement) {
throw new Error('Failed to create updated page')
}
const isFirstPage = !frame.canGoBack()
if (isFirstPage) {
// NOTE not so sure of bellow with the new NS6 method for replace
//
// The "replacePage" strategy does not work on the first page
// of the stack.
//
// Resulting bug:
// - launch
// - change first page => HMR
// - navigate to other page
// - back
// => actual: back to OS
// => expected: back to page 1
//
// Fortunately, we can overwrite history in this case.
//
const nativeView = newPageElement.nativeView
frame.navigate(
Object.assign(
{},
{
create: () => nativeView,
clearHistory: true,
},
getNavTransition(hotOptions)
)
)
} else {
replacePage(frame, newPageElement, hotOptions)
}
} else {
const backEntry = frame.backStack.find(
({ resolvedPage: page }) => page === previousPageView
)
if (!backEntry) {
// well... looks like we didn't make it to history after all
return
}
// replace existing nativeView
const newPageElement = this.createPage()
if (newPageElement) {
backEntry.resolvedPage = newPageElement.nativeView
} else {
throw new Error('Failed to create updated page')
}
}
}
// modalParent is the page on which showModal(...) was called
// oldPageElement is the modal content, that we're actually trying to reload
rerenderModal(modalParent, modalView) {
const modalData = getModalData(modalView)
modalData.closeCallback = () => {
const nativePageElement = this.createPage()
if (!nativePageElement) {
throw new Error('Failed to created updated modal page')
}
const { nativeView } = nativePageElement
const { originalOptions } = modalData
// Options will get monkey patched again, the only work left for us
// is to try to reduce visual disturbances.
//
// FIXME Even that proves too much unfortunately... Apparently TNS
// does not respect the `animated` option in this context:
// https://docs.nativescript.org/api-reference/interfaces/_ui_core_view_base_.showmodaloptions#animated
//
const options = Object.assign({}, originalOptions, { animated: false })
modalParent.showModal(nativeView, options)
}
modalView.closeModal()
}
createPage() {
const {
instance: { refreshComponent },
} = this
const { nativePageElement, relayNativeNavigatedFrom } = this
const oldNativeView = nativePageElement.nativeView
// rerender
const target = document.createElement('fragment')
// not using conservative for now, since there's nothing in place here to
// leverage it (yet?) -- and it might be easier to miss breakages in native
// only code paths
refreshComponent(target, null)
// this.nativePageElement is updated in afterMount, triggered by proxy / hooks
const newPageElement = this.nativePageElement
// update event proxy
oldNativeView.off('navigatedFrom', relayNativeNavigatedFrom)
nativePageElement.nativeView.on('navigatedFrom', relayNativeNavigatedFrom)
return newPageElement
}
relayNativeNavigatedFrom({ isBackNavigation }) {
const { originalNativeView, navigatedFromHandler } = this
if (!isBackNavigation) {
return
}
if (originalNativeView) {
const { off } = originalNativeView
const ownOff = originalNativeView.hasOwnProperty('off')
originalNativeView.off = function() {
this.navigatedFromHandler = null
if (ownOff) {
originalNativeView.off = off
} else {
delete originalNativeView.off
}
}
}
if (navigatedFromHandler) {
return navigatedFromHandler.apply(this, arguments)
}
}
renderError(err /* , target, anchor */) {
// TODO fallback on TNS error handler for now... at least our error
// is more informative
throw err
}
}