webpack.config.base.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. const fs = require('fs')
  2. const path = require('path')
  3. const webpack = require('webpack')
  4. const electronRoot = path.resolve(__dirname, '../..')
  5. const onlyPrintingGraph = !!process.env.PRINT_WEBPACK_GRAPH
  6. class AccessDependenciesPlugin {
  7. apply(compiler) {
  8. // Only hook into webpack when we are printing the dependency graph
  9. if (!onlyPrintingGraph) return
  10. compiler.hooks.compilation.tap('AccessDependenciesPlugin', compilation => {
  11. compilation.hooks.finishModules.tap('AccessDependenciesPlugin', modules => {
  12. const filePaths = modules.map(m => m.resource).filter(p => p).map(p => path.relative(electronRoot, p))
  13. console.info(JSON.stringify(filePaths))
  14. })
  15. })
  16. }
  17. }
  18. module.exports = ({
  19. alwaysHasNode,
  20. loadElectronFromAlternateTarget,
  21. targetDeletesNodeGlobals,
  22. target
  23. }) => {
  24. let entry = path.resolve(electronRoot, 'lib', target, 'init.ts')
  25. if (!fs.existsSync(entry)) {
  26. entry = path.resolve(electronRoot, 'lib', target, 'init.js')
  27. }
  28. return ({
  29. mode: 'development',
  30. devtool: 'inline-source-map',
  31. entry,
  32. target: alwaysHasNode ? 'node' : 'web',
  33. output: {
  34. filename: `${target}.bundle.js`
  35. },
  36. resolve: {
  37. alias: {
  38. '@electron/internal': path.resolve(electronRoot, 'lib'),
  39. 'electron': path.resolve(electronRoot, 'lib', loadElectronFromAlternateTarget || target, 'api', 'exports', 'electron.js'),
  40. // Force timers to resolve to our dependency that doens't use window.postMessage
  41. 'timers': path.resolve(electronRoot, 'node_modules', 'timers-browserify', 'main.js')
  42. },
  43. extensions: ['.ts', '.js']
  44. },
  45. module: {
  46. rules: [{
  47. test: /\.ts$/,
  48. loader: 'ts-loader',
  49. options: {
  50. configFile: path.resolve(electronRoot, 'tsconfig.electron.json'),
  51. transpileOnly: onlyPrintingGraph,
  52. ignoreDiagnostics: [6059]
  53. }
  54. }]
  55. },
  56. node: {
  57. __dirname: false,
  58. __filename: false,
  59. // We provide our own "timers" import above, any usage of setImmediate inside
  60. // one of our renderer bundles should import it from the 'timers' package
  61. setImmediate: false,
  62. },
  63. plugins: [
  64. new AccessDependenciesPlugin(),
  65. ...(targetDeletesNodeGlobals ? [
  66. new webpack.ProvidePlugin({
  67. process: ['@electron/internal/renderer/webpack-provider', 'process'],
  68. global: ['@electron/internal/renderer/webpack-provider', '_global'],
  69. Buffer: ['@electron/internal/renderer/webpack-provider', 'Buffer'],
  70. })
  71. ] : []),
  72. new webpack.ProvidePlugin({
  73. Promise: ['@electron/internal/common/webpack-globals-provider', 'Promise'],
  74. }),
  75. ]
  76. })
  77. }