From 6f9de76046554f6adc4de44935f25a4e8e06ee4f Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Mon, 19 Oct 2015 23:17:38 +0200 Subject: [PATCH] added CSS Modules values support --- lib/processCss.js | 2 + package.json | 3 +- test/moduleTestCases/values/expected.css | 5 ++ test/moduleTestCases/values/source.css | 9 ++++ test/valuesTest.js | 69 ++++++++++++++++++++++++ 5 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 test/moduleTestCases/values/expected.css create mode 100644 test/moduleTestCases/values/source.css create mode 100644 test/valuesTest.js diff --git a/lib/processCss.js b/lib/processCss.js index 4e445061..07275d0b 100644 --- a/lib/processCss.js +++ b/lib/processCss.js @@ -11,6 +11,7 @@ var getLocalIdent = require("./getLocalIdent"); var localByDefault = require("postcss-modules-local-by-default"); var extractImports = require("postcss-modules-extract-imports"); var modulesScope = require("postcss-modules-scope"); +var modulesValues = require("postcss-modules-values"); var cssnano = require("cssnano"); var parserPlugin = postcss.plugin("css-loader-parser", function(options) { @@ -144,6 +145,7 @@ module.exports = function processCss(inputSource, inputMap, options, callback) { } }), extractImports(), + modulesValues, modulesScope({ generateScopedName: function(exportName) { return getLocalIdent(options.loaderContext, localIdentName, exportName, { diff --git a/package.json b/package.json index 9ade06f6..29fcf663 100644 --- a/package.json +++ b/package.json @@ -11,8 +11,9 @@ "lodash.camelcase": "^3.0.1", "postcss": "^5.0.6", "postcss-modules-extract-imports": "1.0.0-beta2", - "postcss-modules-local-by-default": "1.0.0-beta1", + "postcss-modules-local-by-default": "1.0.0-beta2", "postcss-modules-scope": "1.0.0-beta2", + "postcss-modules-values": "^1.1.0", "source-list-map": "^0.1.4" }, "devDependencies": { diff --git a/test/moduleTestCases/values/expected.css b/test/moduleTestCases/values/expected.css new file mode 100644 index 00000000..ad85f81a --- /dev/null +++ b/test/moduleTestCases/values/expected.css @@ -0,0 +1,5 @@ +._a_ { + background: red; + background: green; + background: red; +} diff --git a/test/moduleTestCases/values/source.css b/test/moduleTestCases/values/source.css new file mode 100644 index 00000000..27b0fcf6 --- /dev/null +++ b/test/moduleTestCases/values/source.css @@ -0,0 +1,9 @@ +@value aaa: red; +@value bbb: green; +@value ccc: aaa; + +.a { + background: aaa; + background: bbb; + background: ccc; +} diff --git a/test/valuesTest.js b/test/valuesTest.js new file mode 100644 index 00000000..57858407 --- /dev/null +++ b/test/valuesTest.js @@ -0,0 +1,69 @@ +/*globals describe */ + +var testLocals = require("./helpers").testLocals; +var test = require("./helpers").test; + +function testLocal(name, input, result, localsResult, query, modules) { + result.locals = localsResult; + test(name, input, result, query, modules); +} + +describe("values", function() { + testLocals("should export values", + "@value def: red; @value ghi: 1px solid black", + { + def: "red", + ghi: "1px solid black" + }, + "" + ); + testLocals("should export values and locals", + "@value def: red; .ghi { color: def; }", + { + def: "red", + ghi: "_ghi" + }, + "?modules&localIdentName=_[local]" + ); + testLocal("should import values from other module", + "@value def from './file'; .ghi { color: def; }", [ + [ 2, "", "" ], + [ 1, ".ghi { color: red; }", "" ] + ], { + def: "red" + }, "", { + "./file": (function() { + var a = [[2, "", ""]]; + a.locals = { + def: "red" + }; + return a; + })() + } + ); + testLocal("should import values with renaming", + "@value def as aaa from './file1'; @value def as bbb from './file2'; .ghi { background: aaa, bbb, def; }", [ + [ 2, "", "" ], + [ 3, "", "" ], + [ 1, ".ghi { background: red, green, def; }", "" ] + ], { + aaa: "red", + bbb: "green" + }, "", { + "./file1": (function() { + var a = [[2, "", ""]]; + a.locals = { + def: "red" + }; + return a; + })(), + "./file2": (function() { + var a = [[3, "", ""]]; + a.locals = { + def: "green" + }; + return a; + })() + } + ); +});