Skip to content

Commit ebc64c3

Browse files
committed
Support file resolving
1 parent 51361f6 commit ebc64c3

File tree

8 files changed

+66
-18
lines changed

8 files changed

+66
-18
lines changed

.tape.js

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const path = require('path');
2+
13
module.exports = {
24
'basic/test': {
35
message: 'can inline rules from same file',
@@ -41,4 +43,13 @@ module.exports = {
4143
'complex/test': {
4244
message: 'all combined',
4345
},
46+
'resolvesFiles/test': {
47+
message: 'knows how to resolve files',
48+
options: {
49+
paths: [
50+
path.join(process.cwd(), '/test/resolvesFiles/some/other/path'),
51+
path.join(process.cwd(), '/test/resolvesFiles/weird'),
52+
],
53+
},
54+
},
4455
};

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sector-labs/postcss-inline-class",
3-
"version": "0.0.5",
3+
"version": "0.0.6",
44
"description": "Inline the declarations of other CSS classes in your CSS classes.",
55
"main": "src/index.js",
66
"repository": "https://github.com/sectorlabs/postcss-inline-class",
@@ -22,9 +22,9 @@
2222
"node": ">=6.0.0"
2323
},
2424
"dependencies": {
25+
"lodash": "latest",
2526
"postcss": "^7.0.1",
26-
"resolve": "^1.1.7",
27-
"lodash": "latest"
27+
"resolve": "^1.1.7"
2828
},
2929
"devDependencies": {
3030
"eslint": "^6.8.0",

src/index.js

+31-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const path = require('path');
22

33
const postcss = require('postcss');
4+
const resolvePath = require('resolve');
45

56
const readFile = require('./utils').readFile;
67
const parseImportPath = require('./utils').parseImportPath;
@@ -9,6 +10,11 @@ const findInlineDeclarations = require('./findInlineDeclarations');
910
const findNestedRules = require('./findNestedRules');
1011
const findMediaQueries = require('./findMediaQueries');
1112

13+
const resolveFile = (filePath, options) =>
14+
new Promise((resolve, reject) => {
15+
resolvePath(filePath, options, (err, path) => (err ? reject(err) : resolve(path)));
16+
});
17+
1218
const processAtRule = (onError, atRule, root, targetClass, importPath) => {
1319
const matchedDeclarations = findInlineDeclarations(root, targetClass);
1420
const nestedRules = findNestedRules(root, targetClass);
@@ -45,7 +51,7 @@ const processAtRule = (onError, atRule, root, targetClass, importPath) => {
4551
return [...nestedRules, ...mediaQueries];
4652
};
4753

48-
const walkAtRule = (root, result, promises) => (atRule) => {
54+
const walkAtRule = (root, result, promises, resolve) => (atRule) => {
4955
const params = postcss.list.space(atRule.params);
5056
const targetClass = params[0];
5157

@@ -60,25 +66,30 @@ const walkAtRule = (root, result, promises) => (atRule) => {
6066
}
6167

6268
const importPath = parseImportPath(params.slice(-1)[0]);
63-
const resolvedPath = path.resolve(path.dirname(root.source.input.file), importPath);
64-
6569
promises.push(
66-
readFile(resolvedPath)
67-
.then((rawData) => {
68-
const importedRoot = postcss.parse(rawData);
69-
return processAtRule(onError, atRule, importedRoot, targetClass, importPath);
70-
})
70+
resolve(importPath)
71+
.then((resolvedPath) =>
72+
readFile(resolvedPath).then((rawData) => {
73+
const importedRoot = postcss.parse(rawData);
74+
return processAtRule(onError, atRule, importedRoot, targetClass, importPath);
75+
}),
76+
)
7177
.catch(() => {
7278
onError(`Could not find file '${importPath}'`);
7379
return [];
7480
}),
7581
);
7682
};
7783

78-
const processFile = (root, result) => (resolve) => {
79-
const promises = [];
84+
const processFile = (root, result, paths) => (resolve) => {
85+
const fileResolver = (importPath) =>
86+
resolveFile(importPath, {
87+
basedir: path.dirname(root.source.input.file),
88+
paths,
89+
});
8090

81-
const atRuleWalker = walkAtRule(root, result, promises);
91+
const promises = [];
92+
const atRuleWalker = walkAtRule(root, result, promises, fileResolver);
8293

8394
root.walkRules((rule) => rule.walkAtRules('inline', atRuleWalker));
8495

@@ -90,6 +101,12 @@ const processFile = (root, result) => (resolve) => {
90101
.catch(resolve);
91102
};
92103

93-
module.exports = postcss.plugin('postcss-inline-class', () => (root, result) =>
94-
new Promise(processFile(root, result)),
95-
);
104+
const defaultOptions = Object.freeze({
105+
paths: [],
106+
extensions: [''],
107+
});
108+
109+
module.exports = postcss.plugin('postcss-inline-class', (options) => {
110+
return (root, result) =>
111+
new Promise(processFile(root, result, (options || defaultOptions).paths));
112+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.a {
2+
color: red;
3+
}

test/resolvesFiles/test.css

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.b {
2+
@inline .a from 'bar.css';
3+
}
4+
5+
.c {
6+
@inline .a from 'dir/foo.css';
7+
}

test/resolvesFiles/test.expect.css

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.b {
2+
color: red;
3+
}
4+
5+
.c {
6+
color: blue;
7+
}

test/resolvesFiles/weird/dir/foo.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.a {
2+
color: blue;
3+
}

yarn.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ locate-path@^2.0.0:
763763
p-locate "^2.0.0"
764764
path-exists "^3.0.0"
765765

766-
lodash@^4.17.14, lodash@^4.17.15:
766+
lodash@^4.17.14, lodash@^4.17.15, lodash@latest:
767767
version "4.17.15"
768768
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.15.tgz#b447f6670a0455bbfeedd11392eff330ea097548"
769769
integrity sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==

0 commit comments

Comments
 (0)