|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | | -import Parser from 'css-modules-loader-core/lib/parser'; |
| 3 | +import { plugin } from 'postcss'; |
4 | 4 |
|
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 = {}; |
11 | 12 |
|
12 | | - fetchImport(importNode, relativeTo, depNr) { |
| 13 | + const fetchImport = (importNode, relativeTo, depNr) => { |
13 | 14 | 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); |
16 | 17 |
|
17 | 18 | importNode.each(decl => { |
18 | 19 | if (decl.type === 'decl') { |
19 | | - this.translations[decl.prop] = exports[decl.value] |
| 20 | + translations[decl.prop] = exports[decl.value]; |
20 | 21 | } |
21 | 22 | }); |
22 | 23 |
|
23 | 24 | importNode.removeSelf(); |
24 | 25 | } |
25 | 26 |
|
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 | + }); |
37 | 35 | } |
38 | | -} |
39 | 36 |
|
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