From d6b3873e0d115f7556db991f226bc4a8913083e2 Mon Sep 17 00:00:00 2001 From: Glen Maddern Date: Mon, 7 Sep 2015 15:58:58 +1000 Subject: [PATCH 01/23] I'm reasonably happy with this example syntax --- test/test-cases.js | 39 +++++++++++++------------ test/test-cases/aliases/borders.css | 3 ++ test/test-cases/aliases/breakpoints.css | 1 + test/test-cases/aliases/colors.css | 2 ++ test/test-cases/aliases/expected.css | 12 ++++++++ test/test-cases/aliases/expected.json | 3 ++ test/test-cases/aliases/source.css | 15 ++++++++++ 7 files changed, 56 insertions(+), 19 deletions(-) create mode 100644 test/test-cases/aliases/borders.css create mode 100644 test/test-cases/aliases/breakpoints.css create mode 100644 test/test-cases/aliases/colors.css create mode 100644 test/test-cases/aliases/expected.css create mode 100644 test/test-cases/aliases/expected.json create mode 100644 test/test-cases/aliases/source.css diff --git a/test/test-cases.js b/test/test-cases.js index c6adcff..1c80fac 100644 --- a/test/test-cases.js +++ b/test/test-cases.js @@ -18,6 +18,7 @@ Object.keys( pipelines ).forEach( dirname => { describe( dirname, () => { let testDir = path.join( __dirname, dirname ) fs.readdirSync( testDir ).forEach( testCase => { + if (testCase !== 'aliases') return if ( fs.existsSync( path.join( testDir, testCase, "source.css" ) ) ) { it( "should " + testCase.replace( /-/g, " " ), done => { let expected = normalize( fs.readFileSync( path.join( testDir, testCase, "expected.css" ), "utf-8" ) ) @@ -34,22 +35,22 @@ Object.keys( pipelines ).forEach( dirname => { } ) // special case for testing multiple sources -describe( 'multiple sources', () => { - let testDir = path.join( __dirname, 'test-cases' ) - let testCase = 'multiple-sources'; - let dirname = 'test-cases'; - if ( fs.existsSync( path.join( testDir, testCase, "source1.css" ) ) ) { - it( "should " + testCase.replace( /-/g, " " ), done => { - let expected = normalize( fs.readFileSync( path.join( testDir, testCase, "expected.css" ), "utf-8" ) ) - let loader = new FileSystemLoader( testDir, pipelines[dirname] ) - let expectedTokens = JSON.parse( fs.readFileSync( path.join( testDir, testCase, "expected.json" ), "utf-8" ) ) - loader.fetch( `${testCase}/source1.css`, "/" ).then( tokens1 => { - loader.fetch( `${testCase}/source2.css`, "/" ).then( tokens2 => { - assert.equal( loader.finalSource, expected ) - const tokens = Object.assign({}, tokens1, tokens2); - assert.equal( JSON.stringify( tokens ), JSON.stringify( expectedTokens ) ) - } ).then( done, done ) - }) - } ); - } -} ); +//describe( 'multiple sources', () => { +// let testDir = path.join( __dirname, 'test-cases' ) +// let testCase = 'multiple-sources'; +// let dirname = 'test-cases'; +// if ( fs.existsSync( path.join( testDir, testCase, "source1.css" ) ) ) { +// it( "should " + testCase.replace( /-/g, " " ), done => { +// let expected = normalize( fs.readFileSync( path.join( testDir, testCase, "expected.css" ), "utf-8" ) ) +// let loader = new FileSystemLoader( testDir, pipelines[dirname] ) +// let expectedTokens = JSON.parse( fs.readFileSync( path.join( testDir, testCase, "expected.json" ), "utf-8" ) ) +// loader.fetch( `${testCase}/source1.css`, "/" ).then( tokens1 => { +// loader.fetch( `${testCase}/source2.css`, "/" ).then( tokens2 => { +// assert.equal( loader.finalSource, expected ) +// const tokens = Object.assign({}, tokens1, tokens2); +// assert.equal( JSON.stringify( tokens ), JSON.stringify( expectedTokens ) ) +// } ).then( done, done ) +// }) +// } ); +// } +//} ); diff --git a/test/test-cases/aliases/borders.css b/test/test-cases/aliases/borders.css new file mode 100644 index 0000000..f3670f7 --- /dev/null +++ b/test/test-cases/aliases/borders.css @@ -0,0 +1,3 @@ +.dashed { + border: 4px dashed; +} diff --git a/test/test-cases/aliases/breakpoints.css b/test/test-cases/aliases/breakpoints.css new file mode 100644 index 0000000..100c190 --- /dev/null +++ b/test/test-cases/aliases/breakpoints.css @@ -0,0 +1 @@ +@define small: (max-width: 599px); diff --git a/test/test-cases/aliases/colors.css b/test/test-cases/aliases/colors.css new file mode 100644 index 0000000..a713e49 --- /dev/null +++ b/test/test-cases/aliases/colors.css @@ -0,0 +1,2 @@ +@define primary: aquamarine; +@define secondary: red; diff --git a/test/test-cases/aliases/expected.css b/test/test-cases/aliases/expected.css new file mode 100644 index 0000000..1c2f926 --- /dev/null +++ b/test/test-cases/aliases/expected.css @@ -0,0 +1,12 @@ +._aliases_borders__bar { + color: red; +} +._aliases_source__foo { + background: aquamarine; + border-color: red; +} +@media (max-width: 599px) { + ._aliases_source__foo { + background: white; + } +} diff --git a/test/test-cases/aliases/expected.json b/test/test-cases/aliases/expected.json new file mode 100644 index 0000000..abf2cf7 --- /dev/null +++ b/test/test-cases/aliases/expected.json @@ -0,0 +1,3 @@ +{ + "foo": "_aliases_borders__bar _aliases_source__foo" +} diff --git a/test/test-cases/aliases/source.css b/test/test-cases/aliases/source.css new file mode 100644 index 0000000..aae3567 --- /dev/null +++ b/test/test-cases/aliases/source.css @@ -0,0 +1,15 @@ +@alias "./borders.css" as borders; +@alias small from "./breakpoints.css"; +@alias secondary, primary as blue from "./colors.css"; + +.foo { + composes: dashed from borders; + background: blue; + border-color: secondary; +} + +@media small { + .foo { + background: white; + } +} From f13d8ca63215db2101418077a566c7364f9f88bc Mon Sep 17 00:00:00 2001 From: Glen Maddern Date: Sun, 13 Sep 2015 17:08:14 +1000 Subject: [PATCH 02/23] ok smarty --- src/index.js | 3 ++- src/wip.js | 12 ++++++++++++ test/test-cases/aliases/breakpoints.css | 2 +- test/test-cases/aliases/colors.css | 4 ++-- test/test-cases/aliases/source.css | 6 +++--- 5 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 src/wip.js diff --git a/src/index.js b/src/index.js index f5994ef..b72bfd9 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,7 @@ import postcss from 'postcss' import localByDefault from 'postcss-modules-local-by-default' import extractImports from 'postcss-modules-extract-imports' import scope from 'postcss-modules-scope' +import wip from "./wip" import Parser from './parser' @@ -26,4 +27,4 @@ export default class Core { Core.localByDefault = localByDefault Core.extractImports = extractImports Core.scope = scope -Core.defaultPlugins = [localByDefault, extractImports, scope] +Core.defaultPlugins = [wip, localByDefault, extractImports, scope] diff --git a/src/wip.js b/src/wip.js new file mode 100644 index 0000000..394e8cf --- /dev/null +++ b/src/wip.js @@ -0,0 +1,12 @@ +const matchImports = /^(.+?)\s+from\s+(?:"([^"]+)"|'([^']+)')$/ + +export default css => { + css.eachAtRule('alias', atRule => { + let imports = matchImports.exec(atRule.params) + if (imports) { + + } else { + console.log(atRule.params) + } + }) +} diff --git a/test/test-cases/aliases/breakpoints.css b/test/test-cases/aliases/breakpoints.css index 100c190..bc85fe0 100644 --- a/test/test-cases/aliases/breakpoints.css +++ b/test/test-cases/aliases/breakpoints.css @@ -1 +1 @@ -@define small: (max-width: 599px); +@let small: (max-width: 599px); diff --git a/test/test-cases/aliases/colors.css b/test/test-cases/aliases/colors.css index a713e49..f6674cd 100644 --- a/test/test-cases/aliases/colors.css +++ b/test/test-cases/aliases/colors.css @@ -1,2 +1,2 @@ -@define primary: aquamarine; -@define secondary: red; +@let primary: aquamarine; +@let secondary: red; diff --git a/test/test-cases/aliases/source.css b/test/test-cases/aliases/source.css index aae3567..2a625b3 100644 --- a/test/test-cases/aliases/source.css +++ b/test/test-cases/aliases/source.css @@ -1,6 +1,6 @@ -@alias "./borders.css" as borders; -@alias small from "./breakpoints.css"; -@alias secondary, primary as blue from "./colors.css"; +@let borders: "./borders.css"; +@import small from "./breakpoints.css"; +@import secondary, blue as primary from "./colors.css"; .foo { composes: dashed from borders; From 514e8ace7e114ba463244fdf57408da1778733a5 Mon Sep 17 00:00:00 2001 From: Glen Maddern Date: Sun, 13 Sep 2015 18:08:01 +1000 Subject: [PATCH 03/23] think my regex fu is ok --- src/wip.js | 15 +++++++++++++-- test/test-cases/aliases/source.css | 4 ++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/wip.js b/src/wip.js index 394e8cf..daa3e0c 100644 --- a/src/wip.js +++ b/src/wip.js @@ -1,10 +1,21 @@ const matchImports = /^(.+?)\s+from\s+(?:"([^"]+)"|'([^']+)')$/ +const matchLet = /(?:,\s+|^)([\w-]+):?\s+("[^"]*"|'[^']*'|[^,]+)\s?/g export default css => { - css.eachAtRule('alias', atRule => { + let declarations = {} + css.eachAtRule(/^-?let$/, atRule => { + console.log(atRule.params) + + let matches + while (matches = matchLet.exec(atRule.params)) { + let [/*match*/, key, value] = matches + declarations[key] = value + } + }) + css.eachAtRule(/^-?import$/, atRule => { let imports = matchImports.exec(atRule.params) if (imports) { - + //console.log(imports) } else { console.log(atRule.params) } diff --git a/test/test-cases/aliases/source.css b/test/test-cases/aliases/source.css index 2a625b3..84f9d35 100644 --- a/test/test-cases/aliases/source.css +++ b/test/test-cases/aliases/source.css @@ -1,5 +1,5 @@ -@let borders: "./borders.css"; -@import small from "./breakpoints.css"; +@let borders: "./borders.css", breakpoints: "./breakpoints"; +@import small from breakpoints; @import secondary, blue as primary from "./colors.css"; .foo { From 2e294ae9b523b517d7557bbe3b1247976f569063 Mon Sep 17 00:00:00 2001 From: Glen Maddern Date: Sun, 13 Sep 2015 18:56:03 +1000 Subject: [PATCH 04/23] adding replacements for declaration values --- src/wip.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/wip.js b/src/wip.js index daa3e0c..ed03864 100644 --- a/src/wip.js +++ b/src/wip.js @@ -1,11 +1,10 @@ const matchImports = /^(.+?)\s+from\s+(?:"([^"]+)"|'([^']+)')$/ const matchLet = /(?:,\s+|^)([\w-]+):?\s+("[^"]*"|'[^']*'|[^,]+)\s?/g +const matchConstName = /[\w-]+/g export default css => { let declarations = {} css.eachAtRule(/^-?let$/, atRule => { - console.log(atRule.params) - let matches while (matches = matchLet.exec(atRule.params)) { let [/*match*/, key, value] = matches @@ -20,4 +19,15 @@ export default css => { console.log(atRule.params) } }) + /* Perform replacements */ + css.eachDecl(decl => { + let matches + while (matches = matchConstName.exec(decl.value)) { + let replacement = declarations[matches[0]] + if (replacement) { + decl.value = decl.value.slice(0, matches.index) + replacement + decl.value.slice(matchConstName.lastIndex) + matchConstName.lastIndex -= matches[0].length - replacement.length + } + } + }) } From 2b1952878c51d01649f457211c123995d0e22dfe Mon Sep 17 00:00:00 2001 From: Glen Maddern Date: Sun, 13 Sep 2015 19:50:07 +1000 Subject: [PATCH 05/23] got constants coming through in declaration values! --- src/index.js | 2 +- src/wip.js | 86 ++++++++++++++++++++++++------ test/test-cases/aliases/source.css | 2 +- 3 files changed, 73 insertions(+), 17 deletions(-) diff --git a/src/index.js b/src/index.js index b72bfd9..ba7ab71 100644 --- a/src/index.js +++ b/src/index.js @@ -27,4 +27,4 @@ export default class Core { Core.localByDefault = localByDefault Core.extractImports = extractImports Core.scope = scope -Core.defaultPlugins = [wip, localByDefault, extractImports, scope] +Core.defaultPlugins = [wip, css => console.log(css.toResult().css), localByDefault, extractImports, scope] diff --git a/src/wip.js b/src/wip.js index ed03864..e474ac4 100644 --- a/src/wip.js +++ b/src/wip.js @@ -1,8 +1,26 @@ -const matchImports = /^(.+?)\s+from\s+(?:"([^"]+)"|'([^']+)')$/ +import postcss from 'postcss'; + +const matchImports = /^(.+?)\s+from\s+("[^"]*"|'[^']*')$/ const matchLet = /(?:,\s+|^)([\w-]+):?\s+("[^"]*"|'[^']*'|[^,]+)\s?/g const matchConstName = /[\w-]+/g +const matchImport = /^([\w-]+)(?:\s+as\s+([\w-]+))?/ +let options = {} +let importIndex = 0 +let createImportedName = options && options.createImportedName || ((importName/*, path*/) => `i__const_${importName.replace(/\W/g, '_')}_${importIndex++}`) + +const replace = (declarations, object, propName) => { + let matches + while (matches = matchConstName.exec(object[propName])) { + let replacement = declarations[matches[0]] + if (replacement) { + object[propName] = object[propName].slice(0, matches.index) + replacement + object[propName].slice(matchConstName.lastIndex) + matchConstName.lastIndex -= matches[0].length - replacement.length + } + } +} export default css => { + /* Find any local let rules and store them*/ let declarations = {} css.eachAtRule(/^-?let$/, atRule => { let matches @@ -11,23 +29,61 @@ export default css => { declarations[key] = value } }) + + console.log(declarations) + /* We want to export anything defined by now, but don't add it to the CSS yet or + it well get picked up by the replacement stuff */ + let exportDeclarations = Object.keys(declarations).map(key => postcss.decl({ + value: declarations[key], + prop: key, + before: "\n ", + _autoprefixerDisabled: true + })) + + /* Find imports and insert ICSS tmp vars */ + let importAliases = [] css.eachAtRule(/^-?import$/, atRule => { - let imports = matchImports.exec(atRule.params) - if (imports) { - //console.log(imports) - } else { - console.log(atRule.params) + let matches = matchImports.exec(atRule.params) + if (matches) { + let [/*match*/, aliases, path] = matches + let imports = aliases.split(/\s*,\s*/).map(alias => { + let tokens = matchImport.exec(alias) + if (tokens) { + let [/*match*/, theirName, myName = theirName] = tokens + let importedName = createImportedName(myName) + declarations[myName] = importedName + return {theirName, importedName} + } else { + throw new Error(`@import statement "${alias}" is invalid!`) + } + }) + importAliases.push({path, imports}) } }) + /* Perform replacements */ - css.eachDecl(decl => { - let matches - while (matches = matchConstName.exec(decl.value)) { - let replacement = declarations[matches[0]] - if (replacement) { - decl.value = decl.value.slice(0, matches.index) + replacement + decl.value.slice(matchConstName.lastIndex) - matchConstName.lastIndex -= matches[0].length - replacement.length - } - } + css.eachDecl(decl => replace(declarations, decl, 'value')) + + /* Add import rules */ + importAliases.forEach(({path, imports}) => { + css.prepend(postcss.rule({ + selector: `:import(${path})`, + after: "\n", + nodes: imports.map(({theirName, importedName}) => postcss.decl({ + value: theirName, + prop: importedName, + before: "\n ", + _autoprefixerDisabled: true + })) + })) }) + + /* Add export rules if any */ + if (exportDeclarations.length > 0) { + css.prepend(postcss.rule({ + selector: `:export`, + after: "\n", + nodes: exportDeclarations + })) + } } diff --git a/test/test-cases/aliases/source.css b/test/test-cases/aliases/source.css index 84f9d35..932e74d 100644 --- a/test/test-cases/aliases/source.css +++ b/test/test-cases/aliases/source.css @@ -1,6 +1,6 @@ @let borders: "./borders.css", breakpoints: "./breakpoints"; @import small from breakpoints; -@import secondary, blue as primary from "./colors.css"; +@import secondary, primary as blue from "./colors.css"; .foo { composes: dashed from borders; From d2b89a1d6cdd2f28055cd90758901f9ca5dab251 Mon Sep 17 00:00:00 2001 From: Glen Maddern Date: Sun, 13 Sep 2015 20:02:13 +1000 Subject: [PATCH 06/23] YES IT IS PASSING OMG --- src/parser.js | 7 ++---- src/replace-symbols.js | 17 +++++++++++++ src/wip.js | 35 +++++++++++---------------- test/test-cases/aliases/expected.css | 7 ++++-- test/test-cases/aliases/expected.json | 4 ++- test/test-cases/aliases/source.css | 2 +- 6 files changed, 42 insertions(+), 30 deletions(-) create mode 100644 src/replace-symbols.js diff --git a/src/parser.js b/src/parser.js index 536e9cd..a54dabb 100644 --- a/src/parser.js +++ b/src/parser.js @@ -1,4 +1,5 @@ const importRegexp = /^:import\((.+)\)$/ +import replaceSymbols from './replace-symbols' export default class Parser { constructor( pathFetcher, trace ) { @@ -26,11 +27,7 @@ export default class Parser { } linkImportedSymbols( css ) { - css.eachDecl( decl => { - Object.keys(this.translations).forEach( translation => { - decl.value = decl.value.replace(translation, this.translations[translation]) - } ) - }) + replaceSymbols(css, this.translations) } extractExports( css ) { diff --git a/src/replace-symbols.js b/src/replace-symbols.js new file mode 100644 index 0000000..31b37e2 --- /dev/null +++ b/src/replace-symbols.js @@ -0,0 +1,17 @@ +const matchConstName = /[\w-]+/g + +const replace = (declarations, object, propName) => { + let matches + while (matches = matchConstName.exec(object[propName])) { + let replacement = declarations[matches[0]] + if (replacement) { + object[propName] = object[propName].slice(0, matches.index) + replacement + object[propName].slice(matchConstName.lastIndex) + matchConstName.lastIndex -= matches[0].length - replacement.length + } + } +} + +export default (css, translations) => { + css.eachDecl(decl => replace(translations, decl, 'value')) + css.eachAtRule('media', atRule => replace(translations, atRule, 'params')) +} diff --git a/src/wip.js b/src/wip.js index e474ac4..71b075f 100644 --- a/src/wip.js +++ b/src/wip.js @@ -1,40 +1,29 @@ -import postcss from 'postcss'; +import postcss from 'postcss' +import replaceSymbols from './replace-symbols' -const matchImports = /^(.+?)\s+from\s+("[^"]*"|'[^']*')$/ +const matchImports = /^(.+?)\s+from\s+("[^"]*"|'[^']*'|[\w-]+)$/ const matchLet = /(?:,\s+|^)([\w-]+):?\s+("[^"]*"|'[^']*'|[^,]+)\s?/g -const matchConstName = /[\w-]+/g const matchImport = /^([\w-]+)(?:\s+as\s+([\w-]+))?/ let options = {} let importIndex = 0 let createImportedName = options && options.createImportedName || ((importName/*, path*/) => `i__const_${importName.replace(/\W/g, '_')}_${importIndex++}`) -const replace = (declarations, object, propName) => { - let matches - while (matches = matchConstName.exec(object[propName])) { - let replacement = declarations[matches[0]] - if (replacement) { - object[propName] = object[propName].slice(0, matches.index) + replacement + object[propName].slice(matchConstName.lastIndex) - matchConstName.lastIndex -= matches[0].length - replacement.length - } - } -} - export default css => { /* Find any local let rules and store them*/ - let declarations = {} + let translations = {} css.eachAtRule(/^-?let$/, atRule => { let matches while (matches = matchLet.exec(atRule.params)) { let [/*match*/, key, value] = matches - declarations[key] = value + translations[key] = value + atRule.removeSelf() } }) - console.log(declarations) /* We want to export anything defined by now, but don't add it to the CSS yet or it well get picked up by the replacement stuff */ - let exportDeclarations = Object.keys(declarations).map(key => postcss.decl({ - value: declarations[key], + let exportDeclarations = Object.keys(translations).map(key => postcss.decl({ + value: translations[key], prop: key, before: "\n ", _autoprefixerDisabled: true @@ -46,23 +35,27 @@ export default css => { let matches = matchImports.exec(atRule.params) if (matches) { let [/*match*/, aliases, path] = matches + // We can use constants for path names + if (translations[path]) path = translations[path] let imports = aliases.split(/\s*,\s*/).map(alias => { let tokens = matchImport.exec(alias) if (tokens) { let [/*match*/, theirName, myName = theirName] = tokens let importedName = createImportedName(myName) - declarations[myName] = importedName + translations[myName] = importedName return {theirName, importedName} } else { throw new Error(`@import statement "${alias}" is invalid!`) } }) importAliases.push({path, imports}) + atRule.removeSelf() } }) + console.log(translations) /* Perform replacements */ - css.eachDecl(decl => replace(declarations, decl, 'value')) + replaceSymbols(css, translations) /* Add import rules */ importAliases.forEach(({path, imports}) => { diff --git a/test/test-cases/aliases/expected.css b/test/test-cases/aliases/expected.css index 1c2f926..26d4b12 100644 --- a/test/test-cases/aliases/expected.css +++ b/test/test-cases/aliases/expected.css @@ -1,10 +1,13 @@ -._aliases_borders__bar { - color: red; +._aliases_borders__dashed { + border: 4px dashed; } + + ._aliases_source__foo { background: aquamarine; border-color: red; } + @media (max-width: 599px) { ._aliases_source__foo { background: white; diff --git a/test/test-cases/aliases/expected.json b/test/test-cases/aliases/expected.json index abf2cf7..9cd043b 100644 --- a/test/test-cases/aliases/expected.json +++ b/test/test-cases/aliases/expected.json @@ -1,3 +1,5 @@ { - "foo": "_aliases_borders__bar _aliases_source__foo" + "borders": "\"./borders.css\"", + "breakpoints": "\"./breakpoints.css\"", + "foo": "_aliases_source__foo _aliases_borders__dashed" } diff --git a/test/test-cases/aliases/source.css b/test/test-cases/aliases/source.css index 932e74d..8d6e516 100644 --- a/test/test-cases/aliases/source.css +++ b/test/test-cases/aliases/source.css @@ -1,4 +1,4 @@ -@let borders: "./borders.css", breakpoints: "./breakpoints"; +@let borders: "./borders.css", breakpoints: "./breakpoints.css"; @import small from breakpoints; @import secondary, primary as blue from "./colors.css"; From 7b2501f3b332387ff95166442b6f19dd88903154 Mon Sep 17 00:00:00 2001 From: Glen Maddern Date: Sun, 13 Sep 2015 20:19:03 +1000 Subject: [PATCH 07/23] tidying up --- src/index.js | 2 +- src/wip.js | 4 +- test/test-cases.js | 39 +++++++++---------- .../{aliases => constants}/borders.css | 0 .../{aliases => constants}/breakpoints.css | 0 .../{aliases => constants}/colors.css | 0 .../{aliases => constants}/expected.css | 6 +-- .../{aliases => constants}/expected.json | 2 +- .../{aliases => constants}/source.css | 2 +- 9 files changed, 28 insertions(+), 27 deletions(-) rename test/test-cases/{aliases => constants}/borders.css (100%) rename test/test-cases/{aliases => constants}/breakpoints.css (100%) rename test/test-cases/{aliases => constants}/colors.css (100%) rename test/test-cases/{aliases => constants}/expected.css (61%) rename test/test-cases/{aliases => constants}/expected.json (56%) rename test/test-cases/{aliases => constants}/source.css (88%) diff --git a/src/index.js b/src/index.js index ba7ab71..b72bfd9 100644 --- a/src/index.js +++ b/src/index.js @@ -27,4 +27,4 @@ export default class Core { Core.localByDefault = localByDefault Core.extractImports = extractImports Core.scope = scope -Core.defaultPlugins = [wip, css => console.log(css.toResult().css), localByDefault, extractImports, scope] +Core.defaultPlugins = [wip, localByDefault, extractImports, scope] diff --git a/src/wip.js b/src/wip.js index 71b075f..2b3ef0a 100644 --- a/src/wip.js +++ b/src/wip.js @@ -53,7 +53,9 @@ export default css => { } }) - console.log(translations) + /* If we have no translations, don't continue */ + if (!Object.keys(translations).length) return + /* Perform replacements */ replaceSymbols(css, translations) diff --git a/test/test-cases.js b/test/test-cases.js index 1c80fac..c6adcff 100644 --- a/test/test-cases.js +++ b/test/test-cases.js @@ -18,7 +18,6 @@ Object.keys( pipelines ).forEach( dirname => { describe( dirname, () => { let testDir = path.join( __dirname, dirname ) fs.readdirSync( testDir ).forEach( testCase => { - if (testCase !== 'aliases') return if ( fs.existsSync( path.join( testDir, testCase, "source.css" ) ) ) { it( "should " + testCase.replace( /-/g, " " ), done => { let expected = normalize( fs.readFileSync( path.join( testDir, testCase, "expected.css" ), "utf-8" ) ) @@ -35,22 +34,22 @@ Object.keys( pipelines ).forEach( dirname => { } ) // special case for testing multiple sources -//describe( 'multiple sources', () => { -// let testDir = path.join( __dirname, 'test-cases' ) -// let testCase = 'multiple-sources'; -// let dirname = 'test-cases'; -// if ( fs.existsSync( path.join( testDir, testCase, "source1.css" ) ) ) { -// it( "should " + testCase.replace( /-/g, " " ), done => { -// let expected = normalize( fs.readFileSync( path.join( testDir, testCase, "expected.css" ), "utf-8" ) ) -// let loader = new FileSystemLoader( testDir, pipelines[dirname] ) -// let expectedTokens = JSON.parse( fs.readFileSync( path.join( testDir, testCase, "expected.json" ), "utf-8" ) ) -// loader.fetch( `${testCase}/source1.css`, "/" ).then( tokens1 => { -// loader.fetch( `${testCase}/source2.css`, "/" ).then( tokens2 => { -// assert.equal( loader.finalSource, expected ) -// const tokens = Object.assign({}, tokens1, tokens2); -// assert.equal( JSON.stringify( tokens ), JSON.stringify( expectedTokens ) ) -// } ).then( done, done ) -// }) -// } ); -// } -//} ); +describe( 'multiple sources', () => { + let testDir = path.join( __dirname, 'test-cases' ) + let testCase = 'multiple-sources'; + let dirname = 'test-cases'; + if ( fs.existsSync( path.join( testDir, testCase, "source1.css" ) ) ) { + it( "should " + testCase.replace( /-/g, " " ), done => { + let expected = normalize( fs.readFileSync( path.join( testDir, testCase, "expected.css" ), "utf-8" ) ) + let loader = new FileSystemLoader( testDir, pipelines[dirname] ) + let expectedTokens = JSON.parse( fs.readFileSync( path.join( testDir, testCase, "expected.json" ), "utf-8" ) ) + loader.fetch( `${testCase}/source1.css`, "/" ).then( tokens1 => { + loader.fetch( `${testCase}/source2.css`, "/" ).then( tokens2 => { + assert.equal( loader.finalSource, expected ) + const tokens = Object.assign({}, tokens1, tokens2); + assert.equal( JSON.stringify( tokens ), JSON.stringify( expectedTokens ) ) + } ).then( done, done ) + }) + } ); + } +} ); diff --git a/test/test-cases/aliases/borders.css b/test/test-cases/constants/borders.css similarity index 100% rename from test/test-cases/aliases/borders.css rename to test/test-cases/constants/borders.css diff --git a/test/test-cases/aliases/breakpoints.css b/test/test-cases/constants/breakpoints.css similarity index 100% rename from test/test-cases/aliases/breakpoints.css rename to test/test-cases/constants/breakpoints.css diff --git a/test/test-cases/aliases/colors.css b/test/test-cases/constants/colors.css similarity index 100% rename from test/test-cases/aliases/colors.css rename to test/test-cases/constants/colors.css diff --git a/test/test-cases/aliases/expected.css b/test/test-cases/constants/expected.css similarity index 61% rename from test/test-cases/aliases/expected.css rename to test/test-cases/constants/expected.css index 26d4b12..a843857 100644 --- a/test/test-cases/aliases/expected.css +++ b/test/test-cases/constants/expected.css @@ -1,15 +1,15 @@ -._aliases_borders__dashed { +._constants_borders__dashed { border: 4px dashed; } -._aliases_source__foo { +._constants_source__foo { background: aquamarine; border-color: red; } @media (max-width: 599px) { - ._aliases_source__foo { + ._constants_source__foo { background: white; } } diff --git a/test/test-cases/aliases/expected.json b/test/test-cases/constants/expected.json similarity index 56% rename from test/test-cases/aliases/expected.json rename to test/test-cases/constants/expected.json index 9cd043b..7cde827 100644 --- a/test/test-cases/aliases/expected.json +++ b/test/test-cases/constants/expected.json @@ -1,5 +1,5 @@ { "borders": "\"./borders.css\"", "breakpoints": "\"./breakpoints.css\"", - "foo": "_aliases_source__foo _aliases_borders__dashed" + "foo": "_constants_source__foo _constants_borders__dashed" } diff --git a/test/test-cases/aliases/source.css b/test/test-cases/constants/source.css similarity index 88% rename from test/test-cases/aliases/source.css rename to test/test-cases/constants/source.css index 8d6e516..a731753 100644 --- a/test/test-cases/aliases/source.css +++ b/test/test-cases/constants/source.css @@ -1,5 +1,5 @@ @let borders: "./borders.css", breakpoints: "./breakpoints.css"; -@import small from breakpoints; +@-import small from breakpoints; @import secondary, primary as blue from "./colors.css"; .foo { From 323d49d73fbe38fc9288db074e124e33df4f8ad7 Mon Sep 17 00:00:00 2001 From: Glen Maddern Date: Sun, 13 Sep 2015 20:56:25 +1000 Subject: [PATCH 08/23] expanded the breakpoints definition --- test/test-cases/constants/breakpoints.css | 4 +++- test/test-cases/constants/expected.css | 5 +++++ test/test-cases/constants/source.css | 7 ++++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/test/test-cases/constants/breakpoints.css b/test/test-cases/constants/breakpoints.css index bc85fe0..33257e3 100644 --- a/test/test-cases/constants/breakpoints.css +++ b/test/test-cases/constants/breakpoints.css @@ -1 +1,3 @@ -@let small: (max-width: 599px); +@let small (max-width: 599px); +@let medium (min-width: 600px) and (max-width: 959px); +@let large (min-width: 960px); diff --git a/test/test-cases/constants/expected.css b/test/test-cases/constants/expected.css index a843857..59ea63f 100644 --- a/test/test-cases/constants/expected.css +++ b/test/test-cases/constants/expected.css @@ -13,3 +13,8 @@ background: white; } } +@media (min-width: 600px) and (max-width: 959px) { + ._constants_source__foo { + background: peru; + } +} diff --git a/test/test-cases/constants/source.css b/test/test-cases/constants/source.css index a731753..ec8863c 100644 --- a/test/test-cases/constants/source.css +++ b/test/test-cases/constants/source.css @@ -1,5 +1,5 @@ @let borders: "./borders.css", breakpoints: "./breakpoints.css"; -@-import small from breakpoints; +@-import small, medium from breakpoints; @import secondary, primary as blue from "./colors.css"; .foo { @@ -13,3 +13,8 @@ background: white; } } +@media medium { + .foo { + background: peru; + } +} From d91582628ca3ce0460fcda8d050cd7d7a9f6011a Mon Sep 17 00:00:00 2001 From: Glen Maddern Date: Mon, 14 Sep 2015 00:47:39 +1000 Subject: [PATCH 09/23] added an example where a file is being imported and composed --- package.json | 2 +- src/index.js | 4 ++-- src/{wip.js => postcss-modules-constants.js} | 0 test/test-cases/constants/colors.css | 18 ++++++++++++++++-- test/test-cases/constants/expected.css | 16 ++++++++++++++-- test/test-cases/constants/expected.json | 2 +- test/test-cases/constants/source.css | 3 ++- 7 files changed, 36 insertions(+), 9 deletions(-) rename src/{wip.js => postcss-modules-constants.js} (100%) diff --git a/package.json b/package.json index ecec1f9..2742655 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ }, "dependencies": { "postcss": "^4.1.11", - "postcss-modules-extract-imports": "^0.0.5", + "postcss-modules-extract-imports": "^0.0.6", "postcss-modules-local-by-default": "^0.0.9", "postcss-modules-scope": "^0.0.8" }, diff --git a/src/index.js b/src/index.js index b72bfd9..b89be09 100644 --- a/src/index.js +++ b/src/index.js @@ -2,7 +2,7 @@ import postcss from 'postcss' import localByDefault from 'postcss-modules-local-by-default' import extractImports from 'postcss-modules-extract-imports' import scope from 'postcss-modules-scope' -import wip from "./wip" +import constants from "./postcss-modules-constants" import Parser from './parser' @@ -27,4 +27,4 @@ export default class Core { Core.localByDefault = localByDefault Core.extractImports = extractImports Core.scope = scope -Core.defaultPlugins = [wip, localByDefault, extractImports, scope] +Core.defaultPlugins = [constants, localByDefault, extractImports, scope] diff --git a/src/wip.js b/src/postcss-modules-constants.js similarity index 100% rename from src/wip.js rename to src/postcss-modules-constants.js diff --git a/test/test-cases/constants/colors.css b/test/test-cases/constants/colors.css index f6674cd..cf01725 100644 --- a/test/test-cases/constants/colors.css +++ b/test/test-cases/constants/colors.css @@ -1,2 +1,16 @@ -@let primary: aquamarine; -@let secondary: red; +@let primary: #f01; +@let secondary: #2f2; + +.text-primary { + color: primary; +} +.bg-primary { + background-color: primary; +} + +.text-secondary { + color: secondary; +} +.bg-secondary { + background-color: secondary; +} diff --git a/test/test-cases/constants/expected.css b/test/test-cases/constants/expected.css index 59ea63f..c47e0fc 100644 --- a/test/test-cases/constants/expected.css +++ b/test/test-cases/constants/expected.css @@ -1,11 +1,23 @@ ._constants_borders__dashed { border: 4px dashed; } +._constants_colors__text-primary { + color: #f01; +} +._constants_colors__bg-primary { + background-color: #f01; +} +._constants_colors__text-secondary { + color: #2f2; +} +._constants_colors__bg-secondary { + background-color: #2f2; +} ._constants_source__foo { - background: aquamarine; - border-color: red; + box-shadow: 0 0 10px #f01; + border-color: #2f2; } @media (max-width: 599px) { diff --git a/test/test-cases/constants/expected.json b/test/test-cases/constants/expected.json index 7cde827..2e70dc4 100644 --- a/test/test-cases/constants/expected.json +++ b/test/test-cases/constants/expected.json @@ -1,5 +1,5 @@ { "borders": "\"./borders.css\"", "breakpoints": "\"./breakpoints.css\"", - "foo": "_constants_source__foo _constants_borders__dashed" + "foo": "_constants_source__foo _constants_borders__dashed _constants_colors__text-secondary" } diff --git a/test/test-cases/constants/source.css b/test/test-cases/constants/source.css index ec8863c..fd4017f 100644 --- a/test/test-cases/constants/source.css +++ b/test/test-cases/constants/source.css @@ -4,7 +4,8 @@ .foo { composes: dashed from borders; - background: blue; + composes: text-secondary from "./colors.css"; + box-shadow: 0 0 10px blue; border-color: secondary; } From 08bae4d63d034b2ac51809bc873a194216bc7d51 Mon Sep 17 00:00:00 2001 From: Glen Maddern Date: Thu, 17 Sep 2015 18:23:35 +1000 Subject: [PATCH 10/23] changing to @define and @import-define --- src/postcss-modules-constants.js | 4 ++-- test/test-cases/constants/breakpoints.css | 6 +++--- test/test-cases/constants/colors.css | 4 ++-- test/test-cases/constants/expected.css | 3 +++ test/test-cases/constants/source.css | 7 +++++-- 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/postcss-modules-constants.js b/src/postcss-modules-constants.js index 2b3ef0a..79d5d3c 100644 --- a/src/postcss-modules-constants.js +++ b/src/postcss-modules-constants.js @@ -11,7 +11,7 @@ let createImportedName = options && options.createImportedName || ((importName/* export default css => { /* Find any local let rules and store them*/ let translations = {} - css.eachAtRule(/^-?let$/, atRule => { + css.eachAtRule(/^define$/, atRule => { let matches while (matches = matchLet.exec(atRule.params)) { let [/*match*/, key, value] = matches @@ -31,7 +31,7 @@ export default css => { /* Find imports and insert ICSS tmp vars */ let importAliases = [] - css.eachAtRule(/^-?import$/, atRule => { + css.eachAtRule(/^import(-define)?$/, atRule => { let matches = matchImports.exec(atRule.params) if (matches) { let [/*match*/, aliases, path] = matches diff --git a/test/test-cases/constants/breakpoints.css b/test/test-cases/constants/breakpoints.css index 33257e3..b1de8ac 100644 --- a/test/test-cases/constants/breakpoints.css +++ b/test/test-cases/constants/breakpoints.css @@ -1,3 +1,3 @@ -@let small (max-width: 599px); -@let medium (min-width: 600px) and (max-width: 959px); -@let large (min-width: 960px); +@define small (max-width: 599px); +@define medium (min-width: 600px) and (max-width: 959px); +@define large (min-width: 960px); diff --git a/test/test-cases/constants/colors.css b/test/test-cases/constants/colors.css index cf01725..969de28 100644 --- a/test/test-cases/constants/colors.css +++ b/test/test-cases/constants/colors.css @@ -1,5 +1,5 @@ -@let primary: #f01; -@let secondary: #2f2; +@define primary: #f01; +@define secondary: #2f2; .text-primary { color: primary; diff --git a/test/test-cases/constants/expected.css b/test/test-cases/constants/expected.css index c47e0fc..b5f566b 100644 --- a/test/test-cases/constants/expected.css +++ b/test/test-cases/constants/expected.css @@ -15,6 +15,9 @@ background-color: #2f2; } +/* Imports without a "from" are just passed through */ +@import url('./old-skool.css'); + ._constants_source__foo { box-shadow: 0 0 10px #f01; border-color: #2f2; diff --git a/test/test-cases/constants/source.css b/test/test-cases/constants/source.css index fd4017f..38016dd 100644 --- a/test/test-cases/constants/source.css +++ b/test/test-cases/constants/source.css @@ -1,7 +1,10 @@ -@let borders: "./borders.css", breakpoints: "./breakpoints.css"; -@-import small, medium from breakpoints; +@define borders: "./borders.css", breakpoints: "./breakpoints.css"; +@import-define small, medium from breakpoints; @import secondary, primary as blue from "./colors.css"; +/* Imports without a "from" are just passed through */ +@import url('./old-skool.css'); + .foo { composes: dashed from borders; composes: text-secondary from "./colors.css"; From eddbd967a004f7387a270b914efcf6be4a1aa9b9 Mon Sep 17 00:00:00 2001 From: Glen Maddern Date: Sat, 19 Sep 2015 14:20:56 +0200 Subject: [PATCH 11/23] using icss-replace-symbols --- package.json | 1 + src/parser.js | 2 +- src/postcss-modules-constants.js | 2 +- src/replace-symbols.js | 17 ----------------- 4 files changed, 3 insertions(+), 19 deletions(-) delete mode 100644 src/replace-symbols.js diff --git a/package.json b/package.json index 2742655..d15b372 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "test": "test" }, "dependencies": { + "icss-replace-symbols": "^1.0.0", "postcss": "^4.1.11", "postcss-modules-extract-imports": "^0.0.6", "postcss-modules-local-by-default": "^0.0.9", diff --git a/src/parser.js b/src/parser.js index a54dabb..c3d1731 100644 --- a/src/parser.js +++ b/src/parser.js @@ -1,5 +1,5 @@ const importRegexp = /^:import\((.+)\)$/ -import replaceSymbols from './replace-symbols' +import replaceSymbols from 'icss-replace-symbols' export default class Parser { constructor( pathFetcher, trace ) { diff --git a/src/postcss-modules-constants.js b/src/postcss-modules-constants.js index 79d5d3c..9cbdc62 100644 --- a/src/postcss-modules-constants.js +++ b/src/postcss-modules-constants.js @@ -1,5 +1,5 @@ import postcss from 'postcss' -import replaceSymbols from './replace-symbols' +import replaceSymbols from 'icss-replace-symbols' const matchImports = /^(.+?)\s+from\s+("[^"]*"|'[^']*'|[\w-]+)$/ const matchLet = /(?:,\s+|^)([\w-]+):?\s+("[^"]*"|'[^']*'|[^,]+)\s?/g diff --git a/src/replace-symbols.js b/src/replace-symbols.js deleted file mode 100644 index 31b37e2..0000000 --- a/src/replace-symbols.js +++ /dev/null @@ -1,17 +0,0 @@ -const matchConstName = /[\w-]+/g - -const replace = (declarations, object, propName) => { - let matches - while (matches = matchConstName.exec(object[propName])) { - let replacement = declarations[matches[0]] - if (replacement) { - object[propName] = object[propName].slice(0, matches.index) + replacement + object[propName].slice(matchConstName.lastIndex) - matchConstName.lastIndex -= matches[0].length - replacement.length - } - } -} - -export default (css, translations) => { - css.eachDecl(decl => replace(translations, decl, 'value')) - css.eachAtRule('media', atRule => replace(translations, atRule, 'params')) -} From 38e38c75c17fdfecca0f5f69b3b51872abdee55c Mon Sep 17 00:00:00 2001 From: Glen Maddern Date: Sat, 19 Sep 2015 17:04:30 +0200 Subject: [PATCH 12/23] using the beta plugins, we are postcss v5 compliant! --- package.json | 17 ++++--- src/index.js | 7 +-- src/postcss-modules-constants.js | 84 -------------------------------- 3 files changed, 13 insertions(+), 95 deletions(-) delete mode 100644 src/postcss-modules-constants.js diff --git a/package.json b/package.json index d15b372..e97e6fb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "css-modules-loader-core", - "version": "0.0.12", + "version": "1.0.0-beta1", "description": "A loader-agnostic CSS Modules implementation, based on PostCSS", "main": "lib/index.js", "directories": { @@ -8,17 +8,18 @@ }, "dependencies": { "icss-replace-symbols": "^1.0.0", - "postcss": "^4.1.11", - "postcss-modules-extract-imports": "^0.0.6", - "postcss-modules-local-by-default": "^0.0.9", - "postcss-modules-scope": "^0.0.8" + "postcss": "^5.0.5", + "postcss-modules-constants": "^1.0.0", + "postcss-modules-extract-imports": "1.0.0-beta2", + "postcss-modules-local-by-default": "css-modules/postcss-modules-local-by-default", + "postcss-modules-scope": "1.0.0-beta2" }, "devDependencies": { "babel": "^5.5.4", - "babel-eslint": "^3.1.14", + "babel-eslint": "^4.1.3", "babelify": "^6.1.2", - "chokidar-cli": "^0.2.1", - "eslint": "^0.22.1", + "chokidar-cli": "^1.0.1", + "eslint": "^1.5.0", "mocha": "^2.2.5" }, "scripts": { diff --git a/src/index.js b/src/index.js index b89be09..7a4ed09 100644 --- a/src/index.js +++ b/src/index.js @@ -2,7 +2,7 @@ import postcss from 'postcss' import localByDefault from 'postcss-modules-local-by-default' import extractImports from 'postcss-modules-extract-imports' import scope from 'postcss-modules-scope' -import constants from "./postcss-modules-constants" +import constants from 'postcss-modules-constants' import Parser from './parser' @@ -22,9 +22,10 @@ export default class Core { } } - -// These three plugins are aliased under this package for simplicity. +// These four plugins are aliased under this package for simplicity. Core.localByDefault = localByDefault Core.extractImports = extractImports Core.scope = scope +Core.constants = constants + Core.defaultPlugins = [constants, localByDefault, extractImports, scope] diff --git a/src/postcss-modules-constants.js b/src/postcss-modules-constants.js deleted file mode 100644 index 9cbdc62..0000000 --- a/src/postcss-modules-constants.js +++ /dev/null @@ -1,84 +0,0 @@ -import postcss from 'postcss' -import replaceSymbols from 'icss-replace-symbols' - -const matchImports = /^(.+?)\s+from\s+("[^"]*"|'[^']*'|[\w-]+)$/ -const matchLet = /(?:,\s+|^)([\w-]+):?\s+("[^"]*"|'[^']*'|[^,]+)\s?/g -const matchImport = /^([\w-]+)(?:\s+as\s+([\w-]+))?/ -let options = {} -let importIndex = 0 -let createImportedName = options && options.createImportedName || ((importName/*, path*/) => `i__const_${importName.replace(/\W/g, '_')}_${importIndex++}`) - -export default css => { - /* Find any local let rules and store them*/ - let translations = {} - css.eachAtRule(/^define$/, atRule => { - let matches - while (matches = matchLet.exec(atRule.params)) { - let [/*match*/, key, value] = matches - translations[key] = value - atRule.removeSelf() - } - }) - - /* We want to export anything defined by now, but don't add it to the CSS yet or - it well get picked up by the replacement stuff */ - let exportDeclarations = Object.keys(translations).map(key => postcss.decl({ - value: translations[key], - prop: key, - before: "\n ", - _autoprefixerDisabled: true - })) - - /* Find imports and insert ICSS tmp vars */ - let importAliases = [] - css.eachAtRule(/^import(-define)?$/, atRule => { - let matches = matchImports.exec(atRule.params) - if (matches) { - let [/*match*/, aliases, path] = matches - // We can use constants for path names - if (translations[path]) path = translations[path] - let imports = aliases.split(/\s*,\s*/).map(alias => { - let tokens = matchImport.exec(alias) - if (tokens) { - let [/*match*/, theirName, myName = theirName] = tokens - let importedName = createImportedName(myName) - translations[myName] = importedName - return {theirName, importedName} - } else { - throw new Error(`@import statement "${alias}" is invalid!`) - } - }) - importAliases.push({path, imports}) - atRule.removeSelf() - } - }) - - /* If we have no translations, don't continue */ - if (!Object.keys(translations).length) return - - /* Perform replacements */ - replaceSymbols(css, translations) - - /* Add import rules */ - importAliases.forEach(({path, imports}) => { - css.prepend(postcss.rule({ - selector: `:import(${path})`, - after: "\n", - nodes: imports.map(({theirName, importedName}) => postcss.decl({ - value: theirName, - prop: importedName, - before: "\n ", - _autoprefixerDisabled: true - })) - })) - }) - - /* Add export rules if any */ - if (exportDeclarations.length > 0) { - css.prepend(postcss.rule({ - selector: `:export`, - after: "\n", - nodes: exportDeclarations - })) - } -} From 377aeb0a59fa375ff328ea645b3315b998abefbf Mon Sep 17 00:00:00 2001 From: Glen Maddern Date: Tue, 22 Sep 2015 03:40:18 +0200 Subject: [PATCH 13/23] bumping to beta2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e97e6fb..049c7eb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "css-modules-loader-core", - "version": "1.0.0-beta1", + "version": "1.0.0-beta2", "description": "A loader-agnostic CSS Modules implementation, based on PostCSS", "main": "lib/index.js", "directories": { From d66853a399e451f71b5b8d0a4d6045cddeabba5c Mon Sep 17 00:00:00 2001 From: Glen Maddern Date: Tue, 22 Sep 2015 08:12:06 +0200 Subject: [PATCH 14/23] using local-by-default beta --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 049c7eb..b5efd65 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "css-modules-loader-core", - "version": "1.0.0-beta2", + "version": "1.0.0-beta3", "description": "A loader-agnostic CSS Modules implementation, based on PostCSS", "main": "lib/index.js", "directories": { @@ -11,7 +11,7 @@ "postcss": "^5.0.5", "postcss-modules-constants": "^1.0.0", "postcss-modules-extract-imports": "1.0.0-beta2", - "postcss-modules-local-by-default": "css-modules/postcss-modules-local-by-default", + "postcss-modules-local-by-default": "1.0.0-beta1", "postcss-modules-scope": "1.0.0-beta2" }, "devDependencies": { From 1d8e60887adcc89c6feac3a85c3d68ebf5ec0685 Mon Sep 17 00:00:00 2001 From: Glen Maddern Date: Tue, 22 Sep 2015 11:47:25 +0200 Subject: [PATCH 15/23] renamed the project to values --- package.json | 2 +- src/index.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index b5efd65..8ba9035 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "dependencies": { "icss-replace-symbols": "^1.0.0", "postcss": "^5.0.5", - "postcss-modules-constants": "^1.0.0", + "postcss-modules-values": "^1.0.0", "postcss-modules-extract-imports": "1.0.0-beta2", "postcss-modules-local-by-default": "1.0.0-beta1", "postcss-modules-scope": "1.0.0-beta2" diff --git a/src/index.js b/src/index.js index 7a4ed09..92795bd 100644 --- a/src/index.js +++ b/src/index.js @@ -2,7 +2,7 @@ import postcss from 'postcss' import localByDefault from 'postcss-modules-local-by-default' import extractImports from 'postcss-modules-extract-imports' import scope from 'postcss-modules-scope' -import constants from 'postcss-modules-constants' +import values from 'postcss-modules-values' import Parser from './parser' @@ -23,9 +23,9 @@ export default class Core { } // These four plugins are aliased under this package for simplicity. +Core.values = values Core.localByDefault = localByDefault Core.extractImports = extractImports Core.scope = scope -Core.constants = constants -Core.defaultPlugins = [constants, localByDefault, extractImports, scope] +Core.defaultPlugins = [values, localByDefault, extractImports, scope] From 2b5a65dc43e39c1ed9ad40b7c1537ec4aff27bc3 Mon Sep 17 00:00:00 2001 From: Glen Maddern Date: Tue, 22 Sep 2015 11:51:59 +0200 Subject: [PATCH 16/23] updated test cases to use the new stuff --- test/test-cases/constants/breakpoints.css | 3 --- test/test-cases/constants/expected.json | 5 ----- .../test-cases/{constants => values}/borders.css | 0 test/test-cases/values/breakpoints.css | 3 +++ test/test-cases/{constants => values}/colors.css | 4 ++-- .../{constants => values}/expected.css | 16 ++++++++-------- test/test-cases/values/expected.json | 9 +++++++++ test/test-cases/{constants => values}/source.css | 6 +++--- 8 files changed, 25 insertions(+), 21 deletions(-) delete mode 100644 test/test-cases/constants/breakpoints.css delete mode 100644 test/test-cases/constants/expected.json rename test/test-cases/{constants => values}/borders.css (100%) create mode 100644 test/test-cases/values/breakpoints.css rename test/test-cases/{constants => values}/colors.css (78%) rename test/test-cases/{constants => values}/expected.css (60%) create mode 100644 test/test-cases/values/expected.json rename test/test-cases/{constants => values}/source.css (66%) diff --git a/test/test-cases/constants/breakpoints.css b/test/test-cases/constants/breakpoints.css deleted file mode 100644 index b1de8ac..0000000 --- a/test/test-cases/constants/breakpoints.css +++ /dev/null @@ -1,3 +0,0 @@ -@define small (max-width: 599px); -@define medium (min-width: 600px) and (max-width: 959px); -@define large (min-width: 960px); diff --git a/test/test-cases/constants/expected.json b/test/test-cases/constants/expected.json deleted file mode 100644 index 2e70dc4..0000000 --- a/test/test-cases/constants/expected.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "borders": "\"./borders.css\"", - "breakpoints": "\"./breakpoints.css\"", - "foo": "_constants_source__foo _constants_borders__dashed _constants_colors__text-secondary" -} diff --git a/test/test-cases/constants/borders.css b/test/test-cases/values/borders.css similarity index 100% rename from test/test-cases/constants/borders.css rename to test/test-cases/values/borders.css diff --git a/test/test-cases/values/breakpoints.css b/test/test-cases/values/breakpoints.css new file mode 100644 index 0000000..0b80b8b --- /dev/null +++ b/test/test-cases/values/breakpoints.css @@ -0,0 +1,3 @@ +@value small (max-width: 599px); +@value medium (min-width: 600px) and (max-width: 959px); +@value large (min-width: 960px); diff --git a/test/test-cases/constants/colors.css b/test/test-cases/values/colors.css similarity index 78% rename from test/test-cases/constants/colors.css rename to test/test-cases/values/colors.css index 969de28..ff83776 100644 --- a/test/test-cases/constants/colors.css +++ b/test/test-cases/values/colors.css @@ -1,5 +1,5 @@ -@define primary: #f01; -@define secondary: #2f2; +@value primary: #f01; +@value secondary: #2f2; .text-primary { color: primary; diff --git a/test/test-cases/constants/expected.css b/test/test-cases/values/expected.css similarity index 60% rename from test/test-cases/constants/expected.css rename to test/test-cases/values/expected.css index b5f566b..6713279 100644 --- a/test/test-cases/constants/expected.css +++ b/test/test-cases/values/expected.css @@ -1,35 +1,35 @@ -._constants_borders__dashed { +._values_borders__dashed { border: 4px dashed; } -._constants_colors__text-primary { +._values_colors__text-primary { color: #f01; } -._constants_colors__bg-primary { +._values_colors__bg-primary { background-color: #f01; } -._constants_colors__text-secondary { +._values_colors__text-secondary { color: #2f2; } -._constants_colors__bg-secondary { +._values_colors__bg-secondary { background-color: #2f2; } /* Imports without a "from" are just passed through */ @import url('./old-skool.css'); -._constants_source__foo { +._values_source__foo { box-shadow: 0 0 10px #f01; border-color: #2f2; } @media (max-width: 599px) { - ._constants_source__foo { + ._values_source__foo { background: white; } } @media (min-width: 600px) and (max-width: 959px) { - ._constants_source__foo { + ._values_source__foo { background: peru; } } diff --git a/test/test-cases/values/expected.json b/test/test-cases/values/expected.json new file mode 100644 index 0000000..ee98683 --- /dev/null +++ b/test/test-cases/values/expected.json @@ -0,0 +1,9 @@ +{ + "borders": "\"./borders.css\"", + "breakpoints": "\"./breakpoints.css\"", + "small": "(max-width: 599px)", + "medium": "(min-width: 600px) and (max-width: 959px)", + "secondary": "#2f2", + "blue": "#f01", + "foo": "_values_source__foo _values_borders__dashed _values_colors__text-secondary" +} diff --git a/test/test-cases/constants/source.css b/test/test-cases/values/source.css similarity index 66% rename from test/test-cases/constants/source.css rename to test/test-cases/values/source.css index 38016dd..2c63c32 100644 --- a/test/test-cases/constants/source.css +++ b/test/test-cases/values/source.css @@ -1,6 +1,6 @@ -@define borders: "./borders.css", breakpoints: "./breakpoints.css"; -@import-define small, medium from breakpoints; -@import secondary, primary as blue from "./colors.css"; +@value borders: "./borders.css", breakpoints: "./breakpoints.css"; +@value small, medium from breakpoints; +@value secondary, primary as blue from "./colors.css"; /* Imports without a "from" are just passed through */ @import url('./old-skool.css'); From 9f8e9b5523a38d37b1da81375db01bee726271bf Mon Sep 17 00:00:00 2001 From: Glen Maddern Date: Tue, 22 Sep 2015 11:52:11 +0200 Subject: [PATCH 17/23] beta4 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8ba9035..cd00a56 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "css-modules-loader-core", - "version": "1.0.0-beta3", + "version": "1.0.0-beta4", "description": "A loader-agnostic CSS Modules implementation, based on PostCSS", "main": "lib/index.js", "directories": { From 08156aef147e35ae984839c142a96744fd036718 Mon Sep 17 00:00:00 2001 From: Glen Maddern Date: Tue, 20 Oct 2015 18:28:25 +1100 Subject: [PATCH 18/23] dont push old stuff --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index cd00a56..cb23091 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "build": "babel --out-dir lib src", "autotest": "chokidar src test -c 'npm test'", "test": "mocha --compilers js:babel/register", - "prepublish": "npm run build" + "prepublish": "rm -rf lib/* && npm run build" }, "repository": { "type": "git", From 75b21ebb433d48e034e3d5d584e4d61a47ce67ad Mon Sep 17 00:00:00 2001 From: Glen Maddern Date: Tue, 20 Oct 2015 18:43:41 +1100 Subject: [PATCH 19/23] update & lock down deps --- package.json | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index cb23091..6df9c00 100644 --- a/package.json +++ b/package.json @@ -7,20 +7,20 @@ "test": "test" }, "dependencies": { - "icss-replace-symbols": "^1.0.0", - "postcss": "^5.0.5", - "postcss-modules-values": "^1.0.0", + "icss-replace-symbols": "1.0.2", + "postcss": "5.0.10", + "postcss-modules-values": "1.1.0", "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", "postcss-modules-scope": "1.0.0-beta2" }, "devDependencies": { - "babel": "^5.5.4", - "babel-eslint": "^4.1.3", - "babelify": "^6.1.2", - "chokidar-cli": "^1.0.1", - "eslint": "^1.5.0", - "mocha": "^2.2.5" + "babel": "5.8.23", + "babel-eslint": "4.1.3", + "babelify": "6.3.0", + "chokidar-cli": "1.1.0", + "eslint": "1.7.2", + "mocha": "2.3.3" }, "scripts": { "lint": "eslint src", From 8cb6a18cc69d30dacdb1ebb185856157d8d038c1 Mon Sep 17 00:00:00 2001 From: Glen Maddern Date: Tue, 20 Oct 2015 18:46:15 +1100 Subject: [PATCH 20/23] whitespace diffs --- test/test-cases/values/expected.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test-cases/values/expected.css b/test/test-cases/values/expected.css index 6713279..0562fb8 100644 --- a/test/test-cases/values/expected.css +++ b/test/test-cases/values/expected.css @@ -1,6 +1,7 @@ ._values_borders__dashed { border: 4px dashed; } + ._values_colors__text-primary { color: #f01; } @@ -14,7 +15,6 @@ ._values_colors__bg-secondary { background-color: #2f2; } - /* Imports without a "from" are just passed through */ @import url('./old-skool.css'); From 62f4d1970f1a441100fb1da509a2cb585a488b4f Mon Sep 17 00:00:00 2001 From: Glen Maddern Date: Tue, 20 Oct 2015 18:50:22 +1100 Subject: [PATCH 21/23] woo no more deprecation warnings --- src/parser.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/parser.js b/src/parser.js index c3d1731..8dfb1dc 100644 --- a/src/parser.js +++ b/src/parser.js @@ -45,7 +45,7 @@ export default class Parser { this.exportTokens[decl.prop] = decl.value } } ) - exportNode.removeSelf() + exportNode.remove() } fetchImport( importNode, relativeTo, depNr ) { @@ -57,7 +57,7 @@ export default class Parser { this.translations[decl.prop] = exports[decl.value] } } ) - importNode.removeSelf() + importNode.remove() }, err => console.log( err ) ) } } From bad88832e7bd8b6d242508614aa5c05d47d27cb1 Mon Sep 17 00:00:00 2001 From: Glen Maddern Date: Wed, 21 Oct 2015 21:00:30 +1100 Subject: [PATCH 22/23] bumping deps to 1.0 --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 6df9c00..64c1f49 100644 --- a/package.json +++ b/package.json @@ -10,9 +10,9 @@ "icss-replace-symbols": "1.0.2", "postcss": "5.0.10", "postcss-modules-values": "1.1.0", - "postcss-modules-extract-imports": "1.0.0-beta2", + "postcss-modules-extract-imports": "1.0.0", "postcss-modules-local-by-default": "1.0.0", - "postcss-modules-scope": "1.0.0-beta2" + "postcss-modules-scope": "1.0.0" }, "devDependencies": { "babel": "5.8.23", From bf24ee6473694c9e924d645736f5115b21b28f34 Mon Sep 17 00:00:00 2001 From: Glen Maddern Date: Wed, 21 Oct 2015 21:02:46 +1100 Subject: [PATCH 23/23] just throwing a little global test case in there --- test/test-cases/multiple-dependencies/expected.json | 2 +- test/test-cases/multiple-dependencies/source.css | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/test/test-cases/multiple-dependencies/expected.json b/test/test-cases/multiple-dependencies/expected.json index 047434d..7327ebb 100644 --- a/test/test-cases/multiple-dependencies/expected.json +++ b/test/test-cases/multiple-dependencies/expected.json @@ -1,3 +1,3 @@ { - "a": "_multiple_dependencies_source__a _multiple_dependencies_b__b1 _multiple_dependencies_d__d1 _multiple_dependencies_d__d2 _multiple_dependencies_b__b2 _multiple_dependencies_c__c" + "a": "_multiple_dependencies_source__a _multiple_dependencies_b__b1 _multiple_dependencies_d__d1 _multiple_dependencies_d__d2 _multiple_dependencies_b__b2 _multiple_dependencies_c__c something-global" } diff --git a/test/test-cases/multiple-dependencies/source.css b/test/test-cases/multiple-dependencies/source.css index 46e1595..0824a26 100644 --- a/test/test-cases/multiple-dependencies/source.css +++ b/test/test-cases/multiple-dependencies/source.css @@ -1,6 +1,7 @@ .a { composes: b1 b2 from "./b.css"; composes: c from "./c.css"; + composes: something-global from global; color: #aaa; }