index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.createProgram = void 0;
  4. const vue = require("@vue/language-core");
  5. const vueTs = require("@vue/typescript");
  6. const shared_1 = require("./shared");
  7. const windowsPathReg = /\\/g;
  8. function createProgram(options) {
  9. if (!options.options.noEmit && !options.options.emitDeclarationOnly)
  10. throw toThrow('js emit is not supported');
  11. if (!options.options.noEmit && options.options.noEmitOnError)
  12. throw toThrow('noEmitOnError is not supported');
  13. if (options.options.extendedDiagnostics || options.options.generateTrace)
  14. throw toThrow('--extendedDiagnostics / --generateTrace is not supported, please run `Write Virtual Files` in VSCode to write virtual files and use `--extendedDiagnostics` / `--generateTrace` via tsc instead of vue-tsc to debug.');
  15. if (!options.host)
  16. throw toThrow('!options.host');
  17. const ts = require('typescript');
  18. let program = options.oldProgram;
  19. if (shared_1.state.hook) {
  20. program = shared_1.state.hook.program;
  21. program.__vue.options = options;
  22. }
  23. else if (!program) {
  24. const ctx = {
  25. projectVersion: 0,
  26. options,
  27. get languageHost() {
  28. return languageHost;
  29. },
  30. get vueCompilerOptions() {
  31. return vueCompilerOptions;
  32. },
  33. get languageService() {
  34. return vueTsLs;
  35. },
  36. };
  37. const vueCompilerOptions = getVueCompilerOptions();
  38. const scripts = new Map();
  39. const languageHost = {
  40. workspacePath: ctx.options.host.getCurrentDirectory().replace(windowsPathReg, '/'),
  41. rootPath: ctx.options.host.getCurrentDirectory().replace(windowsPathReg, '/'),
  42. getCompilationSettings: () => ctx.options.options,
  43. getScriptFileNames: () => {
  44. return ctx.options.rootNames;
  45. },
  46. getScriptSnapshot,
  47. getProjectVersion: () => {
  48. return ctx.projectVersion.toString();
  49. },
  50. getProjectReferences: () => ctx.options.projectReferences,
  51. getCancellationToken: ctx.options.host.getCancellationToken ? () => ctx.options.host.getCancellationToken() : undefined,
  52. };
  53. const vueTsLs = vueTs.createLanguageService(languageHost, vueCompilerOptions, ts, ts.sys);
  54. program = vueTs.getProgram(ts, vueTsLs.__internal__.context, vueTsLs, ts.sys);
  55. program.__vue = ctx;
  56. function getVueCompilerOptions() {
  57. const tsConfig = ctx.options.options.configFilePath;
  58. if (typeof tsConfig === 'string') {
  59. return vue.createParsedCommandLine(ts, ts.sys, tsConfig).vueOptions;
  60. }
  61. return {};
  62. }
  63. function getScriptSnapshot(fileName) {
  64. return getScript(fileName)?.scriptSnapshot;
  65. }
  66. function getScript(fileName) {
  67. const script = scripts.get(fileName);
  68. if (script?.projectVersion === ctx.projectVersion) {
  69. return script;
  70. }
  71. const modifiedTime = ts.sys.getModifiedTime?.(fileName)?.valueOf() ?? 0;
  72. if (script?.modifiedTime === modifiedTime) {
  73. return script;
  74. }
  75. if (ctx.options.host.fileExists(fileName)) {
  76. const fileContent = ctx.options.host.readFile(fileName);
  77. if (fileContent !== undefined) {
  78. const script = {
  79. projectVersion: ctx.projectVersion,
  80. modifiedTime,
  81. scriptSnapshot: ts.ScriptSnapshot.fromString(fileContent),
  82. version: ctx.options.host.createHash?.(fileContent) ?? fileContent,
  83. };
  84. scripts.set(fileName, script);
  85. return script;
  86. }
  87. }
  88. }
  89. }
  90. else {
  91. const ctx = program.__vue;
  92. ctx.options = options;
  93. ctx.projectVersion++;
  94. }
  95. const vueCompilerOptions = program.__vue.vueCompilerOptions;
  96. if (vueCompilerOptions?.hooks) {
  97. const index = (shared_1.state.hook?.index ?? -1) + 1;
  98. if (index < vueCompilerOptions.hooks.length) {
  99. const hookPath = vueCompilerOptions.hooks[index];
  100. const hook = require(hookPath);
  101. shared_1.state.hook = {
  102. program,
  103. index,
  104. worker: (async () => await hook(program))(),
  105. };
  106. throw 'hook';
  107. }
  108. }
  109. for (const rootName of options.rootNames) {
  110. // register file watchers
  111. options.host.getSourceFile(rootName, ts.ScriptTarget.ESNext);
  112. }
  113. return program;
  114. }
  115. exports.createProgram = createProgram;
  116. function toThrow(msg) {
  117. console.error(msg);
  118. return msg;
  119. }
  120. //# sourceMappingURL=index.js.map