Skip to content

Commit e2e171a

Browse files
authored
Merge pull request #102 from css-modules/jest
Use yarn and test with jest
2 parents 13669e4 + cd1874f commit e2e171a

File tree

8 files changed

+3478
-209
lines changed

8 files changed

+3478
-209
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
2-
lib/
2+
coverage
3+
lib

.npmignore

Whitespace-only changes.

.travis.yml

-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,3 @@ node_js:
33
- "4"
44
- "6"
55
- "node"
6-
script: npm run travis
7-
8-
before_install:
9-
- '[ "${TRAVIS_NODE_VERSION}" != "0.10" ] || npm install -g npm'

package.json

+31-20
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,32 @@
33
"version": "1.3.0",
44
"description": "PostCSS plugin for CSS Modules to pass arbitrary values between your module files",
55
"main": "lib/index.js",
6+
"files": [
7+
"lib"
8+
],
69
"scripts": {
7-
"lint": "standard src test",
810
"build": "babel --out-dir lib src",
9-
"autotest": "chokidar src test -c 'npm test'",
10-
"test": "mocha --compilers js:babel-core/register",
11-
"posttest": "npm run lint && npm run build",
12-
"travis": "npm run test",
13-
"prepublish": "npm run build"
11+
"test": "jest --coverage",
12+
"precommit": "lint-staged",
13+
"prepublish": "yarn run test && yarn run build"
14+
},
15+
"lint-staged": {
16+
"*.js": [
17+
"prettier --single-quote --no-semi --write",
18+
"git add"
19+
]
20+
},
21+
"babel": {
22+
"presets": [
23+
[
24+
"env",
25+
{
26+
"targets": {
27+
"node": 4
28+
}
29+
}
30+
]
31+
]
1432
},
1533
"repository": {
1634
"type": "git",
@@ -29,23 +47,16 @@
2947
"homepage": "https://github.com/css-modules/postcss-modules-values#readme",
3048
"devDependencies": {
3149
"babel-cli": "^6.5.2",
32-
"babel-core": "^6.5.2",
33-
"babel-plugin-add-module-exports": "^0.2.1",
34-
"babel-preset-es2015": "^6.3.13",
35-
"chokidar": "^1.2.0",
36-
"mocha": "^3.0.2",
37-
"standard": "^8.4.0"
50+
"babel-jest": "^20.0.3",
51+
"babel-preset-env": "^1.5.0",
52+
"husky": "^0.13.3",
53+
"jest": "^20.0.3",
54+
"lint-staged": "^3.4.2",
55+
"prettier": "^1.3.1",
56+
"strip-indent": "^2.0.0"
3857
},
3958
"dependencies": {
4059
"icss-replace-symbols": "^1.1.0",
4160
"postcss": "^6.0.1"
42-
},
43-
"babel": {
44-
"presets": [
45-
"es2015"
46-
],
47-
"plugins": [
48-
"add-module-exports"
49-
]
5061
}
5162
}

src/index.js

+37-26
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
1-
import postcss from 'postcss'
2-
import replaceSymbols, {replaceAll} from 'icss-replace-symbols'
1+
const postcss = require('postcss')
2+
const { default: replaceSymbols, replaceAll } = require('icss-replace-symbols')
33

44
const matchImports = /^(.+?|\([\s\S]+?\))\s+from\s+("[^"]*"|'[^']*'|[\w-]+)$/
55
const matchValueDefinition = /(?:\s+|^)([\w-]+):?\s+(.+?)\s*$/g
66
const matchImport = /^([\w-]+)(?:\s+as\s+([\w-]+))?/
77
let options = {}
88
let importIndex = 0
9-
let createImportedName = options && options.createImportedName || ((importName/*, path*/) => `i__const_${importName.replace(/\W/g, '_')}_${importIndex++}`)
9+
let createImportedName =
10+
(options && options.createImportedName) ||
11+
((importName /*, path*/) =>
12+
`i__const_${importName.replace(/\W/g, '_')}_${importIndex++}`)
1013

