css-loader 1.0.1 → 2.1.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.
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ class CssSyntaxError extends Error {
9
+ constructor(error) {
10
+ super(error);
11
+ const {
12
+ reason,
13
+ line,
14
+ column
15
+ } = error;
16
+ this.name = 'CssSyntaxError'; // Based on https://github.com/postcss/postcss/blob/master/lib/css-syntax-error.es6#L132
17
+ // We don't need `plugin` and `file` properties.
18
+
19
+ this.message = `${this.name}\n\n`;
20
+
21
+ if (typeof line !== 'undefined') {
22
+ this.message += `(${line}:${column}) `;
23
+ }
24
+
25
+ this.message += `${reason}`;
26
+ const code = error.showSourceCode();
27
+
28
+ if (code) {
29
+ this.message += `\n\n${code}\n`;
30
+ } // We don't need stack https://github.com/postcss/postcss/blob/master/docs/guidelines/runner.md#31-dont-show-js-stack-for-csssyntaxerror
31
+
32
+
33
+ this.stack = false;
34
+ }
35
+
36
+ }
37
+
38
+ exports.default = CssSyntaxError;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ class Warning extends Error {
9
+ constructor(warning) {
10
+ super(warning);
11
+ const {
12
+ text,
13
+ line,
14
+ column
15
+ } = warning;
16
+ this.name = 'Warning'; // Based on https://github.com/postcss/postcss/blob/master/lib/warning.es6#L74
17
+ // We don't need `plugin` properties.
18
+
19
+ this.message = `${this.name}\n\n`;
20
+
21
+ if (typeof line !== 'undefined') {
22
+ this.message += `(${line}:${column}) `;
23
+ }
24
+
25
+ this.message += `${text}`; // We don't need stack https://github.com/postcss/postcss/blob/master/docs/guidelines/runner.md#31-dont-show-js-stack-for-csssyntaxerror
26
+
27
+ this.stack = false;
28
+ }
29
+
30
+ }
31
+
32
+ exports.default = Warning;
package/dist/cjs.js ADDED
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+
3
+ const loader = require('./index');
4
+
5
+ module.exports = loader.default;
package/dist/index.js ADDED
@@ -0,0 +1,266 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = loader;
7
+
8
+ var _schemaUtils = _interopRequireDefault(require("schema-utils"));
9
+
10
+ var _postcss = _interopRequireDefault(require("postcss"));
11
+
12
+ var _package = _interopRequireDefault(require("postcss/package.json"));
13
+
14
+ var _postcssModulesLocalByDefault = _interopRequireDefault(require("postcss-modules-local-by-default"));
15
+
16
+ var _postcssModulesExtractImports = _interopRequireDefault(require("postcss-modules-extract-imports"));
17
+
18
+ var _postcssModulesScope = _interopRequireDefault(require("postcss-modules-scope"));
19
+
20
+ var _postcssModulesValues = _interopRequireDefault(require("postcss-modules-values"));
21
+
22
+ var _loaderUtils = require("loader-utils");
23
+
24
+ var _camelCase = _interopRequireDefault(require("lodash/camelCase"));
25
+
26
+ var _options = _interopRequireDefault(require("./options.json"));
27
+
28
+ var _plugins = require("./plugins");
29
+
30
+ var _utils = require("./utils");
31
+
32
+ var _Warning = _interopRequireDefault(require("./Warning"));
33
+
34
+ var _CssSyntaxError = _interopRequireDefault(require("./CssSyntaxError"));
35
+
36
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
37
+
38
+ /*
39
+ MIT License http://www.opensource.org/licenses/mit-license.php
40
+ Author Tobias Koppers @sokra
41
+ */
42
+ function loader(content, map, meta) {
43
+ const options = (0, _loaderUtils.getOptions)(this) || {};
44
+ (0, _schemaUtils.default)(_options.default, options, 'CSS Loader');
45
+ const callback = this.async();
46
+ const sourceMap = options.sourceMap || false;
47
+ /* eslint-disable no-param-reassign */
48
+
49
+ if (sourceMap) {
50
+ if (map) {
51
+ if (typeof map === 'string') {
52
+ map = JSON.stringify(map);
53
+ }
54
+
55
+ if (map.sources) {
56
+ map.sources = map.sources.map(source => source.replace(/\\/g, '/'));
57
+ map.sourceRoot = '';
58
+ }
59
+ }
60
+ } else {
61
+ // Some loaders (example `"postcss-loader": "1.x.x"`) always generates source map, we should remove it
62
+ map = null;
63
+ }
64
+ /* eslint-enable no-param-reassign */
65
+ // Reuse CSS AST (PostCSS AST e.g 'postcss-loader') to avoid reparsing
66
+
67
+
68
+ if (meta) {
69
+ const {
70
+ ast
71
+ } = meta;
72
+
73
+ if (ast && ast.type === 'postcss' && ast.version === _package.default.version) {
74
+ // eslint-disable-next-line no-param-reassign
75
+ content = ast.root;
76
+ }
77
+ }
78
+
79
+ const plugins = [];
80
+
81
+ if (options.modules) {
82
+ const loaderContext = this;
83
+ const mode = typeof options.modules === 'boolean' ? 'local' : options.modules;
84
+ plugins.push(_postcssModulesValues.default, (0, _postcssModulesLocalByDefault.default)({
85
+ mode
86
+ }), (0, _postcssModulesExtractImports.default)(), (0, _postcssModulesScope.default)({
87
+ generateScopedName: function generateScopedName(exportName) {
88
+ const localIdentName = options.localIdentName || '[hash:base64]';
89
+ const customGetLocalIdent = options.getLocalIdent || _utils.getLocalIdent;
90
+ return customGetLocalIdent(loaderContext, localIdentName, exportName, {
91
+ regExp: options.localIdentRegExp,
92
+ hashPrefix: options.hashPrefix || '',
93
+ context: options.context
94
+ });
95
+ }
96
+ }));
97
+ }
98
+
99
+ if (options.import !== false) {
100
+ plugins.push((0, _plugins.importParser)({
101
+ filter: (0, _utils.getFilter)(options.import, this.resourcePath)
102
+ }));
103
+ }
104
+
105
+ if (options.url !== false) {
106
+ plugins.push((0, _plugins.urlParser)({
107
+ filter: (0, _utils.getFilter)(options.url, this.resourcePath, value => (0, _loaderUtils.isUrlRequest)(value))
108
+ }));
109
+ }
110
+
111
+ plugins.push((0, _plugins.icssParser)());
112
+ (0, _postcss.default)(plugins).process(content, {
113
+ // we need a prefix to avoid path rewriting of PostCSS
114
+ from: `/css-loader!${(0, _loaderUtils.getRemainingRequest)(this).split('!').pop()}`,
115
+ to: (0, _loaderUtils.getCurrentRequest)(this).split('!').pop(),
116
+ map: options.sourceMap ? {
117
+ prev: map,
118
+ sourcesContent: true,
119
+ inline: false,
120
+ annotation: false
121
+ } : null
122
+ }).then(result => {
123
+ result.warnings().forEach(warning => this.emitWarning(new _Warning.default(warning)));
124
+ const messages = result.messages || []; // Run other loader (`postcss-loader`, `sass-loader` and etc) for importing CSS
125
+
126
+ const importUrlPrefix = (0, _utils.getImportPrefix)(this, options.importLoaders); // Prepare replacer to change from `___CSS_LOADER_IMPORT___INDEX___` to `require('./file.css').locals`
127
+
128
+ const importItemReplacer = placeholder => {
129
+ const match = _utils.placholderRegExps.importItem.exec(placeholder);
130
+
131
+ const idx = Number(match[1]);
132
+ const message = messages.find( // eslint-disable-next-line no-shadow
133
+ message => message.type === 'icss-import' && message.item && message.item.index === idx);
134
+
135
+ if (!message) {
136
+ return placeholder;
137
+ }
138
+
139
+ const {
140
+ item
141
+ } = message;
142
+ const importUrl = importUrlPrefix + (0, _loaderUtils.urlToRequest)(item.url);
143
+
144
+ if (options.exportOnlyLocals) {
145
+ return `" + require(${(0, _loaderUtils.stringifyRequest)(this, importUrl)})[${JSON.stringify(item.export)}] + "`;
146
+ }
147
+
148
+ return `" + require(${(0, _loaderUtils.stringifyRequest)(this, importUrl)}).locals[${JSON.stringify(item.export)}] + "`;
149
+ };
150
+
151
+ const exports = messages.filter(message => message.type === 'export').reduce((accumulator, message) => {
152
+ const {
153
+ key,
154
+ value
155
+ } = message.item;
156
+ let valueAsString = JSON.stringify(value);
157
+ valueAsString = valueAsString.replace(_utils.placholderRegExps.importItemG, importItemReplacer);
158
+
159
+ function addEntry(k) {
160
+ accumulator.push(`\t${JSON.stringify(k)}: ${valueAsString}`);
161
+ }
162
+
163
+ let targetKey;
164
+
165
+ switch (options.camelCase) {
166
+ case true:
167
+ addEntry(key);
168
+ targetKey = (0, _camelCase.default)(key);
169
+
170
+ if (targetKey !== key) {
171
+ addEntry(targetKey);
172
+ }
173
+
174
+ break;
175
+
176
+ case 'dashes':
177
+ addEntry(key);
178
+ targetKey = (0, _utils.dashesCamelCase)(key);
179
+
180
+ if (targetKey !== key) {
181
+ addEntry(targetKey);
182
+ }
183
+
184
+ break;
185
+
186
+ case 'only':
187
+ addEntry((0, _camelCase.default)(key));
188
+ break;
189
+
190
+ case 'dashesOnly':
191
+ addEntry((0, _utils.dashesCamelCase)(key));
192
+ break;
193
+
194
+ default:
195
+ addEntry(key);
196
+ break;
197
+ }
198
+
199
+ return accumulator;
200
+ }, []);
201
+
202
+ if (options.exportOnlyLocals) {
203
+ return callback(null, exports.length > 0 ? `module.exports = {\n${exports.join(',\n')}\n};` : '');
204
+ }
205
+
206
+ const imports = messages.filter(message => message.type === 'import').map(message => {
207
+ const {
208
+ url
209
+ } = message.item;
210
+ const media = message.item.media || '';
211
+
212
+ if (!(0, _loaderUtils.isUrlRequest)(url)) {
213
+ return `exports.push([module.id, ${JSON.stringify(`@import url(${url});`)}, ${JSON.stringify(media)}]);`;
214
+ }
215
+
216
+ const importUrl = importUrlPrefix + (0, _loaderUtils.urlToRequest)(url);
217
+ return `exports.i(require(${(0, _loaderUtils.stringifyRequest)(this, importUrl)}), ${JSON.stringify(media)});`;
218
+ }, this);
219
+ let cssAsString = JSON.stringify(result.css).replace(_utils.placholderRegExps.importItemG, importItemReplacer); // Helper for ensuring valid CSS strings from requires
220
+
221
+ let hasUrlEscapeHelper = false;
222
+ messages.filter(message => message.type === 'url').forEach(message => {
223
+ if (!hasUrlEscapeHelper) {
224
+ imports.push(`var urlEscape = require(${(0, _loaderUtils.stringifyRequest)(this, require.resolve('./runtime/url-escape.js'))});`);
225
+ hasUrlEscapeHelper = true;
226
+ }
227
+
228
+ const {
229
+ item
230
+ } = message;
231
+ const {
232
+ url,
233
+ placeholder,
234
+ needQuotes
235
+ } = item; // Remove `#hash` and `?#hash` from `require`
236
+
237
+ const [normalizedUrl, singleQuery, hashValue] = url.split(/(\?)?#/);
238
+ const hash = singleQuery || hashValue ? `"${singleQuery ? '?' : ''}${hashValue ? `#${hashValue}` : ''}"` : '';
239
+ imports.push(`var ${placeholder} = urlEscape(require(${(0, _loaderUtils.stringifyRequest)(this, (0, _loaderUtils.urlToRequest)(normalizedUrl))})${hash ? ` + ${hash}` : ''}${needQuotes ? ', true' : ''});`);
240
+ cssAsString = cssAsString.replace(new RegExp(placeholder, 'g'), () => `" + ${placeholder} + "`);
241
+ });
242
+ let newMap = result.map;
243
+
244
+ if (sourceMap && newMap) {
245
+ // Add a SourceMap
246
+ newMap = newMap.toJSON();
247
+
248
+ if (newMap.sources) {
249
+ newMap.sources = newMap.sources.map(source => source.split('!').pop().replace(/\\/g, '/'), this);
250
+ newMap.sourceRoot = '';
251
+ }
252
+
253
+ newMap.file = newMap.file.split('!').pop().replace(/\\/g, '/');
254
+ newMap = JSON.stringify(newMap);
255
+ }
256
+
257
+ const runtimeCode = `exports = module.exports = require(${(0, _loaderUtils.stringifyRequest)(this, require.resolve('./runtime/api'))})(${!!sourceMap});\n`;
258
+ const importCode = imports.length > 0 ? `// Imports\n${imports.join('\n')}\n\n` : '';
259
+ const moduleCode = `// Module\nexports.push([module.id, ${cssAsString}, ""${newMap ? `,${newMap}` : ''}]);\n\n`;
260
+ const exportsCode = exports.length > 0 ? `// Exports\nexports.locals = {\n${exports.join(',\n')}\n};` : ''; // Embed runtime
261
+
262
+ return callback(null, runtimeCode + importCode + moduleCode + exportsCode);
263
+ }).catch(error => {
264
+ callback(error.name === 'CssSyntaxError' ? new _CssSyntaxError.default(error) : error);
265
+ });
266
+ }
@@ -0,0 +1,93 @@
1
+ {
2
+ "additionalProperties": false,
3
+ "properties": {
4
+ "url": {
5
+ "anyOf": [
6
+ {
7
+ "type": "boolean"
8
+ },
9
+ {
10
+ "instanceof": "Function"
11
+ }
12
+ ]
13
+ },
14
+ "import": {
15
+ "anyOf": [
16
+ {
17
+ "type": "boolean"
18
+ },
19
+ {
20
+ "instanceof": "Function"
21
+ }
22
+ ]
23
+ },
24
+ "modules": {
25
+ "anyOf": [
26
+ {
27
+ "type": "boolean"
28
+ },
29
+ {
30
+ "type": "string",
31
+ "enum": ["local", "global"]
32
+ }
33
+ ]
34
+ },
35
+ "localIdentName": {
36
+ "type": "string"
37
+ },
38
+ "localIdentRegExp": {
39
+ "anyOf": [
40
+ {
41
+ "type": "string"
42
+ },
43
+ {
44
+ "instanceof": "RegExp"
45
+ }
46
+ ]
47
+ },
48
+ "context": {
49
+ "type": "string"
50
+ },
51
+ "hashPrefix": {
52
+ "type": "string"
53
+ },
54
+ "getLocalIdent": {
55
+ "anyOf": [
56
+ {
57
+ "type": "boolean"
58
+ },
59
+ {
60
+ "instanceof": "Function"
61
+ }
62
+ ]
63
+ },
64
+ "sourceMap": {
65
+ "type": "boolean"
66
+ },
67
+ "camelCase": {
68
+ "anyOf": [
69
+ {
70
+ "type": "boolean"
71
+ },
72
+ {
73
+ "type": "string",
74
+ "enum": ["dashes", "only", "dashesOnly"]
75
+ }
76
+ ]
77
+ },
78
+ "importLoaders": {
79
+ "anyOf": [
80
+ {
81
+ "type": "boolean"
82
+ },
83
+ {
84
+ "type": "number"
85
+ }
86
+ ]
87
+ },
88
+ "exportOnlyLocals": {
89
+ "type": "boolean"
90
+ }
91
+ },
92
+ "type": "object"
93
+ }
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "importParser", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _postcssImportParser.default;
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "icssParser", {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _postcssIcssParser.default;
16
+ }
17
+ });
18
+ Object.defineProperty(exports, "urlParser", {
19
+ enumerable: true,
20
+ get: function () {
21
+ return _postcssUrlParser.default;
22
+ }
23
+ });
24
+
25
+ var _postcssImportParser = _interopRequireDefault(require("./postcss-import-parser"));
26
+
27
+ var _postcssIcssParser = _interopRequireDefault(require("./postcss-icss-parser"));
28
+
29
+ var _postcssUrlParser = _interopRequireDefault(require("./postcss-url-parser"));
30
+
31
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _postcss = _interopRequireDefault(require("postcss"));
9
+
10
+ var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
11
+
12
+ var _icssUtils = require("icss-utils");
13
+
14
+ var _loaderUtils = _interopRequireDefault(require("loader-utils"));
15
+
16
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
+
18
+ const pluginName = 'postcss-icss-parser';
19
+
20
+ var _default = _postcss.default.plugin(pluginName, () => function process(css, result) {
21
+ const imports = {};
22
+ const icss = (0, _icssUtils.extractICSS)(css);
23
+ const exports = icss.icssExports;
24
+ Object.keys(icss.icssImports).forEach(key => {
25
+ const url = _loaderUtils.default.parseString(key);
26
+
27
+ Object.keys(icss.icssImports[key]).forEach(prop => {
28
+ const index = Object.keys(imports).length;
29
+ imports[`$${prop}`] = index;
30
+ result.messages.push({
31
+ pluginName,
32
+ type: 'icss-import',
33
+ item: {
34
+ url,
35
+ export: icss.icssImports[key][prop],
36
+ index
37
+ }
38
+ });
39
+ const alreadyIncluded = result.messages.find(message => message.pluginName === pluginName && message.type === 'import' && message.item.url === url && message.item.media === '');
40
+
41
+ if (alreadyIncluded) {
42
+ return;
43
+ }
44
+
45
+ result.messages.push({
46
+ pluginName,
47
+ type: 'import',
48
+ item: {
49
+ url,
50
+ media: ''
51
+ }
52
+ });
53
+ });
54
+ });
55
+
56
+ function replaceImportsInString(str) {
57
+ const tokens = (0, _postcssValueParser.default)(str);
58
+ tokens.walk(node => {
59
+ if (node.type !== 'word') {
60
+ return;
61
+ }
62
+
63
+ const token = node.value;
64
+ const importIndex = imports[`$${token}`];
65
+
66
+ if (typeof importIndex === 'number') {
67
+ // eslint-disable-next-line no-param-reassign
68
+ node.value = `___CSS_LOADER_IMPORT___${importIndex}___`;
69
+ }
70
+ });
71
+ return tokens.toString();
72
+ } // Replace tokens in declarations
73
+
74
+
75
+ css.walkDecls(decl => {
76
+ // eslint-disable-next-line no-param-reassign
77
+ decl.value = replaceImportsInString(decl.value.toString());
78
+ }); // Replace tokens in at-rules
79
+
80
+ css.walkAtRules(atrule => {
81
+ // Due reusing `ast` from `postcss-loader` some plugins may lack
82
+ // `params` property, we need to account for this possibility
83
+ if (atrule.params) {
84
+ // eslint-disable-next-line no-param-reassign
85
+ atrule.params = replaceImportsInString(atrule.params.toString());
86
+ }
87
+ }); // Replace tokens in export
88
+
89
+ Object.keys(exports).forEach(exportName => {
90
+ result.messages.push({
91
+ pluginName,
92
+ type: 'export',
93
+ item: {
94
+ key: exportName,
95
+ value: replaceImportsInString(exports[exportName])
96
+ }
97
+ });
98
+ });
99
+ });
100
+
101
+ exports.default = _default;
@@ -0,0 +1,110 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _postcss = _interopRequireDefault(require("postcss"));
9
+
10
+ var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
11
+
12
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
+
14
+ const pluginName = 'postcss-import-parser';
15
+
16
+ function getArg(nodes) {
17
+ return nodes.length !== 0 && nodes[0].type === 'string' ? nodes[0].value : _postcssValueParser.default.stringify(nodes);
18
+ }
19
+
20
+ function getUrl(node) {
21
+ if (node.type === 'function' && node.value.toLowerCase() === 'url') {
22
+ return getArg(node.nodes);
23
+ }
24
+
25
+ if (node.type === 'string') {
26
+ return node.value;
27
+ }
28
+
29
+ return null;
30
+ }
31
+
32
+ function parseImport(params) {
33
+ const {
34
+ nodes
35
+ } = (0, _postcssValueParser.default)(params);
36
+
37
+ if (nodes.length === 0) {
38
+ return null;
39
+ }
40
+
41
+ const url = getUrl(nodes[0]);
42
+
43
+ if (!url || url.trim().length === 0) {
44
+ return null;
45
+ }
46
+
47
+ return {
48
+ url,
49
+ media: _postcssValueParser.default.stringify(nodes.slice(1)).trim().toLowerCase()
50
+ };
51
+ }
52
+
53
+ function walkAtRules(css, result, filter) {
54
+ const items = [];
55
+ css.walkAtRules(/^import$/i, atRule => {
56
+ // Convert only top-level @import
57
+ if (atRule.parent.type !== 'root') {
58
+ return;
59
+ }
60
+
61
+ if (atRule.nodes) {
62
+ result.warn("It looks like you didn't end your @import statement correctly. " + 'Child nodes are attached to it.', {
63
+ node: atRule
64
+ });
65
+ return;
66
+ }
67
+
68
+ const parsed = parseImport(atRule.params);
69
+
70
+ if (!parsed) {
71
+ // eslint-disable-next-line consistent-return
72
+ return result.warn(`Unable to find uri in '${atRule.toString()}'`, {
73
+ node: atRule
74
+ });
75
+ }
76
+
77
+ if (filter && !filter(parsed)) {
78
+ return;
79
+ }
80
+
81
+ atRule.remove();
82
+ const {
83
+ url,
84
+ media
85
+ } = parsed;
86
+ items.push({
87
+ url,
88
+ media
89
+ });
90
+ });
91
+ return items;
92
+ }
93
+
94
+ function uniq(array) {
95
+ return array.reduce((acc, d) => !acc.find(el => el.url === d.url && el.media === d.media) ? [...acc, d] : acc, []);
96
+ }
97
+
98
+ var _default = _postcss.default.plugin(pluginName, (options = {}) => function process(css, result) {
99
+ const traversed = walkAtRules(css, result, options.filter);
100
+ const paths = uniq(traversed);
101
+ paths.forEach(item => {
102
+ result.messages.push({
103
+ pluginName,
104
+ type: 'import',
105
+ item
106
+ });
107
+ });
108
+ });
109
+
110
+ exports.default = _default;