From f6673c8460336e63bec229ea005056791016560c Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Mon, 29 May 2017 17:49:43 +0300 Subject: [PATCH] fix: preserve leading underscore in class names (#543) --- lib/processCss.js | 28 +++++++++++----------------- package.json | 1 + test/localTest.js | 5 +++++ yarn.lock | 18 ++++++++++++++---- 4 files changed, 31 insertions(+), 21 deletions(-) diff --git a/lib/processCss.js b/lib/processCss.js index 04dcdb1d..8c688e34 100644 --- a/lib/processCss.js +++ b/lib/processCss.js @@ -9,6 +9,7 @@ var loaderUtils = require("loader-utils"); var assign = require("object-assign"); var getLocalIdent = require("./getLocalIdent"); +var icssUtils = require('icss-utils'); var localByDefault = require("postcss-modules-local-by-default"); var extractImports = require("postcss-modules-extract-imports"); var modulesScope = require("postcss-modules-scope"); @@ -65,24 +66,17 @@ var parserPlugin = postcss.plugin("css-loader-parser", function(options) { }); } - css.walkRules(function(rule) { - if(rule.selector === ":export") { - rule.walkDecls(function(decl) { - exports[decl.prop] = decl.value; - }); - rule.remove(); - } else if(/^:import\(.+\)$/.test(rule.selector)) { - var match = /^:import\((.+)\)$/.exec(rule.selector); - var url = loaderUtils.parseString(match[1]); - rule.walkDecls(function(decl) { - imports["$" + decl.prop] = importItems.length; - importItems.push({ - url: url, - export: decl.value - }); + var icss = icssUtils.extractICSS(css); + exports = icss.icssExports; + Object.keys(icss.icssImports).forEach(function(key) { + var url = loaderUtils.parseString(key); + Object.keys(icss.icssImports[key]).forEach(function(prop) { + imports["$" + prop] = importItems.length; + importItems.push({ + url: url, + export: icss.icssImports[key][prop] }); - rule.remove(); - } + }) }); Object.keys(exports).forEach(function(exportName) { diff --git a/package.json b/package.json index 2d17a16d..b13646b8 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "babel-code-frame": "^6.11.0", "css-selector-tokenizer": "^0.7.0", "cssnano": ">=2.6.1 <4", + "icss-utils": "^2.1.0", "loader-utils": "^1.0.2", "lodash.camelcase": "^4.3.0", "object-assign": "^4.0.1", diff --git a/test/localTest.js b/test/localTest.js index e2146bdf..81c55434 100644 --- a/test/localTest.js +++ b/test/localTest.js @@ -217,4 +217,9 @@ describe("local", function() { ], { test: "_--test" }, "?localIdentName=--[local]"); + testLocal("saves underscore prefix in exported class names", ":export { _test: _test }", [ + [1, "", ""] + ], { + _test: '_test' + }, "?localIdentName=[local]"); }); diff --git a/yarn.lock b/yarn.lock index 32bd1f0b..b5ab0cdc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -520,10 +520,6 @@ conventional-recommended-bump@^0.3.0: meow "^3.3.0" object-assign "^4.0.1" -convert-source-map@^1.3.0: - version "1.4.0" - resolved "https://registry.yarnpkg.com/convert-source-map/-/convert-source-map-1.4.0.tgz#e3dad195bf61bfe13a7a3c73e9876ec14a0268f3" - core-util-is@~1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7" @@ -1164,6 +1160,12 @@ icss-replace-symbols@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/icss-replace-symbols/-/icss-replace-symbols-1.0.2.tgz#cb0b6054eb3af6edc9ab1d62d01933e2d4c8bfa5" +icss-utils@^2.1.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/icss-utils/-/icss-utils-2.1.0.tgz#83f0a0ec378bf3246178b6c2ad9136f135b1c962" + dependencies: + postcss "^6.0.1" + ignore@^3.2.0: version "3.2.4" resolved "https://registry.yarnpkg.com/ignore/-/ignore-3.2.4.tgz#4055e03596729a8fabe45a43c100ad5ed815c4e8" @@ -2036,6 +2038,14 @@ postcss@^5.0.10, postcss@^5.0.11, postcss@^5.0.12, postcss@^5.0.13, postcss@^5.0 source-map "^0.5.6" supports-color "^3.2.3" +postcss@^6.0.1: + version "6.0.1" + resolved "https://registry.yarnpkg.com/postcss/-/postcss-6.0.1.tgz#000dbd1f8eef217aa368b9a212c5fc40b2a8f3f2" + dependencies: + chalk "^1.1.3" + source-map "^0.5.6" + supports-color "^3.2.3" + prelude-ls@~1.1.2: version "1.1.2" resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54"