a zip code crypto-currency system good for red ONLY

NodeSourcePlugin.js 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const AliasPlugin = require("enhanced-resolve/lib/AliasPlugin");
  7. const ParserHelpers = require("../ParserHelpers");
  8. const nodeLibsBrowser = require("node-libs-browser");
  9. module.exports = class NodeSourcePlugin {
  10. constructor(options) {
  11. this.options = options;
  12. }
  13. apply(compiler) {
  14. const options = this.options;
  15. if(options === false) // allow single kill switch to turn off this plugin
  16. return;
  17. function getPathToModule(module, type) {
  18. if(type === true || (type === undefined && nodeLibsBrowser[module])) {
  19. if(!nodeLibsBrowser[module]) throw new Error(`No browser version for node.js core module ${module} available`);
  20. return nodeLibsBrowser[module];
  21. } else if(type === "mock") {
  22. return require.resolve(`node-libs-browser/mock/${module}`);
  23. } else if(type === "empty") {
  24. return require.resolve("node-libs-browser/mock/empty");
  25. } else return module;
  26. }
  27. function addExpression(parser, name, module, type, suffix) {
  28. suffix = suffix || "";
  29. parser.plugin(`expression ${name}`, function() {
  30. if(this.state.module && this.state.module.resource === getPathToModule(module, type)) return;
  31. const mockModule = ParserHelpers.requireFileAsExpression(this.state.module.context, getPathToModule(module, type));
  32. return ParserHelpers.addParsedVariableToModule(this, name, mockModule + suffix);
  33. });
  34. }
  35. compiler.plugin("compilation", function(compilation, params) {
  36. params.normalModuleFactory.plugin("parser", function(parser, parserOptions) {
  37. if(parserOptions.node === false)
  38. return;
  39. let localOptions = options;
  40. if(parserOptions.node)
  41. localOptions = Object.assign({}, localOptions, parserOptions.node);
  42. if(localOptions.global) {
  43. parser.plugin("expression global", function() {
  44. const retrieveGlobalModule = ParserHelpers.requireFileAsExpression(this.state.module.context, require.resolve("../../buildin/global.js"));
  45. return ParserHelpers.addParsedVariableToModule(this, "global", retrieveGlobalModule);
  46. });
  47. }
  48. if(localOptions.process) {
  49. const processType = localOptions.process;
  50. addExpression(parser, "process", "process", processType);
  51. }
  52. if(localOptions.console) {
  53. const consoleType = localOptions.console;
  54. addExpression(parser, "console", "console", consoleType);
  55. }
  56. const bufferType = localOptions.Buffer;
  57. if(bufferType) {
  58. addExpression(parser, "Buffer", "buffer", bufferType, ".Buffer");
  59. }
  60. if(localOptions.setImmediate) {
  61. const setImmediateType = localOptions.setImmediate;
  62. addExpression(parser, "setImmediate", "timers", setImmediateType, ".setImmediate");
  63. addExpression(parser, "clearImmediate", "timers", setImmediateType, ".clearImmediate");
  64. }
  65. });
  66. });
  67. compiler.plugin("after-resolvers", (compiler) => {
  68. Object.keys(nodeLibsBrowser).forEach((lib) => {
  69. if(options[lib] !== false) {
  70. compiler.resolvers.normal.apply(
  71. new AliasPlugin("described-resolve", {
  72. name: lib,
  73. onlyModule: true,
  74. alias: getPathToModule(lib, options[lib])
  75. }, "resolve")
  76. );
  77. }
  78. });
  79. });
  80. }
  81. };