configurator.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. const merge = require('deepmerge');
  2. const defaults = require('./config');
  3. const utils = require('./utils');
  4. const loaderDefaults = defaults.loader;
  5. const isomorphicSpriteModule = 'svg-sprite-loader/runtime/sprite.build';
  6. const isomorphicSymbolModule = 'svg-baker-runtime/symbol';
  7. const isTargetBrowser = target => target === 'web' || target === 'electron-renderer';
  8. /**
  9. * @param {Object} params
  10. * @param {Object} [params.config] Parsed loader config {@see SVGSpriteLoaderConfig}
  11. * @param {LoaderContext} context Loader context {@see https://webpack.js.org/api/loaders/#the-loader-context}
  12. * @return {Object}
  13. */
  14. module.exports = function configurator({ config, context, target }) {
  15. const module = context._module;
  16. const compiler = context._compiler;
  17. const compilerName = compiler.name;
  18. const autoConfigured = {
  19. spriteModule: isTargetBrowser(target) ? loaderDefaults.spriteModule : isomorphicSpriteModule,
  20. symbolModule: isTargetBrowser(target) ? loaderDefaults.symbolModule : isomorphicSymbolModule,
  21. extract: utils.isModuleShouldBeExtracted(module),
  22. esModule: context.version && context.version >= 2
  23. };
  24. const finalConfig = merge.all([loaderDefaults, autoConfigured, config || {}]);
  25. /**
  26. * esModule should be `false` when compiles via extract-text-webpack-plugin or html-webpack-plugin.
  27. * Because this compilers executes module as usual node module so export should be always in commonjs style.
  28. * This could be dropped when Node.js will support ES modules natively :)
  29. * @see https://git.io/vS7Sn
  30. * @see https://git.io/v9w60
  31. */
  32. if (compilerName && (
  33. compilerName.includes('extract-text-webpack-plugin') ||
  34. compilerName.includes('html-webpack-plugin')
  35. )) {
  36. finalConfig.esModule = false;
  37. }
  38. return finalConfig;
  39. };