typescript-svelte-plugin 0.3.10 → 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.
|
@@ -7,7 +7,8 @@ const utils_1 = require("./utils");
|
|
|
7
7
|
* Caches resolved modules.
|
|
8
8
|
*/
|
|
9
9
|
class ModuleResolutionCache {
|
|
10
|
-
constructor() {
|
|
10
|
+
constructor(projectService) {
|
|
11
|
+
this.projectService = projectService;
|
|
11
12
|
this.cache = new Map();
|
|
12
13
|
}
|
|
13
14
|
/**
|
|
@@ -30,8 +31,9 @@ class ModuleResolutionCache {
|
|
|
30
31
|
* @param resolvedModuleName full path of the module
|
|
31
32
|
*/
|
|
32
33
|
delete(resolvedModuleName) {
|
|
34
|
+
resolvedModuleName = this.projectService.toCanonicalFileName(resolvedModuleName);
|
|
33
35
|
this.cache.forEach((val, key) => {
|
|
34
|
-
if (val.resolvedFileName === resolvedModuleName) {
|
|
36
|
+
if (this.projectService.toCanonicalFileName(val.resolvedFileName) === resolvedModuleName) {
|
|
35
37
|
this.cache.delete(key);
|
|
36
38
|
}
|
|
37
39
|
});
|
|
@@ -40,7 +42,9 @@ class ModuleResolutionCache {
|
|
|
40
42
|
this.cache.clear();
|
|
41
43
|
}
|
|
42
44
|
getKey(moduleName, containingFile) {
|
|
43
|
-
return
|
|
45
|
+
return (this.projectService.toCanonicalFileName(containingFile) +
|
|
46
|
+
':::' +
|
|
47
|
+
this.projectService.toCanonicalFileName((0, utils_1.ensureRealSvelteFilePath)(moduleName)));
|
|
44
48
|
}
|
|
45
49
|
}
|
|
46
50
|
/**
|
|
@@ -55,7 +59,7 @@ class ModuleResolutionCache {
|
|
|
55
59
|
function patchModuleLoader(logger, snapshotManager, typescript, lsHost, project, configManager) {
|
|
56
60
|
var _a;
|
|
57
61
|
const svelteSys = (0, svelte_sys_1.createSvelteSys)(logger);
|
|
58
|
-
const moduleCache = new ModuleResolutionCache();
|
|
62
|
+
const moduleCache = new ModuleResolutionCache(project.projectService);
|
|
59
63
|
const origResolveModuleNames = (_a = lsHost.resolveModuleNames) === null || _a === void 0 ? void 0 : _a.bind(lsHost);
|
|
60
64
|
lsHost.resolveModuleNames = resolveModuleNames;
|
|
61
65
|
const origRemoveFile = project.removeFile.bind(project);
|
|
@@ -3,6 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.ProjectSvelteFilesManager = void 0;
|
|
4
4
|
const utils_1 = require("./utils");
|
|
5
5
|
class ProjectSvelteFilesManager {
|
|
6
|
+
static getInstance(projectName) {
|
|
7
|
+
return this.instances.get(projectName);
|
|
8
|
+
}
|
|
6
9
|
constructor(typescript, project, serverHost, snapshotManager, parsedCommandLine, configManager) {
|
|
7
10
|
this.typescript = typescript;
|
|
8
11
|
this.project = project;
|
|
@@ -18,9 +21,6 @@ class ProjectSvelteFilesManager {
|
|
|
18
21
|
configManager.onConfigurationChanged(this.onConfigChanged.bind(this));
|
|
19
22
|
ProjectSvelteFilesManager.instances.set(project.getProjectName(), this);
|
|
20
23
|
}
|
|
21
|
-
static getInstance(projectName) {
|
|
22
|
-
return this.instances.get(projectName);
|
|
23
|
-
}
|
|
24
24
|
updateProjectConfig(serviceHost) {
|
|
25
25
|
var _a;
|
|
26
26
|
const parsedCommandLine = (_a = serviceHost.getParsedCommandLine) === null || _a === void 0 ? void 0 : _a.call(serviceHost, (0, utils_1.getConfigPathForProject)(this.project));
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type ts from 'typescript/lib/tsserverlibrary';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
type LineChar = ts.LineAndCharacter;
|
|
3
|
+
type FileMapping = LineMapping[];
|
|
4
|
+
type LineMapping = CharacterMapping[];
|
|
5
|
+
type CharacterMapping = [
|
|
6
6
|
number,
|
|
7
7
|
number,
|
|
8
8
|
number,
|
|
@@ -199,11 +199,12 @@ class SvelteSnapshotManager {
|
|
|
199
199
|
this.patchProjectServiceReadFile();
|
|
200
200
|
}
|
|
201
201
|
get(fileName) {
|
|
202
|
-
return this.snapshots.get(fileName);
|
|
202
|
+
return this.snapshots.get(this.projectService.toCanonicalFileName(fileName));
|
|
203
203
|
}
|
|
204
204
|
create(fileName) {
|
|
205
|
-
|
|
206
|
-
|
|
205
|
+
const canonicalFilePath = this.projectService.toCanonicalFileName(fileName);
|
|
206
|
+
if (this.snapshots.has(canonicalFilePath)) {
|
|
207
|
+
return this.snapshots.get(canonicalFilePath);
|
|
207
208
|
}
|
|
208
209
|
// This will trigger projectService.host.readFile which is patched below
|
|
209
210
|
const scriptInfo = this.projectService.getOrCreateScriptInfoForNormalizedPath(this.typescript.server.toNormalizedPath(fileName), false);
|
|
@@ -217,75 +218,93 @@ class SvelteSnapshotManager {
|
|
|
217
218
|
catch (e) {
|
|
218
219
|
this.logger.log('Loading Snapshot failed', fileName);
|
|
219
220
|
}
|
|
220
|
-
const snapshot = this.snapshots.get(fileName);
|
|
221
|
+
const snapshot = this.snapshots.get(this.projectService.toCanonicalFileName(fileName));
|
|
221
222
|
if (!snapshot) {
|
|
222
223
|
this.logger.log('Svelte snapshot was not found after trying to load script snapshot for', fileName);
|
|
223
224
|
return; // should never get here
|
|
224
225
|
}
|
|
225
226
|
snapshot.setAndPatchScriptInfo(scriptInfo);
|
|
226
|
-
this.snapshots.set(
|
|
227
|
+
this.snapshots.set(canonicalFilePath, snapshot);
|
|
227
228
|
return snapshot;
|
|
228
229
|
}
|
|
229
230
|
patchProjectServiceReadFile() {
|
|
230
|
-
|
|
231
|
-
this.projectService.host
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
// If someone wants to get back the behavior they can add an ambient module definition
|
|
240
|
-
// on their own.
|
|
241
|
-
const normalizedPath = path.replace(/\\/g, '/');
|
|
242
|
-
if (normalizedPath.endsWith('node_modules/svelte/types/runtime/ambient.d.ts')) {
|
|
243
|
-
return '';
|
|
244
|
-
}
|
|
245
|
-
else if (normalizedPath.endsWith('svelte2tsx/svelte-shims.d.ts')) {
|
|
246
|
-
let originalText = readFile(path) || '';
|
|
247
|
-
if (!originalText.includes('// -- start svelte-ls-remove --')) {
|
|
248
|
-
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);
|
|
249
240
|
}
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
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) || '';
|
|
259
264
|
const isTsFile = true; // TODO check file contents? TS might be okay with importing ts into js.
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
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);
|
|
269
277
|
}
|
|
270
|
-
|
|
271
|
-
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('');
|
|
272
285
|
}
|
|
273
|
-
|
|
274
|
-
|
|
286
|
+
// @ts-ignore
|
|
287
|
+
this.projectService.host[onReadSvelteFile].forEach((listener) => listener(path, code, isTsFile, mapper));
|
|
288
|
+
return code;
|
|
275
289
|
}
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
this.logger.debug('Error:', e);
|
|
279
|
-
// Return something either way, else "X is not a module" errors will appear
|
|
280
|
-
// in the TS files that use this file.
|
|
281
|
-
return 'export default class extends Svelte2TsxComponent<any,any,any> {}';
|
|
290
|
+
else {
|
|
291
|
+
return readFile(path, encoding);
|
|
282
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);
|
|
283
301
|
}
|
|
284
302
|
else {
|
|
285
|
-
|
|
303
|
+
this.snapshots.set(canonicalFilePath, new SvelteSnapshot(this.typescript, path, svelteCode, mapper, this.logger, isTsFile));
|
|
286
304
|
}
|
|
287
|
-
};
|
|
305
|
+
});
|
|
288
306
|
}
|
|
289
307
|
}
|
|
290
308
|
exports.SvelteSnapshotManager = SvelteSnapshotManager;
|
|
309
|
+
const onReadSvelteFile = Symbol('sveltePluginPatchSymbol');
|
|
291
310
|
//# sourceMappingURL=svelte-snapshots.js.map
|