languageModule.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.createLanguages = exports.createVueLanguage = void 0;
  4. const path_1 = require("path");
  5. const plugins_1 = require("./plugins");
  6. const sourceFile_1 = require("./sourceFile");
  7. const sharedTypes = require("./utils/globalTypes");
  8. const ts_1 = require("./utils/ts");
  9. const fileRegistries = [];
  10. function getVueFileRegistry(key, plugins) {
  11. let fileRegistry = fileRegistries.find(r => r.key === key
  12. && r.plugins.length === plugins.length
  13. && r.plugins.every(plugin => plugins.includes(plugin)))?.files;
  14. if (!fileRegistry) {
  15. fileRegistry = new Map();
  16. fileRegistries.push({
  17. key: key,
  18. plugins: plugins,
  19. files: fileRegistry,
  20. });
  21. }
  22. return fileRegistry;
  23. }
  24. function createVueLanguage(ts, compilerOptions = {}, _vueCompilerOptions = {}, codegenStack = false) {
  25. const vueCompilerOptions = (0, ts_1.resolveVueCompilerOptions)(_vueCompilerOptions);
  26. const plugins = (0, plugins_1.getDefaultVueLanguagePlugins)(ts, compilerOptions, vueCompilerOptions, codegenStack);
  27. const keys = [
  28. ...Object.keys(vueCompilerOptions)
  29. .sort()
  30. .filter(key => key !== 'plugins')
  31. .map(key => [key, vueCompilerOptions[key]]),
  32. [...new Set(plugins.map(plugin => plugin.requiredCompilerOptions ?? []).flat())]
  33. .sort()
  34. .map(key => [key, compilerOptions[key]]),
  35. ];
  36. const fileRegistry = getVueFileRegistry(JSON.stringify(keys), _vueCompilerOptions.plugins ?? []);
  37. const allowLanguageIds = new Set(['vue']);
  38. if (vueCompilerOptions.extensions.includes('.md')) {
  39. allowLanguageIds.add('markdown');
  40. }
  41. if (vueCompilerOptions.extensions.includes('.html')) {
  42. allowLanguageIds.add('html');
  43. }
  44. return {
  45. createVirtualFile(fileName, snapshot, languageId) {
  46. if ((languageId && allowLanguageIds.has(languageId))
  47. || (!languageId && vueCompilerOptions.extensions.some(ext => fileName.endsWith(ext)))) {
  48. if (fileRegistry.has(fileName)) {
  49. const reusedVueFile = fileRegistry.get(fileName);
  50. reusedVueFile.update(snapshot);
  51. return reusedVueFile;
  52. }
  53. const vueFile = new sourceFile_1.VueFile(fileName, snapshot, vueCompilerOptions, plugins, ts, codegenStack);
  54. fileRegistry.set(fileName, vueFile);
  55. return vueFile;
  56. }
  57. },
  58. updateVirtualFile(sourceFile, snapshot) {
  59. sourceFile.update(snapshot);
  60. },
  61. resolveHost(host) {
  62. const sharedTypesSnapshot = ts.ScriptSnapshot.fromString(sharedTypes.getTypesCode(vueCompilerOptions));
  63. const sharedTypesFileName = path_1.posix.join(host.rootPath, sharedTypes.baseName);
  64. return {
  65. ...host,
  66. resolveModuleName(moduleName, impliedNodeFormat) {
  67. if (impliedNodeFormat === ts.ModuleKind.ESNext && vueCompilerOptions.extensions.some(ext => moduleName.endsWith(ext))) {
  68. return `${moduleName}.js`;
  69. }
  70. return host.resolveModuleName?.(moduleName, impliedNodeFormat) ?? moduleName;
  71. },
  72. getScriptFileNames() {
  73. return [
  74. sharedTypesFileName,
  75. ...host.getScriptFileNames(),
  76. ];
  77. },
  78. getScriptSnapshot(fileName) {
  79. if (fileName === sharedTypesFileName) {
  80. return sharedTypesSnapshot;
  81. }
  82. return host.getScriptSnapshot(fileName);
  83. },
  84. };
  85. },
  86. };
  87. }
  88. exports.createVueLanguage = createVueLanguage;
  89. /**
  90. * @deprecated planed to remove in 2.0, please use getOrCreateVueLanguage instead of
  91. */
  92. function createLanguages(compilerOptions = {}, vueCompilerOptions = {}, ts = require('typescript'), codegenStack = false) {
  93. return [
  94. createVueLanguage(ts, compilerOptions, vueCompilerOptions, codegenStack),
  95. ...vueCompilerOptions.experimentalAdditionalLanguageModules?.map(module => require(module)) ?? [],
  96. ];
  97. }
  98. exports.createLanguages = createLanguages;
  99. //# sourceMappingURL=languageModule.js.map