Merge branch 'master' into strict-null

pull/5693/head
TruongSinh Tran-Nguyen 5 years ago
commit 037693c296

@ -37,6 +37,8 @@ If you have a stack trace to include, we recommend putting inside a `<details>`
</details> </details>
**Information about your Svelte project:** **Information about your Svelte project:**
To make your life easier, just run `npx envinfo --system --npmPackages svelte,rollup,webpack --binaries --browsers` and paste the output here.
- Your browser and the version: (e.x. Chrome 52.1, Firefox 48.0, IE 10) - Your browser and the version: (e.x. Chrome 52.1, Firefox 48.0, IE 10)
- Your operating system: (e.x. OS X 10, Ubuntu Linux 19.10, Windows XP, etc) - Your operating system: (e.x. OS X 10, Ubuntu Linux 19.10, Windows XP, etc)

@ -1,5 +1,24 @@
# Svelte changelog # Svelte changelog
## 3.31.0
* Use a separate `SvelteComponentTyped` interface for typed components ([#5738](https://github.com/sveltejs/svelte/pull/5738))
## 3.30.1
* Support consuming decoded sourcemaps as created by the `source-map` library's `SourceMapGenerator` ([#5722](https://github.com/sveltejs/svelte/issues/5722))
* Actually export `hasContext` ([#5726](https://github.com/sveltejs/svelte/issues/5726))
## 3.30.0
* Add a typed `SvelteComponent` interface ([#5431](https://github.com/sveltejs/svelte/pull/5431))
* Support spread into `<slot>` props ([#5456](https://github.com/sveltejs/svelte/issues/5456))
* Fix setting reactive dependencies which don't appear in the template to `undefined` ([#5538](https://github.com/sveltejs/svelte/issues/5538))
* Support preprocessor sourcemaps during compilation ([#5584](https://github.com/sveltejs/svelte/pull/5584))
* Fix ordering of elements when using `{#if}` inside `{#key}` ([#5680](https://github.com/sveltejs/svelte/issues/5680))
* Add `hasContext` lifecycle function ([#5690](https://github.com/sveltejs/svelte/pull/5690))
* Fix missing `walk` types in `svelte/compiler` ([#5696](https://github.com/sveltejs/svelte/pull/5696))
## 3.29.7 ## 3.29.7
* Include `./register` in exports map ([#5670](https://github.com/sveltejs/svelte/issues/5670)) * Include `./register` in exports map ([#5670](https://github.com/sveltejs/svelte/issues/5670))

1164
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -1,6 +1,6 @@
{ {
"name": "svelte", "name": "svelte",
"version": "3.29.7", "version": "3.31.0",
"description": "Cybernetically enhanced web apps", "description": "Cybernetically enhanced web apps",
"module": "index.mjs", "module": "index.mjs",
"main": "index", "main": "index",
@ -95,6 +95,7 @@
}, },
"homepage": "https://github.com/sveltejs/svelte#README", "homepage": "https://github.com/sveltejs/svelte#README",
"devDependencies": { "devDependencies": {
"@ampproject/remapping": "^0.3.0",
"@rollup/plugin-commonjs": "^11.0.0", "@rollup/plugin-commonjs": "^11.0.0",
"@rollup/plugin-json": "^4.0.1", "@rollup/plugin-json": "^4.0.1",
"@rollup/plugin-node-resolve": "^6.0.0", "@rollup/plugin-node-resolve": "^6.0.0",
@ -105,16 +106,16 @@
"@sveltejs/eslint-config": "github:sveltejs/eslint-config#v5.6.0", "@sveltejs/eslint-config": "github:sveltejs/eslint-config#v5.6.0",
"@types/mocha": "^7.0.0", "@types/mocha": "^7.0.0",
"@types/node": "^8.10.53", "@types/node": "^8.10.53",
"@typescript-eslint/eslint-plugin": "^3.0.2", "@typescript-eslint/eslint-plugin": "^4.9.0",
"@typescript-eslint/parser": "^3.0.2", "@typescript-eslint/parser": "^4.9.0",
"acorn": "^7.4.0", "acorn": "^7.4.0",
"agadoo": "^1.1.0", "agadoo": "^1.1.0",
"c8": "^5.0.1", "c8": "^5.0.1",
"code-red": "^0.1.4", "code-red": "^0.1.4",
"codecov": "^3.5.0", "codecov": "^3.5.0",
"css-tree": "1.0.0-alpha22", "css-tree": "1.0.0-alpha22",
"eslint": "^7.1.0", "eslint": "^7.15.0",
"eslint-plugin-import": "^2.20.2", "eslint-plugin-import": "^2.22.1",
"eslint-plugin-svelte3": "^2.7.3", "eslint-plugin-svelte3": "^2.7.3",
"estree-walker": "^1.0.0", "estree-walker": "^1.0.0",
"is-reference": "^1.1.4", "is-reference": "^1.1.4",
@ -128,6 +129,7 @@
"rollup": "^1.27.14", "rollup": "^1.27.14",
"source-map": "^0.7.3", "source-map": "^0.7.3",
"source-map-support": "^0.5.13", "source-map-support": "^0.5.13",
"sourcemap-codec": "^1.4.8",
"tiny-glob": "^0.2.6", "tiny-glob": "^0.2.6",
"tslib": "^1.10.0", "tslib": "^1.10.0",
"typescript": "^3.5.3" "typescript": "^3.5.3"

@ -20,7 +20,7 @@ const ts_plugin = is_publish
const external = id => id.startsWith('svelte/'); const external = id => id.startsWith('svelte/');
fs.writeFileSync(`./compiler.d.ts`, `export { compile, parse, preprocess, VERSION } from './types/compiler/index';`); fs.writeFileSync(`./compiler.d.ts`, `export { compile, parse, preprocess, walk, VERSION } from './types/compiler/index';`);
export default [ export default [
/* runtime */ /* runtime */

@ -55,6 +55,6 @@ Once you've tinkered a bit and understood how everything fits together, you can
npx degit your-name/template my-new-project npx degit your-name/template my-new-project
``` ```
And that's it! Do `npm run build` to create a production-ready version of your app, and check the project template's [README](https://github.com/sveltejs/template/blob/master/README.md) for instructions on how to easily deploy your app to the web with [Now](https://zeit.co/now) or [Surge](http://surge.sh/). And that's it! Do `npm run build` to create a production-ready version of your app, and check the project template's [README](https://github.com/sveltejs/template/blob/master/README.md) for instructions on how to easily deploy your app to the web with [Vercel](https://vercel.com) or [Surge](http://surge.sh/).
You're not restricted to using Rollup — there are also integrations for [webpack](https://github.com/sveltejs/svelte-loader), [Browserify](https://github.com/tehshrike/sveltify) and others, or you can use the [Svelte CLI](https://github.com/sveltejs/svelte-cli) (Update from 2019: with Svelte 3 the CLI was deprecated and we now use [sirv-cli](https://www.npmjs.com/package/sirv-cli) in our template. Feel free to use whatever tool you like!) or the [API](https://github.com/sveltejs/svelte/tree/v2#api) directly. If you make a project template using one of these tools, please share it with the [Svelte Discord chatroom](chat), or via [@sveltejs](https://twitter.com/sveltejs) on Twitter! You're not restricted to using Rollup — there are also integrations for [webpack](https://github.com/sveltejs/svelte-loader), [Browserify](https://github.com/tehshrike/sveltify) and others, or you can use the [Svelte CLI](https://github.com/sveltejs/svelte-cli) (Update from 2019: with Svelte 3 the CLI was deprecated and we now use [sirv-cli](https://www.npmjs.com/package/sirv-cli) in our template. Feel free to use whatever tool you like!) or the [API](https://github.com/sveltejs/svelte/tree/v2#api) directly. If you make a project template using one of these tools, please share it with the [Svelte Discord chatroom](chat), or via [@sveltejs](https://twitter.com/sveltejs) on Twitter!

@ -58,9 +58,7 @@ To write code, you need a good editor. The most popular choice is [Visual Studio
We're going to follow the instructions in part two of [The easiest way to get started with Svelte](/blog/the-easiest-way-to-get-started). We're going to follow the instructions in part two of [The easiest way to get started with Svelte](/blog/the-easiest-way-to-get-started).
First, we'll use npx to run [degit](https://github.com/Rich-Harris/degit), a program for cloning project templates from [GitHub](https://github.com) and other code storage websites. You don't have to use a project template, but it means you have to do a lot less setup work. First, we'll use npx to run [degit](https://github.com/Rich-Harris/degit), a program for cloning project templates from [GitHub](https://github.com) and other code storage websites. You don't have to use a project template, but it means you have to do a lot less setup work. You will need to have [Git](https://git-scm.com/) installed in order to use degit. (Eventually you'll probably have to learn [Git](https://git-scm.com/) itself, which most programmers use to manage their projects.)
(Eventually you'll probably have to learn [git](https://git-scm.com/), which most programmers use to manage their projects. But you don't need to worry about it just yet.)
On the command line, navigate to where you want to create a new project, then type the following lines (you can paste the whole lot, but you'll develop better muscle memory if you get into the habit of writing each line out one at a time then running it): On the command line, navigate to where you want to create a new project, then type the following lines (you can paste the whole lot, but you'll develop better muscle memory if you get into the habit of writing each line out one at a time then running it):

@ -0,0 +1,71 @@
---
title: What's new in Svelte: December 2020
description: Better tooling, export maps and improvements to slots and context
author: Daniel Sandoval
authorURL: https://desandoval.net
---
It's the last "What's new in Svelte" of the year and there's lots to celebrate! This month's coverage includes updates from `rollup-plugin-svelte`, `Sapper` and `SvelteKit` and a bunch of showcases from the Svelte community!
## New features & impactful bug fixes
1. `$$props`, `$$restProps`, and `$$slots` are all now supported in custom web components (**3.29.5**, [Example](https://svelte.dev/repl/ad8e6f39cd20403dacd1be84d71e498d?version=3.29.5)) and `slot` components now support spread props: `<slot {...foo} />` (**3.30.0**)
2. A new `hasContext` lifecycle function makes it easy to check whether a `key` has been set in the context of a parent component (**3.30.0** & **3.30.1**, [Docs](https://svelte.dev/docs#hasContext))
3. There is now a new `SvelteComponentTyped` class which makes it easier to add strongly typed components that extend base Svelte components. Component library and framework authors rejoice! An example: `export class YourComponent extends SvelteComponentTyped<{aProp: boolean}, {click: MouseEvent}, {default: {aSlot: string}}> {}` (**3.31.0**, [RFC](https://github.com/sveltejs/rfcs/pull/37))
4. Transitions within `{:else}` blocks should now complete successfully (**3.29.5**, [Example](https://svelte.dev/repl/49cef205e5da459594ef2eafcbd41593?version=3.29.5))
5. Svelte now includes an export map, which explicitly states which files can be imported from its npm package (**3.29.5** with some fixes in **3.29.6**, **3.29.7** and **3.30.0**)
6. `rollup-plugin-svelte` had a new [7.0.0 release](https://github.com/sveltejs/rollup-plugin-svelte/blob/master/CHANGELOG.md). The biggest change is that the `css` option was removed. Users who were using that option should add another plugin like `rollup-plugin-css-only` as demonstrated [in the template](https://github.com/sveltejs/template/blob/5b1135c286f7a649daa99825a077586655051649/rollup.config.js#L48)
## What's going on in Sapper?
Lots of new TypeScript definition improvements to make editing Sapper apps even easier! CSS for dynamic imports also should now work in `client.js` files. (Unreleased)
## What's the deal with SvelteKit?
We're glad you asked! If you didn't catch Rich's blog post from early last month, [you can find it here](https://svelte.dev/blog/whats-the-deal-with-sveltekit)!
For all the features and bugfixes see the CHANGELOGs for [Svelte](https://github.com/sveltejs/svelte/blob/master/CHANGELOG.md) and [Sapper](https://github.com/sveltejs/sapper/blob/master/CHANGELOG.md).
---
## Community Showcase
**Apps & Sites**
- [narration.studio](https://narration.studio/) (Chrome Only) is an automatic in-browser audio recording & editing platform for voice over narration.
- [Vippet](https://vippet.netlify.app/) is a video recording and editing tool for the browser.
- [Pattern Monster](https://pattern.monster/) is a simple online pattern generator to create repeatable SVG patterns.
- [Plant-based diets](https://planetbaseddiets.panda.org/) is a website from the World Wildlife Foundation (WWF) built with Svelte.
- [johnells.se](https://www.johnells.se/) is a Swedish fashion e-commerce site, built with [Crown](https://crownframework.com/) - a Svelte-powered framework.
- [sentence-length](https://sentence-length.netlify.app/) is a learning and analysis tool to show how some authors play with different lengths, while others stick with one.
- [svelte-presenter](https://github.com/stephane-vanraes/svelte-presenter) lets you quickly make good looking presentations using Svelte and mdsvex.
**Demos**
- [u/loopcake got SSR working in Java Spring Boot](https://www.reddit.com/r/sveltejs/comments/jkh5up/svelte_ssr_but_its_java_spring_boot_and_its_native/) for all the Java shops out there looking to render Svelte server-side.
- [svelte-liquid-swipe](https://github.com/tncrazvan/svelte-liquid-swipe) shows off a fancy interaction pattern using svg paths.
- [Crossfade Link Animation](https://svelte.dev/repl/7f68e148caf04b2787bb6f296208f870?version=3.29.7) demonstrates how to animate between navigation links using a crossfade (made by Blu, from the Discord community)
- [Clip-Path Transitions](https://svelte.dev/repl/b5ad281ae8024b629b545c70c9e8764d?version=3.29.7) showcases how to use clip paths and custom transitions to create magical in-and-out transitions (made by Faber, from the Discord community)
**Learning Resources**
- [lihautan](https://www.youtube.com/channel/UCbmC3HP3FaAFdcZkui8YoMQ/featured) has been making easy-to-follow videos to share his in-depth knowledge of Svelte.
- [Lessons From Building a Static Site Generator](https://nicholasreese.com/lessons-from-building-a-static-site-generator/) shares the backstory and thinking behind Elder.js - and the design decision made along the way.
- [Svelte Tutorial and Projects Course ](https://www.udemy.com/course/svelte-tutorial-and-projects-course/) is a udemy course by John Smilga where students learn Svelte.js by building interesting projects.
- [Building Pastebin on IPFS - with FastAPI, Svelte, and IPFS](https://amalshaji.wtf/building-pastebin-on-ipfs-with-fastapi-svelte-and-ipfs) explains how to make a distributed pastebin-like application.
**Components, Libraries & Tools**
- [svelte-crossword](https://russellgoldenberg.github.io/svelte-crossword/) is a customizable crossword puzzle component for Svelte.
- [svelte-cloudinary](https://github.com/cupcakearmy/svelte-cloudinary) makes it easy to integrate Cloudinary with Svelte (including Typescript and SSR support)
- [Svelte Nova](https://extensions.panic.com/extensions/sb.lao/sb.lao.svelte-nova/) extends the new Nova editor to support Svelte
- [saos](https://github.com/shiryel/saos) is a small svelte component to animate your elements on scroll.
- [Svelte-nStore](https://github.com/lacikawiz/svelte-nStore) is a general purpose store replacement that fulfills the Svelte store contract and adds getter and calculation features.
- [svelte-slimscroll](https://github.com/MelihAltintas/svelte-slimscroll) is a Svelte Action that transforms any div into a scrollable area with a nice scrollbar.
- [svelte-typewriter](https://github.com/henriquehbr/svelte-typewriter) is a simple and reusable typewriter effect for your Svelte applications
- [svelte-store-router](https://github.com/zyxd/svelte-store-router) is a store-based router for Svelte that suggests that routing is just another global state and History API changes are just an optional side-effects of this state.
- [Routify](https://routify.dev/blog/routify-2-released) just released version 2 of its Svelte router.
- [svelte-error-boundary](https://www.npmjs.com/package/@crownframework/svelte-error-boundary) provides a simple error boundary component for Svelte that can be can be used with both DOM and SSR targets.
- [svelte2dts](https://www.npmjs.com/package/svelte2dts) generates d.ts files from svelte files, creating truly sharable and well typed components.
## See you next month!
Got an idea for something to add to the Showcase? Want to get involved more with Svelte? We're always looking for maintainers, contributors and fanatics... Check out the [Svelte Society](https://sveltesociety.dev/), [Reddit](https://www.reddit.com/r/sveltejs/) and [Discord](https://discord.com/invite/yy75DKs) to get involved!
That's all for the year, folks! See you in January 😎

@ -130,6 +130,32 @@ Any top-level statement (i.e. not inside a block or a function) can be made reac
--- ---
Only values which directly appear within the `$:` block will become dependencies of the reactive statement. For example, in the code below `total` will only update when `x` changes, but not `y`.
```sv
<script>
let x = 0;
let y = 0;
function yPlusAValue(value) {
return value + y;
}
$: total = yPlusAValue(x);
</script>
Total: {total}
<button on:click={() => x++}>
Increment X
</button>
<button on:click={() => y++}>
Increment Y
</button>
```
---
If a statement consists entirely of an assignment to an undeclared variable, Svelte will inject a `let` declaration on your behalf. If a statement consists entirely of an assignment to an undeclared variable, Svelte will inject a `let` declaration on your behalf.
```sv ```sv

@ -102,7 +102,7 @@ onDestroy(callback: () => void)
--- ---
Schedules a callback to run once the component is unmounted. Schedules a callback to run immediately before the component is unmounted.
Out of `onMount`, `beforeUpdate`, `afterUpdate` and `onDestroy`, this is the only one that runs inside a server-side component. Out of `onMount`, `beforeUpdate`, `afterUpdate` and `onDestroy`, this is the only one that runs inside a server-side component.
@ -178,6 +178,26 @@ Retrieves the context that belongs to the closest parent component with the spec
</script> </script>
``` ```
#### `hasContext`
```js
hasContext: boolean = hasContext(key: any)
```
---
Checks whether a given `key` has been set in the context of a parent component. Must be called during component initialisation.
```sv
<script>
import { hasContext } from 'svelte';
if (hasContext('answer')) {
// do something
}
</script>
```
#### `createEventDispatcher` #### `createEventDispatcher`
```js ```js

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.9 KiB

@ -29,7 +29,9 @@ import add_to_set from './utils/add_to_set';
import check_graph_for_cycles from './utils/check_graph_for_cycles'; import check_graph_for_cycles from './utils/check_graph_for_cycles';
import { print, x, b } from 'code-red'; import { print, x, b } from 'code-red';
import { is_reserved_keyword } from './utils/reserved_keywords'; import { is_reserved_keyword } from './utils/reserved_keywords';
import { apply_preprocessor_sourcemap } from '../utils/string_with_sourcemap';
import Element from './nodes/Element'; import Element from './nodes/Element';
import { DecodedSourceMap, RawSourceMap } from '@ampproject/remapping/dist/types/types';
interface ComponentOptions { interface ComponentOptions {
namespace?: string; namespace?: string;
@ -326,6 +328,8 @@ export default class Component {
js.map.sourcesContent = [ js.map.sourcesContent = [
this.source this.source
]; ];
js.map = apply_preprocessor_sourcemap(this.file, js.map, compile_options.sourcemap as (string | RawSourceMap | DecodedSourceMap));
} }
return { return {

@ -11,6 +11,7 @@ const valid_options = [
'format', 'format',
'name', 'name',
'filename', 'filename',
'sourcemap',
'generate', 'generate',
'outputFilename', 'outputFilename',
'cssOutputFilename', 'cssOutputFilename',

@ -15,7 +15,7 @@ export default class Slot extends Element {
super(component, parent, scope, info); super(component, parent, scope, info);
info.attributes.forEach(attr => { info.attributes.forEach(attr => {
if (attr.type !== 'Attribute') { if (attr.type !== 'Attribute' && attr.type !== 'Spread') {
component.error(attr, { component.error(attr, {
code: 'invalid-slot-directive', code: 'invalid-slot-directive',
message: '<slot> cannot have directives' message: '<slot> cannot have directives'

@ -111,8 +111,9 @@ export default class Renderer {
// these determine whether variable is included in initial context // these determine whether variable is included in initial context
// array, so must have the highest priority // array, so must have the highest priority
if (variable.export_name) member.priority += 16; if (variable.is_reactive_dependency && (variable.mutated || variable.reassigned)) member.priority += 16;
if (variable.referenced) member.priority += 32; if (variable.export_name) member.priority += 32;
if (variable.referenced) member.priority += 64;
} else if (member.is_non_contextual) { } else if (member.is_non_contextual) {
// determine whether variable is included in initial context // determine whether variable is included in initial context
// array, so must have the highest priority // array, so must have the highest priority
@ -131,7 +132,7 @@ export default class Renderer {
while (i--) { while (i--) {
const member = this.context[i]; const member = this.context[i];
if (member.variable) { if (member.variable) {
if (member.variable.referenced || member.variable.export_name) break; if (member.variable.referenced || member.variable.export_name || (member.variable.is_reactive_dependency && (member.variable.mutated || member.variable.reassigned))) break;
} else if (member.is_non_contextual) { } else if (member.is_non_contextual) {
break; break;
} }
@ -220,7 +221,7 @@ export default class Renderer {
.reduce((lhs, rhs) => x`${lhs}, ${rhs}`); .reduce((lhs, rhs) => x`${lhs}, ${rhs}`);
} }
dirty(names, is_reactive_declaration = false): Expression { dirty(names: string[], is_reactive_declaration = false): Expression {
const renderer = this; const renderer = this;
const dirty = (is_reactive_declaration const dirty = (is_reactive_declaration

@ -7,6 +7,8 @@ import { extract_names, Scope } from '../utils/scope';
import { invalidate } from './invalidate'; import { invalidate } from './invalidate';
import Block from './Block'; import Block from './Block';
import { ClassDeclaration, FunctionExpression, Node, Statement, ObjectExpression, Expression } from 'estree'; import { ClassDeclaration, FunctionExpression, Node, Statement, ObjectExpression, Expression } from 'estree';
import { apply_preprocessor_sourcemap } from '../../utils/string_with_sourcemap';
import { RawSourceMap, DecodedSourceMap } from '@ampproject/remapping/dist/types/types';
export default function dom( export default function dom(
component: Component, component: Component,
@ -30,6 +32,9 @@ export default function dom(
} }
const css = component.stylesheet.render(options.filename, !options.customElement); const css = component.stylesheet.render(options.filename, !options.customElement);
css.map = apply_preprocessor_sourcemap(options.filename, css.map, options.sourcemap as string | RawSourceMap | DecodedSourceMap);
const styles = component.stylesheet.has_styles && options.dev const styles = component.stylesheet.has_styles && options.dev
? `${css.code}\n/*# sourceMappingURL=${css.map.toUrl()} */` ? `${css.code}\n/*# sourceMappingURL=${css.map.toUrl()} */`
: css.code; : css.code;

@ -670,7 +670,7 @@ export default class ElementWrapper extends Wrapper {
// handle edge cases for elements // handle edge cases for elements
if (this.node.name === 'select') { if (this.node.name === 'select') {
const dependencies = new Set(); const dependencies = new Set<string>();
for (const attr of this.attributes) { for (const attr of this.attributes) {
for (const dep of attr.node.dependencies) { for (const dep of attr.node.dependencies) {
dependencies.add(dep); dependencies.add(dep);

@ -44,7 +44,7 @@ export default class KeyBlockWrapper extends Wrapper {
renderer, renderer,
this.block, this.block,
node.children, node.children,
parent, this,
strip_whitespace, strip_whitespace,
next_sibling next_sibling
); );

@ -8,7 +8,6 @@ import { sanitize } from '../../../utils/names';
import add_to_set from '../../utils/add_to_set'; import add_to_set from '../../utils/add_to_set';
import get_slot_data from '../../utils/get_slot_data'; import get_slot_data from '../../utils/get_slot_data';
import { is_reserved_keyword } from '../../utils/reserved_keywords'; import { is_reserved_keyword } from '../../utils/reserved_keywords';
import Expression from '../../nodes/shared/Expression';
import is_dynamic from './shared/is_dynamic'; import is_dynamic from './shared/is_dynamic';
import { Identifier, ObjectExpression } from 'estree'; import { Identifier, ObjectExpression } from 'estree';
import create_debugging_comment from './shared/create_debugging_comment'; import create_debugging_comment from './shared/create_debugging_comment';
@ -82,6 +81,7 @@ export default class SlotWrapper extends Wrapper {
} }
let get_slot_changes_fn; let get_slot_changes_fn;
let get_slot_spread_changes_fn;
let get_slot_context_fn; let get_slot_context_fn;
if (this.node.values.size > 0) { if (this.node.values.size > 0) {
@ -90,25 +90,17 @@ export default class SlotWrapper extends Wrapper {
const changes = x`{}` as ObjectExpression; const changes = x`{}` as ObjectExpression;
const dependencies = new Set(); const spread_dynamic_dependencies = new Set<string>();
this.node.values.forEach(attribute => { this.node.values.forEach(attribute => {
attribute.chunks.forEach(chunk => { if (attribute.type === 'Spread') {
if ((chunk as Expression).dependencies) { add_to_set(spread_dynamic_dependencies, Array.from(attribute.dependencies).filter((name) => this.is_dependency_dynamic(name)));
add_to_set(dependencies, (chunk as Expression).contextual_dependencies); } else {
const dynamic_dependencies = Array.from(attribute.dependencies).filter((name) => this.is_dependency_dynamic(name));
// add_to_set(dependencies, (chunk as Expression).dependencies);
(chunk as Expression).dependencies.forEach(name => {
const variable = renderer.component.var_lookup.get(name);
if (variable && !variable.hoistable) dependencies.add(name);
});
}
});
const dynamic_dependencies = Array.from(attribute.dependencies).filter((name) => this.is_dependency_dynamic(name));
if (dynamic_dependencies.length > 0) { if (dynamic_dependencies.length > 0) {
changes.properties.push(p`${attribute.name}: ${renderer.dirty(dynamic_dependencies)}`); changes.properties.push(p`${attribute.name}: ${renderer.dirty(dynamic_dependencies)}`);
}
} }
}); });
@ -116,6 +108,13 @@ export default class SlotWrapper extends Wrapper {
const ${get_slot_changes_fn} = #dirty => ${changes}; const ${get_slot_changes_fn} = #dirty => ${changes};
const ${get_slot_context_fn} = #ctx => ${get_slot_data(this.node.values, block)}; const ${get_slot_context_fn} = #ctx => ${get_slot_data(this.node.values, block)};
`); `);
if (spread_dynamic_dependencies.size) {
get_slot_spread_changes_fn = renderer.component.get_unique_name(`get_${sanitize(slot_name)}_slot_spread_changes`);
renderer.blocks.push(b`
const ${get_slot_spread_changes_fn} = #dirty => ${renderer.dirty(Array.from(spread_dynamic_dependencies))} > 0 ? -1 : 0;
`);
}
} else { } else {
get_slot_changes_fn = 'null'; get_slot_changes_fn = 'null';
get_slot_context_fn = 'null'; get_slot_context_fn = 'null';
@ -170,7 +169,11 @@ export default class SlotWrapper extends Wrapper {
? Array.from(this.fallback.dependencies).filter((name) => this.is_dependency_dynamic(name)) ? Array.from(this.fallback.dependencies).filter((name) => this.is_dependency_dynamic(name))
: []; : [];
const slot_update = b` const slot_update = get_slot_spread_changes_fn ? b`
if (${slot}.p && ${renderer.dirty(dynamic_dependencies)}) {
@update_slot_spread(${slot}, ${slot_definition}, #ctx, ${renderer.reference('$$scope')}, #dirty, ${get_slot_changes_fn}, ${get_slot_spread_changes_fn}, ${get_slot_context_fn});
}
`: b`
if (${slot}.p && ${renderer.dirty(dynamic_dependencies)}) { if (${slot}.p && ${renderer.dirty(dynamic_dependencies)}) {
@update_slot(${slot}, ${slot_definition}, #ctx, ${renderer.reference('$$scope')}, #dirty, ${get_slot_changes_fn}, ${get_slot_context_fn}); @update_slot(${slot}, ${slot_definition}, #ctx, ${renderer.reference('$$scope')}, #dirty, ${get_slot_changes_fn}, ${get_slot_context_fn});
} }

@ -9,6 +9,14 @@ export default function get_slot_data(values: Map<string, Attribute>, block: Blo
properties: Array.from(values.values()) properties: Array.from(values.values())
.filter(attribute => attribute.name !== 'name') .filter(attribute => attribute.name !== 'name')
.map(attribute => { .map(attribute => {
if (attribute.is_spread) {
const argument = get_spread_value(block, attribute);
return {
type: 'SpreadElement',
argument
};
}
const value = get_value(block, attribute); const value = get_value(block, attribute);
return p`${attribute.name}: ${value}`; return p`${attribute.name}: ${value}`;
}) })
@ -29,3 +37,7 @@ function get_value(block: Block, attribute: Attribute) {
return value; return value;
} }
function get_spread_value(block: Block, attribute: Attribute) {
return block ? attribute.expression.manipulate(block) : attribute.expression.node;
}

@ -110,6 +110,7 @@ export interface CompileOptions {
filename?: string; filename?: string;
generate?: 'dom' | 'ssr' | false; generate?: 'dom' | 'ssr' | false;
sourcemap?: object | string;
outputFilename?: string; outputFilename?: string;
cssOutputFilename?: string; cssOutputFilename?: string;
sveltePath?: string; sveltePath?: string;

@ -1,6 +1,11 @@
import { RawSourceMap, DecodedSourceMap } from '@ampproject/remapping/dist/types/types';
import { decode as decode_mappings } from 'sourcemap-codec';
import { getLocator } from 'locate-character';
import { StringWithSourcemap, sourcemap_add_offset, combine_sourcemaps } from '../utils/string_with_sourcemap';
export interface Processed { export interface Processed {
code: string; code: string;
map?: object | string; map?: string | object; // we are opaque with the type here to avoid dependency on the remapping module for our public types.
dependencies?: string[]; dependencies?: string[];
} }
@ -37,12 +42,18 @@ function parse_attributes(str: string) {
interface Replacement { interface Replacement {
offset: number; offset: number;
length: number; length: number;
replacement: string; replacement: StringWithSourcemap;
} }
async function replace_async(str: string, re: RegExp, func: (...any) => Promise<string>) { async function replace_async(
filename: string,
source: string,
get_location: ReturnType<typeof getLocator>,
re: RegExp,
func: (...any) => Promise<StringWithSourcemap>
): Promise<StringWithSourcemap> {
const replacements: Array<Promise<Replacement>> = []; const replacements: Array<Promise<Replacement>> = [];
str.replace(re, (...args) => { source.replace(re, (...args) => {
replacements.push( replacements.push(
func(...args).then( func(...args).then(
res => res =>
@ -55,16 +66,132 @@ async function replace_async(str: string, re: RegExp, func: (...any) => Promise<
); );
return ''; return '';
}); });
let out = ''; const out = new StringWithSourcemap();
let last_end = 0; let last_end = 0;
for (const { offset, length, replacement } of await Promise.all( for (const { offset, length, replacement } of await Promise.all(
replacements replacements
)) { )) {
out += str.slice(last_end, offset) + replacement; // content = unchanged source characters before the replaced segment
const content = StringWithSourcemap.from_source(
filename, source.slice(last_end, offset), get_location(last_end));
out.concat(content).concat(replacement);
last_end = offset + length; last_end = offset + length;
} }
out += str.slice(last_end); // final_content = unchanged source characters after last replaced segment
return out; const final_content = StringWithSourcemap.from_source(
filename, source.slice(last_end), get_location(last_end));
return out.concat(final_content);
}
/**
* Import decoded sourcemap from mozilla/source-map/SourceMapGenerator
* Forked from source-map/lib/source-map-generator.js
* from methods _serializeMappings and toJSON.
* We cannot use source-map.d.ts types, because we access hidden properties.
*/
function decoded_sourcemap_from_generator(generator: any) {
let previous_generated_line = 1;
const converted_mappings = [[]];
let result_line;
let result_segment;
let mapping;
const source_idx = generator._sources.toArray()
.reduce((acc, val, idx) => (acc[val] = idx, acc), {});
const name_idx = generator._names.toArray()
.reduce((acc, val, idx) => (acc[val] = idx, acc), {});
const mappings = generator._mappings.toArray();
result_line = converted_mappings[0];
for (let i = 0, len = mappings.length; i < len; i++) {
mapping = mappings[i];
if (mapping.generatedLine > previous_generated_line) {
while (mapping.generatedLine > previous_generated_line) {
converted_mappings.push([]);
previous_generated_line++;
}
result_line = converted_mappings[mapping.generatedLine - 1]; // line is one-based
} else if (i > 0) {
const previous_mapping = mappings[i - 1];
if (
// sorted by selectivity
mapping.generatedColumn === previous_mapping.generatedColumn &&
mapping.originalColumn === previous_mapping.originalColumn &&
mapping.name === previous_mapping.name &&
mapping.generatedLine === previous_mapping.generatedLine &&
mapping.originalLine === previous_mapping.originalLine &&
mapping.source === previous_mapping.source
) {
continue;
}
}
result_line.push([mapping.generatedColumn]);
result_segment = result_line[result_line.length - 1];
if (mapping.source != null) {
result_segment.push(...[
source_idx[mapping.source],
mapping.originalLine - 1, // line is one-based
mapping.originalColumn
]);
if (mapping.name != null) {
result_segment.push(name_idx[mapping.name]);
}
}
}
const map = {
version: generator._version,
sources: generator._sources.toArray(),
names: generator._names.toArray(),
mappings: converted_mappings
};
if (generator._file != null) {
(map as any).file = generator._file;
}
// not needed: map.sourcesContent and map.sourceRoot
return map;
}
/**
* Convert a preprocessor output and its leading prefix and trailing suffix into StringWithSourceMap
*/
function get_replacement(
filename: string,
offset: number,
get_location: ReturnType<typeof getLocator>,
original: string,
processed: Processed,
prefix: string,
suffix: string
): StringWithSourcemap {
// Convert the unchanged prefix and suffix to StringWithSourcemap
const prefix_with_map = StringWithSourcemap.from_source(
filename, prefix, get_location(offset));
const suffix_with_map = StringWithSourcemap.from_source(
filename, suffix, get_location(offset + prefix.length + original.length));
// Convert the preprocessed code and its sourcemap to a StringWithSourcemap
let decoded_map: DecodedSourceMap;
if (processed.map) {
decoded_map = typeof processed.map === 'string' ? JSON.parse(processed.map) : processed.map;
if (typeof(decoded_map.mappings) === 'string') {
decoded_map.mappings = decode_mappings(decoded_map.mappings);
}
if ((decoded_map as any)._mappings && decoded_map.constructor.name === 'SourceMapGenerator') {
// import decoded sourcemap from mozilla/source-map/SourceMapGenerator
decoded_map = decoded_sourcemap_from_generator(decoded_map);
}
sourcemap_add_offset(decoded_map, get_location(offset + prefix.length));
}
const processed_with_map = StringWithSourcemap.from_processed(processed.code, decoded_map);
// Surround the processed code with the prefix and suffix, retaining valid sourcemappings
return prefix_with_map.concat(processed_with_map).concat(suffix_with_map);
} }
export default async function preprocess( export default async function preprocess(
@ -76,60 +203,92 @@ export default async function preprocess(
const filename = (options && options.filename) || preprocessor.filename; // legacy const filename = (options && options.filename) || preprocessor.filename; // legacy
const dependencies = []; const dependencies = [];
const preprocessors = Array.isArray(preprocessor) ? preprocessor : [preprocessor]; const preprocessors = preprocessor
? Array.isArray(preprocessor) ? preprocessor : [preprocessor]
: [];
const markup = preprocessors.map(p => p.markup).filter(Boolean); const markup = preprocessors.map(p => p.markup).filter(Boolean);
const script = preprocessors.map(p => p.script).filter(Boolean); const script = preprocessors.map(p => p.script).filter(Boolean);
const style = preprocessors.map(p => p.style).filter(Boolean); const style = preprocessors.map(p => p.style).filter(Boolean);
// sourcemap_list is sorted in reverse order from last map (index 0) to first map (index -1)
// so we use sourcemap_list.unshift() to add new maps
// https://github.com/ampproject/remapping#multiple-transformations-of-a-file
const sourcemap_list: Array<DecodedSourceMap | RawSourceMap> = [];
// TODO keep track: what preprocessor generated what sourcemap? to make debugging easier = detect low-resolution sourcemaps in fn combine_mappings
for (const fn of markup) { for (const fn of markup) {
// run markup preprocessor
const processed = await fn({ const processed = await fn({
content: source, content: source,
filename filename
}); });
if (processed && processed.dependencies) dependencies.push(...processed.dependencies);
source = processed ? processed.code : source; if (!processed) continue;
if (processed.dependencies) dependencies.push(...processed.dependencies);
source = processed.code;
if (processed.map) {
sourcemap_list.unshift(
typeof(processed.map) === 'string'
? JSON.parse(processed.map)
: processed.map
);
}
} }
for (const fn of script) { async function preprocess_tag_content(tag_name: 'style' | 'script', preprocessor: Preprocessor) {
source = await replace_async( const get_location = getLocator(source);
const tag_regex = tag_name === 'style'
? /<!--[^]*?-->|<style(\s[^]*?)?(?:>([^]*?)<\/style>|\/>)/gi
: /<!--[^]*?-->|<script(\s[^]*?)?(?:>([^]*?)<\/script>|\/>)/gi;
const res = await replace_async(
filename,
source, source,
/<!--[^]*?-->|<script(\s[^]*?)?(?:>([^]*?)<\/script>|\/>)/gi, get_location,
async (match, attributes = '', content = '') => { tag_regex,
async (match, attributes = '', content = '', offset) => {
const no_change = () => StringWithSourcemap.from_source(
filename, match, get_location(offset));
if (!attributes && !content) { if (!attributes && !content) {
return match; return no_change();
} }
attributes = attributes || ''; attributes = attributes || '';
const processed = await fn({ content = content || '';
// run script preprocessor
const processed = await preprocessor({
content, content,
attributes: parse_attributes(attributes), attributes: parse_attributes(attributes),
filename filename
}); });
if (processed && processed.dependencies) dependencies.push(...processed.dependencies);
return processed ? `<script${attributes}>${processed.code}</script>` : match; if (!processed) return no_change();
if (processed.dependencies) dependencies.push(...processed.dependencies);
return get_replacement(filename, offset, get_location, content, processed, `<${tag_name}${attributes}>`, `</${tag_name}>`);
} }
); );
source = res.string;
sourcemap_list.unshift(res.map);
}
for (const fn of script) {
await preprocess_tag_content('script', fn);
} }
for (const fn of style) { for (const fn of style) {
source = await replace_async( await preprocess_tag_content('style', fn);
source,
/<!--[^]*?-->|<style(\s[^]*?)?(?:>([^]*?)<\/style>|\/>)/gi,
async (match, attributes = '', content = '') => {
if (!attributes && !content) {
return match;
}
const processed: Processed = await fn({
content,
attributes: parse_attributes(attributes),
filename
});
if (processed && processed.dependencies) dependencies.push(...processed.dependencies);
return processed ? `<style${attributes}>${processed.code}</style>` : match;
}
);
} }
// Combine all the source maps for each preprocessor function into one
const map: RawSourceMap = combine_sourcemaps(
filename,
sourcemap_list
);
return { return {
// TODO return separated output, in future version where svelte.compile supports it: // TODO return separated output, in future version where svelte.compile supports it:
// style: { code: styleCode, map: styleMap }, // style: { code: styleCode, map: styleMap },
@ -138,7 +297,7 @@ export default async function preprocess(
code: source, code: source,
dependencies: [...new Set(dependencies)], dependencies: [...new Set(dependencies)],
map: (map as object),
toString() { toString() {
return source; return source;
} }

@ -0,0 +1,276 @@
import { DecodedSourceMap, RawSourceMap, SourceMapLoader } from '@ampproject/remapping/dist/types/types';
import remapping from '@ampproject/remapping';
import { SourceMap } from 'magic-string';
type SourceLocation = {
line: number;
column: number;
};
function last_line_length(s: string) {
return s.length - s.lastIndexOf('\n') - 1;
}
// mutate map in-place
export function sourcemap_add_offset(
map: DecodedSourceMap, offset: SourceLocation
) {
if (map.mappings.length == 0) return map;
// shift columns in first line
const segment_list = map.mappings[0];
for (let segment = 0; segment < segment_list.length; segment++) {
const seg = segment_list[segment];
if (seg[3]) seg[3] += offset.column;
}
// shift lines
for (let line = 0; line < map.mappings.length; line++) {
const segment_list = map.mappings[line];
for (let segment = 0; segment < segment_list.length; segment++) {
const seg = segment_list[segment];
if (seg[2]) seg[2] += offset.line;
}
}
}
function merge_tables<T>(this_table: T[], other_table: T[]): [T[], number[], boolean, boolean] {
const new_table = this_table.slice();
const idx_map = [];
other_table = other_table || [];
let val_changed = false;
for (const [other_idx, other_val] of other_table.entries()) {
const this_idx = this_table.indexOf(other_val);
if (this_idx >= 0) {
idx_map[other_idx] = this_idx;
} else {
const new_idx = new_table.length;
new_table[new_idx] = other_val;
idx_map[other_idx] = new_idx;
val_changed = true;
}
}
let idx_changed = val_changed;
if (val_changed) {
if (idx_map.find((val, idx) => val != idx) === undefined) {
// idx_map is identity map [0, 1, 2, 3, 4, ....]
idx_changed = false;
}
}
return [new_table, idx_map, val_changed, idx_changed];
}
function pushArray<T>(_this: T[], other: T[]) {
// We use push to mutate in place for memory and perf reasons
// We use the for loop instead of _this.push(...other) to avoid the JS engine's function argument limit (65,535 in JavascriptCore)
for (let i = 0; i < other.length; i++) {
_this.push(other[i]);
}
}
export class StringWithSourcemap {
string: string;
map: DecodedSourceMap;
constructor(string = '', map: DecodedSourceMap = null) {
this.string = string;
if (map) {
this.map = map as DecodedSourceMap;
} else {
this.map = {
version: 3,
mappings: [],
sources: [],
names: []
};
}
}
/**
* concat in-place (mutable), return this (chainable)
* will also mutate the `other` object
*/
concat(other: StringWithSourcemap): StringWithSourcemap {
// noop: if one is empty, return the other
if (other.string == '') return this;
if (this.string == '') {
this.string = other.string;
this.map = other.map;
return this;
}
this.string += other.string;
const m1 = this.map;
const m2 = other.map;
if (m2.mappings.length == 0) return this;
// combine sources and names
const [sources, new_source_idx, sources_changed, sources_idx_changed] = merge_tables(m1.sources, m2.sources);
const [names, new_name_idx, names_changed, names_idx_changed] = merge_tables(m1.names, m2.names);
if (sources_changed) m1.sources = sources;
if (names_changed) m1.names = names;
// unswitched loops are faster
if (sources_idx_changed && names_idx_changed) {
for (let line = 0; line < m2.mappings.length; line++) {
const segment_list = m2.mappings[line];
for (let segment = 0; segment < segment_list.length; segment++) {
const seg = segment_list[segment];
if (seg[1]) seg[1] = new_source_idx[seg[1]];
if (seg[4]) seg[4] = new_name_idx[seg[4]];
}
}
} else if (sources_idx_changed) {
for (let line = 0; line < m2.mappings.length; line++) {
const segment_list = m2.mappings[line];
for (let segment = 0; segment < segment_list.length; segment++) {
const seg = segment_list[segment];
if (seg[1]) seg[1] = new_source_idx[seg[1]];
}
}
} else if (names_idx_changed) {
for (let line = 0; line < m2.mappings.length; line++) {
const segment_list = m2.mappings[line];
for (let segment = 0; segment < segment_list.length; segment++) {
const seg = segment_list[segment];
if (seg[4]) seg[4] = new_name_idx[seg[4]];
}
}
}
// combine the mappings
// combine
// 1. last line of first map
// 2. first line of second map
// columns of 2 must be shifted
const column_offset = last_line_length(this.string);
if (m2.mappings.length > 0 && column_offset > 0) {
const first_line = m2.mappings[0];
for (let i = 0; i < first_line.length; i++) {
first_line[i][0] += column_offset;
}
}
// combine last line + first line
pushArray(m1.mappings[m1.mappings.length - 1], m2.mappings.shift());
// append other lines
pushArray(m1.mappings, m2.mappings);
return this;
}
static from_processed(string: string, map?: DecodedSourceMap): StringWithSourcemap {
if (map) return new StringWithSourcemap(string, map);
if (string == '') return new StringWithSourcemap();
map = { version: 3, names: [], sources: [], mappings: [] };
// add empty SourceMapSegment[] for every line
const line_count = (string.match(/\n/g) || '').length;
for (let i = 0; i < line_count; i++) map.mappings.push([]);
return new StringWithSourcemap(string, map);
}
static from_source(
source_file: string, source: string, offset?: SourceLocation
): StringWithSourcemap {
if (!offset) offset = { line: 0, column: 0 };
const map: DecodedSourceMap = { version: 3, names: [], sources: [source_file], mappings: [] };
if (source == '') return new StringWithSourcemap(source, map);
// we create a high resolution identity map here,
// we know that it will eventually be merged with svelte's map,
// at which stage the resolution will decrease.
const line_list = source.split('\n');
for (let line = 0; line < line_list.length; line++) {
map.mappings.push([]);
const token_list = line_list[line].split(/([^\d\w\s]|\s+)/g);
for (let token = 0, column = 0; token < token_list.length; token++) {
if (token_list[token] == '') continue;
map.mappings[line].push([column, 0, offset.line + line, column]);
column += token_list[token].length;
}
}
// shift columns in first line
const segment_list = map.mappings[0];
for (let segment = 0; segment < segment_list.length; segment++) {
segment_list[segment][3] += offset.column;
}
return new StringWithSourcemap(source, map);
}
}
export function combine_sourcemaps(
filename: string,
sourcemap_list: Array<DecodedSourceMap | RawSourceMap>
): RawSourceMap {
if (sourcemap_list.length == 0) return null;
let map_idx = 1;
const map: RawSourceMap =
sourcemap_list.slice(0, -1)
.find(m => m.sources.length !== 1) === undefined
? remapping( // use array interface
// only the oldest sourcemap can have multiple sources
sourcemap_list,
() => null,
true // skip optional field `sourcesContent`
)
: remapping( // use loader interface
sourcemap_list[0], // last map
function loader(sourcefile) {
if (sourcefile === filename && sourcemap_list[map_idx]) {
return sourcemap_list[map_idx++]; // idx 1, 2, ...
// bundle file = branch node
} else {
return null; // source file = leaf node
}
} as SourceMapLoader,
true
);
if (!map.file) delete map.file; // skip optional field `file`
return map;
}
// browser vs node.js
const b64enc = typeof btoa == 'function' ? btoa : b => Buffer.from(b).toString('base64');
export function apply_preprocessor_sourcemap(filename: string, svelte_map: SourceMap, preprocessor_map_input: string | DecodedSourceMap | RawSourceMap): SourceMap {
if (!svelte_map || !preprocessor_map_input) return svelte_map;
const preprocessor_map = typeof preprocessor_map_input === 'string' ? JSON.parse(preprocessor_map_input) : preprocessor_map_input;
const result_map = combine_sourcemaps(
filename,
[
svelte_map as RawSourceMap,
preprocessor_map
]
) as RawSourceMap;
// Svelte expects a SourceMap which includes toUrl and toString. Instead of wrapping our output in a class,
// we just tack on the extra properties.
Object.defineProperties(result_map, {
toString: {
enumerable: false,
value: function toString() {
return JSON.stringify(this);
}
},
toUrl: {
enumerable: false,
value: function toUrl() {
return 'data:application/json;charset=utf-8;base64,' + b64enc(this.toString());
}
}
});
return result_map as SourceMap;
}

@ -7,7 +7,9 @@ export {
afterUpdate, afterUpdate,
setContext, setContext,
getContext, getContext,
hasContext,
tick, tick,
createEventDispatcher, createEventDispatcher,
SvelteComponentDev as SvelteComponent SvelteComponentDev as SvelteComponent,
SvelteComponentTyped
} from 'svelte/internal'; } from 'svelte/internal';

@ -212,6 +212,9 @@ if (typeof HTMLElement === 'function') {
}; };
} }
/**
* Base class for Svelte components. Used when dev=false.
*/
export class SvelteComponent { export class SvelteComponent {
$$: T$$; $$: T$$;
$$set?: ($$props: any) => void; $$set?: ($$props: any) => void;

@ -100,12 +100,22 @@ export function validate_slots(name, slot, keys) {
type Props = Record<string, any>; type Props = Record<string, any>;
export interface SvelteComponentDev { export interface SvelteComponentDev {
$set(props?: Props): void; $set(props?: Props): void;
$on<T = any>(event: string, callback: (event: CustomEvent<T>) => void): () => void; $on(event: string, callback: (event: any) => void): () => void;
$destroy(): void; $destroy(): void;
[accessor: string]: any; [accessor: string]: any;
} }
/**
* Base class for Svelte components with some minor dev-enhancements. Used when dev=true.
*/
export class SvelteComponentDev extends SvelteComponent { export class SvelteComponentDev extends SvelteComponent {
/**
* @private
* For type checking capabilities only.
* Does not exist at runtime.
* ### DO NOT USE!
*/
$$prop_def: Props;
constructor(options: { constructor(options: {
target: Element; target: Element;
anchor?: Element; anchor?: Element;
@ -133,6 +143,90 @@ export class SvelteComponentDev extends SvelteComponent {
$inject_state() {} $inject_state() {}
} }
// TODO https://github.com/microsoft/TypeScript/issues/41770 is the reason
// why we have to split out SvelteComponentTyped to not break existing usage of SvelteComponent.
// Try to find a better way for Svelte 4.0.
export interface SvelteComponentTyped<
Props extends Record<string, any> = any,
Events extends Record<string, any> = any,
Slots extends Record<string, any> = any // eslint-disable-line @typescript-eslint/no-unused-vars
> {
$set(props?: Partial<Props>): void;
$on<K extends Extract<keyof Events, string>>(type: K, callback: (e: Events[K]) => void): () => void;
$destroy(): void;
[accessor: string]: any;
}
/**
* Base class to create strongly typed Svelte components.
* This only exists for typing purposes and should be used in `.d.ts` files.
*
* ### Example:
*
* You have component library on npm called `component-library`, from which
* you export a component called `MyComponent`. For Svelte+TypeScript users,
* you want to provide typings. Therefore you create a `index.d.ts`:
* ```ts
* import { SvelteComponentTyped } from "svelte";
* export class MyComponent extends SvelteComponentTyped<{foo: string}> {}
* ```
* Typing this makes it possible for IDEs like VS Code with the Svelte extension
* to provide intellisense and to use the component like this in a Svelte file
* with TypeScript:
* ```svelte
* <script lang="ts">
* import { MyComponent } from "component-library";
* </script>
* <MyComponent foo={'bar'} />
* ```
*
* #### Why not make this part of `SvelteComponent(Dev)`?
* Because
* ```ts
* class ASubclassOfSvelteComponent extends SvelteComponent<{foo: string}> {}
* const component: typeof SvelteComponent = ASubclassOfSvelteComponent;
* ```
* will throw a type error, so we need to seperate the more strictly typed class.
*/
export class SvelteComponentTyped<
Props extends Record<string, any> = any,
Events extends Record<string, any> = any,
Slots extends Record<string, any> = any
> extends SvelteComponentDev {
/**
* @private
* For type checking capabilities only.
* Does not exist at runtime.
* ### DO NOT USE!
*/
$$prop_def: Props;
/**
* @private
* For type checking capabilities only.
* Does not exist at runtime.
* ### DO NOT USE!
*/
$$events_def: Events;
/**
* @private
* For type checking capabilities only.
* Does not exist at runtime.
* ### DO NOT USE!
*/
$$slot_def: Slots;
constructor(options: {
target: Element;
anchor?: Element;
props?: Props;
hydrate?: boolean;
intro?: boolean;
$$inline?: boolean;
}) {
super(options);
}
}
export function loop_guard(timeout) { export function loop_guard(timeout) {
const start = Date.now(); const start = Date.now();
return () => { return () => {

@ -54,6 +54,10 @@ export function getContext<T>(key): T {
return get_current_component().$$.context.get(key); return get_current_component().$$.context.get(key);
} }
export function hasContext(key): boolean {
return get_current_component().$$.context.has(key);
}
// TODO figure out if we still want to support // TODO figure out if we still want to support
// shorthand events, or if we want to implement // shorthand events, or if we want to implement
// a real bubbling mechanism // a real bubbling mechanism

@ -48,7 +48,7 @@ export function transition_in(block, local?: 0 | 1) {
} }
} }
export function transition_out(block, local: 0 | 1, detach: 0 | 1, callback) { export function transition_out(block, local: 0 | 1, detach?: 0 | 1, callback?) {
if (block && block.o) { if (block && block.o) {
if (outroing.has(block)) return; if (outroing.has(block)) return;
outroing.add(block); outroing.add(block);

@ -117,6 +117,14 @@ export function update_slot(slot, slot_definition, ctx, $$scope, dirty, get_slot
} }
} }
export function update_slot_spread(slot, slot_definition, ctx, $$scope, dirty, get_slot_changes_fn, get_slot_spread_changes_fn, get_slot_context_fn) {
const slot_changes = get_slot_spread_changes_fn(dirty) | get_slot_changes(slot_definition, $$scope, dirty, get_slot_changes_fn);
if (slot_changes) {
const slot_context = get_slot_context(slot_definition, ctx, $$scope, get_slot_context_fn);
slot.p(slot_context, slot_changes);
}
}
export function exclude_internal_props(props) { export function exclude_internal_props(props) {
const result = {}; const result = {};
for (const k in props) if (k[0] !== '$') result[k] = props[k]; for (const k in props) if (k[0] !== '$') result[k] = props[k];

@ -62,7 +62,7 @@ function instance($$self, $$props, $$invalidate) {
} }
}; };
return [x]; return [x, y];
} }
class Component extends SvelteComponent { class Component extends SvelteComponent {

@ -12,15 +12,15 @@ function instance($$self, $$props, $$invalidate) {
$$self.$$.update = () => { $$self.$$.update = () => {
if ($$self.$$.dirty & /*x*/ 1) { if ($$self.$$.dirty & /*x*/ 1) {
$: $$invalidate(2, b = x); $: $$invalidate(1, b = x);
} }
if ($$self.$$.dirty & /*b*/ 4) { if ($$self.$$.dirty & /*b*/ 2) {
$: a = b; $: a = b;
} }
}; };
return [x]; return [x, b];
} }
class Component extends SvelteComponent { class Component extends SvelteComponent {

@ -64,7 +64,7 @@ function instance($$self, $$props, $$invalidate) {
}; };
$: x = a * 2; $: x = a * 2;
return [y]; return [y, b];
} }
class Component extends SvelteComponent { class Component extends SvelteComponent {

@ -24,6 +24,9 @@ describe('preprocess', () => {
config.options || { filename: 'input.svelte' } config.options || { filename: 'input.svelte' }
); );
fs.writeFileSync(`${__dirname}/samples/${dir}/_actual.html`, result.code); fs.writeFileSync(`${__dirname}/samples/${dir}/_actual.html`, result.code);
if (result.map) {
fs.writeFileSync(`${__dirname}/samples/${dir}/_actual.html.map`, JSON.stringify(result.map, null, 2));
}
assert.equal(result.code, expected); assert.equal(result.code, expected);

@ -0,0 +1,7 @@
<script>
export let obj;
export let c;
export let d;
</script>
<slot {c} {...obj} {d} />

@ -0,0 +1,55 @@
export default {
props: {
obj: { a: 1, b: 42 },
c: 5,
d: 10
},
html: `
<p>1</p>
<p>42</p>
<p>5</p>
<p>10</p>
`,
test({ assert, target, component }) {
component.obj = { a: 2, b: 50, c: 30 };
assert.htmlEqual(target.innerHTML, `
<p>2</p>
<p>50</p>
<p>30</p>
<p>10</p>
`);
component.c = 22;
assert.htmlEqual(target.innerHTML, `
<p>2</p>
<p>50</p>
<p>30</p>
<p>10</p>
`);
component.d = 44;
assert.htmlEqual(target.innerHTML, `
<p>2</p>
<p>50</p>
<p>30</p>
<p>44</p>
`);
component.obj = { a: 9, b: 12 };
assert.htmlEqual(target.innerHTML, `
<p>9</p>
<p>12</p>
<p>22</p>
<p>44</p>
`);
component.c = 88;
assert.htmlEqual(target.innerHTML, `
<p>9</p>
<p>12</p>
<p>88</p>
<p>44</p>
`);
}
};

@ -0,0 +1,14 @@
<script>
import Nested from './Nested.svelte';
export let obj;
export let c;
export let d;
</script>
<Nested {obj} {c} {d} let:a let:b let:c let:d>
<p>{a}</p>
<p>{b}</p>
<p>{c}</p>
<p>{d}</p>
</Nested>

@ -0,0 +1,7 @@
<script>
import { hasContext } from 'svelte';
const has = hasContext('test');
</script>
<div>{has}</div>

@ -0,0 +1,11 @@
<script>
import { setContext } from 'svelte';
export let value = '';
if (value) {
setContext('test', value);
}
</script>
<slot></slot>

@ -0,0 +1,6 @@
export default {
html: `
<div>true</div>
<div>false</div>
`
};

@ -0,0 +1,12 @@
<script>
import Nested from "./Nested.svelte";
import Leaf from "./Leaf.svelte";
</script>
<Nested value="bar">
<Leaf />
</Nested>
<Nested>
<Leaf />
</Nested>

@ -0,0 +1,21 @@
export default {
html: `
<section>
<div>Second</div>
</section>
<button>Click</button>
`,
async test({ assert, component, target, window }) {
const button = target.querySelector('button');
await button.dispatchEvent(new window.Event('click'));
assert.htmlEqual(target.innerHTML, `
<section>
<div>First</div>
<div>Second</div>
</section>
<button>Click</button>
`);
}
};

@ -0,0 +1,17 @@
<script>
let slide = 0;
let num = false;
const changeNum = () => num = !num;
</script>
<section>
{#key slide}
{#if num}
<div>First</div>
{/if}
{/key}
<div>Second</div>
</section>
<button on:click={changeNum}>Click</button>

@ -0,0 +1,26 @@
export default {
html: `
<p>42</p>
<p>42</p>
`,
async test({ assert, component, target }) {
await component.updateStore(undefined);
assert.htmlEqual(target.innerHTML, '<p>undefined</p><p>42</p>');
await component.updateStore(33);
assert.htmlEqual(target.innerHTML, '<p>33</p><p>42</p>');
await component.updateStore(undefined);
assert.htmlEqual(target.innerHTML, '<p>undefined</p><p>42</p>');
await component.updateVar(undefined);
assert.htmlEqual(target.innerHTML, '<p>undefined</p><p>undefined</p>');
await component.updateVar(33);
assert.htmlEqual(target.innerHTML, '<p>undefined</p><p>33</p>');
await component.updateVar(undefined);
assert.htmlEqual(target.innerHTML, '<p>undefined</p><p>undefined</p>');
}
};

@ -0,0 +1,19 @@
<script>
import { writable } from 'svelte/store';
let store = writable(42);
let variable = 42;
let value;
let value2;
$: value = $store;
$: value2 = variable;
export function updateStore(value) {
store.set(value);
}
export function updateVar(value) {
variable = value;
}
</script>
<p>{ value }</p>
<p>{ value2 }</p>

@ -12,7 +12,7 @@ require.extensions['.js'] = function(module, filename) {
.replace(/^import (\w+) from ['"]([^'"]+)['"];?/gm, 'var {default: $1} = require("$2");') .replace(/^import (\w+) from ['"]([^'"]+)['"];?/gm, 'var {default: $1} = require("$2");')
.replace(/^import {([^}]+)} from ['"](.+)['"];?/gm, 'var {$1} = require("$2");') .replace(/^import {([^}]+)} from ['"](.+)['"];?/gm, 'var {$1} = require("$2");')
.replace(/^export default /gm, 'exports.default = ') .replace(/^export default /gm, 'exports.default = ')
.replace(/^export (const|let|var|class|function) (\w+)/gm, (match, type, name) => { .replace(/^export (const|let|var|class|function|async\s+function) (\w+)/gm, (match, type, name) => {
exports.push(name); exports.push(name);
return `${type} ${name}`; return `${type} ${name}`;
}) })

@ -0,0 +1,20 @@
import MagicString from 'magic-string';
export function magic_string_preprocessor_result(filename: string, src: MagicString) {
return {
code: src.toString(),
map: src.generateMap({
source: filename,
hires: true,
includeContent: false
})
};
}
export function magic_string_replace_all(src: MagicString, search: string, replace: string) {
let idx = src.original.indexOf(search);
if (idx == -1) throw new Error('search not found in src');
do {
src.overwrite(idx, idx + search.length, replace, { storeName: true });
} while ((idx = src.original.indexOf(search, idx + 1)) != -1);
}

@ -37,7 +37,7 @@ describe('sourcemaps', () => {
const preprocessed = await svelte.preprocess( const preprocessed = await svelte.preprocess(
input.code, input.code,
config.preprocess || {}, config.preprocess || {},
{ config.options || {
filename: 'input.svelte' filename: 'input.svelte'
} }
); );
@ -46,8 +46,10 @@ describe('sourcemaps', () => {
preprocessed.code, { preprocessed.code, {
filename: 'input.svelte', filename: 'input.svelte',
// filenames for sourcemaps // filenames for sourcemaps
sourcemap: preprocessed.map,
outputFilename: `${outputName}.js`, outputFilename: `${outputName}.js`,
cssOutputFilename: `${outputName}.css` cssOutputFilename: `${outputName}.css`,
...(config.compile_options || {})
}); });
js.code = js.code.replace( js.code = js.code.replace(
@ -107,7 +109,7 @@ describe('sourcemaps', () => {
css.mapConsumer = css.map && await new SourceMapConsumer(css.map); css.mapConsumer = css.map && await new SourceMapConsumer(css.map);
css.locate = getLocator(css.code || ''); css.locate = getLocator(css.code || '');
css.locate_1 = getLocator(css.code || '', { offsetLine: 1 }); css.locate_1 = getLocator(css.code || '', { offsetLine: 1 });
test({ assert, input, preprocessed, js, css }); await test({ assert, input, preprocessed, js, css });
}); });
}); });
}); });

@ -0,0 +1,21 @@
import MagicString from 'magic-string';
import { magic_string_preprocessor_result, magic_string_replace_all } from '../../helpers';
export default {
compile_options: {
dev: true
},
preprocess: [
{ style: ({ content, filename }) => {
const src = new MagicString(content);
magic_string_replace_all(src, '--replace-me-once', '\n --done-replace-once');
magic_string_replace_all(src, '--replace-me-twice', '\n--almost-done-replace-twice');
return magic_string_preprocessor_result(filename, src);
} },
{ style: ({ content, filename }) => {
const src = new MagicString(content);
magic_string_replace_all(src, '--almost-done-replace-twice', '\n --done-replace-twice');
return magic_string_preprocessor_result(filename, src);
} }
]
};

@ -0,0 +1,15 @@
<h1>Testing Styles</h1>
<h2>Testing Styles 2</h2>
<div>Testing Styles 3</div>
<script>export const b = 2;</script>
<style>
h1 {
--replace-me-once: red;
}
h2 {
--replace-me-twice: green;
}
div {
--keep-me: blue;
}
</style>

@ -0,0 +1,40 @@
import { SourceMapConsumer } from 'source-map';
const b64dec = s => Buffer.from(s, 'base64').toString();
export async function test({ assert, css, js }) {
// We check that the css source map embedded in the js is accurate
const match = js.code.match(/\tstyle\.textContent = "(.*?)(?:\\n\/\*# sourceMappingURL=data:(.*?);charset=(.*?);base64,(.*?) \*\/)?";\n/);
assert.notEqual(match, null);
const [mimeType, encoding, cssMapBase64] = match.slice(2);
assert.equal(mimeType, 'application/json');
assert.equal(encoding, 'utf-8');
const cssMapJson = b64dec(cssMapBase64);
css.mapConsumer = await new SourceMapConsumer(cssMapJson);
// TODO make util fn + move to test index.js
const sourcefile = 'input.svelte';
[
// TODO how to get line + column numbers?
[css, '--keep-me', 13, 2],
[css, '--done-replace-once', 6, 5],
[css, '--done-replace-twice', 9, 5]
]
.forEach(([where, content, line, column]) => {
assert.deepEqual(
where.mapConsumer.originalPositionFor(
where.locate_1(content)
),
{
source: sourcefile,
name: null,
line,
column
},
`failed to locate "${content}" from "${sourcefile}"`
);
});
}

@ -0,0 +1,15 @@
import MagicString from 'magic-string';
import { magic_string_preprocessor_result, magic_string_replace_all } from '../../helpers';
export default {
js_map_sources: [], // test component has no scripts
preprocess: {
markup: ({ content, filename }) => {
const src = new MagicString(content);
magic_string_replace_all(src, 'replace me', 'success');
return magic_string_preprocessor_result(filename, src);
}
}
};

@ -0,0 +1,2 @@
<h1>decoded-sourcemap</h1>
<div>replace me</div>

@ -0,0 +1,19 @@
export function test({ assert, input, preprocessed }) {
const expected = input.locate('replace me');
const start = preprocessed.locate('success');
const actualbar = preprocessed.mapConsumer.originalPositionFor({
line: start.line + 1,
column: start.column
});
assert.deepEqual(actualbar, {
source: 'input.svelte',
name: 'replace me',
line: expected.line + 1,
column: expected.column
});
}

@ -0,0 +1,12 @@
import MagicString from 'magic-string';
import { magic_string_preprocessor_result, magic_string_replace_all } from '../../helpers';
export default {
preprocess: {
markup: ({ content, filename }) => {
const src = new MagicString(content);
magic_string_replace_all(src, 'baritone', 'bar');
return magic_string_preprocessor_result(filename, src);
}
}
};

@ -0,0 +1,5 @@
<script>
export let foo;
</script>
{foo.baritone.baz}

@ -0,0 +1,32 @@
export function test({ assert, input, js }) {
const expectedBar = input.locate('baritone.baz');
const expectedBaz = input.locate('.baz');
let start = js.locate('bar.baz');
const actualbar = js.mapConsumer.originalPositionFor({
line: start.line + 1,
column: start.column
});
assert.deepEqual(actualbar, {
source: 'input.svelte',
name: 'baritone',
line: expectedBar.line + 1,
column: expectedBar.column
});
start = js.locate('.baz');
const actualbaz = js.mapConsumer.originalPositionFor({
line: start.line + 1,
column: start.column
});
assert.deepEqual(actualbaz, {
source: 'input.svelte',
name: null,
line: expectedBaz.line + 1,
column: expectedBaz.column
});
}

@ -0,0 +1,25 @@
import MagicString from 'magic-string';
import { magic_string_preprocessor_result, magic_string_replace_all } from '../../helpers';
export default {
preprocess: {
markup: ({ content, filename }) => {
const src = new MagicString(content);
magic_string_replace_all(src, 'baritone', 'bar');
magic_string_replace_all(src, '--bazitone', '--baz');
return magic_string_preprocessor_result(filename, src);
},
script: ({ content, filename }) => {
const src = new MagicString(content);
const idx = content.indexOf('bar');
src.prependLeft(idx, ' ');
return magic_string_preprocessor_result(filename, src);
},
style: ({ content, filename }) => {
const src = new MagicString(content);
const idx = content.indexOf('--baz');
src.prependLeft(idx, ' ');
return magic_string_preprocessor_result(filename, src);
}
}
};

@ -0,0 +1,9 @@
<script>
export let foo = { baritone: 5 }
</script>
<style>
h1 {
background-color: var(--bazitone);
}
</style>
<h1>multiple {foo}</h1>

@ -0,0 +1,32 @@
export function test({ assert, input, js, css }) {
const expectedBar = input.locate('baritone');
const expectedBaz = input.locate('--bazitone');
let start = js.locate('bar');
const actualbar = js.mapConsumer.originalPositionFor({
line: start.line + 1,
column: start.column
});
assert.deepEqual(actualbar, {
source: 'input.svelte',
name: 'baritone',
line: expectedBar.line + 1,
column: expectedBar.column
});
start = css.locate('--baz');
const actualbaz = css.mapConsumer.originalPositionFor({
line: start.line + 1,
column: start.column
});
assert.deepEqual(actualbaz, {
source: 'input.svelte',
name: '--bazitone',
line: expectedBaz.line + 1,
column: expectedBaz.column
});
}

@ -0,0 +1,12 @@
import MagicString from 'magic-string';
import { magic_string_preprocessor_result, magic_string_replace_all } from '../../helpers';
export default {
preprocess: {
script: ({ content, filename }) => {
const src = new MagicString(content);
magic_string_replace_all(src, 'baritone', 'bar');
return magic_string_preprocessor_result(filename, src);
}
}
};

@ -0,0 +1,9 @@
<style>
h1 {
color: red;
}
</style>
<script>
export let foo = { baritone: { baz: 5 } }
</script>
<h1>{foo.bar.baz}</h1>

@ -0,0 +1,32 @@
export function test({ assert, input, js }) {
const expectedBar = input.locate('baritone:');
const expectedBaz = input.locate('baz:');
let start = js.locate('bar:');
const actualbar = js.mapConsumer.originalPositionFor({
line: start.line + 1,
column: start.column
});
assert.deepEqual(actualbar, {
source: 'input.svelte',
name: 'baritone',
line: expectedBar.line + 1,
column: expectedBar.column
}, "couldn't find bar: in source");
start = js.locate('baz:');
const actualbaz = js.mapConsumer.originalPositionFor({
line: start.line + 1,
column: start.column
});
assert.deepEqual(actualbaz, {
source: 'input.svelte',
name: null,
line: expectedBaz.line + 1,
column: expectedBaz.column
}, "couldn't find baz: in source");
}

@ -0,0 +1,12 @@
import MagicString from 'magic-string';
import { magic_string_preprocessor_result, magic_string_replace_all } from '../../helpers';
export default {
preprocess: {
style: ({ content, filename }) => {
const src = new MagicString(content);
magic_string_replace_all(src, 'baritone', 'bar');
return magic_string_preprocessor_result(filename, src);
}
}
};

@ -0,0 +1,12 @@
<h1>Testing Styles</h1>
<h2>Testing Styles 2</h2>
<script>export const b = 2;</script>
<style>
h1 {
--baritone: red;
}
h2 {
--baz: blue;
}
</style>

@ -0,0 +1,32 @@
export function test({ assert, input, css }) {
const expectedBar = input.locate('--baritone');
const expectedBaz = input.locate('--baz');
let start = css.locate('--bar');
const actualbar = css.mapConsumer.originalPositionFor({
line: start.line + 1,
column: start.column
});
assert.deepEqual(actualbar, {
source: 'input.svelte',
name: null,
line: expectedBar.line + 1,
column: expectedBar.column
}, "couldn't find bar in source");
start = css.locate('--baz');
const actualbaz = css.mapConsumer.originalPositionFor({
line: start.line + 1,
column: start.column
});
assert.deepEqual(actualbaz, {
source: 'input.svelte',
name: null,
line: expectedBaz.line + 1,
column: expectedBaz.column
}, "couldn't find baz in source");
}

@ -0,0 +1,25 @@
import MagicString from 'magic-string';
import { SourceMapConsumer, SourceMapGenerator } from 'source-map';
export default {
preprocess: {
style: async ({ content, filename }) => {
const src = new MagicString(content);
const idx = content.indexOf('baritone');
src.overwrite(idx, idx+'baritone'.length, 'bar');
const map = SourceMapGenerator.fromSourceMap(
await new SourceMapConsumer(
// sourcemap must be encoded for SourceMapConsumer
src.generateMap({
source: filename,
hires: true,
includeContent: false
})
)
);
return { code: src.toString(), map };
}
}
};

@ -0,0 +1,12 @@
<h1>Testing Styles</h1>
<h2>Testing Styles 2</h2>
<script>export const b = 2;</script>
<style>
h1 {
--baritone: red;
}
h2 {
--baz: blue;
}
</style>

@ -0,0 +1 @@
export { test } from '../preprocessed-styles/test';

@ -0,0 +1,33 @@
import MagicString from 'magic-string';
import { magic_string_preprocessor_result, magic_string_replace_all } from '../../helpers';
export default {
preprocess: [
{
markup: ({ content, filename }) => {
const src = new MagicString(content);
magic_string_replace_all(src, 'baritone', 'bar');
magic_string_replace_all(src,'--bazitone', '--baz');
magic_string_replace_all(src,'old_name_1', 'temp_new_name_1');
magic_string_replace_all(src,'old_name_2', 'temp_new_name_2');
return magic_string_preprocessor_result(filename, src);
}
},
{
markup: ({ content, filename }) => {
const src = new MagicString(content);
magic_string_replace_all(src, 'temp_new_name_1', 'temp_temp_new_name_1');
magic_string_replace_all(src, 'temp_new_name_2', 'temp_temp_new_name_2');
return magic_string_preprocessor_result(filename, src);
}
},
{
markup: ({ content, filename }) => {
const src = new MagicString(content);
magic_string_replace_all(src, 'temp_temp_new_name_1', 'new_name_1');
magic_string_replace_all(src, 'temp_temp_new_name_2', 'new_name_2');
return magic_string_preprocessor_result(filename, src);
}
}
]
};

@ -0,0 +1,12 @@
<script>
export let old_name_1 = { baritone: 5 };
let old_name_2 = 'value_2';
</script>
<style>
div {
background-color: var(--bazitone);
}
</style>
<h1>use-names</h1>
<div>{old_name_1.baritone}</div>
<pre style="color: var(--bazitone)">{old_name_2}</pre>

@ -0,0 +1,42 @@
// needed for workaround, TODO remove
import { getLocator } from 'locate-character';
export function test({ assert, preprocessed, js, css }) {
assert.deepEqual(
preprocessed.map.names.sort(),
['baritone', '--bazitone', 'old_name_1', 'old_name_2'].sort()
);
function test_name(old_name, new_name, where) {
let loc = { character: -1 };
while (loc = where.locate(new_name, loc.character + 1)) {
const actualMapping = where.mapConsumer.originalPositionFor({
line: loc.line + 1, column: loc.column
});
if (actualMapping.line === null) {
// location is not mapped - ignore
continue;
}
assert.equal(actualMapping.name, old_name);
}
if (loc === undefined) {
// workaround for bug in locate-character, TODO remove
// https://github.com/Rich-Harris/locate-character/pull/5
where.locate = getLocator(where.code);
}
}
test_name('baritone', 'bar', js);
test_name('baritone', 'bar', preprocessed);
test_name('--bazitone', '--baz', css);
test_name('--bazitone', '--baz', preprocessed);
test_name('old_name_1', 'new_name_1', js);
test_name('old_name_1', 'new_name_1', preprocessed);
test_name('old_name_2', 'new_name_2', js);
test_name('old_name_2', 'new_name_2', preprocessed);
}

@ -0,0 +1,60 @@
/* eslint-disable import/no-duplicates */
/* the code that transforms these to commonjs, can't handle "MagicString, { Bundle } from.." */
import MagicString from 'magic-string';
import { Bundle } from 'magic-string';
function add(bundle, filename, source) {
bundle.addSource({
filename,
content: new MagicString(source),
separator: '\n'
//separator: '' // ERROR. probably a bug in magic-string
});
}
function result(bundle, filename) {
return {
code: bundle.toString(),
map: bundle.generateMap({
file: filename,
includeContent: false,
hires: true // required for remapping
})
};
}
export default {
js_map_sources: [
'input.svelte',
'foo.js',
'bar.js',
'foo2.js',
'bar2.js'
],
preprocess: [
{
script: ({ content, filename }) => {
const bundle = new Bundle();
add(bundle, filename, content);
add(bundle, 'foo.js', 'var answer = 42; // foo.js\n');
add(bundle, 'bar.js', 'console.log(answer); // bar.js\n');
return result(bundle, filename);
}
},
{
script: ({ content, filename }) => {
const bundle = new Bundle();
add(bundle, filename, content);
add(bundle, 'foo2.js', 'var answer2 = 84; // foo2.js\n');
add(bundle, 'bar2.js', 'console.log(answer2); // bar2.js\n');
return result(bundle, filename);
}
}
]
};

@ -0,0 +1,4 @@
<script>
export let name;
</script>
<h1>sourcemap-sources</h1>

@ -0,0 +1,29 @@
export function test({ assert, preprocessed, js }) {
assert.equal(preprocessed.error, undefined);
// sourcemap stores location only for 'answer = 42;'
// not for 'var answer = 42;'
[
[js, 'foo.js', 'answer = 42;', 4],
[js, 'bar.js', 'console.log(answer);', 0],
[js, 'foo2.js', 'answer2 = 84;', 4],
[js, 'bar2.js', 'console.log(answer2);', 0]
]
.forEach(([where, sourcefile, content, column]) => {
assert.deepEqual(
where.mapConsumer.originalPositionFor(
where.locate_1(content)
),
{
source: sourcefile,
name: null,
line: 1,
column
},
`failed to locate "${content}" from "${sourcefile}"`
);
});
}

@ -24,6 +24,7 @@
"strict": true, "strict": true,
"noImplicitThis": true, "noImplicitThis": true,
"noUnusedLocals": true, "noUnusedLocals": true,
"noUnusedParameters": true "noUnusedParameters": true,
"typeRoots": ["./node_modules/@types"]
} }
} }

Loading…
Cancel
Save