@vue/language-core 3.0.7 → 3.1.0-alpha.0

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.
Files changed (36) hide show
  1. package/lib/codegen/globalTypes.js +19 -13
  2. package/lib/codegen/localTypes.d.ts +2 -4
  3. package/lib/codegen/localTypes.js +10 -33
  4. package/lib/codegen/script/component.d.ts +0 -3
  5. package/lib/codegen/script/component.js +15 -22
  6. package/lib/codegen/script/context.d.ts +2 -4
  7. package/lib/codegen/script/index.d.ts +4 -6
  8. package/lib/codegen/script/index.js +70 -59
  9. package/lib/codegen/script/scriptSetup.js +140 -141
  10. package/lib/codegen/script/template.d.ts +1 -3
  11. package/lib/codegen/script/template.js +90 -16
  12. package/lib/codegen/template/context.js +1 -1
  13. package/lib/codegen/template/element.js +1 -3
  14. package/lib/codegen/template/elementProps.js +2 -6
  15. package/lib/codegen/template/index.d.ts +1 -1
  16. package/lib/codegen/template/index.js +10 -10
  17. package/lib/codegen/utils/index.d.ts +1 -0
  18. package/lib/codegen/utils/index.js +7 -0
  19. package/lib/compilerOptions.js +1 -3
  20. package/lib/languagePlugin.js +8 -19
  21. package/lib/parsers/scriptRanges.d.ts +0 -1
  22. package/lib/parsers/scriptRanges.js +6 -9
  23. package/lib/parsers/scriptSetupRanges.d.ts +0 -6
  24. package/lib/parsers/scriptSetupRanges.js +7 -93
  25. package/lib/plugins/vue-tsx.d.ts +2 -5
  26. package/lib/plugins/vue-tsx.js +15 -10
  27. package/lib/types.d.ts +1 -1
  28. package/lib/utils/shared.d.ts +0 -1
  29. package/lib/utils/shared.js +0 -4
  30. package/lib/virtualFile/vueFile.d.ts +5 -10
  31. package/lib/virtualFile/vueFile.js +3 -10
  32. package/package.json +2 -3
  33. package/lib/utils/ts.d.ts +0 -24
  34. package/lib/utils/ts.js +0 -296
  35. package/lib/utils/vue2TemplateCompiler.d.ts +0 -2
  36. package/lib/utils/vue2TemplateCompiler.js +0 -90
