css-loader 2.1.0 → 2.1.1

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/CHANGELOG.md CHANGED
@@ -2,6 +2,18 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ <a name="2.1.1"></a>
6
+ ## [2.1.1](https://github.com/webpack-contrib/css-loader/compare/v2.1.0...v2.1.1) (2019-03-07)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * do not break selector with escaping ([#896](https://github.com/webpack-contrib/css-loader/issues/896)) ([0ba8c66](https://github.com/webpack-contrib/css-loader/commit/0ba8c66))
12
+ * source map generation when `sourceRoot` is present ([#901](https://github.com/webpack-contrib/css-loader/issues/901)) ([e9ce745](https://github.com/webpack-contrib/css-loader/commit/e9ce745))
13
+ * sourcemap generating when previous loader pass sourcemap as string ([#905](https://github.com/webpack-contrib/css-loader/issues/905)) ([3797e4d](https://github.com/webpack-contrib/css-loader/commit/3797e4d))
14
+
15
+
16
+
5
17
  <a name="2.1.0"></a>
6
18
  # [2.1.0](https://github.com/webpack-contrib/css-loader/compare/v2.0.2...v2.1.0) (2018-12-25)
7
19
 
package/README.md CHANGED
@@ -109,23 +109,26 @@ module.exports = {
109
109
 
110
110
  ## Options
111
111
 
112
- | Name | Type | Default | Description |
113
- | :-----------------------------------------: | :-------------------: | :-------------: | :------------------------------------------ |
114
- | **[`url`](#url)** | `{Boolean\|Function}` | `true` | Enable/Disable `url()` handling |
115
- | **[`import`](#import)** | `{Boolean\/Function}` | `true` | Enable/Disable @import handling |
116
- | **[`modules`](#modules)** | `{Boolean\|String}` | `false` | Enable/Disable CSS Modules and setup mode |
117
- | **[`localIdentName`](#localidentname)** | `{String}` | `[hash:base64]` | Configure the generated ident |
118
- | **[`sourceMap`](#sourcemap)** | `{Boolean}` | `false` | Enable/Disable Sourcemaps |
119
- | **[`camelCase`](#camelcase)** | `{Boolean\|String}` | `false` | Export Classnames in CamelCase |
120
- | **[`importLoaders`](#importloaders)** | `{Number}` | `0` | Number of loaders applied before CSS loader |
121
- | **[`exportOnlyLocals`](#exportonlylocals)** | `{Boolean}` | `false` | Export only locals |
112
+ | Name | Type | Default | Description |
113
+ | :-----------------------------------------: | :-------------------: | :-------------: | :----------------------------------------------------------------------- |
114
+ | **[`url`](#url)** | `{Boolean\|Function}` | `true` | Enable/Disable `url()` handling |
115
+ | **[`import`](#import)** | `{Boolean\/Function}` | `true` | Enable/Disable @import handling |
116
+ | **[`modules`](#modules)** | `{Boolean\|String}` | `false` | Enable/Disable CSS Modules and setup mode |
117
+ | **[`localIdentName`](#localidentname)** | `{String}` | `[hash:base64]` | Configure the generated ident |
118
+ | **[`context`](#context)** | `{String}` | `undefined` | Allow to redefine basic loader context for local ident name |
119
+ | **[`hashPrefix`](#hashprefix)** | `{String}` | `undefined` | Allow to add custom hash to generate more unique classes |
120
+ | **[`getLocalIdent`](#getlocalident)** | `{Function}` | `undefined` | Configure the function to generate classname based on a different schema |
121
+ | **[`sourceMap`](#sourcemap)** | `{Boolean}` | `false` | Enable/Disable Sourcemaps |
122
+ | **[`camelCase`](#camelcase)** | `{Boolean\|String}` | `false` | Export Classnames in CamelCase |
123
+ | **[`importLoaders`](#importloaders)** | `{Number}` | `0` | Number of loaders applied before CSS loader |
124
+ | **[`exportOnlyLocals`](#exportonlylocals)** | `{Boolean}` | `false` | Export only locals |
122
125
 
123
126
  ### `url`
124
127
 
125
128
  Type: `Boolean|Function`
126
129
  Default: `true`
127
130
 
128
- Control `url()` resolving. Absolute urls are not resolving.
131
+ Control `url()` resolving. Absolute URLs and root-relative URLs are not resolving.
129
132
 
130
133
  Examples resolutions:
131
134
 
@@ -461,7 +464,66 @@ module.exports = {
461
464
  };
462
465
  ```
463
466
 
467
+ ### `context`
468
+
469
+ Type: `String`
470
+ Default: `undefined`
471
+
472
+ Allow to redefine basic loader context for local ident name.
473
+ By default we use `rootContext` of loader.
474
+
475
+ **webpack.config.js**
476
+
477
+ ```js
478
+ module.exports = {
479
+ module: {
480
+ rules: [
481
+ {
482
+ test: /\.css$/,
483
+ loader: 'css-loader',
484
+ options: {
485
+ modules: true,
486
+ context: path.resolve(__dirname, 'context'),
487
+ },
488
+ },
489
+ ],
490
+ },
491
+ };
492
+ ```
493
+
494
+ ### `hashPrefix`
495
+
496
+ Type: `String`
497
+ Default: `undefined`
498
+
499
+ Allow to add custom hash to generate more unique classes.
500
+
501
+ **webpack.config.js**
502
+
503
+ ```js
504
+ module.exports = {
505
+ module: {
506
+ rules: [
507
+ {
508
+ test: /\.css$/,
509
+ loader: 'css-loader',
510
+ options: {
511
+ modules: true,
512
+ hashPrefix: 'hash',
513
+ },
514
+ },
515
+ ],
516
+ },
517
+ };
518
+ ```
519
+
520
+ ### `getLocalIdent`
521
+
522
+ Type: `Function`
523
+ Default: `undefined`
524
+
464
525
  You can also specify the absolute path to your custom `getLocalIdent` function to generate classname based on a different schema.
526
+ By default we use built-in function to generate a classname.
465
527
 
466
528
  **webpack.config.js**
467
529
 
@@ -474,9 +536,6 @@ module.exports = {
474
536
  loader: 'css-loader',
475
537
  options: {
476
538
  modules: true,
477
- context: path.resolve(__dirname, 'context'), // Allow to redefine basic loader context for `local-ident-name`
478
- hashPrefix: 'hash', // Allow to add custom hash to generate more unique classes
479
- localIdentName: '[path][name]__[local]--[hash:base64:5]',
480
539
  getLocalIdent: (context, localIdentName, localName, options) => {
481
540
  return 'whatever_random_class_name';
482
541
  },
@@ -490,7 +549,7 @@ module.exports = {
490
549
  ### `sourceMap`
491
550
 
492
551
  Type: `Boolean`
493
- Default: `true`
552
+ Default: `false`
494
553
 
495
554
  To include source maps set the `sourceMap` option.
496
555
 
@@ -654,7 +713,8 @@ module.exports = {
654
713
  ### Extract
655
714
 
656
715
  For production builds it's recommended to extract the CSS from your bundle being able to use parallel loading of CSS/JS resources later on.
657
- - This can be achieved by using the [mini-css-extract-plugin](https://github.com/webpack-contrib/mini-css-extract-plugin) to extract the CSS when running in production mode.
716
+
717
+ - This can be achieved by using the [mini-css-extract-plugin](https://github.com/webpack-contrib/mini-css-extract-plugin) to extract the CSS when running in production mode.
658
718
 
659
719
  - As an alternative, if seeking better development performance and css outputs that mimic production. [extract-css-chunks-webpack-plugin](https://github.com/faceyspacey/extract-css-chunks-webpack-plugin) offers a hot module reload friendly, extended version of mini-css-extract-plugin. HMR real CSS files in dev, works like mini-css in non-dev
660
720
 
@@ -670,21 +730,15 @@ Please take a moment to read our contributing guidelines if you haven't yet done
670
730
 
671
731
  [npm]: https://img.shields.io/npm/v/css-loader.svg
672
732
  [npm-url]: https://npmjs.com/package/css-loader
673
-
674
733
  [node]: https://img.shields.io/node/v/css-loader.svg
675
734
  [node-url]: https://nodejs.org
676
-
677
735
  [deps]: https://david-dm.org/webpack-contrib/css-loader.svg
678
736
  [deps-url]: https://david-dm.org/webpack-contrib/css-loader
679
-
680
737
  [tests]: https://img.shields.io/circleci/project/github/webpack-contrib/css-loader.svg
681
738
  [tests-url]: https://circleci.com/gh/webpack-contrib/css-loader
682
-
683
739
  [cover]: https://codecov.io/gh/webpack-contrib/css-loader/branch/master/graph/badge.svg
684
740
  [cover-url]: https://codecov.io/gh/webpack-contrib/css-loader
685
-
686
741
  [chat]: https://badges.gitter.im/webpack/webpack.svg
687
742
  [chat-url]: https://gitter.im/webpack/webpack
688
-
689
743
  [size]: https://packagephobia.now.sh/badge?p=css-loader
690
744
  [size-url]: https://packagephobia.now.sh/result?p=css-loader
package/dist/index.js CHANGED
@@ -21,7 +21,7 @@ var _postcssModulesValues = _interopRequireDefault(require("postcss-modules-valu
21
21
 
22
22
  var _loaderUtils = require("loader-utils");
23
23
 
24
- var _camelCase = _interopRequireDefault(require("lodash/camelCase"));
24
+ var _normalizePath = _interopRequireDefault(require("normalize-path"));
25
25
 
26
26
  var _options = _interopRequireDefault(require("./options.json"));
27
27
 
@@ -48,13 +48,24 @@ function loader(content, map, meta) {
48
48
 
49
49
  if (sourceMap) {
50
50
  if (map) {
51
+ // Some loader emit source map as string
52
+ // Strip any JSON XSSI avoidance prefix from the string (as documented in the source maps specification), and then parse the string as JSON.
51
53
  if (typeof map === 'string') {
52
- map = JSON.stringify(map);
54
+ map = JSON.parse(map.replace(/^\)]}'[^\n]*\n/, ''));
55
+ } // Source maps should use forward slash because it is URLs (https://github.com/mozilla/source-map/issues/91)
56
+ // We should normalize path because previous loaders like `sass-loader` using backslash when generate source map
57
+
58
+
59
+ if (map.file) {
60
+ map.file = (0, _normalizePath.default)(map.file);
61
+ }
62
+
63
+ if (map.sourceRoot) {
64
+ map.sourceRoot = (0, _normalizePath.default)(map.sourceRoot);
53
65
  }
54
66
 
55
67
  if (map.sources) {
56
- map.sources = map.sources.map(source => source.replace(/\\/g, '/'));
57
- map.sourceRoot = '';
68
+ map.sources = map.sources.map(source => (0, _normalizePath.default)(source));
58
69
  }
59
70
  }
60
71
  } else {
@@ -110,12 +121,10 @@ function loader(content, map, meta) {
110
121
 
111
122
  plugins.push((0, _plugins.icssParser)());
112
123
  (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()}`,
124
+ from: (0, _loaderUtils.getRemainingRequest)(this).split('!').pop(),
115
125
  to: (0, _loaderUtils.getCurrentRequest)(this).split('!').pop(),
116
126
  map: options.sourceMap ? {
117
127
  prev: map,
118
- sourcesContent: true,
119
128
  inline: false,
120
129
  annotation: false
121
130
  } : null
@@ -165,7 +174,7 @@ function loader(content, map, meta) {
165
174
  switch (options.camelCase) {
166
175
  case true:
167
176
  addEntry(key);
168
- targetKey = (0, _camelCase.default)(key);
177
+ targetKey = (0, _utils.camelCase)(key);
169
178
 
170
179
  if (targetKey !== key) {
171
180
  addEntry(targetKey);
@@ -184,7 +193,7 @@ function loader(content, map, meta) {
184
193
  break;
185
194
 
186
195
  case 'only':
187
- addEntry((0, _camelCase.default)(key));
196
+ addEntry((0, _utils.camelCase)(key));
188
197
  break;
189
198
 
190
199
  case 'dashesOnly':
@@ -239,24 +248,9 @@ function loader(content, map, meta) {
239
248
  imports.push(`var ${placeholder} = urlEscape(require(${(0, _loaderUtils.stringifyRequest)(this, (0, _loaderUtils.urlToRequest)(normalizedUrl))})${hash ? ` + ${hash}` : ''}${needQuotes ? ', true' : ''});`);
240
249
  cssAsString = cssAsString.replace(new RegExp(placeholder, 'g'), () => `" + ${placeholder} + "`);
241
250
  });
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
251
  const runtimeCode = `exports = module.exports = require(${(0, _loaderUtils.stringifyRequest)(this, require.resolve('./runtime/api'))})(${!!sourceMap});\n`;
258
252
  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`;
253
+ const moduleCode = `// Module\nexports.push([module.id, ${cssAsString}, ""${result.map ? `,${result.map}` : ''}]);\n\n`;
260
254
  const exportsCode = exports.length > 0 ? `// Exports\nexports.locals = {\n${exports.join(',\n')}\n};` : ''; // Embed runtime
261
255
 
262
256
  return callback(null, runtimeCode + importCode + moduleCode + exportsCode);
@@ -9,8 +9,6 @@ var _postcss = _interopRequireDefault(require("postcss"));
9
9
 
10
10
  var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
11
11
 
12
- var _lodash = _interopRequireDefault(require("lodash"));
13
-
14
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
13
 
16
14
  const pluginName = 'postcss-url-parser';
@@ -96,10 +94,21 @@ function walkDeclsWithUrl(css, result, filter) {
96
94
  return items;
97
95
  }
98
96
 
97
+ function uniqWith(array, comparator) {
98
+ return array.reduce((acc, d) => !acc.some(item => comparator(d, item)) ? [...acc, d] : acc, []);
99
+ }
100
+
101
+ function flatten(array) {
102
+ return array.reduce((a, b) => a.concat(b), []);
103
+ }
104
+
105
+ function isEqual(value, other) {
106
+ return value.url === other.url && value.needQuotes === other.needQuotes;
107
+ }
108
+
99
109
  var _default = _postcss.default.plugin(pluginName, (options = {}) => function process(css, result) {
100
110
  const traversed = walkDeclsWithUrl(css, result, options.filter);
101
-
102
- const paths = _lodash.default.uniqWith(_lodash.default.flatten(traversed.map(item => item.urls)), _lodash.default.isEqual);
111
+ const paths = uniqWith(flatten(traversed.map(item => item.urls)), isEqual);
103
112
 
104
113
  if (paths.length === 0) {
105
114
  return;
@@ -128,12 +137,7 @@ var _default = _postcss.default.plugin(pluginName, (options = {}) => function pr
128
137
  });
129
138
  traversed.forEach(item => {
130
139
  walkUrls(item.parsed, (node, url, needQuotes) => {
131
- const value = _lodash.default.find(placeholders, {
132
- path: {
133
- url,
134
- needQuotes
135
- }
136
- });
140
+ const value = placeholders.find(placeholder => placeholder.path.url === url && placeholder.path.needQuotes === needQuotes);
137
141
 
138
142
  if (!value) {
139
143
  return;
package/dist/utils.js CHANGED
@@ -5,12 +5,15 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.getImportPrefix = getImportPrefix;
7
7
  exports.getLocalIdent = getLocalIdent;
8
+ exports.camelCase = camelCase;
8
9
  exports.dashesCamelCase = dashesCamelCase;
9
10
  exports.getFilter = getFilter;
10
11
  exports.placholderRegExps = void 0;
11
12
 
12
13
  var _path = _interopRequireDefault(require("path"));
13
14
 
15
+ var _camelcase = _interopRequireDefault(require("camelcase"));
16
+
14
17
  var _loaderUtils = _interopRequireDefault(require("loader-utils"));
15
18
 
16
19
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
@@ -19,6 +22,8 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
19
22
  MIT License http://www.opensource.org/licenses/mit-license.php
20
23
  Author Tobias Koppers @sokra
21
24
  */
25
+
26
+ /* eslint-disable line-comment-position */
22
27
  const placholderRegExps = {
23
28
  importItemG: /___CSS_LOADER_IMPORT___([0-9]+)___/g,
24
29
  importItem: /___CSS_LOADER_IMPORT___([0-9]+)___/
@@ -35,10 +40,30 @@ function getImportPrefix(loaderContext, importLoaders) {
35
40
  return `-!${loadersRequest}!`;
36
41
  }
37
42
 
43
+ function camelCase(str) {
44
+ return (0, _camelcase.default)(str);
45
+ }
46
+
38
47
  function dashesCamelCase(str) {
39
48
  return str.replace(/-+(\w)/g, (match, firstLetter) => firstLetter.toUpperCase());
40
49
  }
41
50
 
51
+ const whitespace = '[\\x20\\t\\r\\n\\f]';
52
+ const unescapeRegExp = new RegExp(`\\\\([\\da-f]{1,6}${whitespace}?|(${whitespace})|.)`, 'ig');
53
+
54
+ function unescape(str) {
55
+ return str.replace(unescapeRegExp, (_, escaped, escapedWhitespace) => {
56
+ const high = `0x${escaped}` - 0x10000; // NaN means non-codepoint
57
+ // Workaround erroneous numeric interpretation of +"0x"
58
+ // eslint-disable-next-line no-self-compare
59
+
60
+ return high !== high || escapedWhitespace ? escaped : high < 0 ? // BMP codepoint
61
+ String.fromCharCode(high + 0x10000) : // Supplemental Plane codepoint (surrogate pair)
62
+ // eslint-disable-next-line no-bitwise
63
+ String.fromCharCode(high >> 10 | 0xd800, high & 0x3ff | 0xdc00);
64
+ });
65
+ }
66
+
42
67
  function getLocalIdent(loaderContext, localIdentName, localName, options) {
43
68
  if (!options.context) {
44
69
  // eslint-disable-next-line no-param-reassign
@@ -48,7 +73,7 @@ function getLocalIdent(loaderContext, localIdentName, localName, options) {
48
73
  const request = _path.default.relative(options.context, loaderContext.resourcePath).replace(/\\/g, '/'); // eslint-disable-next-line no-param-reassign
49
74
 
50
75
 
51
- options.content = `${options.hashPrefix + request}+${localName}`; // eslint-disable-next-line no-param-reassign
76
+ options.content = `${options.hashPrefix + request}+${unescape(localName)}`; // eslint-disable-next-line no-param-reassign
52
77
 
53
78
  localIdentName = localIdentName.replace(/\[local\]/gi, localName);
54
79
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "css-loader",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
4
4
  "description": "css loader module for webpack",
5
5
  "license": "MIT",
6
6
  "repository": "webpack-contrib/css-loader",
@@ -13,20 +13,22 @@
13
13
  },
14
14
  "scripts": {
15
15
  "start": "npm run build -- -w",
16
+ "prebuild": "npm run clean",
16
17
  "build": "cross-env NODE_ENV=production babel src -d dist --ignore 'src/**/*.test.js' --copy-files",
17
18
  "clean": "del-cli dist",
18
19
  "commitlint": "commitlint",
19
20
  "commitmsg": "commitlint -e $GIT_PARAMS",
20
21
  "lint": "eslint --cache src test",
21
- "prebuild": "npm run clean",
22
22
  "prepublish": "npm run build",
23
23
  "release": "standard-version",
24
24
  "security": "npm audit",
25
- "test": "jest",
25
+ "test:only": "jest",
26
26
  "test:watch": "jest --watch",
27
27
  "test:coverage": "jest --collectCoverageFrom='src/**/*.js' --coverage",
28
+ "pretest": "npm run lint",
29
+ "test": "npm run test:only",
28
30
  "ci:lint": "npm run lint && npm run security",
29
- "ci:test": "npm run test -- --runInBand",
31
+ "ci:test": "npm run test:only -- --runInBand",
30
32
  "ci:coverage": "npm run test:coverage -- --runInBand",
31
33
  "ci:lint:commits": "commitlint --from=origin/master --to=${CIRCLE_SHA1}",
32
34
  "defaults": "webpack-defaults"
@@ -40,13 +42,14 @@
40
42
  "webpack": "^4.0.0"
41
43
  },
42
44
  "dependencies": {
43
- "icss-utils": "^4.0.0",
44
- "loader-utils": "^1.2.1",
45
- "lodash": "^4.17.11",
46
- "postcss": "^7.0.6",
45
+ "icss-utils": "^4.1.0",
46
+ "loader-utils": "^1.2.3",
47
+ "camelcase": "^5.2.0",
48
+ "normalize-path": "^3.0.0",
49
+ "postcss": "^7.0.14",
47
50
  "postcss-modules-extract-imports": "^2.0.0",
48
- "postcss-modules-local-by-default": "^2.0.3",
49
- "postcss-modules-scope": "^2.0.0",
51
+ "postcss-modules-local-by-default": "^2.0.6",
52
+ "postcss-modules-scope": "^2.1.0",
50
53
  "postcss-modules-values": "^2.0.0",
51
54
  "postcss-value-parser": "^3.3.0",
52
55
  "schema-utils": "^1.0.0"
@@ -60,17 +63,16 @@
60
63
  "@commitlint/config-conventional": "^7.1.2",
61
64
  "@webpack-contrib/defaults": "^3.0.0",
62
65
  "@webpack-contrib/eslint-config-webpack": "^3.0.0",
63
- "babel-core": "^7.0.0-bridge.0",
64
- "babel-jest": "^23.6.0",
66
+ "babel-jest": "^24.1.0",
65
67
  "cross-env": "^5.2.0",
66
- "del": "^3.0.0",
68
+ "del": "^4.0.0",
67
69
  "del-cli": "^1.1.0",
68
70
  "eslint": "^5.9.0",
69
71
  "eslint-plugin-import": "^2.14.0",
70
72
  "eslint-plugin-prettier": "^3.0.0",
71
73
  "file-loader": "^3.0.1",
72
74
  "husky": "^1.2.0",
73
- "jest": "^23.6.0",
75
+ "jest": "^24.1.0",
74
76
  "lint-staged": "^8.1.0",
75
77
  "memory-fs": "^0.4.1",
76
78
  "postcss-loader": "^3.0.0",
@@ -78,7 +80,7 @@
78
80
  "prettier": "^1.15.2",
79
81
  "sass": "^1.15.1",
80
82
  "sass-loader": "^7.1.0",
81
- "standard-version": "^4.0.0",
83
+ "standard-version": "^5.0.0",
82
84
  "strip-ansi": "^5.0.0",
83
85
  "webpack": "^4.26.1"
84
86
  },
@@ -117,5 +119,10 @@
117
119
  "extends": [
118
120
  "@commitlint/config-conventional"
119
121
  ]
122
+ },
123
+ "prettier": {
124
+ "singleQuote": true,
125
+ "trailingComma": "es5",
126
+ "arrowParens": "always"
120
127
  }
121
128
  }