11const fs = require ( 'fs' ) ;
2- const { pbjs, pbts } = require ( 'protobufjs/ cli' ) ;
2+ const { pbjs, pbts } = require ( 'protobufjs- cli' ) ;
33const protobuf = require ( 'protobufjs' ) ;
44const tmp = require ( 'tmp-promise' ) ;
55const validateOptions = require ( 'schema-utils' ) . validate ;
66
77const { getOptions } = require ( 'loader-utils' ) ;
88
9+ const TARGET_STATIC_MODULE = 'static-module' ;
10+
911/** @type { Parameters<typeof validateOptions>[0] } */
1012const schema = {
1113 type : 'object' ,
1214 properties : {
13- json : {
14- type : 'boolean ' ,
15- default : false ,
15+ target : {
16+ type : 'string ' ,
17+ default : TARGET_STATIC_MODULE ,
1618 } ,
1719 paths : {
1820 type : 'array' ,
@@ -41,23 +43,6 @@ const schema = {
4143 } ,
4244 } ,
4345
44- // pbts config is only applicable if the pbjs target is
45- // `static-module`, i.e. if the `json` flag is false. We enforce
46- // this at the schema level; see
47- // https://json-schema.org/understanding-json-schema/reference/conditionals.html#implication.
48- anyOf : [
49- {
50- properties : {
51- json : { const : true } ,
52- pbts : { const : false } ,
53- } ,
54- } ,
55- {
56- not : {
57- properties : { json : { const : true } } ,
58- } ,
59- } ,
60- ] ,
6146 additionalProperties : false ,
6247} ;
6348
@@ -68,8 +53,9 @@ const schema = {
6853 *
6954 * @typedef {{ args: string[] } } PbtsOptions
7055 * @typedef {{
71- * json: boolean, paths: string[], pbjsArgs: string[],
72- * pbts: boolean | PbtsOptions
56+ * paths: string[], pbjsArgs: string[],
57+ * pbts: boolean | PbtsOptions,
58+ * target: string,
7359 * }} LoaderOptions
7460 */
7561
@@ -133,133 +119,139 @@ module.exports = function protobufJsLoader(source) {
133119 throw new Error ( 'Failed to request async execution from webpack' ) ;
134120 }
135121
136- const defaultPaths = ( ( ) => {
137- if ( 'options' in this ) {
138- // For webpack@2 and webpack@3. property loaderContext.options
139- // was deprecated in webpack@3 and removed in webpack@4.
140- return ( this . options . resolve || { } ) . modules ;
141- }
122+ try {
123+ const defaultPaths = ( ( ) => {
124+ if ( 'options' in this ) {
125+ // For webpack@2 and webpack@3. property loaderContext.options
126+ // was deprecated in webpack@3 and removed in webpack@4.
127+ return ( this . options . resolve || { } ) . modules ;
128+ }
142129
143- if ( this . _compiler ) {
144- // For webpack@4 and webpack@5. The `_compiler` property is
145- // deprecated, but still works as of webpack@5.
146- return ( this . _compiler . options . resolve || { } ) . modules ;
147- }
130+ if ( this . _compiler ) {
131+ // For webpack@4 and webpack@5. The `_compiler` property is
132+ // deprecated, but still works as of webpack@5.
133+ return ( this . _compiler . options . resolve || { } ) . modules ;
134+ }
148135
149- return undefined ;
150- } ) ( ) ;
136+ return undefined ;
137+ } ) ( ) ;
151138
152- /** @type LoaderOptions */
153- const options = {
154- json : false ,
139+ /** @type LoaderOptions */
140+ const options = {
141+ target : TARGET_STATIC_MODULE ,
155142
156- // Default to the paths given to the compiler.
157- paths : defaultPaths || [ ] ,
143+ // Default to the paths given to the compiler.
144+ paths : defaultPaths || [ ] ,
158145
159- pbjsArgs : [ ] ,
146+ pbjsArgs : [ ] ,
160147
161- pbts : false ,
148+ pbts : false ,
162149
163- ...getOptions ( this ) ,
164- } ;
165- try {
150+ ...getOptions ( this ) ,
151+ } ;
166152 validateOptions ( schema , options , { name : 'protobufjs-loader' } ) ;
167- } catch ( err ) {
168- callback ( err instanceof Error ? err : new Error ( `${ err } ` ) , undefined ) ;
169- return ;
170- }
171153
172- /** @type { string } */
173- let filename ;
174- tmp
175- . file ( )
176- . then ( ( o ) => {
177- filename = o . path ;
178- return new Promise ( ( resolve , reject ) => {
179- fs . write ( o . fd , source , ( err , bytesWritten ) => {
180- if ( err ) {
181- reject ( err ) ;
182- } else {
183- resolve ( bytesWritten ) ;
184- }
154+ /** @type { string } */
155+ let filename ;
156+ tmp
157+ . file ( )
158+ . then ( ( o ) => {
159+ filename = o . path ;
160+ return new Promise ( ( resolve , reject ) => {
161+ fs . write ( o . fd , source , ( err , bytesWritten ) => {
162+ if ( err ) {
163+ reject ( err ) ;
164+ } else {
165+ resolve ( bytesWritten ) ;
166+ }
167+ } ) ;
185168 } ) ;
186- } ) ;
187- } )
188- . then ( ( ) => {
189- const { paths } = options ;
169+ } )
170+ . then ( ( ) => {
171+ const { paths } = options ;
190172
191- const loadDependencies = new Promise ( ( resolve , reject ) => {
192- const root = new protobuf . Root ( ) ;
193- root . resolvePath = ( origin , target ) => {
194- // Adapted from
195- // https://github.com/dcodeIO/protobuf.js/blob/master/cli/pbjs.js
196- const normOrigin = protobuf . util . path . normalize ( origin ) ;
197- const normTarget = protobuf . util . path . normalize ( target ) ;
173+ const loadDependencies = new Promise ( ( resolve , reject ) => {
174+ const root = new protobuf . Root ( ) ;
175+ root . resolvePath = ( origin , target ) => {
176+ // Adapted from
177+ // https://github.com/dcodeIO/protobuf.js/blob/master/cli/pbjs.js
178+ const normOrigin = protobuf . util . path . normalize ( origin ) ;
179+ const normTarget = protobuf . util . path . normalize ( target ) ;
198180
199- let resolved = protobuf . util . path . resolve (
200- normOrigin ,
201- normTarget ,
202- true
203- ) ;
204- const idx = resolved . lastIndexOf ( 'google/protobuf/' ) ;
205- if ( idx > - 1 ) {
206- const altname = resolved . substring ( idx ) ;
207- if ( altname in protobuf . common ) {
208- resolved = altname ;
181+ let resolved = protobuf . util . path . resolve (
182+ normOrigin ,
183+ normTarget ,
184+ true
185+ ) ;
186+ const idx = resolved . lastIndexOf ( 'google/protobuf/' ) ;
187+ if ( idx > - 1 ) {
188+ const altname = resolved . substring ( idx ) ;
189+ if ( altname in protobuf . common ) {
190+ resolved = altname ;
191+ }
209192 }
210- }
211193
212- if ( fs . existsSync ( resolved ) ) {
213- // Don't add a dependency on the temp file
214- if ( resolved !== filename ) {
215- self . addDependency ( resolved ) ;
194+ if ( fs . existsSync ( resolved ) ) {
195+ // Don't add a dependency on the temp file
196+ if ( resolved !== filename ) {
197+ self . addDependency ( resolved ) ;
198+ }
199+ return resolved ;
216200 }
217- return resolved ;
218- }
219201
220- for ( let i = 0 ; i < paths . length ; i += 1 ) {
221- const iresolved = protobuf . util . path . resolve (
222- `${ paths [ i ] } /` ,
223- target
224- ) ;
225- if ( fs . existsSync ( iresolved ) ) {
226- self . addDependency ( iresolved ) ;
227- return iresolved ;
202+ for ( let i = 0 ; i < paths . length ; i += 1 ) {
203+ const iresolved = protobuf . util . path . resolve (
204+ `${ paths [ i ] } /` ,
205+ target
206+ ) ;
207+ if ( fs . existsSync ( iresolved ) ) {
208+ self . addDependency ( iresolved ) ;
209+ return iresolved ;
210+ }
228211 }
229- }
230212
231- return null ;
232- } ;
233- protobuf . load ( filename , root , ( err , result ) => {
234- if ( err ) {
235- reject ( err ) ;
236- } else {
237- resolve ( result ) ;
238- }
239- } ) ;
240- } ) ;
241-
242- let args = options . pbjsArgs ;
243- paths . forEach ( ( path ) => {
244- args = args . concat ( [ '-p' , path ] ) ;
245- } ) ;
246- args = args
247- . concat ( [ '-t' , options . json ? 'json-module' : 'static-module' ] )
248- . concat ( [ filename ] ) ;
249-
250- pbjs . main ( args , ( err , result ) => {
251- // Make sure we've added all dependencies before completing.
252- loadDependencies
253- . catch ( ( depErr ) => {
254- callback ( depErr ) ;
255- } )
256- . then ( ( ) => {
257- if ( ! options . pbts || err ) {
258- callback ( err , result ) ;
213+ return null ;
214+ } ;
215+ protobuf . load ( filename , root , ( err , result ) => {
216+ if ( err ) {
217+ reject ( err ) ;
259218 } else {
260- execPbts ( self . resourcePath , options . pbts , result || '' , callback ) ;
219+ resolve ( result ) ;
261220 }
262221 } ) ;
222+ } ) ;
223+
224+ /** @type { string[] } */
225+ let args = [ '-t' , options . target ] ;
226+ paths . forEach ( ( path ) => {
227+ args = args . concat ( [ '-p' , path ] ) ;
228+ } ) ;
229+ args = args . concat ( options . pbjsArgs ) . concat ( [ filename ] ) ;
230+
231+ pbjs . main ( args , ( err , result ) => {
232+ // Make sure we've added all dependencies before completing.
233+ loadDependencies
234+ . catch ( ( depErr ) => {
235+ callback ( depErr ) ;
236+ } )
237+ . then ( ( ) => {
238+ if ( ! options . pbts || err ) {
239+ callback ( err , result ) ;
240+ } else {
241+ execPbts (
242+ self . resourcePath ,
243+ options . pbts ,
244+ result || '' ,
245+ callback
246+ ) ;
247+ }
248+ } ) ;
249+ } ) ;
250+ } )
251+ . catch ( ( err ) => {
252+ callback ( err instanceof Error ? err : new Error ( `${ err } ` ) , undefined ) ;
263253 } ) ;
264- } ) ;
254+ } catch ( err ) {
255+ callback ( err instanceof Error ? err : new Error ( `${ err } ` ) , undefined ) ;
256+ }
265257} ;
0 commit comments