@typescript/vfs 1.4.0 → 1.5.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.
@@ -1,62 +1,51 @@
1
1
  function _extends() {
2
- _extends = Object.assign || function (target) {
2
+ _extends = Object.assign ? Object.assign.bind() : function (target) {
3
3
  for (var i = 1; i < arguments.length; i++) {
4
4
  var source = arguments[i];
5
-
6
5
  for (var key in source) {
7
6
  if (Object.prototype.hasOwnProperty.call(source, key)) {
8
7
  target[key] = source[key];
9
8
  }
10
9
  }
11
10
  }
12
-
13
11
  return target;
14
12
  };
15
-
16
13
  return _extends.apply(this, arguments);
17
14
  }
18
15
 
19
16
  var hasLocalStorage = false;
20
-
21
17
  try {
22
18
  hasLocalStorage = typeof localStorage !== "undefined";
23
19
  } catch (error) {}
24
-
25
20
  var hasProcess = typeof process !== "undefined";
26
21
  var shouldDebug = hasLocalStorage && /*#__PURE__*/localStorage.getItem("DEBUG") || hasProcess && process.env.DEBUG;
27
22
  var debugLog = shouldDebug ? console.log : function (_message) {
28
23
  return "";
29
24
  };
30
- /**
31
- * Makes a virtual copy of the TypeScript environment. This is the main API you want to be using with
32
- * @typescript/vfs. A lot of the other exposed functions are used by this function to get set up.
33
- *
34
- * @param sys an object which conforms to the TS Sys (a shim over read/write access to the fs)
35
- * @param rootFiles a list of files which are considered inside the project
36
- * @param ts a copy pf the TypeScript module
37
- * @param compilerOptions the options for this compiler run
38
- * @param customTransformers custom transformers for this compiler run
25
+ /**
26
+ * Makes a virtual copy of the TypeScript environment. This is the main API you want to be using with
27
+ * @typescript/vfs. A lot of the other exposed functions are used by this function to get set up.
28
+ *
29
+ * @param sys an object which conforms to the TS Sys (a shim over read/write access to the fs)
30
+ * @param rootFiles a list of files which are considered inside the project
31
+ * @param ts a copy pf the TypeScript module
32
+ * @param compilerOptions the options for this compiler run
33
+ * @param customTransformers custom transformers for this compiler run
39
34
  */
40
-
41
35
  function createVirtualTypeScriptEnvironment(sys, rootFiles, ts, compilerOptions, customTransformers) {
42
36
  if (compilerOptions === void 0) {
43
37
  compilerOptions = {};
44
38
  }
45
-
46
39
  var mergedCompilerOpts = _extends({}, defaultCompilerOptions(ts), compilerOptions);
47
-
48
40
  var _createVirtualLanguag = createVirtualLanguageServiceHost(sys, rootFiles, mergedCompilerOpts, ts, customTransformers),
49
- languageServiceHost = _createVirtualLanguag.languageServiceHost,
50
- _updateFile = _createVirtualLanguag.updateFile;
51
-
41
+ languageServiceHost = _createVirtualLanguag.languageServiceHost,
42
+ _updateFile = _createVirtualLanguag.updateFile;
52
43
  var languageService = ts.createLanguageService(languageServiceHost);
53
44
  var diagnostics = languageService.getCompilerOptionsDiagnostics();
54
-
55
45
  if (diagnostics.length) {
56
46
  var compilerHost = createVirtualCompilerHost(sys, compilerOptions, ts);
57
47
  throw new Error(ts.formatDiagnostics(diagnostics, compilerHost.compilerHost));
58
48
  }
59
-
60
49
  return {
61
50
  // @ts-ignore
62
51
  name: "vfs",
@@ -64,7 +53,6 @@ function createVirtualTypeScriptEnvironment(sys, rootFiles, ts, compilerOptions,
64
53
  languageService: languageService,
65
54
  getSourceFile: function getSourceFile(fileName) {
66
55
  var _languageService$getP;
67
-
68
56
  return (_languageService$getP = languageService.getProgram()) == null ? void 0 : _languageService$getP.getSourceFile(fileName);
69
57
  },
70
58
  createFile: function createFile(fileName, content) {
@@ -72,49 +60,49 @@ function createVirtualTypeScriptEnvironment(sys, rootFiles, ts, compilerOptions,
72
60
  },
73
61
  updateFile: function updateFile(fileName, content, optPrevTextSpan) {
74
62
  var prevSourceFile = languageService.getProgram().getSourceFile(fileName);
75
-
76
63
  if (!prevSourceFile) {
77
64
  throw new Error("Did not find a source file for " + fileName);
78
65
  }
79
-
80
- var prevFullContents = prevSourceFile.text; // TODO: Validate if the default text span has a fencepost error?
81
-
66
+ var prevFullContents = prevSourceFile.text;
67
+ // TODO: Validate if the default text span has a fencepost error?
82
68
  var prevTextSpan = optPrevTextSpan != null ? optPrevTextSpan : ts.createTextSpan(0, prevFullContents.length);
83
69
  var newText = prevFullContents.slice(0, prevTextSpan.start) + content + prevFullContents.slice(prevTextSpan.start + prevTextSpan.length);
84
70
  var newSourceFile = ts.updateSourceFile(prevSourceFile, newText, {
85
71
  span: prevTextSpan,
86
72
  newLength: content.length
87
73
  });
88
-
89
74
  _updateFile(newSourceFile);
90
75
  }
91
76
  };
92
77
  }
93
- /**
94
- * Grab the list of lib files for a particular target, will return a bit more than necessary (by including
95
- * the dom) but that's OK
96
- *
97
- * @param target The compiler settings target baseline
98
- * @param ts A copy of the TypeScript module
78
+ // TODO: This could be replaced by grabbing: https://github.com/microsoft/TypeScript/blob/main/src/lib/libs.json
79
+ // and then using that to generate the list of files from the server, but it is not included in the npm package
80
+ /**
81
+ * Grab the list of lib files for a particular target, will return a bit more than necessary (by including
82
+ * the dom) but that's OK, we're really working with the constraint that you can't get a list of files
83
+ * when running in a browser.
84
+ *
85
+ * @param target The compiler settings target baseline
86
+ * @param ts A copy of the TypeScript module
99
87
  */
100
-
101
88
  var knownLibFilesForCompilerOptions = function knownLibFilesForCompilerOptions(compilerOptions, ts) {
102
89
  var target = compilerOptions.target || ts.ScriptTarget.ES5;
103
90
  var lib = compilerOptions.lib || [];
104
- var files = ["lib.d.ts", "lib.dom.d.ts", "lib.dom.iterable.d.ts", "lib.webworker.d.ts", "lib.webworker.importscripts.d.ts", "lib.scripthost.d.ts", "lib.es5.d.ts", "lib.es6.d.ts", "lib.es2015.collection.d.ts", "lib.es2015.core.d.ts", "lib.es2015.d.ts", "lib.es2015.generator.d.ts", "lib.es2015.iterable.d.ts", "lib.es2015.promise.d.ts", "lib.es2015.proxy.d.ts", "lib.es2015.reflect.d.ts", "lib.es2015.symbol.d.ts", "lib.es2015.symbol.wellknown.d.ts", "lib.es2016.array.include.d.ts", "lib.es2016.d.ts", "lib.es2016.full.d.ts", "lib.es2017.d.ts", "lib.es2017.full.d.ts", "lib.es2017.intl.d.ts", "lib.es2017.object.d.ts", "lib.es2017.sharedmemory.d.ts", "lib.es2017.string.d.ts", "lib.es2017.typedarrays.d.ts", "lib.es2018.asyncgenerator.d.ts", "lib.es2018.asynciterable.d.ts", "lib.es2018.d.ts", "lib.es2018.full.d.ts", "lib.es2018.intl.d.ts", "lib.es2018.promise.d.ts", "lib.es2018.regexp.d.ts", "lib.es2019.array.d.ts", "lib.es2019.d.ts", "lib.es2019.full.d.ts", "lib.es2019.object.d.ts", "lib.es2019.string.d.ts", "lib.es2019.symbol.d.ts", "lib.es2020.d.ts", "lib.es2020.full.d.ts", "lib.es2020.string.d.ts", "lib.es2020.symbol.wellknown.d.ts", "lib.es2020.bigint.d.ts", "lib.es2020.promise.d.ts", "lib.es2020.sharedmemory.d.ts", "lib.es2020.intl.d.ts", "lib.es2021.d.ts", "lib.es2021.full.d.ts", "lib.es2021.promise.d.ts", "lib.es2021.string.d.ts", "lib.es2021.weakref.d.ts", "lib.esnext.d.ts", "lib.esnext.full.d.ts", "lib.esnext.intl.d.ts", "lib.esnext.promise.d.ts", "lib.esnext.string.d.ts", "lib.esnext.weakref.d.ts"];
91
+ // Note that this will include files which can't be found for particular versions of TS
92
+ // TODO: Replace this with some sort of API call if https://github.com/microsoft/TypeScript/pull/54011
93
+ // or similar is merged.
94
+ var files = ["lib.d.ts", "lib.decorators.d.ts", "lib.decorators.legacy.d.ts", "lib.dom.d.ts", "lib.dom.iterable.d.ts", "lib.webworker.d.ts", "lib.webworker.importscripts.d.ts", "lib.webworker.iterable.d.ts", "lib.scripthost.d.ts", "lib.es5.d.ts", "lib.es6.d.ts", "lib.es2015.collection.d.ts", "lib.es2015.core.d.ts", "lib.es2015.d.ts", "lib.es2015.generator.d.ts", "lib.es2015.iterable.d.ts", "lib.es2015.promise.d.ts", "lib.es2015.proxy.d.ts", "lib.es2015.reflect.d.ts", "lib.es2015.symbol.d.ts", "lib.es2015.symbol.wellknown.d.ts", "lib.es2016.array.include.d.ts", "lib.es2016.d.ts", "lib.es2016.full.d.ts", "lib.es2017.d.ts", "lib.es2017.date.d.ts", "lib.es2017.full.d.ts", "lib.es2017.intl.d.ts", "lib.es2017.object.d.ts", "lib.es2017.sharedmemory.d.ts", "lib.es2017.string.d.ts", "lib.es2017.typedarrays.d.ts", "lib.es2018.asyncgenerator.d.ts", "lib.es2018.asynciterable.d.ts", "lib.es2018.d.ts", "lib.es2018.full.d.ts", "lib.es2018.intl.d.ts", "lib.es2018.promise.d.ts", "lib.es2018.regexp.d.ts", "lib.es2019.array.d.ts", "lib.es2019.d.ts", "lib.es2019.full.d.ts", "lib.es2019.intl.d.ts", "lib.es2019.object.d.ts", "lib.es2019.string.d.ts", "lib.es2019.symbol.d.ts", "lib.es2020.bigint.d.ts", "lib.es2020.d.ts", "lib.es2020.date.d.ts", "lib.es2020.full.d.ts", "lib.es2020.intl.d.ts", "lib.es2020.number.d.ts", "lib.es2020.promise.d.ts", "lib.es2020.sharedmemory.d.ts", "lib.es2020.string.d.ts", "lib.es2020.symbol.wellknown.d.ts", "lib.es2021.d.ts", "lib.es2021.full.d.ts", "lib.es2021.intl.d.ts", "lib.es2021.promise.d.ts", "lib.es2021.string.d.ts", "lib.es2021.weakref.d.ts", "lib.es2022.array.d.ts", "lib.es2022.d.ts", "lib.es2022.error.d.ts", "lib.es2022.full.d.ts", "lib.es2022.intl.d.ts", "lib.es2022.object.d.ts", "lib.es2022.regexp.d.ts", "lib.es2022.sharedmemory.d.ts", "lib.es2022.string.d.ts", "lib.es2023.array.d.ts", "lib.es2023.collection.d.ts", "lib.es2023.d.ts", "lib.es2023.full.d.ts", "lib.esnext.array.d.ts", "lib.esnext.asynciterable.d.ts", "lib.esnext.bigint.d.ts", "lib.esnext.d.ts", "lib.esnext.decorators.d.ts", "lib.esnext.disposable.d.ts", "lib.esnext.full.d.ts", "lib.esnext.intl.d.ts", "lib.esnext.promise.d.ts", "lib.esnext.string.d.ts", "lib.esnext.symbol.d.ts", "lib.esnext.weakref.d.ts"];
105
95
  var targetToCut = ts.ScriptTarget[target];
106
96
  var matches = files.filter(function (f) {
107
97
  return f.startsWith("lib." + targetToCut.toLowerCase());
108
98
  });
109
99
  var targetCutIndex = files.indexOf(matches.pop());
110
-
111
100
  var getMax = function getMax(array) {
112
101
  return array && array.length ? array.reduce(function (max, current) {
113
102
  return current > max ? current : max;
114
103
  }) : undefined;
115
- }; // Find the index for everything in
116
-
117
-
104
+ };
105
+ // Find the index for everything in
118
106
  var indexesForCutting = lib.map(function (lib) {
119
107
  var matches = files.filter(function (f) {
120
108
  return f.startsWith("lib." + lib.toLowerCase());
@@ -127,44 +115,42 @@ var knownLibFilesForCompilerOptions = function knownLibFilesForCompilerOptions(c
127
115
  var finalCutIndex = Math.max(targetCutIndex, libCutIndex);
128
116
  return files.slice(0, finalCutIndex + 1);
129
117
  };
130
- /**
131
- * Sets up a Map with lib contents by grabbing the necessary files from
132
- * the local copy of typescript via the file system.
118
+ /**
119
+ * Sets up a Map with lib contents by grabbing the necessary files from
120
+ * the local copy of typescript via the file system.
121
+ *
122
+ * The first two args are un-used, but kept around so as to not cause a
123
+ * semver major bump for no gain to module users.
133
124
  */
134
-
135
- var createDefaultMapFromNodeModules = function createDefaultMapFromNodeModules(compilerOptions, ts, tsLibDirectory) {
136
- var tsModule = ts || require("typescript");
137
-
125
+ var createDefaultMapFromNodeModules = function createDefaultMapFromNodeModules(_compilerOptions, _ts, tsLibDirectory) {
138
126
  var path = requirePath();
139
127
  var fs = requireFS();
140
-
141
128
  var getLib = function getLib(name) {
142
129
  var lib = tsLibDirectory || path.dirname(require.resolve("typescript"));
143
130
  return fs.readFileSync(path.join(lib, name), "utf8");
144
131
  };
145
-
146
- var libs = knownLibFilesForCompilerOptions(compilerOptions, tsModule);
132
+ var libFiles = fs.readdirSync(tsLibDirectory || path.dirname(require.resolve("typescript")));
133
+ var knownLibFiles = libFiles.filter(function (f) {
134
+ return f.startsWith("lib.") && f.endsWith(".d.ts");
135
+ });
147
136
  var fsMap = new Map();
148
- libs.forEach(function (lib) {
137
+ knownLibFiles.forEach(function (lib) {
149
138
  fsMap.set("/" + lib, getLib(lib));
150
139
  });
151
140
  return fsMap;
152
141
  };
153
- /**
154
- * Adds recursively files from the FS into the map based on the folder
142
+ /**
143
+ * Adds recursively files from the FS into the map based on the folder
155
144
  */
156
-
157
145
  var addAllFilesFromFolder = function addAllFilesFromFolder(map, workingDir) {
158
146
  var path = requirePath();
159
147
  var fs = requireFS();
160
-
161
148
  var walk = function walk(dir) {
162
149
  var results = [];
163
150
  var list = fs.readdirSync(dir);
164
151
  list.forEach(function (file) {
165
152
  file = path.join(dir, file);
166
153
  var stat = fs.statSync(file);
167
-
168
154
  if (stat && stat.isDirectory()) {
169
155
  /* Recurse into a subdirectory */
170
156
  results = results.concat(walk(file));
@@ -175,51 +161,44 @@ var addAllFilesFromFolder = function addAllFilesFromFolder(map, workingDir) {
175
161
  });
176
162
  return results;
177
163
  };
178
-
179
164
  var allFiles = walk(workingDir);
180
165
  allFiles.forEach(function (lib) {
181
166
  var fsPath = "/node_modules/@types" + lib.replace(workingDir, "");
182
167
  var content = fs.readFileSync(lib, "utf8");
183
168
  var validExtensions = [".ts", ".tsx"];
184
-
185
169
  if (validExtensions.includes(path.extname(fsPath))) {
186
170
  map.set(fsPath, content);
187
171
  }
188
172
  });
189
173
  };
190
174
  /** Adds all files from node_modules/@types into the FS Map */
191
-
192
175
  var addFilesForTypesIntoFolder = function addFilesForTypesIntoFolder(map) {
193
176
  return addAllFilesFromFolder(map, "node_modules/@types");
194
177
  };
195
- /**
196
- * Create a virtual FS Map with the lib files from a particular TypeScript
197
- * version based on the target, Always includes dom ATM.
198
- *
199
- * @param options The compiler target, which dictates the libs to set up
200
- * @param version the versions of TypeScript which are supported
201
- * @param cache should the values be stored in local storage
202
- * @param ts a copy of the typescript import
203
- * @param lzstring an optional copy of the lz-string import
204
- * @param fetcher an optional replacement for the global fetch function (tests mainly)
205
- * @param storer an optional replacement for the localStorage global (tests mainly)
178
+ /**
179
+ * Create a virtual FS Map with the lib files from a particular TypeScript
180
+ * version based on the target, Always includes dom ATM.
181
+ *
182
+ * @param options The compiler target, which dictates the libs to set up
183
+ * @param version the versions of TypeScript which are supported
184
+ * @param cache should the values be stored in local storage
185
+ * @param ts a copy of the typescript import
186
+ * @param lzstring an optional copy of the lz-string import
187
+ * @param fetcher an optional replacement for the global fetch function (tests mainly)
188
+ * @param storer an optional replacement for the localStorage global (tests mainly)
206
189
  */
207
-
208
190
  var createDefaultMapFromCDN = function createDefaultMapFromCDN(options, version, cache, ts, lzstring, fetcher, storer) {
209
191
  var fetchlike = fetcher || fetch;
210
192
  var fsMap = new Map();
211
193
  var files = knownLibFilesForCompilerOptions(options, ts);
212
194
  var prefix = "https://typescript.azureedge.net/cdn/" + version + "/typescript/lib/";
213
-
214
195
  function zip(str) {
215
196
  return lzstring ? lzstring.compressToUTF16(str) : str;
216
197
  }
217
-
218
198
  function unzip(str) {
219
199
  return lzstring ? lzstring.decompressFromUTF16(str) : str;
220
- } // Map the known libs to a node fetch promise, then return the contents
221
-
222
-
200
+ }
201
+ // Map the known libs to a node fetch promise, then return the contents
223
202
  function uncached() {
224
203
  return Promise.all(files.map(function (lib) {
225
204
  return fetchlike(prefix + lib).then(function (resp) {
@@ -229,13 +208,14 @@ var createDefaultMapFromCDN = function createDefaultMapFromCDN(options, version,
229
208
  contents.forEach(function (text, index) {
230
209
  return fsMap.set("/" + files[index], text);
231
210
  });
232
- });
233
- } // A localstorage and lzzip aware version of the lib files
234
-
235
-
211
+ })
212
+ // Return a NOOP for .d.ts files which aren't in the current build of TypeScript
213
+ ["catch"](function () {});
214
+ }
215
+ // A localstorage and lzzip aware version of the lib files
236
216
  function cached() {
237
217
  var storelike = storer || localStorage;
238
- var keys = Object.keys(localStorage);
218
+ var keys = Object.keys(storelike);
239
219
  keys.forEach(function (key) {
240
220
  // Remove anything which isn't from this version
241
221
  if (key.startsWith("ts-lib-") && !key.startsWith("ts-lib-" + version)) {
@@ -245,7 +225,6 @@ var createDefaultMapFromCDN = function createDefaultMapFromCDN(options, version,
245
225
  return Promise.all(files.map(function (lib) {
246
226
  var cacheKey = "ts-lib-" + version + "-" + lib;
247
227
  var content = storelike.getItem(cacheKey);
248
-
249
228
  if (!content) {
250
229
  // Make the API call and store the text concent in the cache
251
230
  return fetchlike(prefix + lib).then(function (resp) {
@@ -253,34 +232,34 @@ var createDefaultMapFromCDN = function createDefaultMapFromCDN(options, version,
253
232
  }).then(function (t) {
254
233
  storelike.setItem(cacheKey, zip(t));
255
234
  return t;
256
- });
235
+ })
236
+ // Return a NOOP for .d.ts files which aren't in the current build of TypeScript
237
+ ["catch"](function () {});
257
238
  } else {
258
239
  return Promise.resolve(unzip(content));
259
240
  }
260
241
  })).then(function (contents) {
261
242
  contents.forEach(function (text, index) {
262
- var name = "/" + files[index];
263
- fsMap.set(name, text);
243
+ if (text) {
244
+ var name = "/" + files[index];
245
+ fsMap.set(name, text);
246
+ }
264
247
  });
265
248
  });
266
249
  }
267
-
268
250
  var func = cache ? cached : uncached;
269
251
  return func().then(function () {
270
252
  return fsMap;
271
253
  });
272
254
  };
273
-
274
255
  function notImplemented(methodName) {
275
256
  throw new Error("Method '" + methodName + "' is not implemented.");
276
257
  }
277
-
278
258
  function audit(name, fn) {
279
259
  return function () {
280
260
  for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
281
261
  args[_key] = arguments[_key];
282
262
  }
283
-
284
263
  var res = fn.apply(void 0, args);
285
264
  var smallres = typeof res === "string" ? res.slice(0, 80) + "..." : res;
286
265
  debugLog.apply(void 0, ["> " + name].concat(args));
@@ -289,8 +268,6 @@ function audit(name, fn) {
289
268
  };
290
269
  }
291
270
  /** The default compiler options if TypeScript could ever change the compiler options */
292
-
293
-
294
271
  var defaultCompilerOptions = function defaultCompilerOptions(ts) {
295
272
  return _extends({}, ts.getDefaultCompilerOptions(), {
296
273
  jsx: ts.JsxEmit.React,
@@ -302,18 +279,15 @@ var defaultCompilerOptions = function defaultCompilerOptions(ts) {
302
279
  skipDefaultLibCheck: true,
303
280
  moduleResolution: ts.ModuleResolutionKind.NodeJs
304
281
  });
305
- }; // "/DOM.d.ts" => "/lib.dom.d.ts"
306
-
307
-
282
+ };
283
+ // "/DOM.d.ts" => "/lib.dom.d.ts"
308
284
  var libize = function libize(path) {
309
285
  return path.replace("/", "/lib.").toLowerCase();
310
286
  };
311
- /**
312
- * Creates an in-memory System object which can be used in a TypeScript program, this
313
- * is what provides read/write aspects of the virtual fs
287
+ /**
288
+ * Creates an in-memory System object which can be used in a TypeScript program, this
289
+ * is what provides read/write aspects of the virtual fs
314
290
  */
315
-
316
-
317
291
  function createSystem(files) {
318
292
  return {
319
293
  args: [],
@@ -360,18 +334,17 @@ function createSystem(files) {
360
334
  }
361
335
  };
362
336
  }
363
- /**
364
- * Creates a file-system backed System object which can be used in a TypeScript program, you provide
365
- * a set of virtual files which are prioritised over the FS versions, then a path to the root of your
366
- * project (basically the folder your node_modules lives)
337
+ /**
338
+ * Creates a file-system backed System object which can be used in a TypeScript program, you provide
339
+ * a set of virtual files which are prioritised over the FS versions, then a path to the root of your
340
+ * project (basically the folder your node_modules lives)
367
341
  */
368
-
369
342
  function createFSBackedSystem(files, _projectRoot, ts, tsLibDirectory) {
370
343
  // We need to make an isolated folder for the tsconfig, but also need to be able to resolve the
371
344
  // existing node_modules structures going back through the history
372
345
  var root = _projectRoot + "/vfs";
373
- var path = requirePath(); // The default System in TypeScript
374
-
346
+ var path = requirePath();
347
+ // The default System in TypeScript
375
348
  var nodeSys = ts.sys;
376
349
  var tsLib = tsLibDirectory != null ? tsLibDirectory : path.dirname(require.resolve("typescript"));
377
350
  return {
@@ -390,15 +363,13 @@ function createFSBackedSystem(files, _projectRoot, ts, tsLibDirectory) {
390
363
  }),
391
364
  exit: nodeSys.exit,
392
365
  fileExists: audit("fileExists", function (fileName) {
393
- if (files.has(fileName)) return true; // Don't let other tsconfigs end up touching the vfs
394
-
366
+ if (files.has(fileName)) return true;
367
+ // Don't let other tsconfigs end up touching the vfs
395
368
  if (fileName.includes("tsconfig.json") || fileName.includes("tsconfig.json")) return false;
396
-
397
369
  if (fileName.startsWith("/lib")) {
398
370
  var tsLibName = tsLib + "/" + fileName.replace("/", "");
399
371
  return nodeSys.fileExists(tsLibName);
400
372
  }
401
-
402
373
  return nodeSys.fileExists(fileName);
403
374
  }),
404
375
  getCurrentDirectory: function getCurrentDirectory() {
@@ -417,19 +388,15 @@ function createFSBackedSystem(files, _projectRoot, ts, tsLibDirectory) {
417
388
  }),
418
389
  readFile: audit("readFile", function (fileName) {
419
390
  if (files.has(fileName)) return files.get(fileName);
420
-
421
391
  if (fileName.startsWith("/lib")) {
422
392
  var tsLibName = tsLib + "/" + fileName.replace("/", "");
423
393
  var result = nodeSys.readFile(tsLibName);
424
-
425
394
  if (!result) {
426
395
  var libs = nodeSys.readDirectory(tsLib);
427
396
  throw new Error("TSVFS: A request was made for " + tsLibName + " but there wasn't a file found in the file map. You likely have a mismatch in the compiler options for the CDN download vs the compiler program. Existing Libs: " + libs + ".");
428
397
  }
429
-
430
398
  return result;
431
399
  }
432
-
433
400
  return nodeSys.readFile(fileName);
434
401
  }),
435
402
  resolvePath: function resolvePath(path) {
@@ -446,20 +413,17 @@ function createFSBackedSystem(files, _projectRoot, ts, tsLibDirectory) {
446
413
  }
447
414
  };
448
415
  }
449
- /**
450
- * Creates an in-memory CompilerHost -which is essentially an extra wrapper to System
451
- * which works with TypeScript objects - returns both a compiler host, and a way to add new SourceFile
452
- * instances to the in-memory file system.
416
+ /**
417
+ * Creates an in-memory CompilerHost -which is essentially an extra wrapper to System
418
+ * which works with TypeScript objects - returns both a compiler host, and a way to add new SourceFile
419
+ * instances to the in-memory file system.
453
420
  */
454
-
455
421
  function createVirtualCompilerHost(sys, compilerOptions, ts) {
456
422
  var sourceFiles = new Map();
457
-
458
423
  var save = function save(sourceFile) {
459
424
  sourceFiles.set(sourceFile.fileName, sourceFile);
460
425
  return sourceFile;
461
426
  };
462
-
463
427
  var vHost = {
464
428
  compilerHost: _extends({}, sys, {
465
429
  getCanonicalFileName: function getCanonicalFileName(fileName) {
@@ -468,6 +432,7 @@ function createVirtualCompilerHost(sys, compilerOptions, ts) {
468
432
  getDefaultLibFileName: function getDefaultLibFileName() {
469
433
  return "/" + ts.getDefaultLibFileName(compilerOptions);
470
434
  },
435
+ // '/lib.d.ts',
471
436
  // getDefaultLibLocation: () => '/',
472
437
  getDirectories: function getDirectories() {
473
438
  return [];
@@ -491,20 +456,16 @@ function createVirtualCompilerHost(sys, compilerOptions, ts) {
491
456
  };
492
457
  return vHost;
493
458
  }
494
- /**
495
- * Creates an object which can host a language service against the virtual file-system
459
+ /**
460
+ * Creates an object which can host a language service against the virtual file-system
496
461
  */
497
-
498
462
  function createVirtualLanguageServiceHost(sys, rootFiles, compilerOptions, ts, customTransformers) {
499
463
  var fileNames = [].concat(rootFiles);
500
-
501
464
  var _createVirtualCompile = createVirtualCompilerHost(sys, compilerOptions, ts),
502
- compilerHost = _createVirtualCompile.compilerHost,
503
- _updateFile2 = _createVirtualCompile.updateFile;
504
-
465
+ compilerHost = _createVirtualCompile.compilerHost,
466
+ _updateFile2 = _createVirtualCompile.updateFile;
505
467
  var fileVersions = new Map();
506
468
  var projectVersion = 0;
507
-
508
469
  var languageServiceHost = _extends({}, compilerHost, {
509
470
  getProjectVersion: function getProjectVersion() {
510
471
  return projectVersion.toString();
@@ -528,11 +489,9 @@ function createVirtualLanguageServiceHost(sys, rootFiles, compilerOptions, ts, c
528
489
  },
529
490
  getScriptSnapshot: function getScriptSnapshot(fileName) {
530
491
  var contents = sys.readFile(fileName);
531
-
532
- if (contents) {
492
+ if (contents && typeof contents === "string") {
533
493
  return ts.ScriptSnapshot.fromString(contents);
534
494
  }
535
-
536
495
  return;
537
496
  },
538
497
  getScriptVersion: function getScriptVersion(fileName) {
@@ -540,27 +499,22 @@ function createVirtualLanguageServiceHost(sys, rootFiles, compilerOptions, ts, c
540
499
  },
541
500
  writeFile: sys.writeFile
542
501
  });
543
-
544
502
  var lsHost = {
545
503
  languageServiceHost: languageServiceHost,
546
504
  updateFile: function updateFile(sourceFile) {
547
505
  projectVersion++;
548
506
  fileVersions.set(sourceFile.fileName, projectVersion.toString());
549
-
550
507
  if (!fileNames.includes(sourceFile.fileName)) {
551
508
  fileNames.push(sourceFile.fileName);
552
509
  }
553
-
554
510
  _updateFile2(sourceFile);
555
511
  }
556
512
  };
557
513
  return lsHost;
558
514
  }
559
-
560
515
  var requirePath = function requirePath() {
561
516
  return require(String.fromCharCode(112, 97, 116, 104));
562
517
  };
563
-
564
518
  var requireFS = function requireFS() {
565
519
  return require(String.fromCharCode(102, 115));
566
520
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typescript/vfs",
3
- "version": "1.4.0",
3
+ "version": "1.5.1",
4
4
  "license": "MIT",
5
5
  "author": "TypeScript team",
6
6
  "homepage": "https://github.com/microsoft/TypeScript-Website",
@@ -18,14 +18,13 @@
18
18
  "files": [
19
19
  "dist"
20
20
  ],
21
- "scripts": {
22
- "start": "tsdx watch",
23
- "bootstrap": "yarn build",
24
- "build": "tsdx build && yarn make-for-website && yarn make-global",
25
- "make-for-website": "cpy src/index.ts ../sandbox/src/vendor --rename=typescript-vfs.ts",
26
- "make-global": "node scripts/makeGlobals.js",
27
- "test": "tsdx test",
28
- "lint": "tsdx lint"
21
+ "jest": {
22
+ "preset": "ts-jest",
23
+ "testEnvironment": "jsdom",
24
+ "testPathIgnorePatterns": [
25
+ "/node_modules/",
26
+ ".cache"
27
+ ]
29
28
  },
30
29
  "browser": {
31
30
  "fs": false,
@@ -36,11 +35,25 @@
36
35
  "debug": "^4.1.1"
37
36
  },
38
37
  "devDependencies": {
39
- "@types/jest": "^25.1.3",
40
- "babel-jest": "^26.0.6",
38
+ "@types/jest": "^29.5.12",
39
+ "babel-jest": "^29.7.0",
41
40
  "cpy-cli": "^3.1.1",
42
- "tsdx": "^0.14.1",
43
- "tslib": "^1.10.0",
41
+ "dts-cli": "^2.0.5",
42
+ "jest": "^29.5.0",
43
+ "jest-environment-jsdom": "^29.5.0",
44
+ "jest-watch-typeahead": "^2.2.2",
45
+ "lz-string": "^1.5.0",
46
+ "ts-jest": "^29.0.5",
47
+ "tslib": "^2.6.2",
44
48
  "typescript": "*"
49
+ },
50
+ "scripts": {
51
+ "start": "dts watch",
52
+ "bootstrap": "pnpm build",
53
+ "build": "dts build && pnpm make-for-website && pnpm make-global",
54
+ "make-for-website": "cpy src/index.ts ../sandbox/src/vendor --rename=typescript-vfs.ts",
55
+ "make-global": "node scripts/makeGlobals.js",
56
+ "test": "dts test",
57
+ "lint": "dts lint"
45
58
  }
46
- }
59
+ }
package/CHANGELOG.md DELETED
@@ -1,15 +0,0 @@
1
- ### 1.3
2
-
3
- - Adds a JS file into the npm tarball for using with a vanilla script tag, which sets `global.tsvfs` with exported function.
4
-
5
- Unpkg URLS:
6
-
7
- - https://unpkg.com/browse/@typescript/vfs@dist/vfs.globals.js
8
-
9
- ### 1.2
10
-
11
- Updates `createFSBackedSystem` to rely more on the default TypeScript system object which should see twoslash code samples re-using the node_modules from the local project.
12
-
13
- ### 0.0 - 1.0 - 1.1
14
-
15
- Created the lib, got it working