Skip to content

Commit 4680f27

Browse files
committed
Merge pull request #4 from css-modules/interchange-format-tests
Breaking API changes & CSSI tests
2 parents cd05219 + b19de0b commit 4680f27

File tree

17 files changed

+116
-44
lines changed

17 files changed

+116
-44
lines changed

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55

66
## API
77

8+
```js
9+
import Core from 'css-modules-loader-core'
10+
let core = new Core()
11+
```
12+
813
### core.load( sourceString , sourcePath , pathFetcher ) =><br>&nbsp;&nbsp;Promise({ injectableSource, exportTokens })
914

1015
Processes the input CSS `sourceString`, looking for dependencies such as `@import` or `:import`. Any localisation will happen by prefixing a sanitised version of `sourcePath` When dependencies are found, it will ask the `pathFetcher` for each dependency, resolve & inline any imports, and return the following object:
@@ -14,22 +19,21 @@ Processes the input CSS `sourceString`, looking for dependencies such as `@impor
1419

1520
These should map nicely to what your build-tool-specific loader needs to do its job.
1621

17-
### core.plugins = pluginArray
22+
### new Core([plugins])
1823

1924
The default set of plugins is [[postcss-modules-local-by-default](https://github.com/css-modules/postcss-modules-local-by-default), [postcss-modules-extract-imports](https://github.com/css-modules/postcss-modules-extract-imports), [postcss-modules-scope](https://github.com/css-modules/postcss-modules-scope)] (i.e. the CSS Modules specification). This can override which PostCSS plugins you wish to execute, e.g.
2025

2126
```js
22-
import core from 'css-loader-core'
27+
import Core from 'css-loader-core'
2328
import autoprefixer from 'autoprefixer'
2429
import colorFunctions from 'postcss-color-function'
2530

