index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = loader;
  6. var _loaderUtils = require("loader-utils");
  7. var _postcss = _interopRequireDefault(require("postcss"));
  8. var _package = _interopRequireDefault(require("postcss/package.json"));
  9. var _schemaUtils = _interopRequireDefault(require("schema-utils"));
  10. var _semver = require("semver");
  11. var _CssSyntaxError = _interopRequireDefault(require("./CssSyntaxError"));
  12. var _Warning = _interopRequireDefault(require("./Warning"));
  13. var _options = _interopRequireDefault(require("./options.json"));
  14. var _plugins = require("./plugins");
  15. var _utils = require("./utils");
  16. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  17. /*
  18. MIT License http://www.opensource.org/licenses/mit-license.php
  19. Author Tobias Koppers @sokra
  20. */
  21. async function loader(content, map, meta) {
  22. const rawOptions = (0, _loaderUtils.getOptions)(this);
  23. (0, _schemaUtils.default)(_options.default, rawOptions, {
  24. name: 'CSS Loader',
  25. baseDataPath: 'options'
  26. });
  27. const plugins = [];
  28. const callback = this.async();
  29. let options;
  30. try {
  31. options = (0, _utils.normalizeOptions)(rawOptions, this);
  32. } catch (error) {
  33. callback(error);
  34. return;
  35. }
  36. const replacements = [];
  37. const exports = [];
  38. if ((0, _utils.shouldUseModulesPlugins)(options)) {
  39. plugins.push(...(0, _utils.getModulesPlugins)(options, this));
  40. }
  41. const importPluginImports = [];
  42. const importPluginApi = [];
  43. if ((0, _utils.shouldUseImportPlugin)(options)) {
  44. const resolver = this.getResolve({
  45. conditionNames: ['style'],
  46. extensions: ['.css'],
  47. mainFields: ['css', 'style', 'main', '...'],
  48. mainFiles: ['index', '...'],
  49. restrictions: [/\.css$/i]
  50. });
  51. plugins.push((0, _plugins.importParser)({
  52. imports: importPluginImports,
  53. api: importPluginApi,
  54. context: this.context,
  55. rootContext: this.rootContext,
  56. filter: (0, _utils.getFilter)(options.import, this.resourcePath),
  57. resolver,
  58. urlHandler: url => (0, _loaderUtils.stringifyRequest)(this, (0, _utils.getPreRequester)(this)(options.importLoaders) + url)
  59. }));
  60. }
  61. const urlPluginImports = [];
  62. if ((0, _utils.shouldUseURLPlugin)(options)) {
  63. const urlResolver = this.getResolve({
  64. conditionNames: ['asset'],
  65. mainFields: ['asset'],
  66. mainFiles: [],
  67. extensions: []
  68. });
  69. plugins.push((0, _plugins.urlParser)({
  70. imports: urlPluginImports,
  71. replacements,
  72. context: this.context,
  73. rootContext: this.rootContext,
  74. filter: (0, _utils.getFilter)(options.url, this.resourcePath),
  75. resolver: urlResolver,
  76. urlHandler: url => (0, _loaderUtils.stringifyRequest)(this, url)
  77. }));
  78. }
  79. const icssPluginImports = [];
  80. const icssPluginApi = [];
  81. if ((0, _utils.shouldUseIcssPlugin)(options)) {
  82. const icssResolver = this.getResolve({
  83. conditionNames: ['style'],
  84. extensions: [],
  85. mainFields: ['css', 'style', 'main', '...'],
  86. mainFiles: ['index', '...']
  87. });
  88. plugins.push((0, _plugins.icssParser)({
  89. imports: icssPluginImports,
  90. api: icssPluginApi,
  91. replacements,
  92. exports,
  93. context: this.context,
  94. rootContext: this.rootContext,
  95. resolver: icssResolver,
  96. urlHandler: url => (0, _loaderUtils.stringifyRequest)(this, (0, _utils.getPreRequester)(this)(options.importLoaders) + url)
  97. }));
  98. } // Reuse CSS AST (PostCSS AST e.g 'postcss-loader') to avoid reparsing
  99. if (meta) {
  100. const {
  101. ast
  102. } = meta;
  103. if (ast && ast.type === 'postcss' && (0, _semver.satisfies)(ast.version, `^${_package.default.version}`)) {
  104. // eslint-disable-next-line no-param-reassign
  105. content = ast.root;
  106. }
  107. }
  108. const {
  109. resourcePath
  110. } = this;
  111. let result;
  112. try {
  113. result = await (0, _postcss.default)(plugins).process(content, {
  114. from: resourcePath,
  115. to: resourcePath,
  116. map: options.sourceMap ? {
  117. prev: map ? (0, _utils.normalizeSourceMap)(map, resourcePath) : null,
  118. inline: false,
  119. annotation: false
  120. } : false
  121. });
  122. } catch (error) {
  123. if (error.file) {
  124. this.addDependency(error.file);
  125. }
  126. callback(error.name === 'CssSyntaxError' ? new _CssSyntaxError.default(error) : error);
  127. return;
  128. }
  129. for (const warning of result.warnings()) {
  130. this.emitWarning(new _Warning.default(warning));
  131. }
  132. const imports = [].concat(icssPluginImports.sort(_utils.sort)).concat(importPluginImports.sort(_utils.sort)).concat(urlPluginImports.sort(_utils.sort));
  133. const api = [].concat(importPluginApi.sort(_utils.sort)).concat(icssPluginApi.sort(_utils.sort));
  134. if (options.modules.exportOnlyLocals !== true) {
  135. imports.unshift({
  136. importName: '___CSS_LOADER_API_IMPORT___',
  137. url: (0, _loaderUtils.stringifyRequest)(this, require.resolve('./runtime/api'))
  138. });
  139. }
  140. const importCode = (0, _utils.getImportCode)(imports, options);
  141. const moduleCode = (0, _utils.getModuleCode)(result, api, replacements, options, this);
  142. const exportCode = (0, _utils.getExportCode)(exports, replacements, options);
  143. callback(null, `${importCode}${moduleCode}${exportCode}`);
  144. }