项目作者: chentsulin

项目描述 :
webpack target function for electron renderer
高级语言: JavaScript
项目地址: git://github.com/chentsulin/webpack-target-electron-renderer.git
创建时间: 2015-08-26T16:35:49Z
项目社区:https://github.com/chentsulin/webpack-target-electron-renderer

开源协议:MIT License

下载


webpack-target-electron-renderer

NPM version
Build Status
Test coverage
Dependency Status

webpack target function for electron renderer

Install

  1. $ npm install webpack-target-electron-renderer

Usage

  1. var webpackTargetElectronRenderer = require('webpack-target-electron-renderer');
  2. var options = {
  3. entry: entry,
  4. output: output,
  5. module: {
  6. loaders: loaders
  7. },
  8. devtool: opts.devtool,
  9. resolve: {
  10. extensions: extensions,
  11. packageMains: ['webpack', 'browser', 'web', 'browserify', ['jam', 'main'], 'main']
  12. }
  13. }
  14. options.target = webpackTargetElectronRenderer(options)

See also electron-react-boilerplate.

API

webpackTargetElectronRenderer(options)

options

Required
Type: object

just like the object that you used to export by webpack.config.js.

How this module works

There are some built-in webpack build targets, such as 'web', 'node', 'electron', includes several important modules and global variables resolving rules and templates for chunk and hot-update functionalities.

In electron, there are two different kinds of processes: main and renderer. electron-main is almost the same as node environment and just need to set all of electron built-in modules as externals. However, electron-renderer is a little bit different, it’s just like a mix environment between browser and node. So we need to provide a target using JsonpTemplatePlugin, FunctionModulePlugin for browser environment and NodeTargetPlugin and ExternalsPlugin for commonjs and electron bulit-in modules.

Below is the code about how webpack apply target option:

  1. // webpack/WebpackOptionsApply.js
  2. WebpackOptionsApply.prototype.process = function(options, compiler) {
  3. ...
  4. if(typeof options.target === "string") {
  5. switch(options.target) {
  6. case "web":
  7. ...
  8. case "webworker":
  9. ...
  10. case "node":
  11. case "async-node":
  12. ...
  13. case "node-webkit":
  14. ...
  15. case "atom":
  16. case "electron":
  17. ...
  18. default:
  19. throw new Error("Unsupported target '" + options.target + "'.");
  20. }
  21. } else if(options.target !== false) {
  22. options.target(compiler);
  23. } else {
  24. throw new Error("Unsupported target '" + options.target + "'.");
  25. }
  26. ...
  27. }

As you can see, we can provide a function as target and then it will go into this else if branch:

  1. } else if(options.target !== false) {
  2. options.target(compiler);
  3. } else {

That’s it! This is the basic
mechanism about how this module works.

Source code is only 32 LoC now, so it should not be so hard to understand.

Note: webpack#1467 and webpack#2181 has been merged and released (>= v1.12.15), so we can use on webpack 1.x and 2.x now.

Migrate to webpack 2 or webpack 1 >= 1.12.15

Added target: 'electron-renderer' to your webpack.config.js instead of using this module:

  1. module.exports = {
  2. target: 'electron-renderer',
  3. // ...others
  4. };

See the example here.

License

MIT © C.T. Lin