Skip to content

Commit 1383b62

Browse files
Merge pull request #1 from css-modules/chore-deps-update
chore(deps): update
2 parents cf3b45b + e995f11 commit 1383b62

File tree

6 files changed

+3029
-172
lines changed

6 files changed

+3029
-172
lines changed

.eslintrc

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"parserOptions": {
3+
"ecmaVersion": 2018
4+
},
5+
"env": {
6+
"node": true
7+
},
8+
"rules": {
9+
"quotes": [2, "single"]
10+
}
11+
}

.travis.yml

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
language: node_js
22
node_js:
3-
- "4"
43
- "6"
5-
- "node"
4+
- "8"
5+
- "10"
6+
- "11"
67
script: npm run travis
78

8-
before_install:
9-
- '[ "${TRAVIS_NODE_VERSION}" != "0.10" ] || npm install -g npm'
9+
after_success:
10+
- cat ./coverage/lcov.info | node_modules/.bin/coveralls --verbose
11+
- cat ./coverage/coverage.json | node_modules/codecov.io/bin/codecov.io.js
12+
- rm -rf ./coverage

package.json

+18-24
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
{
22
"name": "postcss-modules-values",
3-
"version": "1.3.1",
3+
"version": "2.0.0",
44
"description": "PostCSS plugin for CSS Modules to pass arbitrary values between your module files",
5-
"main": "lib/index.js",
5+
"main": "src/index.js",
6+
"files": [
7+
"src"
8+
],
69
"scripts": {
7-
"lint": "standard src test",
8-
"build": "babel --out-dir lib src",
10+
"lint": "eslint src test",
11+
"pretest": "yarn lint",
12+
"test": "mocha",
913
"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"
14+
"cover": "nyc mocha",
15+
"travis": "yarn lint && yarn cover",
16+
"prepublish": "yarn test"
1417
},
1518
"repository": {
1619
"type": "git",
@@ -28,24 +31,15 @@
2831
},
2932
"homepage": "https://github.com/css-modules/postcss-modules-values#readme",
3033
"devDependencies": {
31-
"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"
34+
"chokidar-cli": "^1.0.1",
35+
"codecov.io": "^0.1.2",
36+
"coveralls": "^3.0.2",
37+
"mocha": "^5.2.0",
38+
"eslint": "^5.9.0",
39+
"nyc": "^13.1.0"
3840
},
3941
"dependencies": {
4042
"icss-replace-symbols": "^1.1.0",
41-
"postcss": "^6.0.1"
42-
},
43-
"babel": {
44-
"presets": [
45-
"es2015"
46-
],
47-
"plugins": [
48-
"add-module-exports"
49-
]
43+
"postcss": "^7.0.6"
5044
}
5145
}

src/index.js

+105-86
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,119 @@
1-
import postcss from 'postcss'
2-
import replaceSymbols, {replaceAll} from 'icss-replace-symbols'
1+
'use strict';
32

4-
const matchImports = /^(.+?|\([\s\S]+?\))\s+from\s+("[^"]*"|'[^']*'|[\w-]+)$/
5-
const matchValueDefinition = /(?:\s+|^)([\w-]+):?\s+(.+?)\s*$/g
6-
const matchImport = /^([\w-]+)(?:\s+as\s+([\w-]+))?/
7-
let options = {}
8-
let importIndex = 0
9-
let createImportedName = options && options.createImportedName || ((importName/*, path*/) => `i__const_${importName.replace(/\W/g, '_')}_${importIndex++}`)
3+
const postcss = require('postcss');
4+
const ICSSReplaceSymbols = require('icss-replace-symbols');
5+
const replaceSymbols = require('icss-replace-symbols');
106

