configurator.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. const merge = require('deepmerge');
  2. const defaults = require('./config').loader;
  3. const utils = require('./utils');
  4. const extractTextPluginCompilerName = 'extract-text-webpack-plugin';
  5. const isomorphicSpriteModule = 'svg-sprite-loader/runtime/sprite.build';
  6. const isomorphicSymbolModule = 'svg-baker-runtime/symbol';
  7. /**
  8. * @param {Object} params
  9. * @param {Object} [params.config] Parsed loader config {@see SVGSpriteLoaderConfig}
  10. * @param {LoaderContext} context Loader context {@see https://webpack.js.org/api/loaders/#the-loader-context}
  11. * @return {Object}
  12. */
  13. module.exports = function configurator({ config, context }) {
  14. const target = context.options.target;
  15. const module = context._module;
  16. const compiler = context._compiler;
  17. const autoConfigured = {
  18. spriteModule: target === 'web' ? defaults.spriteModule : isomorphicSpriteModule,
  19. symbolModule: target === 'web' ? defaults.symbolModule : isomorphicSymbolModule,
  20. extract: utils.isModuleShouldBeExtracted(module),
  21. esModule: context.version && context.version >= 2
  22. };
  23. const finalConfig = merge.all([defaults, autoConfigured, config || {}]);
  24. /**
  25. * Fix for extract-text-webpack-plugin loader which executes module source as node module,
  26. * so export should be always in commonjs style
  27. * @see https://git.io/vS7Sn
  28. */
  29. if (compiler.name === extractTextPluginCompilerName) {
  30. finalConfig.esModule = false;
  31. }
  32. return finalConfig;
  33. };