typescript-svelte-plugin 0.3.11 → 0.3.12
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/src/svelte-snapshots.js +67 -50
- package/package.json +1 -1
|
@@ -228,66 +228,83 @@ class SvelteSnapshotManager {
|
|
|
228
228
|
return snapshot;
|
|
229
229
|
}
|
|
230
230
|
patchProjectServiceReadFile() {
|
|
231
|
-
|
|
232
|
-
this.projectService.host
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
// If someone wants to get back the behavior they can add an ambient module definition
|
|
241
|
-
// on their own.
|
|
242
|
-
const normalizedPath = path.replace(/\\/g, '/');
|
|
243
|
-
if (normalizedPath.endsWith('node_modules/svelte/types/runtime/ambient.d.ts')) {
|
|
244
|
-
return '';
|
|
245
|
-
}
|
|
246
|
-
else if (normalizedPath.endsWith('svelte2tsx/svelte-shims.d.ts')) {
|
|
247
|
-
let originalText = readFile(path) || '';
|
|
248
|
-
if (!originalText.includes('// -- start svelte-ls-remove --')) {
|
|
249
|
-
return originalText; // uses an older version of svelte2tsx
|
|
231
|
+
// @ts-ignore The projectService is shared across some instances, make sure we patch readFile only once
|
|
232
|
+
if (!this.projectService.host[onReadSvelteFile]) {
|
|
233
|
+
this.logger.log('patching projectService host readFile');
|
|
234
|
+
// @ts-ignore
|
|
235
|
+
this.projectService.host[onReadSvelteFile] = [];
|
|
236
|
+
const readFile = this.projectService.host.readFile;
|
|
237
|
+
this.projectService.host.readFile = (path, encoding) => {
|
|
238
|
+
if (!this.configManager.getConfig().enable) {
|
|
239
|
+
return readFile(path, encoding);
|
|
250
240
|
}
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
241
|
+
// The following (very hacky) first two checks make sure that the ambient module definitions
|
|
242
|
+
// that tell TS "every import ending with .svelte is a valid module" are removed.
|
|
243
|
+
// They exist in svelte2tsx and svelte to make sure that people don't
|
|
244
|
+
// get errors in their TS files when importing Svelte files and not using our TS plugin.
|
|
245
|
+
// If someone wants to get back the behavior they can add an ambient module definition
|
|
246
|
+
// on their own.
|
|
247
|
+
const normalizedPath = path.replace(/\\/g, '/');
|
|
248
|
+
if (normalizedPath.endsWith('node_modules/svelte/types/runtime/ambient.d.ts')) {
|
|
249
|
+
return '';
|
|
250
|
+
}
|
|
251
|
+
else if (normalizedPath.endsWith('svelte2tsx/svelte-shims.d.ts')) {
|
|
252
|
+
let originalText = readFile(path) || '';
|
|
253
|
+
if (!originalText.includes('// -- start svelte-ls-remove --')) {
|
|
254
|
+
return originalText; // uses an older version of svelte2tsx or is already patched
|
|
255
|
+
}
|
|
256
|
+
originalText =
|
|
257
|
+
originalText.substring(0, originalText.indexOf('// -- start svelte-ls-remove --')) +
|
|
258
|
+
originalText.substring(originalText.indexOf('// -- end svelte-ls-remove --'));
|
|
259
|
+
return originalText;
|
|
260
|
+
}
|
|
261
|
+
else if ((0, utils_1.isSvelteFilePath)(path)) {
|
|
262
|
+
this.logger.debug('Read Svelte file:', path);
|
|
263
|
+
const svelteCode = readFile(path) || '';
|
|
260
264
|
const isTsFile = true; // TODO check file contents? TS might be okay with importing ts into js.
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
265
|
+
let code;
|
|
266
|
+
let mapper;
|
|
267
|
+
try {
|
|
268
|
+
const result = (0, svelte2tsx_1.svelte2tsx)(svelteCode, {
|
|
269
|
+
filename: path.split('/').pop(),
|
|
270
|
+
isTsFile,
|
|
271
|
+
mode: 'ts',
|
|
272
|
+
typingsNamespace: this.svelteOptions.namespace
|
|
273
|
+
});
|
|
274
|
+
code = result.code;
|
|
275
|
+
mapper = new source_mapper_1.SourceMapper(result.map.mappings);
|
|
276
|
+
this.logger.log('Successfully read Svelte file contents of', path);
|
|
271
277
|
}
|
|
272
|
-
|
|
273
|
-
this.
|
|
278
|
+
catch (e) {
|
|
279
|
+
this.logger.log('Error loading Svelte file:', path, ' Using fallback.');
|
|
280
|
+
this.logger.debug('Error:', e);
|
|
281
|
+
// Return something either way, else "X is not a module" errors will appear
|
|
282
|
+
// in the TS files that use this file.
|
|
283
|
+
code = 'export default class extends Svelte2TsxComponent<any,any,any> {}';
|
|
284
|
+
mapper = new source_mapper_1.SourceMapper('');
|
|
274
285
|
}
|
|
275
|
-
|
|
276
|
-
|
|
286
|
+
// @ts-ignore
|
|
287
|
+
this.projectService.host[onReadSvelteFile].forEach((listener) => listener(path, code, isTsFile, mapper));
|
|
288
|
+
return code;
|
|
277
289
|
}
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
this.logger.debug('Error:', e);
|
|
281
|
-
// Return something either way, else "X is not a module" errors will appear
|
|
282
|
-
// in the TS files that use this file.
|
|
283
|
-
return 'export default class extends Svelte2TsxComponent<any,any,any> {}';
|
|
290
|
+
else {
|
|
291
|
+
return readFile(path, encoding);
|
|
284
292
|
}
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
// @ts-ignore
|
|
296
|
+
this.projectService.host[onReadSvelteFile].push((path, svelteCode, isTsFile, mapper) => {
|
|
297
|
+
const canonicalFilePath = this.projectService.toCanonicalFileName(path);
|
|
298
|
+
const existingSnapshot = this.snapshots.get(canonicalFilePath);
|
|
299
|
+
if (existingSnapshot) {
|
|
300
|
+
existingSnapshot.update(svelteCode, mapper);
|
|
285
301
|
}
|
|
286
302
|
else {
|
|
287
|
-
|
|
303
|
+
this.snapshots.set(canonicalFilePath, new SvelteSnapshot(this.typescript, path, svelteCode, mapper, this.logger, isTsFile));
|
|
288
304
|
}
|
|
289
|
-
};
|
|
305
|
+
});
|
|
290
306
|
}
|
|
291
307
|
}
|
|
292
308
|
exports.SvelteSnapshotManager = SvelteSnapshotManager;
|
|
309
|
+
const onReadSvelteFile = Symbol('sveltePluginPatchSymbol');
|
|
293
310
|
//# sourceMappingURL=svelte-snapshots.js.map
|