Skip to content

Commit cd913bf

Browse files
committed
got the first passing spec!!
1 parent 250e95a commit cd913bf

4 files changed

Lines changed: 66 additions & 5 deletions

File tree

index.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,47 @@ Object.defineProperty(exports, '__esModule', {
66

77
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
88

9+
function _slicedToArray(arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i['return']) _i['return'](); } finally { if (_d) throw _e; } } return _arr; } else { throw new TypeError('Invalid attempt to destructure non-iterable instance'); } }
10+
911
var _postcss = require('postcss');
1012

1113
var _postcss2 = _interopRequireDefault(_postcss);
1214

13-
var processor = function processor(css, result) {};
15+
var declWhitelist = ['extends'],
16+
declFilter = new RegExp('^(' + declWhitelist.join('|') + ')$'),
17+
matchImports = /^([\w ]+) from "([^"]+)"$/;
18+
19+
var processor = function processor(css, result) {
20+
var imports = {};
21+
css.eachDecl(declFilter, function (decl) {
22+
var matches = decl.value.match(matchImports);
23+
if (matches) {
24+
(function () {
25+
var _matches = _slicedToArray(matches, 3);
26+
27+
var _ = _matches[0];
28+
var symbols = _matches[1];
29+
var path = _matches[2];
30+
31+
imports[path] = imports[path] || {};
32+
var tmpSymbols = symbols.split(' ').map(function (s) {
33+
return imports[path][s] = '__tmp-' + processor.getRandomStr();
34+
});
35+
decl.value = tmpSymbols.join(' ');
36+
})();
37+
}
38+
});
39+
Object.keys(imports).forEach(function (path) {
40+
var pathImports = imports[path];
41+
console.log(pathImports);
42+
css.prepend(_postcss2['default'].rule({
43+
selector: ':import("' + path + '")',
44+
nodes: Object.keys(pathImports).map(function (importedSymbol) {
45+
return _postcss2['default'].decl({ prop: importedSymbol, value: pathImports[importedSymbol] });
46+
})
47+
}));
48+
});
49+
};
1450

1551
processor.defaultRandomStr = function () {
1652
return Math.random().toString(36).substr(2, 8);

index.src.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,34 @@
11
import postcss from 'postcss'
22

3-
const processor = (css, result) => {
3+
const declWhitelist = ['extends'],
4+
declFilter = new RegExp(`^(${declWhitelist.join('|')})$`),
5+
matchImports = /^([\w ]+) from "([^"]+)"$/
46

7+
const processor = (css, result) => {
8+
let imports = {}
9+
css.eachDecl(declFilter, (decl) => {
10+
let matches = decl.value.match(matchImports)
11+
if (matches) {
12+
let [_, symbols, path] = matches
13+
imports[path] = imports[path] || {}
14+
let tmpSymbols = symbols.split(' ')
15+
.map(s => imports[path][s] = `__tmp-${processor.getRandomStr()}`)
16+
decl.value = tmpSymbols.join(' ')
17+
}
18+
})
19+
Object.keys(imports).forEach(path => {
20+
let pathImports = imports[path]
21+
console.log(pathImports)
22+
css.prepend(postcss.rule({
23+
selector: `:import("${path}")`,
24+
nodes: Object.keys(pathImports).map(importedSymbol => {
25+
return postcss.decl({prop: importedSymbol, value: pathImports[importedSymbol]})
26+
})
27+
}))
28+
})
529
}
630

7-
processor.defaultRandomStr = () => Math.random().toString(36).substr(2,8)
31+
processor.defaultRandomStr = () => Math.random().toString(36).substr(2, 8)
832
processor.getRandomStr = processor.defaultRandomStr // Easy to be mocked out
933

1034
export default processor

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"scripts": {
77
"watch": "chokidar index.src.js -c 'npm run build'",
88
"build": "babel -o index.js index.src.js",
9-
"test": "mocha --compilers js:babel/register"
9+
"test": "mocha --compilers js:babel/register",
10+
"autotest": "chokidar index.src.js test.js -c 'npm test'"
1011
},
1112
"repository": {
1213
"type": "git",

test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe("processor", () => {
1414
check(
1515
"it should extract an import within a :local",
1616
`:local(.exportName) { extends: importName from "path/library.css"; other: rule; }`,
17-
`:import("path/library.css") { importName: __tmp-rand0ml0l0l; } :local(.exportName) { extends: __tmp-rand0ml0l0l; other: rule; }`,
17+
`:import("path/library.css") {importName: __tmp-rand0ml0l0l; }\n:local(.exportName) { extends: __tmp-rand0ml0l0l; other: rule; }`,
1818
["rand0ml0l0l"]
1919
)
2020

0 commit comments

Comments
 (0)