index.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.removeEmitGlobalTypes = exports.run = void 0;
  4. const runTsc_1 = require("@volar/typescript/lib/quickstart/runTsc");
  5. const vue = require("@vue/language-core");
  6. const windowsPathReg = /\\/g;
  7. function run() {
  8. let runExtensions = ['.vue'];
  9. const extensionsChangedException = new Error('extensions changed');
  10. const main = () => (0, runTsc_1.runTsc)(require.resolve('typescript/lib/tsc'), runExtensions, (ts, options) => {
  11. const { configFilePath } = options.options;
  12. const vueOptions = typeof configFilePath === 'string'
  13. ? vue.createParsedCommandLine(ts, ts.sys, configFilePath.replace(windowsPathReg, '/')).vueOptions
  14. : vue.resolveVueCompilerOptions({});
  15. const allExtensions = [
  16. ...vueOptions.extensions,
  17. ...vueOptions.vitePressExtensions,
  18. ...vueOptions.petiteVueExtensions,
  19. ];
  20. if (runExtensions.length === allExtensions.length
  21. && runExtensions.every(ext => allExtensions.includes(ext))) {
  22. const writeFile = options.host.writeFile.bind(options.host);
  23. options.host.writeFile = (fileName, contents, ...args) => {
  24. return writeFile(fileName, removeEmitGlobalTypes(contents), ...args);
  25. };
  26. const vueLanguagePlugin = vue.createVueLanguagePlugin(ts, id => id, options.host?.useCaseSensitiveFileNames?.() ?? false, () => '', () => options.rootNames.map(rootName => rootName.replace(windowsPathReg, '/')), options.options, vueOptions);
  27. return [vueLanguagePlugin];
  28. }
  29. else {
  30. runExtensions = allExtensions;
  31. throw extensionsChangedException;
  32. }
  33. });
  34. try {
  35. main();
  36. }
  37. catch (err) {
  38. if (err === extensionsChangedException) {
  39. main();
  40. }
  41. }
  42. }
  43. exports.run = run;
  44. const removeEmitGlobalTypesRegexp = /^[^\n]*__VLS_globalTypesStart[\w\W]*__VLS_globalTypesEnd[^\n]*\n?$/mg;
  45. function removeEmitGlobalTypes(dts) {
  46. return dts.replace(removeEmitGlobalTypesRegexp, '');
  47. }
  48. exports.removeEmitGlobalTypes = removeEmitGlobalTypes;
  49. //# sourceMappingURL=index.js.map