@@ -40,14 +40,16 @@ async function buildCJSIndex(name, languages) {
40
40
await fs . writeFile ( `${ process . env . BUILD_DIR } /lib/${ name } .js` , index ) ;
41
41
}
42
42
43
- async function buildNodeLanguage ( language ) {
43
+ async function buildNodeLanguage ( language , options ) {
44
44
const input = { ...config . rollup . node . input , input : language . path } ;
45
45
const output = { ...config . rollup . node . output , file : `${ process . env . BUILD_DIR } /lib/languages/${ language . name } .js` } ;
46
46
await rollupWrite ( input , output ) ;
47
- await rollupWrite ( input , { ...output ,
48
- format : "es" ,
49
- file : output . file . replace ( "/lib/" , "/es/" )
50
- } ) ;
47
+ if ( options . esm ) {
48
+ await rollupWrite ( input , { ...output ,
49
+ format : "es" ,
50
+ file : output . file . replace ( "/lib/" , "/es/" )
51
+ } ) ;
52
+ }
51
53
}
52
54
53
55
const EXCLUDE = [ "join" ] ;
@@ -62,23 +64,24 @@ async function buildESMUtils() {
62
64
return code ;
63
65
}
64
66
} ] ;
65
- const output = {
67
+ await rollupWrite ( input , {
66
68
...config . rollup . node . output ,
67
69
format : "es" ,
68
70
file : `${ process . env . BUILD_DIR } /es/utils/regex.js`
69
- } ;
70
- await rollupWrite ( input , output ) ;
71
+ } ) ;
71
72
}
72
73
73
-
74
- async function buildNodeHighlightJS ( ) {
74
+ async function buildNodeHighlightJS ( options ) {
75
75
const input = { ...config . rollup . node . input , input : `src/highlight.js` } ;
76
76
const output = { ...config . rollup . node . output , file : `${ process . env . BUILD_DIR } /lib/core.js` } ;
77
77
await rollupWrite ( input , output ) ;
78
- await rollupWrite ( input , { ...output ,
79
- format : "es" ,
80
- file : `${ process . env . BUILD_DIR } /es/core.js`
81
- } ) ;
78
+ if ( options . esm ) {
79
+ await rollupWrite ( input , {
80
+ ...output ,
81
+ format : "es" ,
82
+ file : `${ process . env . BUILD_DIR } /es/core.js`
83
+ } ) ;
84
+ }
82
85
}
83
86
84
87
function dual ( file ) {
@@ -88,7 +91,7 @@ function dual(file) {
88
91
} ;
89
92
}
90
93
91
- async function buildPackageJSON ( ) {
94
+ async function buildPackageJSON ( options ) {
92
95
const packageJson = require ( "../package" ) ;
93
96
94
97
const exports = {
@@ -98,16 +101,16 @@ async function buildPackageJSON() {
98
101
"./lib/core" : dual ( "./lib/core.js" ) ,
99
102
"./lib/languages/*" : dual ( "./lib/languages/*.js" ) ,
100
103
} ;
101
- packageJson . exports = exports ;
104
+ if ( options . esm ) packageJson . exports = exports ;
102
105
103
106
await fs . writeFile ( `${ process . env . BUILD_DIR } /package.json` , JSON . stringify ( packageJson , null , 2 ) ) ;
104
107
}
105
108
106
- async function buildLanguages ( languages ) {
109
+ async function buildLanguages ( languages , options ) {
107
110
log ( "Writing languages." ) ;
108
111
await Promise . all (
109
112
languages . map ( async ( lang ) => {
110
- await buildNodeLanguage ( lang ) ;
113
+ await buildNodeLanguage ( lang , options ) ;
111
114
process . stdout . write ( "." ) ;
112
115
} )
113
116
) ;
@@ -126,7 +129,6 @@ const CORE_FILES = [
126
129
127
130
async function buildNode ( options ) {
128
131
mkdir ( "lib/languages" ) ;
129
- mkdir ( "es/languages" ) ;
130
132
mkdir ( "scss" ) ;
131
133
mkdir ( "styles" ) ;
132
134
mkdir ( "types" ) ;
@@ -136,9 +138,13 @@ async function buildNode(options) {
136
138
install ( `./${ file } ` , file ) ;
137
139
} ) ;
138
140
install ( "./src/core.d.ts" , "lib/core.d.ts" ) ;
139
- install ( "./src/core.d.ts" , "es/core.d.ts" ) ;
140
141
install ( "./src/core.d.ts" , "lib/common.d.ts" ) ;
141
- install ( "./src/core.d.ts" , "es/common.d.ts" ) ;
142
+
143
+ if ( options . esm ) {
144
+ mkdir ( "es/languages" ) ;
145
+ install ( "./src/core.d.ts" , "es/core.d.ts" ) ;
146
+ install ( "./src/core.d.ts" , "es/common.d.ts" ) ;
147
+ }
142
148
143
149
log ( "Writing styles." ) ;
144
150
const styles = await fs . readdir ( "./src/styles/" ) ;
@@ -147,22 +153,24 @@ async function buildNode(options) {
147
153
install ( `./src/styles/${ file } ` , `scss/${ file . replace ( ".css" , ".scss" ) } ` ) ;
148
154
} ) ;
149
155
log ( "Writing package.json." ) ;
150
- await buildPackageJSON ( ) ;
156
+ await buildPackageJSON ( options ) ;
151
157
152
158
let languages = await getLanguages ( ) ;
153
159
// filter languages for inclusion in the highlight.js bundle
154
160
languages = filter ( languages , options . languages ) ;
155
161
156
162
const common = languages . filter ( l => l . categories . includes ( "common" ) ) ;
157
- await buildESMIndex ( "index" , languages ) ;
158
- await buildESMIndex ( "common" , common ) ;
159
- await buildESMUtils ( ) ;
163
+ if ( options . esm ) {
164
+ await buildESMIndex ( "index" , languages ) ;
165
+ await buildESMIndex ( "common" , common ) ;
166
+ await buildESMUtils ( ) ;
167
+ }
160
168
await buildCJSIndex ( "index" , languages ) ;
161
169
await buildCJSIndex ( "common" , common ) ;
162
- await buildLanguages ( languages ) ;
170
+ await buildLanguages ( languages , options ) ;
163
171
164
172
log ( "Writing highlight.js" ) ;
165
- await buildNodeHighlightJS ( ) ;
173
+ await buildNodeHighlightJS ( options ) ;
166
174
}
167
175
168
176
module . exports . build = buildNode ;
0 commit comments