@@ -4,23 +4,26 @@ import { toArray } from '../../util/iterables';
44import type { Prism } from '../../core' ;
55import type { ComponentProto , PluginProto } from '../../types' ;
66
7- function getDefaultSrcPath ( ) {
7+ function getDefaultSrcPath ( ) {
88 if ( typeof document !== 'undefined' ) {
99 const script = document . currentScript as HTMLScriptElement | null ;
1010 if ( script ) {
11- const autoloaderFile = / \b p l u g i n s \/ a u t o l o a d e r \/ p r i s m - a u t o l o a d e r \. (?: m i n \. ) ? j s (?: \? [ ^ \r \n / ] * ) ? $ / i;
11+ const autoloaderFile =
12+ / \b p l u g i n s \/ a u t o l o a d e r \/ p r i s m - a u t o l o a d e r \. (?: m i n \. ) ? j s (?: \? [ ^ \r \n / ] * ) ? $ / i;
1213 const prismFile = / ( ^ | \/ ) [ \w - ] + \. (?: m i n \. ) ? m ? j s (?: \? [ ^ \r \n / ] * ) ? $ / i;
1314
1415 const autoloaderPath = script . getAttribute ( 'data-autoloader-path' ) ;
1516 if ( autoloaderPath != null ) {
1617 // data-autoloader-path is set, so just use it
1718 return autoloaderPath . trim ( ) . replace ( / \/ ? $ / , '/' ) ;
18- } else {
19+ }
20+ else {
1921 const src = script . src ;
2022 if ( autoloaderFile . test ( src ) ) {
2123 // the script is the original autoloader script in the usual Prism project structure
2224 return src . replace ( autoloaderFile , '/' ) ;
23- } else if ( prismFile . test ( src ) ) {
25+ }
26+ else if ( prismFile . test ( src ) ) {
2427 // the script is part of a bundle like a custom prism.js from the download page
2528 return src . replace ( prismFile , '$1/' ) ;
2629 }
@@ -31,7 +34,7 @@ function getDefaultSrcPath() {
3134 return './' ;
3235}
3336
34- function pathJoin ( dir : string , file : string ) {
37+ function pathJoin ( dir : string , file : string ) {
3538 return dir . replace ( / \/ $ / , '' ) + '/' + file ;
3639}
3740
@@ -41,7 +44,7 @@ const ignoredLanguages: ReadonlySet<string> = new Set(['none']);
4144 * @param Prism The Prism instance
4245 * @param name The name of the language
4346 */
44- function isLoaded ( Prism : Prism , name : string ) {
47+ function isLoaded ( Prism : Prism , name : string ) {
4548 // resolve alias
4649 const id = resolveAlias ( name ) ;
4750 return Prism . components . has ( id ) || ignoredLanguages . has ( id ) ;
@@ -56,56 +59,60 @@ export class Autoloader {
5659 /**
5760 * @package
5861 */
59- constructor ( Prism : Prism ) {
62+ constructor ( Prism : Prism ) {
6063 this . Prism = Prism ;
6164 }
6265
6366 /**
6467 * Loads all given languages concurrently.
6568 */
66- async loadLanguages ( languages : string | readonly string [ ] ) : Promise < void > {
69+ async loadLanguages ( languages : string | readonly string [ ] ) : Promise < void > {
6770 const toLoad = toArray ( languages )
6871 . map ( resolveAlias )
69- . filter ( ( id ) => ! isLoaded ( this . Prism , id ) ) ;
70-
71- await Promise . all ( toLoad . map ( ( id ) => {
72- const path = pathJoin ( this . srcPath , `languages/${ id } .js` ) ;
73-
74- let promise = this . _importCache . get ( path ) ;
75- if ( promise === undefined ) {
76- promise = import ( path ) . then ( ( exports ) => {
77- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
78- const proto = exports . default as ComponentProto ;
79- this . Prism . components . add ( proto ) ;
80- } ) ;
81- this . _importCache . set ( path , promise ) ;
82- }
83- return promise ;
84- } ) ) ;
72+ . filter ( id => ! isLoaded ( this . Prism , id ) ) ;
73+
74+ await Promise . all (
75+ toLoad . map ( id => {
76+ const path = pathJoin ( this . srcPath , `languages/${ id } .js` ) ;
77+
78+ let promise = this . _importCache . get ( path ) ;
79+ if ( promise === undefined ) {
80+ promise = import ( path ) . then ( exports => {
81+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
82+ const proto = exports . default as ComponentProto ;
83+ this . Prism . components . add ( proto ) ;
84+ } ) ;
85+ this . _importCache . set ( path , promise ) ;
86+ }
87+ return promise ;
88+ } )
89+ ) ;
8590 }
8691
8792 /**
8893 * Loads all given languages concurrently.
8994 *
9095 * This function simply invokes {@link Autoloader#loadLanguages} and logs errors to `console.error`.
9196 */
92- preloadLanguages ( languages : string | readonly string [ ] ) : void {
93- this . loadLanguages ( languages ) . catch ( ( reason ) => {
94- console . error ( `Failed to preload languages (${ toArray ( languages ) . join ( ', ' ) } ): ${ String ( reason ) } ` ) ;
97+ preloadLanguages ( languages : string | readonly string [ ] ) : void {
98+ this . loadLanguages ( languages ) . catch ( reason => {
99+ console . error (
100+ `Failed to preload languages (${ toArray ( languages ) . join ( ', ' ) } ): ${ String ( reason ) } `
101+ ) ;
95102 } ) ;
96103 }
97104}
98105
99106export default {
100107 id : 'autoloader' ,
101- plugin ( Prism ) {
108+ plugin ( Prism ) {
102109 return new Autoloader ( Prism ) ;
103110 } ,
104- effect ( Prism ) {
111+ effect ( Prism ) {
105112 /**
106113 * Returns all additional dependencies of the given element defined by the `data-dependencies` attribute.
107114 */
108- function getDependencies ( element : Element ) {
115+ function getDependencies ( element : Element ) {
109116 let deps = element . getAttribute ( 'data-dependencies' ) ?. trim ( ) ;
110117 if ( ! deps ) {
111118 const parent = getParentPre ( element ) ;
@@ -119,13 +126,15 @@ export default {
119126 /**
120127 * Maps the given name to a list of components that have to be loaded.
121128 */
122- function mapDependency ( name : string ) {
129+ function mapDependency ( name : string ) {
123130 if ( ! name || ignoredLanguages . has ( name ) ) {
124131 return [ ] ;
125- } else if ( / ^ d i f f - ./ i. test ( name ) ) {
132+ }
133+ else if ( / ^ d i f f - ./ i. test ( name ) ) {
126134 // the "diff-xxxx" format is used by the Diff Highlight plugin
127135 return [ 'diff' , name . slice ( 'diff-' . length ) ] ;
128- } else {
136+ }
137+ else {
129138 return [ name ] ;
130139 }
131140 }
@@ -140,7 +149,7 @@ export default {
140149 deps . push ( ...mapDependency ( name ) ) ;
141150 }
142151
143- deps = deps . filter ( ( name ) => ! isLoaded ( Prism , name ) ) ;
152+ deps = deps . filter ( name => ! isLoaded ( Prism , name ) ) ;
144153 if ( deps . length === 0 ) {
145154 // all dependencies are already loaded
146155 return ;
@@ -149,10 +158,12 @@ export default {
149158 const autoloader = Prism . plugins . autoloader as Autoloader ;
150159 autoloader . loadLanguages ( deps ) . then (
151160 ( ) => Prism . highlightElement ( element ) ,
152- ( reason ) => {
153- console . error ( `Failed to load languages (${ deps . join ( ', ' ) } ): ${ String ( reason ) } ` ) ;
161+ reason => {
162+ console . error (
163+ `Failed to load languages (${ deps . join ( ', ' ) } ): ${ String ( reason ) } `
164+ ) ;
154165 }
155166 ) ;
156167 } ) ;
157- }
168+ } ,
158169} as PluginProto < 'autoloader' > ;
0 commit comments