css-loader 3.6.0 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -31,41 +31,92 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
31
31
  MIT License http://www.opensource.org/licenses/mit-license.php
32
32
  Author Tobias Koppers @sokra
33
33
  */
34
- function loader(content, map, meta) {
35
- const options = (0, _loaderUtils.getOptions)(this) || {};
36
- (0, _schemaUtils.default)(_options.default, options, {
34
+ async function loader(content, map, meta) {
35
+ const rawOptions = (0, _loaderUtils.getOptions)(this);
36
+ (0, _schemaUtils.default)(_options.default, rawOptions, {
37
37
  name: 'CSS Loader',
38
38
  baseDataPath: 'options'
39
39
  });
40
- const callback = this.async();
41
- const sourceMap = options.sourceMap || false;
42
40
  const plugins = [];
41
+ const callback = this.async();
42
+ let options;
43
43
 
44
- if ((0, _utils.shouldUseModulesPlugins)(options.modules, this.resourcePath)) {
45
- plugins.push(...(0, _utils.getModulesPlugins)(options, this));
44
+ try {
45
+ options = (0, _utils.normalizeOptions)(rawOptions, this);
46
+ } catch (error) {
47
+ callback(error);
48
+ return;
46
49
  }
47
50
 
48
- const exportType = options.onlyLocals ? 'locals' : 'full';
49
- const preRequester = (0, _utils.getPreRequester)(this);
51
+ const replacements = [];
52
+ const exports = [];
50
53
 
51
- const urlHandler = url => (0, _loaderUtils.stringifyRequest)(this, preRequester(options.importLoaders) + url);
52
-
53
- plugins.push((0, _plugins.icssParser)({
54
- urlHandler
55
- }));
54
+ if ((0, _utils.shouldUseModulesPlugins)(options)) {
55
+ plugins.push(...(0, _utils.getModulesPlugins)(options, this));
56
+ }
56
57
 
57
- if (options.import !== false && exportType === 'full') {
58
+ const importPluginImports = [];
59
+ const importPluginApi = [];
60
+
61
+ if ((0, _utils.shouldUseImportPlugin)(options)) {
62
+ const resolver = this.getResolve({
63
+ conditionNames: ['style'],
64
+ extensions: ['.css'],
65
+ mainFields: ['css', 'style', 'main', '...'],
66
+ mainFiles: ['index', '...'],
67
+ restrictions: [/\.css$/i]
68
+ });
58
69
  plugins.push((0, _plugins.importParser)({
70
+ imports: importPluginImports,
71
+ api: importPluginApi,
72
+ context: this.context,
73
+ rootContext: this.rootContext,
59
74
  filter: (0, _utils.getFilter)(options.import, this.resourcePath),
60
- urlHandler
75
+ resolver,
76
+ urlHandler: url => (0, _loaderUtils.stringifyRequest)(this, (0, _utils.getPreRequester)(this)(options.importLoaders) + url)
61
77
  }));
62
78
  }
63
79
 
64
- if (options.url !== false && exportType === 'full') {
80
+ const urlPluginImports = [];
81
+
82
+ if ((0, _utils.shouldUseURLPlugin)(options)) {
83
+ const urlResolver = this.getResolve({
84
+ conditionNames: ['asset'],
85
+ mainFields: ['asset'],
86
+ mainFiles: [],
87
+ extensions: []
88
+ });
65
89
  plugins.push((0, _plugins.urlParser)({
66
- filter: (0, _utils.getFilter)(options.url, this.resourcePath, value => (0, _loaderUtils.isUrlRequest)(value)),
90
+ imports: urlPluginImports,
91
+ replacements,
92
+ context: this.context,
93
+ rootContext: this.rootContext,
94
+ filter: (0, _utils.getFilter)(options.url, this.resourcePath),
95
+ resolver: urlResolver,
67
96
  urlHandler: url => (0, _loaderUtils.stringifyRequest)(this, url)
68
97
  }));
98
+ }
99
+
100
+ const icssPluginImports = [];
101
+ const icssPluginApi = [];
102
+
103
+ if ((0, _utils.shouldUseIcssPlugin)(options)) {
104
+ const icssResolver = this.getResolve({
105
+ conditionNames: ['style'],
106
+ extensions: [],
107
+ mainFields: ['css', 'style', 'main', '...'],
108
+ mainFiles: ['index', '...']
109
+ });
110
+ plugins.push((0, _plugins.icssParser)({
111
+ imports: icssPluginImports,
112
+ api: icssPluginApi,
113
+ replacements,
114
+ exports,
115
+ context: this.context,
116
+ rootContext: this.rootContext,
117
+ resolver: icssResolver,
118
+ urlHandler: url => (0, _loaderUtils.stringifyRequest)(this, (0, _utils.getPreRequester)(this)(options.importLoaders) + url)
119
+ }));
69
120
  } // Reuse CSS AST (PostCSS AST e.g 'postcss-loader') to avoid reparsing
70
121
 
71
122
 
@@ -80,64 +131,44 @@ function loader(content, map, meta) {
80
131
  }
81
132
  }
82
133
 
83
- (0, _postcss.default)(plugins).process(content, {
84
- from: this.resourcePath,
85
- to: this.resourcePath,
86
- map: options.sourceMap ? {
87
- // Some loaders (example `"postcss-loader": "1.x.x"`) always generates source map, we should remove it
88
- prev: sourceMap && map ? (0, _utils.normalizeSourceMap)(map) : null,
89
- inline: false,
90
- annotation: false
91
- } : false
92
- }).then(result => {
93
- for (const warning of result.warnings()) {
94
- this.emitWarning(new _Warning.default(warning));
95
- }
96
-
97
- const imports = [];
98
- const apiImports = [];
99
- const urlReplacements = [];
100
- const icssReplacements = [];
101
- const exports = [];
102
-
103
- for (const message of result.messages) {
104
- // eslint-disable-next-line default-case
105
- switch (message.type) {
106
- case 'import':
107
- imports.push(message.value);
108
- break;
109
-
110
- case 'api-import':
111
- apiImports.push(message.value);
112
- break;
113
-
114
- case 'url-replacement':
115
- urlReplacements.push(message.value);
116
- break;
117
-
118
- case 'icss-replacement':
119
- icssReplacements.push(message.value);
120
- break;
121
-
122
- case 'export':
123
- exports.push(message.value);
124
- break;
125
- }
126
- }
127
-
128
- const {
129
- localsConvention
130
- } = options;
131
- const esModule = typeof options.esModule !== 'undefined' ? options.esModule : false;
132
- const importCode = (0, _utils.getImportCode)(this, exportType, imports, esModule);
133
- const moduleCode = (0, _utils.getModuleCode)(result, exportType, sourceMap, apiImports, urlReplacements, icssReplacements, esModule);
134
- const exportCode = (0, _utils.getExportCode)(exports, exportType, localsConvention, icssReplacements, esModule);
135
- return callback(null, `${importCode}${moduleCode}${exportCode}`);
136
- }).catch(error => {
134
+ let result;
135
+
136
+ try {
137
+ result = await (0, _postcss.default)(plugins).process(content, {
138
+ from: this.resourcePath,
139
+ to: this.resourcePath,
140
+ map: options.sourceMap ? {
141
+ // Some loaders (example `"postcss-loader": "1.x.x"`) always generates source map, we should remove it
142
+ prev: map ? (0, _utils.normalizeSourceMap)(map) : null,
143
+ inline: false,
144
+ annotation: false
145
+ } : false
146
+ });
147
+ } catch (error) {
137
148
  if (error.file) {
138
149
  this.addDependency(error.file);
139
150
  }
140
151
 
141
152
  callback(error.name === 'CssSyntaxError' ? new _CssSyntaxError.default(error) : error);
142
- });
153
+ return;
154
+ }
155
+
156
+ for (const warning of result.warnings()) {
157
+ this.emitWarning(new _Warning.default(warning));
158
+ }
159
+
160
+ const imports = [].concat(icssPluginImports.sort(_utils.sort)).concat(importPluginImports.sort(_utils.sort)).concat(urlPluginImports.sort(_utils.sort));
161
+ const api = [].concat(importPluginApi.sort(_utils.sort)).concat(icssPluginApi.sort(_utils.sort));
162
+
163
+ if (options.modules.exportOnlyLocals !== true) {
164
+ imports.unshift({
165
+ importName: '___CSS_LOADER_API_IMPORT___',
166
+ url: (0, _loaderUtils.stringifyRequest)(this, require.resolve('./runtime/api'))
167
+ });
168
+ }
169
+
170
+ const importCode = (0, _utils.getImportCode)(imports, options);
171
+ const moduleCode = (0, _utils.getModuleCode)(result, api, replacements, options);
172
+ const exportCode = (0, _utils.getExportCode)(exports, replacements, options);
173
+ callback(null, `${importCode}${moduleCode}${exportCode}`);
143
174
  }
package/dist/options.json CHANGED
@@ -36,7 +36,12 @@
36
36
  "type": "object",
37
37
  "additionalProperties": false,
38
38
  "properties": {
39
+ "compileType": {
40
+ "description": "Controls the extent to which css-loader will process module code (https://github.com/webpack-contrib/css-loader#type)",
41
+ "enum": ["module", "icss"]
42
+ },
39
43
  "auto": {
44
+ "description": "Allows auto enable CSS modules based on filename (https://github.com/webpack-contrib/css-loader#auto).",
40
45
  "anyOf": [
41
46
  {
42
47
  "instanceof": "RegExp"
@@ -50,6 +55,7 @@
50
55
  ]
51
56
  },
52
57
  "mode": {
58
+ "description": "Setup `mode` option (https://github.com/webpack-contrib/css-loader#mode).",
53
59
  "anyOf": [
54
60
  {
55
61
  "enum": ["local", "global", "pure"]
@@ -59,42 +65,67 @@
59
65
  }
60
66
  ]
61
67
  },
62
- "exportGlobals": {
63
- "type": "boolean"
64
- },
65
68
  "localIdentName": {
66
- "type": "string"
69
+ "description": "Allows to configure the generated local ident name (https://github.com/webpack-contrib/css-loader#localidentname).",
70
+ "type": "string",
71
+ "minLength": 1
72
+ },
73
+ "localIdentContext": {
74
+ "description": "Allows to redefine basic loader context for local ident name (https://github.com/webpack-contrib/css-loader#localidentcontext).",
75
+ "type": "string",
76
+ "minLength": 1
77
+ },
78
+ "localIdentHashPrefix": {
79
+ "description": "Allows to add custom hash to generate more unique classes (https://github.com/webpack-contrib/css-loader#localidenthashprefix).",
80
+ "type": "string",
81
+ "minLength": 1
67
82
  },
68
83
  "localIdentRegExp": {
84
+ "description": "Allows to specify custom RegExp for local ident name (https://github.com/webpack-contrib/css-loader#localidentregexp).",
69
85
  "anyOf": [
70
86
  {
71
- "type": "string"
87
+ "type": "string",
88
+ "minLength": 1
72
89
  },
73
90
  {
74
91
  "instanceof": "RegExp"
75
92
  }
76
93
  ]
77
94
  },
78
- "context": {
79
- "type": "string"
95
+ "getLocalIdent": {
96
+ "description": "Allows to specify a function to generate the classname (https://github.com/webpack-contrib/css-loader#getlocalident).",
97
+ "instanceof": "Function"
80
98
  },
81
- "hashPrefix": {
82
- "type": "string"
99
+ "namedExport": {
100
+ "description": "Enables/disables ES modules named export for locals (https://github.com/webpack-contrib/css-loader#namedexport).",
101
+ "type": "boolean"
83
102
  },
84
- "getLocalIdent": {
85
- "anyOf": [
86
- {
87
- "type": "boolean"
88
- },
89
- {
90
- "instanceof": "Function"
91
- }
103
+ "exportGlobals": {
104
+ "description": "Allows to export names from global class or id, so you can use that as local name (https://github.com/webpack-contrib/css-loader#exportglobals).",
105
+ "type": "boolean"
106
+ },
107
+ "exportLocalsConvention": {
108
+ "description": "Style of exported classnames (https://github.com/webpack-contrib/css-loader#localsconvention).",
109
+ "enum": [
110
+ "asIs",
111
+ "camelCase",
112
+ "camelCaseOnly",
113
+ "dashes",
114
+ "dashesOnly"
92
115
  ]
116
+ },
117
+ "exportOnlyLocals": {
118
+ "description": "Export only locals (https://github.com/webpack-contrib/css-loader#exportonlylocals).",
119
+ "type": "boolean"
93
120
  }
94
121
  }
95
122
  }
96
123
  ]
97
124
  },
125
+ "icss": {
126
+ "description": "Enables/Disables handling the CSS module interoperable import/export format ((https://github.com/webpack-contrib/css-loader#icss)",
127
+ "type": "boolean"
128
+ },
98
129
  "sourceMap": {
99
130
  "description": "Enables/Disables generation of source maps (https://github.com/webpack-contrib/css-loader#sourcemap).",
100
131
  "type": "boolean"
@@ -110,14 +141,6 @@
110
141
  }
111
142
  ]
112
143
  },
113
- "localsConvention": {
114
- "description": "Style of exported classnames (https://github.com/webpack-contrib/css-loader#localsconvention).",
115
- "enum": ["asIs", "camelCase", "camelCaseOnly", "dashes", "dashesOnly"]
116
- },
117
- "onlyLocals": {
118
- "description": "Export only locals (https://github.com/webpack-contrib/css-loader#onlylocals).",
119
- "type": "boolean"
120
- },
121
144
  "esModule": {
122
145
  "description": "Use the ES modules syntax (https://github.com/webpack-contrib/css-loader#esmodule).",
123
146
  "type": "boolean"
@@ -9,70 +9,90 @@ var _postcss = _interopRequireDefault(require("postcss"));
9
9
 
10
10
  var _icssUtils = require("icss-utils");
11
11
 
12
- var _loaderUtils = require("loader-utils");
12
+ var _utils = require("../utils");
13
13
 
14
14
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
15
 
16
- function makeRequestableIcssImports(icssImports) {
17
- return Object.keys(icssImports).reduce((accumulator, url) => {
18
- const tokensMap = icssImports[url];
19
- const tokens = Object.keys(tokensMap);
16
+ var _default = _postcss.default.plugin('postcss-icss-parser', options => async css => {
17
+ const importReplacements = Object.create(null);
18
+ const {
19
+ icssImports,
20
+ icssExports
21
+ } = (0, _icssUtils.extractICSS)(css);
22
+ const imports = new Map();
23
+ const tasks = []; // eslint-disable-next-line guard-for-in
20
24
 
21
- if (tokens.length === 0) {
22
- return accumulator;
25
+ for (const url in icssImports) {
26
+ const tokens = icssImports[url];
27
+
28
+ if (Object.keys(tokens).length === 0) {
29
+ // eslint-disable-next-line no-continue
30
+ continue;
23
31
  }
24
32
 
25
- const normalizedUrl = (0, _loaderUtils.urlToRequest)(url);
33
+ let normalizedUrl = url;
34
+ let prefix = '';
35
+ const queryParts = normalizedUrl.split('!');
26
36
 
27
- if (!accumulator[normalizedUrl]) {
28
- // eslint-disable-next-line no-param-reassign
29
- accumulator[normalizedUrl] = tokensMap;
30
- } else {
31
- // eslint-disable-next-line no-param-reassign
32
- accumulator[normalizedUrl] = { ...accumulator[normalizedUrl],
33
- ...tokensMap
34
- };
37
+ if (queryParts.length > 1) {
38
+ normalizedUrl = queryParts.pop();
39
+ prefix = queryParts.join('!');
35
40
  }
36
41
 
37
- return accumulator;
38
- }, {});
39
- }
42
+ const request = (0, _utils.requestify)((0, _utils.normalizeUrl)(normalizedUrl, true), options.rootContext);
43
+
44
+ const doResolve = async () => {
45
+ const {
46
+ resolver,
47
+ context
48
+ } = options;
49
+ const resolvedUrl = await (0, _utils.resolveRequests)(resolver, context, [...new Set([normalizedUrl, request])]);
50
+ return {
51
+ url: resolvedUrl,
52
+ prefix,
53
+ tokens
54
+ };
55
+ };
40
56
 
41
- var _default = _postcss.default.plugin('postcss-icss-parser', options => (css, result) => {
42
- const importReplacements = Object.create(null);
43
- const extractedICSS = (0, _icssUtils.extractICSS)(css);
44
- const icssImports = makeRequestableIcssImports(extractedICSS.icssImports);
45
-
46
- for (const [importIndex, url] of Object.keys(icssImports).entries()) {
47
- const importName = `___CSS_LOADER_ICSS_IMPORT_${importIndex}___`;
48
- result.messages.push({
49
- type: 'import',
50
- value: {
57
+ tasks.push(doResolve());
58
+ }
59
+
60
+ const results = await Promise.all(tasks);
61
+
62
+ for (let index = 0; index <= results.length - 1; index++) {
63
+ const {
64
+ url,
65
+ prefix,
66
+ tokens
67
+ } = results[index];
68
+ const newUrl = prefix ? `${prefix}!${url}` : url;
69
+ const importKey = newUrl;
70
+ let importName = imports.get(importKey);
71
+
72
+ if (!importName) {
73
+ importName = `___CSS_LOADER_ICSS_IMPORT_${imports.size}___`;
74
+ imports.set(importKey, importName);
75
+ options.imports.push({
51
76
  importName,
52
- url: options.urlHandler ? options.urlHandler(url) : url
53
- }
54
- }, {
55
- type: 'api-import',
56
- value: {
57
- type: 'internal',
77
+ url: options.urlHandler(newUrl),
78
+ icss: true,
79
+ index
80
+ });
81
+ options.api.push({
58
82
  importName,
59
- dedupe: true
60
- }
61
- });
62
- const tokenMap = icssImports[url];
63
- const tokens = Object.keys(tokenMap);
83
+ dedupe: true,
84
+ index
85
+ });
86
+ }
64
87
 
65
- for (const [replacementIndex, token] of tokens.entries()) {
66
- const replacementName = `___CSS_LOADER_ICSS_IMPORT_${importIndex}_REPLACEMENT_${replacementIndex}___`;
67
- const localName = tokenMap[token];
88
+ for (const [replacementIndex, token] of Object.keys(tokens).entries()) {
89
+ const replacementName = `___CSS_LOADER_ICSS_IMPORT_${index}_REPLACEMENT_${replacementIndex}___`;
90
+ const localName = tokens[token];
68
91
  importReplacements[token] = replacementName;
69
- result.messages.push({
70
- type: 'icss-replacement',
71
- value: {
72
- replacementName,
73
- importName,
74
- localName
75
- }
92
+ options.replacements.push({
93
+ replacementName,
94
+ importName,
95
+ localName
76
96
  });
77
97
  }
78
98
  }
@@ -81,18 +101,11 @@ var _default = _postcss.default.plugin('postcss-icss-parser', options => (css, r
81
101
  (0, _icssUtils.replaceSymbols)(css, importReplacements);
82
102
  }
83
103
 
84
- const {
85
- icssExports
86
- } = extractedICSS;
87
-
88
104
  for (const name of Object.keys(icssExports)) {
89
105
  const value = (0, _icssUtils.replaceValueSymbols)(icssExports[name], importReplacements);
90
- result.messages.push({
91
- type: 'export',
92
- value: {
93
- name,
94
- value
95
- }
106
+ options.exports.push({
107
+ name,
108
+ value
96
109
  });
97
110
  }
98
111
  });