Skip to content

Commit 64387e6

Browse files
committed
custom parser plugin
1 parent 2d965b0 commit 64387e6

File tree

1 file changed

+50
-24
lines changed

1 file changed

+50
-24
lines changed

src/parser.js

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,66 @@
11
'use strict';
22

3-
import Parser from 'css-modules-loader-core/lib/parser';
3+
import { plugin } from 'postcss';
44

5-
class SyncParser extends Parser {
6-
plugin(css, result) {
7-
this.fetchAllImports(css);
8-
this.linkImportedSymbols(css);
9-
this.extractExports(css);
10-
}
5+
const importRegexp = /^:import\((.+)\)$/
6+
7+
export default plugin('parser', function (opts) {
8+
opts = opts || {};
9+
10+
let exportTokens = opts.exportTokens;
11+
let translations = {};
1112

12-
fetchImport(importNode, relativeTo, depNr) {
13+
const fetchImport = (importNode, relativeTo, depNr) => {
1314
let file = importNode.selector.match( importRegexp )[1];
14-
let depTrace = this.trace + String.fromCharCode(depNr);
15-
let exp = this.pathFetcher(file, relativeTo, depTrace);
15+
let depTrace = opts.trace + String.fromCharCode(depNr);
16+
let exports = opts.pathFetcher(file, relativeTo, depTrace);
1617

1718
importNode.each(decl => {
1819
if (decl.type === 'decl') {
19-
this.translations[decl.prop] = exports[decl.value]
20+
translations[decl.prop] = exports[decl.value];
2021
}
2122
});
2223

2324
importNode.removeSelf();
2425
}
2526

26-
fetchImport( importNode, relativeTo, depNr ) {
27-
let file = importNode.selector.match( importRegexp )[1],
28-
depTrace = this.trace + String.fromCharCode(depNr)
29-
return this.pathFetcher( file, relativeTo, depTrace ).then( exports => {
30-
importNode.each( decl => {
31-
if ( decl.type == 'decl' ) {
32-
this.translations[decl.prop] = exports[decl.value]
33-
}
34-
} )
35-
importNode.removeSelf()
36-
}, err => console.log( err ) )
27+
const fetchAllImports = css => {
28+
let imports = 0;
29+
30+
css.each(node => {
31+
if (node.type === 'rule' && node.selector.match(importRegexp)) {
32+
fetchImport(node, css.source.input.from, imports++);
33+
}
34+
});
3735
}
38-
}
3936

40-
export default SyncParser;
37+
const linkImportedSymbols = css => css.eachDecl(decl => {
38+
Object.keys(translations).forEach(translation => {
39+
decl.value = decl.value.replace(translation, translations[translation])
40+
});
41+
});
42+
43+
const handleExport = exportNode => {
44+
exportNode.each(decl => {
45+
if (decl.type === 'decl') {
46+
Object.keys(translations).forEach(translation => {
47+
decl.value = decl.value.replace(translation, translations[translation])
48+
});
49+
50+
exportTokens[decl.prop] = decl.value;
51+
}
52+
});
53+
54+
exportNode.removeSelf();
55+
}
56+
57+
const extractExports = css => css.each(node => {
58+
if (node.type === 'rule' && node.selector === ':export') handleExport(node);
59+
});
60+
61+
return css => {
62+
fetchAllImports(css);
63+
linkImportedSymbols(css);
64+
extractExports(css);
65+
}
66+
});

0 commit comments

Comments
 (0)