Electron ES6 module import

后端 未结 5 1459
栀梦
栀梦 2020-12-03 11:53

Electron 3.0.0-beta.1 Node 10.2.0 Chromium 66.0.3359.181

The problem I\'m having is importing a module. I created the following protocol:

         


        
5条回答
  •  天命终不由人
    2020-12-03 11:58

    Quick Solution:

    const { protocol } = require( 'electron' )
    const nfs = require( 'fs' )
    const npjoin = require( 'path' ).join
    const es6Path = npjoin( __dirname, 'www' )
    
    // <= v4.x
    // protocol.registerStandardSchemes( [ 'es6' ] )
    
    // >= v5.x
    protocol.registerSchemesAsPrivileged([
      { scheme: 'es6', privileges: { standard: true } }
    ])
    
    app.on( 'ready', () => {
      protocol.registerBufferProtocol( 'es6', ( req, cb ) => {
        nfs.readFile(
          npjoin( es6Path, req.url.replace( 'es6://', '' ) ),
          (e, b) => { cb( { mimeType: 'text/javascript', data: b } ) }
        )
      })
    })

提交回复
热议问题