11-
export default postcss.plugin('postcss-modules-values', () => (css, result) => {
12-
let importAliases = []
13-
let definitions = {}
7+
const matchImports = /^(.+?|\([\s\S]+?\))\s+from\s+("[^"]*"|'[^']*'|[\w-]+)$/;
8+
const matchValueDefinition = /(?:\s+|^)([\w-]+):?\s+(.+?)\s*$/g;
9+
const matchImport = /^([\w-]+)(?:\s+as\s+([\w-]+))?/;
1410

15-
const addDefinition = atRule => {
16-
let matches
17-
while (matches = matchValueDefinition.exec(atRule.params)) {
18-
let [/*match*/, key, value] = matches
19-
// Add to the definitions, knowing that values can refer to each other
20-
definitions[key] = replaceAll(definitions, value)
21-
atRule.remove()
22-
}
23-
}
11+
let options = {};
12+
let importIndex = 0;
13+
let createImportedName =
14+
(options && options.createImportedName) ||
15+
((importName /*, path*/) =>
16+
`i__const_${importName.replace(/\W/g, '_')}_${importIndex++}`);
2417

25-
const addImport = atRule => {
26-
let matches = matchImports.exec(atRule.params)
27-
if (matches) {
28-
let [/*match*/, aliases, path] = matches
29-
// We can use constants for path names
30-
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-
})
42-
importAliases.push({ path, imports })
43-
atRule.remove()
44-
}
45-
}
18+
module.exports = postcss.plugin(
19+
'postcss-modules-values',
20+
() => (css, result) => {
21+
const importAliases = [];
22+
const definitions = {};
4623

47-
/* Look at all the @value statements and treat them as locals or as imports */
48-
css.walkAtRules('value', atRule => {
49-
if (matchImports.exec(atRule.params)) {
50-
addImport(atRule)
51-
} else {
52-
if (atRule.params.indexOf('@value') !== -1) {
53-
result.warn('Invalid value definition: ' + atRule.params)
24+
const addDefinition = atRule => {
25+
let matches;
26+
while ((matches = matchValueDefinition.exec(atRule.params))) {
27+
let [, /*match*/ key, value] = matches;
28+
// Add to the definitions, knowing that values can refer to each other
29+
definitions[key] = replaceSymbols.replaceAll(definitions, value);
30+
atRule.remove();
5431
}
32+
};
5533

56-
addDefinition(atRule)
57-
}
58-
})
34+
const addImport = atRule => {
35+
const matches = matchImports.exec(atRule.params);
36+
if (matches) {
37+
let [, /*match*/ aliases, path] = matches;
38+
// We can use constants for path names
39+
if (definitions[path]) {
40+
path = definitions[path];
41+
}
42+
const imports = aliases
43+
.replace(/^\(\s*([\s\S]+)\s*\)$/, '$1')
44+
.split(/\s*,\s*/)
45+
.map(alias => {
46+
const tokens = matchImport.exec(alias);
47+
if (tokens) {
48+
const [, /*match*/ theirName, myName = theirName] = tokens;
49+
const importedName = createImportedName(myName);
50+
definitions[myName] = importedName;
51+
return { theirName, importedName };
52+
} else {
53+
throw new Error(`@import statement "${alias}" is invalid!`);
54+
}
55+
});
56+
importAliases.push({ path, imports });
57+
atRule.remove();
58+
}
59+
};
5960

60-
/* We want to export anything defined by now, but don't add it to the CSS yet or
61+
/* Look at all the @value statements and treat them as locals or as imports */
62+
css.walkAtRules('value', atRule => {
63+
if (matchImports.exec(atRule.params)) {
64+
addImport(atRule);
65+
} else {
66+
if (atRule.params.indexOf('@value') !== -1) {
67+
result.warn('Invalid value definition: ' + atRule.params);
68+
}
69+
70+
addDefinition(atRule);
71+
}
72+
});
73+
74+
/* We want to export anything defined by now, but don't add it to the CSS yet or
6175
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-
}))
76+
const exportDeclarations = Object.keys(definitions).map(key =>
77+
postcss.decl({
78+
value: definitions[key],
79+
prop: key,
80+
raws: { before: '\n ' }
81+
})
82+
);
6783

68-
/* If we have no definitions, don't continue */
69-
if (!Object.keys(definitions).length) return
84+
/* If we have no definitions, don't continue */
85+
if (!Object.keys(definitions).length) {
86+
return;
87+
}
7088

71-
/* Perform replacements */
72-
replaceSymbols(css, definitions)
89+
/* Perform replacements */
90+
ICSSReplaceSymbols.default(css, definitions);
7391

74-
/* Add export rules if any */
75-
if (exportDeclarations.length > 0) {
76-
let exportRule = postcss.rule({
77-
selector: `:export`,
78-
raws: { after: "\n" }
79-
})
80-
exportRule.append(exportDeclarations)
81-
css.prepend(exportRule)
82-
}
92+
/* Add export rules if any */
93+
if (exportDeclarations.length > 0) {
94+
const exportRule = postcss.rule({
95+
selector: ':export',
96+
raws: { after: '\n' }
97+
});
98+
exportRule.append(exportDeclarations);
99+
css.prepend(exportRule);
100+
}
83101

84-
/* Add import rules */
85-
importAliases.reverse().forEach(({ path, imports }) => {
86-
let importRule = postcss.rule({
87-
selector: `:import(${path})`,
88-
raws: { after: "\n" }
89-
})
90-
imports.forEach(({ theirName, importedName }) => {
91-
importRule.append({
92-
value: theirName,
93-
prop: importedName,
94-
raws: { before: "\n " }
95-
})
96-
})
102+
/* Add import rules */
103+
importAliases.reverse().forEach(({ path, imports }) => {
104+
const importRule = postcss.rule({
105+
selector: `:import(${path})`,
106+
raws: { after: '\n' }
107+
});
108+
imports.forEach(({ theirName, importedName }) => {
109+
importRule.append({
110+
value: theirName,
111+
prop: importedName,
112+
raws: { before: '\n ' }
113+
});
114+
});
97115

98-
css.prepend(importRule)
99-
})
100-
})
116+
css.prepend(importRule);
117+
});
118+
}
119+
);

0 commit comments

Comments
 (0)