Skip to content

Commit cd1874f

Browse files
committed
Add prettier
1 parent 179871d commit cd1874f

File tree

4 files changed

+663
-658
lines changed

4 files changed

+663
-658
lines changed

package.json

+12-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@
88
],
99
"scripts": {
1010
"build": "babel --out-dir lib src",
11-
"lint": "standard src test",
1211
"test": "jest --coverage",
13-
"posttest": "yarn run lint",
14-
"prepublish": "yarn run build"
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+
]
1520
},
1621
"babel": {
1722
"presets": [
@@ -44,8 +49,11 @@
4449
"babel-cli": "^6.5.2",
4550
"babel-jest": "^20.0.3",
4651
"babel-preset-env": "^1.5.0",
52+
"husky": "^0.13.3",
4753
"jest": "^20.0.3",
48-
"standard": "^8.4.0"
54+
"lint-staged": "^3.4.2",
55+
"prettier": "^1.3.1",
56+
"strip-indent": "^2.0.0"
4957
},
5058
"dependencies": {
5159
"icss-replace-symbols": "^1.1.0",

src/index.js

+36-25
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
const postcss = require('postcss')
2-
const {default: replaceSymbols, replaceAll} = require('icss-replace-symbols')
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-
module.exports = 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 @@ module.exports = 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 @@ module.exports = 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 @@ module.exports = 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 @@ module.exports = 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

0 commit comments

Comments
 (0)