@@ -5,12 +5,49 @@ import { grammars, injections } from 'tm-grammars'
55import { COMMENT_HEAD } from './constants'
66
77/**
8- * Languages that includes a lot of embedded langs,
9- * We only load on-demand for these langs.
8+ * Document-like languages that have embedded langs
109 */
11- const LANGS_LAZY_EMBEDDED = [
10+ const LANGS_LAZY_EMBEDDED_ALL = {
11+ markdown : [ ] ,
12+ mdx : [ ] ,
13+ wikitext : [ ] ,
14+ asciidoc : [ ] ,
15+ latex : [ 'tex' ] ,
16+ } as Record < string , string [ ] >
17+
18+ /**
19+ * Single-file-component-like languages that have embedded langs
20+ * For these langs, we exclude the standalone embedded langs from the main bundle
21+ */
22+ const LANGS_LAZY_EMBEDDED_PARTIAL = [
23+ 'vue' ,
24+ 'vue-html' ,
25+ 'svelte' ,
26+ 'pug' ,
27+ 'haml' ,
28+ 'astro' ,
29+ ]
30+
31+ /**
32+ * Languages to be excluded from SFC langs
33+ */
34+ const STANDALONG_LANGS_EMBEDDED = [
35+ 'pug' ,
36+ 'stylus' ,
37+ 'sass' ,
38+ 'scss' ,
39+ 'coffee' ,
40+ 'jsonc' ,
41+ 'json5' ,
42+ 'yaml' ,
43+ 'toml' ,
44+ 'scss' ,
45+ 'graphql' ,
1246 'markdown' ,
13- 'mdx' ,
47+ 'less' ,
48+ 'jsx' ,
49+ 'tsx' ,
50+ 'ruby' ,
1451]
1552
1653export async function prepareLangs ( ) {
@@ -22,6 +59,8 @@ export async function prepareLangs() {
2259
2360 allLangFiles . sort ( )
2461
62+ const resolvedLangs : LanguageRegistration [ ] = [ ]
63+
2564 for ( const file of allLangFiles ) {
2665 const content = await fs . readJSON ( file )
2766 const lang = grammars . find ( i => i . name === content . name ) || injections . find ( i => i . name === content . name )
@@ -40,12 +79,21 @@ export async function prepareLangs() {
4079 }
4180
4281 // We don't load all the embedded langs for markdown
43- if ( LANGS_LAZY_EMBEDDED . includes ( lang . name ) ) {
44- json . embeddedLangsLazy = json . embeddedLangs
45- json . embeddedLangs = [ ]
82+ if ( LANGS_LAZY_EMBEDDED_ALL [ lang . name ] ) {
83+ const includes = LANGS_LAZY_EMBEDDED_ALL [ lang . name ]
84+ json . embeddedLangsLazy = ( json . embeddedLangs || [ ] ) . filter ( i => ! includes . includes ( i ) ) || [ ]
85+ json . embeddedLangs = includes
86+ }
87+ else if ( LANGS_LAZY_EMBEDDED_PARTIAL . includes ( lang . name ) ) {
88+ json . embeddedLangsLazy = ( json . embeddedLangs || [ ] ) . filter ( i => STANDALONG_LANGS_EMBEDDED . includes ( i ) ) || [ ]
89+ json . embeddedLangs = ( json . embeddedLangs || [ ] ) . filter ( i => ! STANDALONG_LANGS_EMBEDDED . includes ( i ) ) || [ ]
4690 }
4791
4892 const deps : string [ ] = json . embeddedLangs || [ ]
93+ resolvedLangs . push ( json )
94+
95+ if ( deps . length > 10 )
96+ console . log ( json . name , json . embeddedLangs )
4997
5098 await fs . writeFile (
5199 `./src/assets/langs/${ lang . name } .js` ,
@@ -102,12 +150,10 @@ export default langs
102150 while ( changed ) {
103151 changed = false
104152 for ( const id of bundledIds ) {
105- if ( LANGS_LAZY_EMBEDDED . includes ( id ) )
106- continue
107- const lang = grammars . find ( i => i . name === id )
153+ const lang = resolvedLangs . find ( i => i . name === id )
108154 if ( ! lang )
109155 continue
110- for ( const e of lang . embedded || [ ] ) {
156+ for ( const e of lang . embeddedLangs || [ ] ) {
111157 if ( ! bundledIds . has ( e ) ) {
112158 bundledIds . add ( e )
113159 changed = true
0 commit comments