karolis-sh / electron-snowpack

Use Snowpack and esbuild for Electron app development
MIT License
50 stars 8 forks source link

Need to add the way to pass additional esbuild config #48

Closed wisedier closed 3 years ago

wisedier commented 3 years ago

In PR #41, we made esbuild make a bundle for preload.js. In this script, there is some cases use third party packages except for 'electron' such as 'lodash' and 'redis'. However default esbuild config 'external' only contains 'electron' so that 'preload.js' cannot import other third party packages. For this reason, I suggest to add the way to pass the additional config to get-esbuild-config.js or load it in the script. If you think it is needed and you can give me some guide to do it with documentation, I will send PR.

Example:


// my-project/package.json
{
  "name": "my-project",
  ...,
  "electron-snowpack": {
    "esbuild": {
      "external": ["electron", "lodash", "redis"]
    }
  },
  ...
}

// electron-snowpack/config/index.js
...
const projectRoot = process.cwd()
const isTS = fs.existsSync(path.join(projectRoot, 'tsconfig.json'));
const packageConfig = require(path.join(projectRoot, 'package.json'));
const userConfig = (packageConfig['electron-snowpack'] && packageConfig['electron-snowpack'].esbuild) || {};

module.exports = {
  ...,
  isTs,
  userConfig,
};

// electron-snowpack/lib/get-esbuild-config.js
...
const config = require('../config');

module.exports = async (more) => {
  ...
  more = Object.assign({}, more, config.userConfig);
  ...
  return {
    platform: 'node',
    format: 'cjs',
    entryPoints: await promisify(glob)('src/main/@(index|preload).[jt]s'),
    outdir: path.join(config.outputDir, 'main'),
    bundle: true,
    external: ['electron'],
    define,
    logLevel,
    ...(dev ? {} : { minify: true }),
    ...more,
  };
}
karolis-sh commented 3 years ago

I'm looking into how preload.js is working/used in practice. It seems I'll also need to update current examples that have security warnings and preload seems to solve that. Unsure how preload gets access to external e.g. lodash, but I'll get there.

karolis-sh commented 3 years ago

Could you provide an example of how to make an external module for preload to consume? I'd like to start with an example and I was under the impression that only electron and Node APIs are exposed to the runtime as external natives.

karolis-sh commented 3 years ago

I'm closing this for now as I do not have a reproducible use case.