11-
export default postcss.plugin('postcss-modules-values', () => (css, result) => {
14+
module.exports = postcss.plugin('postcss-modules-values', () => (
15+
css,
16+
result
17+
) => {
1218
let importAliases = []
1319
let definitions = {}
1420

1521
const addDefinition = atRule => {
1622
let matches
17-
while (matches = matchValueDefinition.exec(atRule.params)) {
18-
let [/*match*/, key, value] = matches
23+
while ((matches = matchValueDefinition.exec(atRule.params))) {
24+
let [, key, value] = matches
1925
// Add to the definitions, knowing that values can refer to each other
2026
definitions[key] = replaceAll(definitions, value)
2127
atRule.remove()
@@ -25,20 +31,23 @@ export default postcss.plugin('postcss-modules-values', () => (css, result) => {
2531
const addImport = atRule => {
2632
let matches = matchImports.exec(atRule.params)
2733
if (matches) {
28-
let [/*match*/, aliases, path] = matches
34+
let [, aliases, path] = matches
2935
// We can use constants for path names
3036
if (definitions[path]) path = definitions[path]
31-
let imports = aliases.replace(/^\(\s*([\s\S]+)\s*\)$/, '$1').split(/\s*,\s*/).map(alias => {
32-
let tokens = matchImport.exec(alias)
33-
if (tokens) {
34-
let [/*match*/, theirName, myName = theirName] = tokens
35-
let importedName = createImportedName(myName)
36-
definitions[myName] = importedName
37-
return { theirName, importedName }
38-
} else {
39-
throw new Error(`@import statement "${alias}" is invalid!`)
40-
}
41-
})
37+
let imports = aliases
38+
.replace(/^\(\s*([\s\S]+)\s*\)$/, '$1')
39+
.split(/\s*,\s*/)
40+
.map(alias => {
41+
let tokens = matchImport.exec(alias)
42+
if (tokens) {
43+
let [, /*match*/ theirName, myName = theirName] = tokens
44+
let importedName = createImportedName(myName)
45+
definitions[myName] = importedName
46+
return { theirName, importedName }
47+
} else {
48+
throw new Error(`@import statement "${alias}" is invalid!`)
49+
}
50+
})
4251
importAliases.push({ path, imports })
4352
atRule.remove()
4453
}
@@ -59,11 +68,13 @@ export default postcss.plugin('postcss-modules-values', () => (css, result) => {
5968

6069
/* We want to export anything defined by now, but don't add it to the CSS yet or
6170
it well get picked up by the replacement stuff */
62-
let exportDeclarations = Object.keys(definitions).map(key => postcss.decl({
63-
value: definitions[key],
64-
prop: key,
65-
raws: { before: "\n " }
66-
}))
71+
let exportDeclarations = Object.keys(definitions).map(key =>
72+
postcss.decl({
73+
value: definitions[key],
74+
prop: key,
75+
raws: { before: '\n ' }
76+
})
77+
)
6778

6879
/* If we have no definitions, don't continue */
6980
if (!Object.keys(definitions).length) return
@@ -75,7 +86,7 @@ export default postcss.plugin('postcss-modules-values', () => (css, result) => {
7586
if (exportDeclarations.length > 0) {
7687
let exportRule = postcss.rule({
7788
selector: `:export`,
78-
raws: { after: "\n" }
89+
raws: { after: '\n' }
7990
})
8091
exportRule.append(exportDeclarations)
8192
css.prepend(exportRule)
@@ -85,13 +96,13 @@ export default postcss.plugin('postcss-modules-values', () => (css, result) => {
8596
importAliases.reverse().forEach(({ path, imports }) => {
8697
let importRule = postcss.rule({
8798
selector: `:import(${path})`,
88-
raws: { after: "\n" }
99+
raws: { after: '\n' }
89100
})
90101
imports.forEach(({ theirName, importedName }) => {
91102
importRule.append({
92103
value: theirName,
93104
prop: importedName,
94-
raws: { before: "\n " }
105+
raws: { before: '\n ' }
95106
})
96107
})
97108

test/index.js

-158
This file was deleted.

0 commit comments

Comments
 (0)