a zip code crypto-currency system good for red ONLY

TemplatedPathPlugin.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Jason Anderson @diurnalist
  4. */
  5. "use strict";
  6. const REGEXP_HASH = /\[hash(?::(\d+))?\]/gi,
  7. REGEXP_CHUNKHASH = /\[chunkhash(?::(\d+))?\]/gi,
  8. REGEXP_NAME = /\[name\]/gi,
  9. REGEXP_ID = /\[id\]/gi,
  10. REGEXP_FILE = /\[file\]/gi,
  11. REGEXP_QUERY = /\[query\]/gi,
  12. REGEXP_FILEBASE = /\[filebase\]/gi;
  13. // Using global RegExp for .test is dangerous
  14. // We use a normal RegExp instead of .test
  15. const REGEXP_HASH_FOR_TEST = new RegExp(REGEXP_HASH.source, "i"),
  16. REGEXP_CHUNKHASH_FOR_TEST = new RegExp(REGEXP_CHUNKHASH.source, "i"),
  17. REGEXP_NAME_FOR_TEST = new RegExp(REGEXP_NAME.source, "i");
  18. // TODO: remove in webpack 3
  19. // Backwards compatibility; expose regexes on Template object
  20. const Template = require("./Template");
  21. Template.REGEXP_HASH = REGEXP_HASH;
  22. Template.REGEXP_CHUNKHASH = REGEXP_CHUNKHASH;
  23. Template.REGEXP_NAME = REGEXP_NAME;
  24. Template.REGEXP_ID = REGEXP_ID;
  25. Template.REGEXP_FILE = REGEXP_FILE;
  26. Template.REGEXP_QUERY = REGEXP_QUERY;
  27. Template.REGEXP_FILEBASE = REGEXP_FILEBASE;
  28. const withHashLength = (replacer, handlerFn) => {
  29. return function(_, hashLength) {
  30. const length = hashLength && parseInt(hashLength, 10);
  31. if(length && handlerFn) {
  32. return handlerFn(length);
  33. }
  34. const hash = replacer.apply(this, arguments);
  35. return length ? hash.slice(0, length) : hash;
  36. };
  37. };
  38. const getReplacer = (value, allowEmpty) => {
  39. return function(match) {
  40. // last argument in replacer is the entire input string
  41. const input = arguments[arguments.length - 1];
  42. if(value === null || value === undefined) {
  43. if(!allowEmpty) throw new Error(`Path variable ${match} not implemented in this context: ${input}`);
  44. return "";
  45. } else {
  46. return `${value}`;
  47. }
  48. };
  49. };
  50. const replacePathVariables = (path, data) => {
  51. const chunk = data.chunk;
  52. const chunkId = chunk && chunk.id;
  53. const chunkName = chunk && (chunk.name || chunk.id);
  54. const chunkHash = chunk && (chunk.renderedHash || chunk.hash);
  55. const chunkHashWithLength = chunk && chunk.hashWithLength;
  56. if(data.noChunkHash && REGEXP_CHUNKHASH_FOR_TEST.test(path)) {
  57. throw new Error(`Cannot use [chunkhash] for chunk in '${path}' (use [hash] instead)`);
  58. }
  59. return path
  60. .replace(REGEXP_HASH, withHashLength(getReplacer(data.hash), data.hashWithLength))
  61. .replace(REGEXP_CHUNKHASH, withHashLength(getReplacer(chunkHash), chunkHashWithLength))
  62. .replace(REGEXP_ID, getReplacer(chunkId))
  63. .replace(REGEXP_NAME, getReplacer(chunkName))
  64. .replace(REGEXP_FILE, getReplacer(data.filename))
  65. .replace(REGEXP_FILEBASE, getReplacer(data.basename))
  66. // query is optional, it's OK if it's in a path but there's nothing to replace it with
  67. .replace(REGEXP_QUERY, getReplacer(data.query, true));
  68. };
  69. class TemplatedPathPlugin {
  70. apply(compiler) {
  71. compiler.plugin("compilation", compilation => {
  72. const mainTemplate = compilation.mainTemplate;
  73. mainTemplate.plugin("asset-path", replacePathVariables);
  74. mainTemplate.plugin("global-hash", function(chunk, paths) {
  75. const outputOptions = this.outputOptions;
  76. const publicPath = outputOptions.publicPath || "";
  77. const filename = outputOptions.filename || "";
  78. const chunkFilename = outputOptions.chunkFilename || outputOptions.filename;
  79. if(REGEXP_HASH_FOR_TEST.test(publicPath) || REGEXP_CHUNKHASH_FOR_TEST.test(publicPath) || REGEXP_NAME_FOR_TEST.test(publicPath))
  80. return true;
  81. if(REGEXP_HASH_FOR_TEST.test(filename))
  82. return true;
  83. if(REGEXP_HASH_FOR_TEST.test(chunkFilename))
  84. return true;
  85. if(REGEXP_HASH_FOR_TEST.test(paths.join("|")))
  86. return true;
  87. });
  88. mainTemplate.plugin("hash-for-chunk", function(hash, chunk) {
  89. const outputOptions = this.outputOptions;
  90. const chunkFilename = outputOptions.chunkFilename || outputOptions.filename;
  91. if(REGEXP_CHUNKHASH_FOR_TEST.test(chunkFilename))
  92. hash.update(JSON.stringify(chunk.getChunkMaps(true, true).hash));
  93. if(REGEXP_NAME_FOR_TEST.test(chunkFilename))
  94. hash.update(JSON.stringify(chunk.getChunkMaps(true, true).name));
  95. });
  96. });
  97. }
  98. }
  99. module.exports = TemplatedPathPlugin;