Skip to content

Commit 05a834d

Browse files
committed
proposal
1 parent 305fbdd commit 05a834d

File tree

8 files changed

+104
-0
lines changed

8 files changed

+104
-0
lines changed

.editorconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
11+
# cheat sheet: http://EditorConfig.org

index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const plugin = require('postcss').plugin;
2+
3+
const importRegexp = /^:import\((.+)\)$/;
4+
5+
/**
6+
* @param {object} [opts]
7+
* @param {boolean} opts.sync
8+
* @return {function}
9+
*/
10+
module.exports = plugin('postcss-modules-resolve-imports', function postcssModulesResolveImports(opts) {
11+
return function resolveImports(css, result) {
12+
// https://github.com/postcss/postcss/blob/master/docs/api.md#inputfile
13+
const filepath = css.source.input.file;
14+
const plugins = result.processor.plugins;
15+
16+
css.walkRules(importRegexp, rule => {
17+
const dependency = RegExp.$1.replace(/^["']|["']$/g, '');
18+
19+
// Шаги -> Резолвим путь -> Загружаем содержимое -> Прогоняем через postcss -> Достаем Root
20+
// -> Резолвим зависимости -> Вставляем ноды в исходное дерево (желательно использовать tracesort)
21+
// Получается, нужен кэш с файлами и конечный резолвер на случай, когда файлы загружены.
22+
//
23+
// По сути можно хранить список файлов и их контент + отдельно помечать глубину загрузки
24+
25+
// path RegExp.$1
26+
});
27+
28+
return css;
29+
};
30+
});

package.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "postcss-modules-resolve-imports",
3+
"version": "1.0.0",
4+
"description": "Resolves ICSS imports",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "npm run test:unit",
8+
"test:unit": "mocha --require test/setup.js --ui tdd test/index.js"
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "git+https://github.com/sullenor/postcss-modules-resolve-imports.git"
13+
},
14+
"keywords": [
15+
"css-modules"
16+
],
17+
"author": "Alexey Litvinov",
18+
"license": "MIT",
19+
"bugs": {
20+
"url": "https://github.com/sullenor/postcss-modules-resolve-imports/issues"
21+
},
22+
"homepage": "https://github.com/sullenor/postcss-modules-resolve-imports#readme",
23+
"dependencies": {
24+
"postcss": "^5.0.17"
25+
},
26+
"devDependencies": {
27+
"mocha": "^2.4.5",
28+
"postcss-modules-extract-imports": "^1.0.0",
29+
"postcss-modules-local-by-default": "^1.0.1",
30+
"postcss-modules-scope": "^1.0.0"
31+
}
32+
}

readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
postcss-modules-resolve-imports
2+
===============================
3+
4+
Resolves ICSS imports. Proposal.

test/external.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.d
2+
{
3+
background: white;
4+
}

test/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const postcss = require('postcss');
2+
3+
const LocalByDefault = require('postcss-modules-local-by-default');
4+
const ExtractImports = require('postcss-modules-extract-imports');
5+
const Scope = require('postcss-modules-scope');
6+
const ResolveImports = require('../index.js');
7+
8+
const readFileSync = require('fs').readFileSync;
9+
const resolve = require('path').resolve;
10+
11+
const css = readFileSync(resolve('test/source.css'), 'utf8');
12+
13+
const result = postcss([LocalByDefault, ExtractImports, Scope, ResolveImports])
14+
.process(css, {from: resolve('test/source.css')})
15+
.css;
16+
17+
console.log(result);

test/setup.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
global.assert = require('assert');

test/source.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.app
2+
{
3+
composes: d from './external.css';
4+
color: blue;
5+
}

0 commit comments

Comments
 (0)