package/lib/utils/ts.js DELETED
@@ -1,296 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CompilerOptionsResolver = void 0;
4
- exports.createParsedCommandLineByJson = createParsedCommandLineByJson;
5
- exports.createParsedCommandLine = createParsedCommandLine;
6
- exports.getDefaultCompilerOptions = getDefaultCompilerOptions;
7
- exports.writeGlobalTypes = writeGlobalTypes;
8
- const shared_1 = require("@vue/shared");
9
- const path_browserify_1 = require("path-browserify");
10
- const globalTypes_1 = require("../codegen/globalTypes");
11
- const shared_2 = require("./shared");
12
- function createParsedCommandLineByJson(ts, host, rootDir, json, configFileName) {
13
- const extendedPaths = new Set();
14
- const proxyHost = {
15
- ...host,
16
- readFile(fileName) {
17
- if (!fileName.endsWith('/package.json')) {
18
- extendedPaths.add(fileName);
19
- }
20
- return host.readFile(fileName);
21
- },
22
- readDirectory() {
23
- return [];
24
- },
25
- };
26
- const parsed = ts.parseJsonConfigFileContent(json, proxyHost, rootDir, {}, configFileName);
27
- const resolver = new CompilerOptionsResolver(host.fileExists);
28
- for (const extendPath of [...extendedPaths].reverse()) {
29
- try {
30
- const configFile = ts.readJsonConfigFile(extendPath, host.readFile);
31
- const obj = ts.convertToObject(configFile, []);
32
- const rawOptions = obj?.vueCompilerOptions ?? {};
33
- resolver.addConfig(rawOptions, path_browserify_1.posix.dirname(configFile.fileName));
34
- }
35
- catch { }
36
- }
37
- // ensure the rootDir is added to the config roots
38
- resolver.addConfig({}, rootDir);
39
- return {
40
- ...parsed,
41
- vueOptions: resolver.build(),
42
- };
43
- }
44
- function createParsedCommandLine(ts, host, configFileName) {
45
- try {
46
- const extendedPaths = new Set();
47
- const proxyHost = {
48
- ...host,
49
- readFile(fileName) {
50
- if (!fileName.endsWith('/package.json')) {
51
- extendedPaths.add(fileName);
52
- }
53
- return host.readFile(fileName);
54
- },
55
- readDirectory() {
56
- return [];
57
- },
58
- };
59
- const config = ts.readJsonConfigFile(configFileName, proxyHost.readFile);
60
- const parsed = ts.parseJsonSourceFileConfigFileContent(config, proxyHost, path_browserify_1.posix.dirname(configFileName), {}, configFileName);
61
- const resolver = new CompilerOptionsResolver(host.fileExists);
62
- for (const extendPath of [...extendedPaths].reverse()) {
63
- try {
64
- const configFile = ts.readJsonConfigFile(extendPath, host.readFile);
65
- const obj = ts.convertToObject(configFile, []);
66
- const rawOptions = obj?.vueCompilerOptions ?? {};
67
- resolver.addConfig(rawOptions, path_browserify_1.posix.dirname(configFile.fileName));
68
- }
69
- catch { }
70
- }
71
- return {
72
- ...parsed,
73
- vueOptions: resolver.build(),
74
- };
75
- }
76
- catch { }
77
- return {
78
- options: {},
79
- errors: [],
80
- vueOptions: getDefaultCompilerOptions(),
81
- };
82
- }
83
- class CompilerOptionsResolver {
84
- constructor(fileExists) {
85
- this.fileExists = fileExists;
86
- this.configRoots = new Set();
87
- this.options = {};
88
- this.plugins = [];
89
- }
90
- addConfig(options, rootDir) {
91
- this.configRoots.add(rootDir);
92
- for (const key in options) {
93
- switch (key) {
94
- case 'target':
95
- if (options[key] === 'auto') {
96
- this.target = findVueVersion(rootDir);
97
- }
98
- else {
99
- this.target = options[key];
100
- }
101
- break;
102
- case 'globalTypesPath':
103
- if (options[key] !== undefined) {
104
- this.globalTypesPath = path_browserify_1.posix.join(rootDir, options[key]);
105
- }
106
- break;
107
- case 'plugins':
108
- this.plugins = (options.plugins ?? [])
109
- .flatMap((pluginPath) => {
110
- try {
111
- const resolvedPath = resolvePath(pluginPath, rootDir);
112
- if (resolvedPath) {
113
- const plugin = require(resolvedPath);
114
- plugin.__moduleName = pluginPath;
115
- return plugin;
116
- }
117
- else {
118
- console.warn('[Vue] Load plugin failed:', pluginPath);
119
- }
120
- }
121
- catch (error) {
122
- console.warn('[Vue] Resolve plugin path failed:', pluginPath, error);
123
- }
124
- return [];
125
- });
126
- break;
127
- default:
128
- // @ts-expect-error
129
- this.options[key] = options[key];
130
- break;
131
- }
132
- }
133
- if (options.target === undefined) {
134
- this.target ??= findVueVersion(rootDir);
135
- }
136
- }
137
- build(defaults) {
138
- defaults ??= getDefaultCompilerOptions(this.target, this.options.lib, this.options.strictTemplates);
139
- const resolvedOptions = {
140
- ...defaults,
141
- ...this.options,
142
- plugins: this.plugins,
143
- macros: {
144
- ...defaults.macros,
145
- ...this.options.macros,
146
- },
147
- composables: {
148
- ...defaults.composables,
149
- ...this.options.composables,
150
- },
151
- fallthroughComponentNames: [
152
- ...defaults.fallthroughComponentNames,
153
- ...this.options.fallthroughComponentNames ?? [],
154
- ].map(shared_2.hyphenateTag),
155
- // https://github.com/vuejs/vue-next/blob/master/packages/compiler-dom/src/transforms/vModel.ts#L49-L51
156
- // https://vuejs.org/guide/essentials/forms.html#form-input-bindings
157
- experimentalModelPropName: Object.fromEntries(Object.entries(this.options.experimentalModelPropName ?? defaults.experimentalModelPropName).map(([k, v]) => [(0, shared_1.camelize)(k), v])),
158
- };
159
- if (resolvedOptions.globalTypesPath === shared_1.NOOP) {
160
- if (this.fileExists && this.globalTypesPath === undefined) {
161
- const fileDirToGlobalTypesPath = new Map();
162
- resolvedOptions.globalTypesPath = fileName => {
163
- const fileDir = path_browserify_1.posix.dirname(fileName);
164
- if (fileDirToGlobalTypesPath.has(fileDir)) {
165
- return fileDirToGlobalTypesPath.get(fileDir);
166
- }
167
- const root = this.findNodeModulesRoot(fileDir, resolvedOptions.lib);
168
- const result = root
169
- ? path_browserify_1.posix.join(root, 'node_modules', '.vue-global-types', (0, globalTypes_1.getGlobalTypesFileName)(resolvedOptions))
170
- : undefined;
171
- fileDirToGlobalTypesPath.set(fileDir, result);
172
- return result;
173
- };
174
- }
175
- else {
176
- resolvedOptions.globalTypesPath = () => this.globalTypesPath;
177
- }
178
- }
179
- return resolvedOptions;
180
- }
181
- findNodeModulesRoot(dir, lib) {
182
- while (!this.fileExists(path_browserify_1.posix.join(dir, 'node_modules', lib, 'package.json'))) {
183
- const parentDir = path_browserify_1.posix.dirname(dir);
184
- if (dir === parentDir) {
185
- return;
186
- }
187
- dir = parentDir;
188
- }
189
- return dir;
190
- }
191
- }
192
- exports.CompilerOptionsResolver = CompilerOptionsResolver;
193
- function findVueVersion(rootDir) {
194
- const resolvedPath = resolvePath('vue/package.json', rootDir);
195
- if (resolvedPath) {
196
- const vuePackageJson = require(resolvedPath);
197
- const versionNumbers = vuePackageJson.version.split('.');
198
- return Number(versionNumbers[0] + '.' + versionNumbers[1]);
199
- }
200
- else {
201
- // console.warn('Load vue/package.json failed from', folder);
202
- }
203
- }
204
- function resolvePath(scriptPath, root) {
205
- try {
206
- if (require?.resolve) {
207
- return require.resolve(scriptPath, { paths: [root] });
208
- }
209
- else {
210
- // console.warn('failed to resolve path:', scriptPath, 'require.resolve is not supported in web');
211
- }
212
- }
213
- catch {
214
- // console.warn(error);
215
- }
216
- }
217
- function getDefaultCompilerOptions(target = 99, lib = 'vue', strictTemplates = false) {
218
- return {
219
- target,
220
- lib,
221
- globalTypesPath: shared_1.NOOP,
222
- extensions: ['.vue'],
223
- vitePressExtensions: [],
224
- petiteVueExtensions: [],
225
- jsxSlots: false,
226
- strictVModel: strictTemplates,
227
- strictCssModules: false,
228
- checkUnknownProps: strictTemplates,
229
- checkUnknownEvents: strictTemplates,
230
- checkUnknownDirectives: strictTemplates,
231
- checkUnknownComponents: strictTemplates,
232
- inferComponentDollarEl: false,
233
- inferComponentDollarRefs: false,
234
- inferTemplateDollarAttrs: false,
235
- inferTemplateDollarEl: false,
236
- inferTemplateDollarRefs: false,
237
- inferTemplateDollarSlots: false,
238
- skipTemplateCodegen: false,
239
- fallthroughAttributes: false,
240
- resolveStyleImports: false,
241
- resolveStyleClassNames: 'scoped',
242
- fallthroughComponentNames: [
243
- 'Transition',
244
- 'KeepAlive',
245
- 'Teleport',
246
- 'Suspense',
247
- ],
248
- dataAttributes: [],
249
- htmlAttributes: ['aria-*'],
250
- optionsWrapper: target >= 2.7
251
- ? [`(await import('${lib}')).defineComponent(`, `)`]
252
- : [`(await import('${lib}')).default.extend(`, `)`],
253
- macros: {
254
- defineProps: ['defineProps'],
255
- defineSlots: ['defineSlots'],
256
- defineEmits: ['defineEmits'],
257
- defineExpose: ['defineExpose'],
258
- defineModel: ['defineModel'],
259
- defineOptions: ['defineOptions'],
260
- withDefaults: ['withDefaults'],
261
- },
262
- composables: {
263
- useAttrs: ['useAttrs'],
264
- useCssModule: ['useCssModule'],
265
- useSlots: ['useSlots'],
266
- useTemplateRef: ['useTemplateRef', 'templateRef'],
267
- },
268
- plugins: [],
269
- experimentalModelPropName: {
270
- '': {
271
- input: true,
272
- },
273
- value: {
274
- input: { type: 'text' },
275
- textarea: true,
276
- select: true,
277
- },
278
- },
279
- };
280
- }
281
- function writeGlobalTypes(vueOptions, writeFile) {
282
- const originalFn = vueOptions.globalTypesPath;
283
- if (!originalFn) {
284
- return;
285
- }
286
- const writed = new Set();
287
- vueOptions.globalTypesPath = (fileName) => {
288
- const result = originalFn(fileName);
289
- if (result && !writed.has(result)) {
290
- writed.add(result);
291
- writeFile(result, (0, globalTypes_1.generateGlobalTypes)(vueOptions));
292
- }
293
- return result;
294
- };
295
- }
296
- //# sourceMappingURL=ts.js.map
@@ -1,2 +0,0 @@
1
- import * as CompilerDOM from '@vue/compiler-dom';
2
- export declare const compile: typeof CompilerDOM.compile;
@@ -1,90 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.compile = void 0;
4
- const CompilerDOM = require("@vue/compiler-dom");
5
- const Vue2TemplateCompiler = require('@vue/compiler-vue2/build');
6
- const compile = (template, options = {}) => {
7
- if (typeof template !== 'string') {
8
- throw new Error(`[@vue/language-core] compile() first argument must be string.`);
9
- }
10
- const onError = options.onError;
11
- const onWarn = options.onWarn;
12
- options.onError = error => {
13
- if (error.code === 33 // :key binding allowed in v-for template child in vue 2
14
- || error.code === 29 // fix https://github.com/vuejs/language-tools/issues/1638
15
- ) {
16
- return;
17
- }
18
- if (onError) {
19
- onError(error);
20
- }
21
- else {
22
- throw error;
23
- }
24
- };
25
- const vue2Result = Vue2TemplateCompiler.compile(template, { outputSourceRange: true });
26
- for (const error of vue2Result.errors) {
27
- onError?.({
28
- code: 'vue-template-compiler',
29
- name: '',
30
- message: error.msg,
31
- loc: {
32
- source: '',
33
- start: { column: -1, line: -1, offset: error.start },
34
- end: { column: -1, line: -1, offset: error.end },
35
- },
36
- });
37
- }
38
- for (const error of vue2Result.tips) {
39
- onWarn?.({
40
- code: 'vue-template-compiler',
41
- name: '',
42
- message: error.msg,
43
- loc: {
44
- source: '',
45
- start: { column: -1, line: -1, offset: error.start },
46
- end: { column: -1, line: -1, offset: error.end },
47
- },
48
- });
49
- }
50
- return baseCompile(template, Object.assign({}, CompilerDOM.parserOptions, options, {
51
- nodeTransforms: [
52
- ...CompilerDOM.DOMNodeTransforms,
53
- ...(options.nodeTransforms || []),
54
- ],
55
- directiveTransforms: Object.assign({}, CompilerDOM.DOMDirectiveTransforms, options.directiveTransforms || {}),
56
- }));
57
- };
58
- exports.compile = compile;
59
- function baseCompile(template, options = {}) {
60
- const onError = options.onError || (error => {
61
- throw error;
62
- });
63
- const isModuleMode = options.mode === 'module';
64
- const prefixIdentifiers = options.prefixIdentifiers === true || isModuleMode;
65
- if (!prefixIdentifiers && options.cacheHandlers) {
66
- onError(CompilerDOM.createCompilerError(49));
67
- }
68
- if (options.scopeId && !isModuleMode) {
69
- onError(CompilerDOM.createCompilerError(50));
70
- }
71
- const ast = CompilerDOM.baseParse(template, options);
72
- const [nodeTransforms, directiveTransforms] = CompilerDOM.getBaseTransformPreset(prefixIdentifiers);
73
- // v-for > v-if in vue 2
74
- const transformIf = nodeTransforms[1];
75
- const transformFor = nodeTransforms[3];
76
- nodeTransforms[1] = transformFor;
77
- nodeTransforms[3] = transformIf;
78
- CompilerDOM.transform(ast, Object.assign({}, options, {
79
- prefixIdentifiers,
80
- nodeTransforms: [
81
- ...nodeTransforms,
82
- ...(options.nodeTransforms || []), // user transforms
83
- ],
84
- directiveTransforms: Object.assign({}, directiveTransforms, options.directiveTransforms || {}),
85
- }));
86
- return CompilerDOM.generate(ast, Object.assign({}, options, {
87
- prefixIdentifiers,
88
- }));
89
- }
90
- //# sourceMappingURL=vue2TemplateCompiler.js.map