@sveltejs/vite-plugin-svelte 1.0.0-next.48 → 1.0.0-next.49

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.cjs CHANGED
@@ -139,18 +139,42 @@ var log = {
139
139
  error: createLogger("error"),
140
140
  setLevel
141
141
  };
142
- function logCompilerWarnings(warnings, options) {
142
+ function logCompilerWarnings(svelteRequest, warnings, options) {
143
+ var _a, _b, _c;
143
144
  const { emitCss, onwarn, isBuild } = options;
144
- const warn = isBuild ? warnBuild : warnDev;
145
- const notIgnoredWarnings = warnings == null ? void 0 : warnings.filter((w) => !ignoreCompilerWarning(w, isBuild, emitCss));
146
- const extraWarnings = buildExtraWarnings(warnings, isBuild);
147
- [...notIgnoredWarnings, ...extraWarnings].forEach((warning) => {
145
+ const sendViaWS = !isBuild && ((_a = options.experimental) == null ? void 0 : _a.sendWarningsToBrowser);
146
+ let warn = isBuild ? warnBuild : warnDev;
147
+ const handledByDefaultWarn = [];
148
+ const notIgnored = warnings == null ? void 0 : warnings.filter((w) => !ignoreCompilerWarning(w, isBuild, emitCss));
149
+ const extra = buildExtraWarnings(warnings, isBuild);
150
+ const allWarnings = [...notIgnored, ...extra];
151
+ if (sendViaWS) {
152
+ const _warn = warn;
153
+ warn = (w) => {
154
+ handledByDefaultWarn.push(w);
155
+ _warn(w);
156
+ };
157
+ }
158
+ allWarnings.forEach((warning) => {
148
159
  if (onwarn) {
149
160
  onwarn(warning, warn);
150
161
  } else {
151
162
  warn(warning);
152
163
  }
153
164
  });
165
+ if (sendViaWS) {
166
+ const message = {
167
+ id: svelteRequest.id,
168
+ filename: svelteRequest.filename,
169
+ normalizedFilename: svelteRequest.normalizedFilename,
170
+ timestamp: svelteRequest.timestamp,
171
+ warnings: handledByDefaultWarn,
172
+ allWarnings,
173
+ rawWarnings: warnings
174
+ };
175
+ log.debug(`sending svelte:warnings message for ${svelteRequest.normalizedFilename}`);
176
+ (_c = (_b = options.server) == null ? void 0 : _b.ws) == null ? void 0 : _c.send("svelte:warnings", message);
177
+ }
154
178
  }
155
179
  function ignoreCompilerWarning(warning, isBuild, emitCss) {
156
180
  return !emitCss && warning.code === "css-unused-selector" || !isBuild && isNoScopableElementWarning(warning);
@@ -197,31 +221,37 @@ function buildExtendedLogMessage(w) {
197
221
 
198
222
  // src/handle-hot-update.ts
199
223
  async function handleHotUpdate(compileSvelte2, ctx, svelteRequest, cache, options) {
200
- const { read, server } = ctx;
201
- const cachedJS = cache.getJS(svelteRequest);
202
- if (!cachedJS) {
203
- log.debug(`handleHotUpdate first call ${svelteRequest.id}`);
224
+ if (!cache.has(svelteRequest)) {
225
+ log.debug(`handleHotUpdate called before initial transform for ${svelteRequest.id}`);
204
226
  return;
205
227
  }
228
+ const { read, server } = ctx;
229
+ const cachedJS = cache.getJS(svelteRequest);
206
230
  const cachedCss = cache.getCSS(svelteRequest);
207
231
  const content = await read();
208
- const compileData = await compileSvelte2(svelteRequest, content, options);
209
- cache.update(compileData);
232
+ let compileData;
233
+ try {
234
+ compileData = await compileSvelte2(svelteRequest, content, options);
235
+ cache.update(compileData);
236
+ } catch (e) {
237
+ cache.setError(svelteRequest, e);
238
+ throw e;
239
+ }
210
240
  const affectedModules = /* @__PURE__ */ new Set();
211
241
  const cssModule = server.moduleGraph.getModuleById(svelteRequest.cssId);
212
242
  const mainModule = server.moduleGraph.getModuleById(svelteRequest.id);
213
243
  const cssUpdated = cssModule && cssChanged(cachedCss, compileData.compiled.css);
214
244
  if (cssUpdated) {
215
- log.debug("handleHotUpdate css changed");
245
+ log.debug(`handleHotUpdate css changed for ${svelteRequest.cssId}`);
216
246
  affectedModules.add(cssModule);
217
247
  }
218
248
  const jsUpdated = mainModule && jsChanged(cachedJS, compileData.compiled.js, svelteRequest.filename);
219
249
  if (jsUpdated) {
220
- log.debug("handleHotUpdate js changed");
250
+ log.debug(`handleHotUpdate js changed for ${svelteRequest.id}`);
221
251
  affectedModules.add(mainModule);
222
252
  }
223
253
  if (!jsUpdated) {
224
- logCompilerWarnings(compileData.compiled.warnings, options);
254
+ logCompilerWarnings(svelteRequest, compileData.compiled.warnings, options);
225
255
  }
226
256
  const result = [...affectedModules].filter(Boolean);
227
257
  const ssrModulesToInvalidate = result.filter((m) => !!m.ssrTransformResult);
@@ -1327,12 +1357,22 @@ var VitePluginSvelteCache = class {
1327
1357
  this._dependencies = /* @__PURE__ */ new Map();
1328
1358
  this._dependants = /* @__PURE__ */ new Map();
1329
1359
  this._resolvedSvelteFields = /* @__PURE__ */ new Map();
1360
+ this._errors = /* @__PURE__ */ new Map();
1330
1361
  }
1331
1362
  update(compileData) {
1363
+ this._errors.delete(compileData.normalizedFilename);
1332
1364
  this.updateCSS(compileData);
1333
1365
  this.updateJS(compileData);
1334
1366
  this.updateDependencies(compileData);
1335
1367
  }
1368
+ has(svelteRequest) {
1369
+ const id = svelteRequest.normalizedFilename;
1370
+ return this._errors.has(id) || this._js.has(id) || this._css.has(id);
1371
+ }
1372
+ setError(svelteRequest, error) {
1373
+ this.remove(svelteRequest, true);
1374
+ this._errors.set(svelteRequest.normalizedFilename, error);
1375
+ }
1336
1376
  updateCSS(compileData) {
1337
1377
  this._css.set(compileData.normalizedFilename, compileData.compiled.css);
1338
1378
  }
@@ -1358,25 +1398,30 @@ var VitePluginSvelteCache = class {
1358
1398
  this._dependants.get(d).delete(compileData.filename);
1359
1399
  });
1360
1400
  }
1361
- remove(svelteRequest) {
1401
+ remove(svelteRequest, keepDependencies = false) {
1362
1402
  const id = svelteRequest.normalizedFilename;
1363
1403
  let removed = false;
1404
+ if (this._errors.delete(id)) {
1405
+ removed = true;
1406
+ }
1364
1407
  if (this._js.delete(id)) {
1365
1408
  removed = true;
1366
1409
  }
1367
1410
  if (this._css.delete(id)) {
1368
1411
  removed = true;
1369
1412
  }
1370
- const dependencies = this._dependencies.get(id);
1371
- if (dependencies) {
1372
- removed = true;
1373
- dependencies.forEach((d) => {
1374
- const dependants = this._dependants.get(d);
1375
- if (dependants && dependants.has(svelteRequest.filename)) {
1376
- dependants.delete(svelteRequest.filename);
1377
- }
1378
- });
1379
- this._dependencies.delete(id);
1413
+ if (!keepDependencies) {
1414
+ const dependencies = this._dependencies.get(id);
1415
+ if (dependencies) {
1416
+ removed = true;
1417
+ dependencies.forEach((d) => {
1418
+ const dependants = this._dependants.get(d);
1419
+ if (dependants && dependants.has(svelteRequest.filename)) {
1420
+ dependants.delete(svelteRequest.filename);
1421
+ }
1422
+ });
1423
+ this._dependencies.delete(id);
1424
+ }
1380
1425
  }
1381
1426
  return removed;
1382
1427
  }
@@ -1388,6 +1433,9 @@ var VitePluginSvelteCache = class {
1388
1433
  return this._js.get(svelteRequest.normalizedFilename);
1389
1434
  }
1390
1435
  }
1436
+ getError(svelteRequest) {
1437
+ return this._errors.get(svelteRequest.normalizedFilename);
1438
+ }
1391
1439
  getDependants(path9) {
1392
1440
  const dependants = this._dependants.get(path9);
1393
1441
  return dependants ? [...dependants] : [];
@@ -1761,9 +1809,10 @@ function svelte(inlineOptions) {
1761
1809
  try {
1762
1810
  compileData = await compileSvelte2(svelteRequest, code, options);
1763
1811
  } catch (e) {
1812
+ cache.setError(svelteRequest, e);
1764
1813
  throw toRollupError(e, options);
1765
1814
  }
1766
- logCompilerWarnings(compileData.compiled.warnings, options);
1815
+ logCompilerWarnings(svelteRequest, compileData.compiled.warnings, options);
1767
1816
  cache.update(compileData);
1768
1817
  if (((_a = compileData.dependencies) == null ? void 0 : _a.length) && options.server) {
1769
1818
  compileData.dependencies.forEach((d) => {
@@ -1785,7 +1834,11 @@ function svelte(inlineOptions) {
1785
1834
  }
1786
1835
  const svelteRequest = requestParser(ctx.file, false, ctx.timestamp);
1787
1836
  if (svelteRequest) {
1788
- return handleHotUpdate(compileSvelte2, ctx, svelteRequest, cache, options);
1837
+ try {
1838
+ return handleHotUpdate(compileSvelte2, ctx, svelteRequest, cache, options);
1839
+ } catch (e) {
1840
+ throw toRollupError(e, options);
1841
+ }
1789
1842
  }
1790
1843
  }
1791
1844
  }