2631
// Don't run local-by-default, but use colorFunctions
2732
// beforehand and autoprefixer afterwards:
28-
core.plugins = [
33+
let core = new Core([
2934
colorFunctions,
3035
core.plugins.extractImports,
3136
core.plugins.scope,
3237
autoprefixer("Last 2 Versions")
33-
]
38+
])
3439
```
35-

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
},
99
"dependencies": {
1010
"postcss": "^4.1.11",
11-
"postcss-modules-extract-imports": "0.0.2",
12-
"postcss-modules-local-by-default": "0.0.7",
13-
"postcss-modules-scope": "0.0.4"
11+
"postcss-modules-extract-imports": "0.0.3",
12+
"postcss-modules-local-by-default": "^0.0.7",
13+
"postcss-modules-scope": "^0.0.5"
1414
},
1515
"devDependencies": {
16-
"babel": "^5.4.7",
17-
"babel-eslint": "^3.1.11",
16+
"babel": "^5.5.4",
17+
"babel-eslint": "^3.1.14",
1818
"babelify": "^6.1.2",
1919
"chokidar-cli": "^0.2.1",
2020
"eslint": "^0.22.1",

src/file-system-loader.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ const traceKeySorter = ( a, b ) => {
2020
};
2121

2222
export default class FileSystemLoader {
23-
constructor( root ) {
23+
constructor( root, plugins ) {
2424
this.root = root
2525
this.sources = {}
2626
this.importNr = 0
27+
this.core = new Core(plugins)
2728
}
2829

2930
fetch( _newPath, relativeTo, _trace ) {
@@ -35,7 +36,7 @@ export default class FileSystemLoader {
3536

3637
fs.readFile( fileRelativePath, "utf-8", ( err, source ) => {
3738
if ( err ) reject( err )
38-
Core.load( source, rootRelativePath, trace, this.fetch.bind( this ) )
39+
this.core.load( source, rootRelativePath, trace, this.fetch.bind( this ) )
3940
.then( ( { injectableSource, exportTokens } ) => {
4041
this.sources[trace] = injectableSource
4142
resolve( exportTokens )

src/index.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,10 @@ import scope from 'postcss-modules-scope'
55

66
import Parser from './parser'
77

8-
export default {
9-
// These three plugins are aliased under this package for simplicity.
10-
localByDefault,
11-
extractImports,
12-
scope,
13-
14-
// The default set of plugins
15-
plugins: [
16-
localByDefault,
17-
extractImports,
18-
scope
19-
],
8+
export default class Core {
9+
constructor( plugins ) {
10+
this.plugins = plugins || Core.defaultPlugins
11+
}
2012

2113
load( sourceString, sourcePath, trace, pathFetcher ) {
2214
let parser = new Parser( pathFetcher, trace )
@@ -28,3 +20,10 @@ export default {
2820
} )
2921
}
3022
}
23+
24+
25+
// These three plugins are aliased under this package for simplicity.
26+
Core.localByDefault = localByDefault
27+
Core.extractImports = extractImports
28+
Core.scope = scope
29+
Core.defaultPlugins = [localByDefault, extractImports, scope]

src/parser.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export default class Parser {
1111

1212
plugin( css, result ) {
1313
return Promise.all( this.fetchAllImports( css ) )
14+
.then( _ => this.linkImportedSymbols( css ) )
1415
.then( _ => this.extractExports( css ) )
1516
}
1617

@@ -24,6 +25,14 @@ export default class Parser {
2425
return imports
2526
}
2627

28+
linkImportedSymbols( css ) {
29+
css.eachDecl( decl => {
30+
Object.keys(this.translations).forEach( translation => {
31+
decl.value = decl.value.replace(translation, this.translations[translation])
32+
} )
33+
})
34+
}
35+
2736
extractExports( css ) {
2837
css.each( node => {
2938
if ( node.type == "rule" && node.selector == ":export" ) this.handleExport( node )
@@ -48,7 +57,7 @@ export default class Parser {
4857
return this.pathFetcher( file, relativeTo, depTrace ).then( exports => {
4958
importNode.each( decl => {
5059
if ( decl.type == 'decl' ) {
51-
this.translations[decl.value] = exports[decl.prop]
60+
this.translations[decl.prop] = exports[decl.value]
5261
}
5362
} )
5463
importNode.removeSelf()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
:export {
2+
blackShadow: x__single_import_export_colors__blackShadow;
3+
}
4+
5+
.x__single_import_export_colors__blackShadow {
6+
box-shadow: 0 0 10px -2px black;
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.x__single_import_export_colors__blackShadow {
2+
box-shadow: 0 0 10px -2px black;
3+
}
4+
.x__single_import_export_source__localName {
5+
color: red;
6+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"localName": "x__single_import_export_source__localName x__single_import_export_colors__blackShadow"
3+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
:import("./colors.css") {
2+
i__tmp_import_djhgdsag: blackShadow;
3+
}
4+
5+
:export {
6+
localName: x__single_import_export_source__localName i__tmp_import_djhgdsag;
7+
}
8+
9+
.x__single_import_export_source__localName {
10+
color: red;
11+
}

test/cssi/pseudo-variables/colors.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
:export {
2+
black: #222;
3+
white: #ddd;
4+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
.x__lol {
3+
color: #222;
4+
background: #ddd;
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"lol": "x__lol"
3+
}

test/cssi/pseudo-variables/source.css

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
:import("./colors.css") {
2+
i__black: black;
3+
i__white: white;
4+
}
5+
6+
:export {
7+
lol: x__lol;
8+
}
9+
10+
.x__lol {
11+
color: i__black;
12+
background: i__white;
13+
}

test/test-cases.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,26 @@ let normalize = ( str ) => {
99
return str.replace( /\r\n?/g, "\n" );
1010
}
1111

12-
describe( "test-cases", () => {
13-
let testDir = path.join( __dirname, "test-cases" )
14-
fs.readdirSync( testDir ).forEach( testCase => {
15-
if ( fs.existsSync( path.join( testDir, testCase, "source.css" ) ) ) {
16-
it( "should " + testCase.replace( /-/g, " " ), done => {
17-
let expected = normalize( fs.readFileSync( path.join( testDir, testCase, "expected.css" ), "utf-8" ) )
18-
let loader = new FileSystemLoader( testDir )
19-
let expectedTokens = JSON.parse( fs.readFileSync( path.join( testDir, testCase, "expected.json" ), "utf-8" ) )
20-
loader.fetch( `${testCase}/source.css`, "/" ).then( tokens => {
21-
assert.equal( loader.finalSource, expected )
22-
assert.equal( JSON.stringify( tokens ), JSON.stringify( expectedTokens ) )
23-
} ).then( done, done )
24-
} );
25-
}
12+
const pipelines = {
13+
"test-cases": undefined,
14+
"cssi": []
15+
}
16+
17+
Object.keys( pipelines ).forEach( dirname => {
18+
describe( dirname, () => {
19+
let testDir = path.join( __dirname, dirname )
20+
fs.readdirSync( testDir ).forEach( testCase => {
21+
if ( fs.existsSync( path.join( testDir, testCase, "source.css" ) ) ) {
22+
it( "should " + testCase.replace( /-/g, " " ), done => {
23+
let expected = normalize( fs.readFileSync( path.join( testDir, testCase, "expected.css" ), "utf-8" ) )
24+
let loader = new FileSystemLoader( testDir, pipelines[dirname] )
25+
let expectedTokens = JSON.parse( fs.readFileSync( path.join( testDir, testCase, "expected.json" ), "utf-8" ) )
26+
loader.fetch( `${testCase}/source.css`, "/" ).then( tokens => {
27+
assert.equal( loader.finalSource, expected )
28+
assert.equal( JSON.stringify( tokens ), JSON.stringify( expectedTokens ) )
29+
} ).then( done, done )
30+
} );
31+
}
32+
} );
2633
} );
27-
} );
34+
} )
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
.b {
2-
extends: d from "./d.css";
2+
composes: d from "./d.css";
33
color: #bbb;
44
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.a {
2-
extends: b from "./b.css";
3-
extends: c from "./c.css";
2+
composes: b from "./b.css";
3+
composes: c from "./c.css";
44
color: #aaa;
55
}
66

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
.localName {
2-
extends: blackShadow from "./colors.css";
2+
composes: blackShadow from "./colors.css";
33
color: red;
44
}

0 commit comments

Comments
 (0)