File tree Expand file tree Collapse file tree 2 files changed +20
-7
lines changed Expand file tree Collapse file tree 2 files changed +20
-7
lines changed Original file line number Diff line number Diff line change @@ -44,6 +44,20 @@ module.exports = {
44
44
footer : "if (typeof exports === 'object' && typeof module !== 'undefined') { module.exports = hljs; }" ,
45
45
interop : false
46
46
}
47
+ } ,
48
+ browser : {
49
+ input : {
50
+ plugins : [
51
+ jsonPlugin ( ) ,
52
+ cjsPlugin ( ) ,
53
+ nodeResolve ( )
54
+ ]
55
+ } ,
56
+ output : {
57
+ name : "hljs" ,
58
+ format : "es" ,
59
+ interop : false
60
+ }
47
61
}
48
62
} ,
49
63
terser : {
Original file line number Diff line number Diff line change @@ -85,16 +85,15 @@ class Language {
85
85
86
86
87
87
async function compileLanguage ( language , options ) {
88
- const IIFE_HEADER_REGEX = / ^ ( v a r h l j s G r a m m a r = ) ? \( f u n c t i o n \( \) / ;
88
+ const EXPORT_REGEX = / e x p o r t d e f a u l t ( . * ) ; / ;
89
89
90
- // TODO: cant we use the source we already have?
91
- const input = { ...build_config . rollup . browser_iife . input , input : language . path } ;
92
- const output = { ...build_config . rollup . browser_iife . output , name : `hljsGrammar` , file : "out.js" } ;
90
+ // We can't use the source as extra language may be written in CJS
91
+ const input = { ...build_config . rollup . browser . input , input : language . path } ;
92
+ const output = { ...build_config . rollup . browser . output , file : "out.js" } ;
93
93
output . footer = null ;
94
94
95
- const data = await rollupCode ( input , output ) ;
96
- const iife = data . replace ( IIFE_HEADER_REGEX , `hljs.registerLanguage('${ language . name } ', function ()` ) ;
97
- const esm = `${ data } ;\nexport default hljsGrammar;` ;
95
+ const esm = await rollupCode ( input , output ) ;
96
+ const iife = `hljs.registerLanguage('${ language . name } ', (function (){"use strict";` + esm . replace ( EXPORT_REGEX , 'return $1' ) + '})())' ;
98
97
99
98
language . module = iife ;
100
99
const miniESM = await Terser . minify ( esm , options . terser ) ;
You can’t perform that action at this time.
0 commit comments