@wuchale/svelte 0.14.3 → 0.15.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,16 +1,11 @@
1
- // $$ cd .. && npm run test
2
1
  import { defaultGenerateLoadID, defaultHeuristic, deepMergeObjects } from 'wuchale';
3
2
  import { SvelteTransformer } from "./transformer.js";
4
3
  import { getDependencies, loaderPathResolver } from 'wuchale/adapter-utils';
5
4
  const topLevelDeclarationsInside = ['$derived', '$derived.by'];
6
- const ignoreElements = ['style', 'path'];
7
5
  const svelteHeuristic = (msgStr, details) => {
8
6
  if (!defaultHeuristic(msgStr, details)) {
9
7
  return false;
10
8
  }
11
- if (ignoreElements.includes(details.element)) {
12
- return false;
13
- }
14
9
  if (details.scope !== 'script') {
15
10
  return true;
16
11
  }
@@ -2,8 +2,8 @@ import type { AnyNode } from "acorn";
2
2
  import { type AST } from "svelte/compiler";
3
3
  import { Message } from 'wuchale';
4
4
  import { Transformer } from 'wuchale/adapter-vanilla';
5
- import type { IndexTracker, HeuristicFunc, TransformOutput, CommentDirectives, CatalogExpr, RuntimeConf } from 'wuchale';
6
- import { MixedVisitor } from "wuchale/adapter-utils";
5
+ import type { IndexTracker, HeuristicFunc, TransformOutput, CatalogExpr, RuntimeConf } from 'wuchale';
6
+ import { MixedVisitor, type CommentDirectives } from "wuchale/adapter-utils";
7
7
  type MixedNodesTypes = AST.Text | AST.Tag | AST.ElementLike | AST.Block | AST.Comment;
8
8
  export declare class SvelteTransformer extends Transformer {
9
9
  currentElement?: string;
@@ -2,7 +2,7 @@ import MagicString from "magic-string";
2
2
  import { parse } from "svelte/compiler";
3
3
  import { Message } from 'wuchale';
4
4
  import { Transformer, parseScript } from 'wuchale/adapter-vanilla';
5
- import { MixedVisitor, nonWhitespaceText, varNames } from "wuchale/adapter-utils";
5
+ import { MixedVisitor, nonWhitespaceText, processCommentDirectives, varNames } from "wuchale/adapter-utils";
6
6
  const nodesWithChildren = ['RegularElement', 'Component'];
7
7
  const rtComponent = 'W_tx_';
8
8
  const snipPrefix = '_w_snippet_';
@@ -77,6 +77,7 @@ export class SvelteTransformer extends Transformer {
77
77
  inCompoundText: this.inCompoundText,
78
78
  scope: 'markup',
79
79
  element: this.currentElement,
80
+ useComponent: this.currentElement !== 'title'
80
81
  });
81
82
  visitRegularElement = (node) => {
82
83
  const currentElement = this.currentElement;
@@ -192,7 +193,7 @@ export class SvelteTransformer extends Transformer {
192
193
  ...this.visitSv(node.fragment),
193
194
  ];
194
195
  visitSvelteHead = (node) => this.visitSv(node.fragment);
195
- visitTitleElement = (node) => this.visitSv(node.fragment);
196
+ visitTitleElement = (node) => this.visitRegularElement(node);
196
197
  visitSvelteWindow = (node) => node.attributes.map(this.visitSv).flat();
197
198
  visitRoot = (node) => {
198
199
  const msgs = [];
@@ -206,7 +207,7 @@ export class SvelteTransformer extends Transformer {
206
207
  msgs.push(...this.visitProgram(node.module.content));
207
208
  this.mstr.appendRight(
208
209
  // @ts-expect-error
209
- this.getRealBodyStart(node.module.content.body), this.initRuntime(this.filename, null, null, {}));
210
+ this.getRealBodyStart(node.module.content.body) ?? node.module.content.start, this.initRuntime(this.filename, null, null, {}));
210
211
  this.additionalState = {}; // reset
211
212
  this.currentRtVar = prevRtVar; // reset
212
213
  }
@@ -219,12 +220,12 @@ export class SvelteTransformer extends Transformer {
219
220
  };
220
221
  visitSv = (node) => {
221
222
  if (node.type === 'Comment') {
222
- const directives = this.processCommentDirectives(node.data.trim());
223
+ this.commentDirectives = processCommentDirectives(node.data.trim(), this.commentDirectives);
223
224
  if (this.lastVisitIsComment) {
224
- this.commentDirectivesStack[this.commentDirectivesStack.length - 1] = directives;
225
+ this.commentDirectivesStack[this.commentDirectivesStack.length - 1] = this.commentDirectives;
225
226
  }
226
227
  else {
227
- this.commentDirectivesStack.push(directives);
228
+ this.commentDirectivesStack.push(this.commentDirectives);
228
229
  }
229
230
  this.lastVisitIsComment = true;
230
231
  return [];
@@ -238,6 +239,9 @@ export class SvelteTransformer extends Transformer {
238
239
  this.commentDirectives = this.commentDirectivesStack.pop();
239
240
  this.lastVisitIsComment = false;
240
241
  }
242
+ if (this.commentDirectives.ignoreFile) {
243
+ return [];
244
+ }
241
245
  if (this.commentDirectives.forceInclude !== false) {
242
246
  msgs = this.visit(node);
243
247
  }
@@ -260,27 +264,28 @@ export class SvelteTransformer extends Transformer {
260
264
  const msgs = this.visitSv(ast);
261
265
  const initRuntime = this.initRuntime(this.filename, null, null, {});
262
266
  if (ast.type === 'Program') {
263
- const bodyStart = this.getRealBodyStart(ast.body);
267
+ const bodyStart = this.getRealBodyStart(ast.body) ?? 0;
264
268
  this.mstr.appendRight(bodyStart, initRuntime);
265
269
  return this.finalize(msgs, bodyStart);
266
270
  }
267
271
  let headerIndex = 0;
268
272
  if (ast.module) {
269
273
  // @ts-ignore
270
- headerIndex = this.getRealBodyStart(ast.module.content.body);
274
+ headerIndex = this.getRealBodyStart(ast.module.content.body) ?? ast.module.content.start;
271
275
  }
272
276
  if (ast.instance) {
277
+ // @ts-expect-error
278
+ const instanceBodyStart = this.getRealBodyStart(ast.instance.content.body) ?? ast.instance.content.start;
273
279
  if (!ast.module) {
274
- // @ts-expect-error
275
- headerIndex = this.getRealBodyStart(ast.instance.content.body);
280
+ headerIndex = instanceBodyStart;
276
281
  }
277
- // @ts-expect-error
278
- this.mstr.appendRight(this.getRealBodyStart(ast.instance.content.body), initRuntime);
282
+ this.mstr.appendRight(instanceBodyStart, initRuntime);
279
283
  }
280
284
  else {
281
- this.mstr.prepend('<script>');
285
+ const instanceStart = ast.module?.end ?? 0;
286
+ this.mstr.prependLeft(instanceStart, '\n<script>');
282
287
  // account index for hmr data here
283
- this.mstr.prependRight(0, `${initRuntime}\n</script>\n`);
288
+ this.mstr.prependRight(instanceStart, `${initRuntime}\n</script>\n`);
284
289
  // now hmr data can be prependRight(0, ...)
285
290
  }
286
291
  const headerAdd = `\nimport ${rtComponent} from "@wuchale/svelte/runtime.svelte"`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wuchale/svelte",
3
- "version": "0.14.3",
3
+ "version": "0.15.1",
4
4
  "description": "Protobuf-like i18n from plain code: Svelte adapter",
5
5
  "scripts": {
6
6
  "dev": "tsc --watch",
@@ -52,7 +52,7 @@
52
52
  "license": "MIT",
53
53
  "dependencies": {
54
54
  "svelte": "^5.37.0",
55
- "wuchale": "^0.15.8"
55
+ "wuchale": "^0.16.4"
56
56
  },
57
57
  "devDependencies": {
58
58
  "acorn": "^8.15.0",