Skip to content

Commit

Permalink
refactor(language-service): process references data at runtime to red…
Browse files Browse the repository at this point in the history
…uce bundle size
  • Loading branch information
KazariEX committed Dec 15, 2024
1 parent b3b088b commit 0448b3c
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 65 deletions.
79 changes: 59 additions & 20 deletions packages/language-service/lib/plugins/data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import type * as html from 'vscode-html-languageservice';

declare module 'vscode-html-languageservice' {
interface ITagData {
__reference?: string;
}

interface IAttributeData {
__reference?: string;
}
}

let locale: { name: string, url: string }[];

export function loadTemplateData(lang: string) {

lang = lang.toLowerCase();
Expand Down Expand Up @@ -37,6 +49,8 @@ export function loadTemplateData(lang: string) {
data = require('../../data/template/en.json');
}

resolveReferences(data);

for (const attr of [...data.globalAttributes ?? []]) {
if (!attr.name.startsWith('v-')) {
data.globalAttributes?.push(
Expand Down Expand Up @@ -67,68 +81,93 @@ export function loadLanguageBlocks(lang: string): html.HTMLDataV1 {

lang = lang.toLowerCase();

let data: html.HTMLDataV1;

if (lang === 'ja') {
return require('../../data/language-blocks/ja.json');
data = require('../../data/language-blocks/ja.json');
}
else if (lang === 'fr') {
return require('../../data/language-blocks/fr.json');
data = require('../../data/language-blocks/fr.json');
}
else if (lang === 'ko') {
return require('../../data/language-blocks/ko.json');
data = require('../../data/language-blocks/ko.json');
}
else if (lang === 'pt-br') {
return require('../../data/language-blocks/pt.json');
data = require('../../data/language-blocks/pt.json');
}
else if (lang === 'zh-cn') {
return require('../../data/language-blocks/zh-cn.json');
data = require('../../data/language-blocks/zh-cn.json');
}
else if (lang === 'zh-tw') {
return require('../../data/language-blocks/zh-hk.json');
data = require('../../data/language-blocks/zh-hk.json');
}
else if (lang === 'it') {
return require('../../data/language-blocks/it.json');
data = require('../../data/language-blocks/it.json');
}
else if (lang === 'cs') {
return require('../../data/language-blocks/cs.json');
data = require('../../data/language-blocks/cs.json');
}
else if (lang === 'ru') {
return require('../../data/language-blocks/ru.json');
data = require('../../data/language-blocks/ru.json');
}
else {
data = require('../../data/language-blocks/en.json');
}

return require('../../data/language-blocks/en.json');
resolveReferences(data);

return data;
}

export function loadModelModifiersData(lang: string): html.HTMLDataV1 {

lang = lang.toLowerCase();

let data: html.HTMLDataV1;

if (lang === 'ja') {
return require('../../data/model-modifiers/ja.json');
data = require('../../data/model-modifiers/ja.json');
}
else if (lang === 'fr') {
return require('../../data/model-modifiers/fr.json');
data = require('../../data/model-modifiers/fr.json');
}
else if (lang === 'ko') {
return require('../../data/model-modifiers/ko.json');
data = require('../../data/model-modifiers/ko.json');
}
else if (lang === 'pt-br') {
return require('../../data/model-modifiers/pt.json');
data = require('../../data/model-modifiers/pt.json');
}
else if (lang === 'zh-cn') {
return require('../../data/model-modifiers/zh-cn.json');
data = require('../../data/model-modifiers/zh-cn.json');
}
else if (lang === 'zh-tw') {
return require('../../data/model-modifiers/zh-hk.json');
data = require('../../data/model-modifiers/zh-hk.json');
}
else if (lang === 'it') {
return require('../../data/model-modifiers/it.json');
data = require('../../data/model-modifiers/it.json');
}
else if (lang === 'cs') {
return require('../../data/model-modifiers/cs.json');
data = require('../../data/model-modifiers/cs.json');
}
else if (lang === 'ru') {
return require('../../data/model-modifiers/ru.json');
data = require('../../data/model-modifiers/ru.json');
}
else {
data = require('../../data/model-modifiers/en.json');
}

return require('../../data/model-modifiers/en.json');
resolveReferences(data);

return data;
}

function resolveReferences(data: html.HTMLDataV1) {
locale ??= require('../../data/locale.json');

for (const item of [...data.globalAttributes ?? [], ...data.tags ?? []]) {
item.references = locale.map(({ name, url }) => ({
name,
url: url + item.__reference
}));
}
}
67 changes: 22 additions & 45 deletions packages/language-service/scripts/update-html-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,22 @@ const langs = [

for (const lang of langs) {
if (lang.supported) {
localeWorker(lang);
templateWorker(lang);
sfcWorker(lang);
modelWorker(lang);
}
}

function localeWorker(lang) {

const data = langs.map(({ name, url }) => ({ name, url }));

const writePath = path.resolve(__dirname, '../data/locale.json');
fs.writeFileSync(writePath, JSON.stringify(data, null, 2));
console.log(writePath);
}

async function sfcWorker(lang) {

const sfcDoc = await fetchText(lang.repoUrl + 'HEAD/src/api/sfc-spec.md', lang.url);
Expand All @@ -115,10 +125,7 @@ async function sfcWorker(lang) {
// { name: 'gql' },
// { name: 'graphql' },
],
references: langs.map(lang => ({
name: lang.name,
url: `${lang.url}api/sfc-spec.html#pre-processors`,
})),
__reference: 'api/sfc-spec.html#pre-processors',
};
/**
* @type {import('vscode-html-languageservice').IAttributeData}
Expand All @@ -129,10 +136,7 @@ async function sfcWorker(lang) {
kind: 'markdown',
value: sfcDoc.split('\n## ')[5].split('\n').slice(1).join('\n').trim(),
},
references: langs.map(lang => ({
name: lang.name,
url: `${lang.url}api/sfc-spec.html#src-imports`,
})),
__reference: 'api/sfc-spec.html#src-imports',
};
const languageBlocks = sfcDoc
.split('\n## ')[2]
Expand All @@ -151,10 +155,7 @@ async function sfcWorker(lang) {
kind: 'markdown',
value: lines.slice(1).join('\n'),
},
references: langs.map(lang => ({
name: lang.name,
url: `${lang.url}api/sfc-spec.html#${normalizeHash(name)}`,
})),
__reference: `api/sfc-spec.html#${normalizeHash(name)}`,
};
if (name === 'template') {
data.attributes.push({
Expand Down Expand Up @@ -196,10 +197,7 @@ async function sfcWorker(lang) {
kind: 'markdown',
value: cssFeaturesDoc.split('\n## ')[1].split('\n').slice(1).join('\n').trim(),
},
references: langs.map(lang => ({
name: lang.name,
url: `${lang.url}api/sfc-css-features.html#scoped-css`,
})),
__reference: 'api/sfc-css-features.html#scoped-css',
});
data.attributes.push({
name: 'module',
Expand All @@ -208,10 +206,7 @@ async function sfcWorker(lang) {
kind: 'markdown',
value: cssFeaturesDoc.split('\n## ')[2].split('\n').slice(1).join('\n').trim(),
},
references: langs.map(lang => ({
name: lang.name,
url: `${lang.url}api/sfc-css-features.html#css-modules`,
})),
__reference: 'api/sfc-css-features.html#css-modules',
});
}
return data;
Expand All @@ -228,7 +223,7 @@ async function sfcWorker(lang) {
name: 'setup',
valueSet: 'v',
description: scriptSetupBlock.description,
references: scriptSetupBlock.references,
__reference: scriptSetupBlock.__reference,
});

/**
Expand Down Expand Up @@ -265,10 +260,7 @@ async function modelWorker(lang) {
kind: 'markdown',
value: lines.slice(1).join('\n').trim(),
},
references: langs.map(lang => ({
name: lang.name,
url: `${lang.url}guide/essentials/forms.html#${normalizeHash(name)}`,
})),
__reference: `guide/essentials/forms.html#${normalizeHash(name)}`,
};
return data;
});
Expand Down Expand Up @@ -310,10 +302,7 @@ async function templateWorker(lang) {
value: lines.slice(1).join('\n'),
},
attributes: [],
references: langs.map(lang => ({
name: lang.name,
url: `${lang.url}api/built-in-components.html#${normalizeHash(name)}`,
})),
__reference: `api/built-in-components.html#${normalizeHash(name)}`,
};
return data;
});
Expand All @@ -333,10 +322,7 @@ async function templateWorker(lang) {
value: lines.slice(1).join('\n'),
},
attributes: [],
references: langs.map(lang => ({
name: lang.name,
url: `${lang.url}api/built-in-special-elements.html#${normalizeHash(name)}`,
})),
__reference: `api/built-in-special-elements.html#${normalizeHash(name)}`,
};
return data;
});
Expand All @@ -361,10 +347,7 @@ async function templateWorker(lang) {
kind: 'markdown',
value: lines.slice(1).join('\n').trim(),
},
references: langs.map(lang => ({
name: lang.name,
url: `${lang.url}api/built-in-directives.html#${normalizeHash(name)}`,
})),
__reference: `api/built-in-directives.html#${normalizeHash(name)}`,
};
return data;
});
Expand All @@ -383,10 +366,7 @@ async function templateWorker(lang) {
kind: 'markdown',
value: lines.slice(1).join('\n').trim(),
},
references: langs.map(lang => ({
name: lang.name,
url: `${lang.url}api/built-in-special-attributes.html#${normalizeHash(name)}`,
})),
__reference: `api/built-in-special-attributes.html#${normalizeHash(name)}`,
};
return data;
});
Expand All @@ -405,10 +385,7 @@ async function templateWorker(lang) {
kind: 'markdown',
value: lines.slice(1).join('\n'),
},
references: langs.map(lang => ({
name: lang.name,
url: `${lang.url}api/ssr.html#${normalizeHash(name)}`,
})),
__reference: `api/ssr.html#${normalizeHash(name)}`,
};
return data;
})[0];
Expand Down

0 comments on commit 0448b3c

Please sign in to comment.