webpack-config.js.pytemplate 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. ////////////////////////////////////////////////////////////////////////////////
  2. // Template vars injected by projess_js.py:
  3. // boolean
  4. const isSandbox = ${IS_SANDBOX};
  5. // Array with absolute file path strings
  6. const entryFilenames = ${ENTRY_FILENAMES};
  7. // folder path string
  8. const outputPath = ${OUTPUT_PATH};
  9. // file name string
  10. const outputFilename = ${OUTPUT_FILENAME};
  11. // Array with absolute folder path strings
  12. const resolveRoots = ${RESOLVE_ROOTS};
  13. // Object, { alias1: 'path1', ... }
  14. const resolveAliases = ${RESOLVE_ALIASES};
  15. // null or Object with key 'sourceMapFilename'
  16. const sourceMapConfig = ${SOURCE_MAP_CONFIG};
  17. ////////////////////////////////////////////////////////////////////////////////
  18. // NOTE: Must escape dollar-signs, because this is a Python template!
  19. const webpack = require('webpack');
  20. module.exports = (() => {
  21. // The basic config:
  22. const config = {
  23. entry: entryFilenames,
  24. output: {
  25. path: outputPath,
  26. filename: outputFilename
  27. },
  28. target: 'node',
  29. resolve: {
  30. root: resolveRoots,
  31. extensions: ['', '.js', '.json'],
  32. alias: resolveAliases
  33. },
  34. resolveLoader: {
  35. root: resolveRoots
  36. }
  37. };
  38. if (sourceMapConfig) {
  39. // Enable webpack's source map output:
  40. config.devtool = 'source-map';
  41. config.output.sourceMapFilename = sourceMapConfig.sourceMapFilename;
  42. config.output.devtoolModuleFilenameTemplate = '[resource-path]';
  43. config.output.devtoolFallbackModuleFilenameTemplate = '[resourcePath]?[hash]';
  44. }
  45. return config;
  46. })();
  47. module.exports.plugins = (() => {
  48. const plugins = [
  49. // Returns a non-zero exit code when webpack reports an error:
  50. require('webpack-fail-plugin'),
  51. // Includes _message_keys_wrapper in every build to mimic old loader.js:
  52. new webpack.ProvidePlugin({ require: '_message_key_wrapper' })
  53. ];
  54. if (isSandbox) {
  55. // Prevents using `require('evil_loader!mymodule')` to execute custom
  56. // loader code during the webpack build.
  57. const RestrictResourcePlugin = require('restrict-resource-webpack-plugin');
  58. const plugin = new RestrictResourcePlugin(/!+/,
  59. 'Custom inline loaders are not permitted.');
  60. plugins.push(plugin);
  61. }
  62. return plugins;
  63. })();
  64. module.exports.module = {
  65. loaders: (() => {
  66. const loaders = [{'test': /\.json$$/, 'loader': 'json-loader'}];
  67. if (isSandbox) {
  68. // See restricted-resource-loader.js, prevents loading files outside
  69. // of the project folder, i.e. `require(../../not_your_business)`:
  70. const restrictLoader = {
  71. 'test': /^.*/, 'loader': 'restricted-resource-loader'
  72. };
  73. loaders.push(restrictLoader);
  74. }
  75. return loaders;
  76. })()
  77. };