All files / vue-cli-plugin-electron-builder/lib webpackConfig.js

94.12% Statements 48/51
92.59% Branches 25/27
100% Functions 9/9
94% Lines 47/50
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 1012x       26x 26x 26x   24x   24x       24x   23x 23x 23x 23x 23x 23x   1x   1x 1x 1x       24x     26x       24x 24x 24x   2x     22x   24x 24x 24x 4x 2x 2x     24x 24x   20x   20x   22x   20x     20x     20x     20x 20x 20x       54x                         24x 24x   10x   24x   2x  
const fs = require('fs')
 
async function chainWebpack (api, pluginOptions, config) {
  const rendererProcessChain =
    pluginOptions.chainWebpackRendererProcess || (config => config)
  const realDirname = 'require("electron").remote.app.getAppPath()'
  if (process.env.IS_ELECTRON) {
    // Add externals
    config.externals(getExternals(api, pluginOptions))
    //   Modify webpack config to work properly with electron
    config
      .target('electron-renderer')
      .node.set('__dirname', false)
      .set('__filename', false)
    if (process.env.NODE_ENV === 'production') {
      //   Set process.env.BASE_URL and __static to absolute file path
      config.plugin('define').tap(args => {
        args[0].__dirname = realDirname
        args[0].__filename = `\`\${${realDirname}}/index.html\``
        args[0]['process.env'].BASE_URL = realDirname
        args[0].__static = realDirname
        return args
      })
    } else Eif (process.env.NODE_ENV === 'development') {
      //   Set __static to absolute path to public folder
      config.plugin('define').tap(args => {
        args[0].__static = JSON.stringify(api.resolve('./public'))
        return args
      })
    }
    // Apply user config
    rendererProcessChain(config)
  }
  // Older generated files expect this
  process.env.VUE_APP_NODE_MODULES_PATH = false
}
// Find all the dependencies without a `main` property or with a `binary` property or set by user and add them as webpack externals
function getExternals (api, pluginOptions) {
  const nodeModulesPath = pluginOptions.nodeModulesPath || './node_modules'
  let nodeModulesPaths = []
  if (Array.isArray(nodeModulesPath)) {
    // Set to user-defined array
    nodeModulesPaths = nodeModulesPath
  } else {
    // Add path to list
    nodeModulesPaths.push(nodeModulesPath)
  }
  const userExternals = pluginOptions.externals || []
  const userExternalsWhitelist = []
  userExternals.forEach((d, i) => {
    if (d.match(/^!.*$/)) {
      userExternals.splice(i, 1)
      userExternalsWhitelist.push(d.replace(/^!/, ''))
    }
  })
  const { dependencies } = require(api.resolve('./package.json'))
  const externalsList = Object.keys(dependencies || {}).filter(dep => {
    // Return true if we want to add a dependency to externals
    try {
      let pgkString
      for (const path of nodeModulesPaths) {
        // Check if package.json exists
        if (fs.existsSync(api.resolve(`${path}/${dep}/package.json`))) {
          // If it does, read it and break
          pgkString = fs
            .readFileSync(api.resolve(`${path}/${dep}/package.json`))
            .toString()
          break
        }
      }
      Iif (!pgkString) {
        throw new Error(`Could not find a package.json for module ${dep}`)
      }
      const pkg = JSON.parse(pgkString)
      const fields = ['main', 'module', 'jsnext:main', 'browser']
      return (
        // Not whitelisted
        userExternalsWhitelist.indexOf(dep) === -1 &&
        // Doesn't have main property
        (!fields.some(field => field in pkg) ||
          // Has binary property
          !!pkg.binary ||
          // Has gypfile property
          !!pkg.gypfile ||
          // Listed in user-defined externals list
          userExternals.indexOf(pkg.name) > -1)
      )
    } catch (e) {
      console.log(e)
      return true
    }
  })
  let externals = {}
  externalsList.forEach(d => {
    // Set external to be required during runtime
    externals[d] = `require("${d}")`
  })
  return externals
}
module.exports = { getExternals, chainWebpack }