Skip to content

Commit 1e70e27

Browse files
authored
Merge pull request #79 from suitcss/upgrade-easy-import
Upgrade easy import
2 parents 5201f1a + e106291 commit 1e70e27

File tree

5 files changed

+90
-787
lines changed

5 files changed

+90
-787
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
### HEAD
22

33
* Refactor to support Node 4 upwards
4+
* Update `postcss-easy-import` to `2.0.0`
45
* Update `fs-extra` to `2.1.2`
56
* Update `autoprefixer` to `6.7.7`
67
* Update `postcss-apply` to `0.6.1`

lib/index.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ const bemLinter = require('postcss-bem-linter');
66
const cssnano = require('cssnano');
77
const difference = require('lodash.difference');
88
const encapsulationPlugins = require('./encapsulation');
9+
const fs = require('fs');
910
const isEmpty = require('lodash.isempty');
11+
const isPromise = require('is-promise');
12+
const pify = require('pify');
1013
const postcssEasyImport = require('postcss-easy-import');
1114
const reporter = require('postcss-reporter');
1215
const stylelint = require('stylelint');
@@ -37,7 +40,7 @@ const defaults = {
3740
ios > 6, android > 4.3, samsung > 3, chromeandroid > 50`
3841
},
3942
'postcss-easy-import': {
40-
transform: identity,
43+
load: getFileContent,
4144
onImport: noop
4245
},
4346
'postcss-reporter': {
@@ -111,16 +114,17 @@ function mergeOptions(options) {
111114
options = options || {};
112115
const mergedOpts = assign({}, defaults, options);
113116
const easyImportOpts = mergedOpts['postcss-easy-import'];
114-
const origTransform = easyImportOpts.transform;
117+
const origLoad = easyImportOpts.load;
115118
const origOnImport = easyImportOpts.onImport;
116119

117120
if (mergedOpts.root) {
118121
easyImportOpts.root = mergedOpts.root;
119122
}
120123

121-
easyImportOpts.transform = (css, filename) => {
122-
const transformedCss = origTransform(css);
123-
return lintFile(transformedCss, mergedOpts, filename).then(result => result.css);
124+
easyImportOpts.load = filename => {
125+
const transformedCss = origLoad(filename);
126+
return lintFile(transformedCss, mergedOpts, filename)
127+
.then(result => result.css);
124128
};
125129

126130
easyImportOpts.onImport = importedFiles => {
@@ -175,12 +179,12 @@ function lintFile(css, options, filename) {
175179
return processor.process(css, options.postcss);
176180
}
177181

178-
function isPromise(obj) {
179-
return typeof obj.then === 'function';
180-
}
181-
182182
function noop() {}
183183

184+
function getFileContent(filename) {
185+
return pify(fs.readFile)(filename, 'utf8');
186+
}
187+
184188
function identity(x) {
185189
return x;
186190
}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
"commander": "~2.9.0",
1818
"cssnano": "^3.3.2",
1919
"fs-extra": "^2.1.2",
20+
"is-promise": "^2.1.0",
2021
"lodash.difference": "^4.0.2",
2122
"lodash.isempty": "^4.1.1",
2223
"object-assign-deep": "0.0.4",
2324
"pad-component": "0.0.1",
25+
"pify": "^2.3.0",
2426
"postcss": "^5.2.17",
2527
"postcss-apply": "^0.6.1",
2628
"postcss-autoreset": "^1.2.0",
@@ -29,7 +31,7 @@
2931
"postcss-color-function": "^3.0.0",
3032
"postcss-custom-media": "^5.0.0",
3133
"postcss-custom-properties": "^5.0.0",
32-
"postcss-easy-import": "^1.0.1",
34+
"postcss-easy-import": "^2.0.0",
3335
"postcss-reporter": "^3.0.0",
3436
"read-file-stdin": "^0.2.1",
3537
"stylelint": "^7.10.1",

test/options.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ describe('using the `onImport` option in postcss-import', () => {
137137
});
138138
});
139139

140-
describe('using the `transform` option in postcss-import', () => {
141-
it('should use a default transform function that just returns the css', done => {
140+
describe('using the `load` option in postcss-import', () => {
141+
it('should use a default load function that just returns the css', done => {
142142
suitcss('@import "./util.css";', {
143143
root: 'test/fixtures',
144144
lint: false
@@ -149,30 +149,30 @@ describe('using the `transform` option in postcss-import', () => {
149149
.catch(done);
150150
});
151151

152-
it('should call a custom transform function with the imported component', done => {
153-
const transformStub = sinon.stub().returns('body { color: blue; }');
152+
it('should call a custom load function with the imported component', done => {
153+
const loadStub = sinon.stub().returns('body { color: blue; }');
154154

155155
suitcss('@import "./util.css";', {
156156
root: 'test/fixtures',
157157
lint: false,
158158
'postcss-easy-import': {
159-
transform: transformStub
159+
load: loadStub
160160
}
161161
}).then(result => {
162-
expect(transformStub.calledOnce).to.be.true;
163-
expect(transformStub.getCall(0).args[0]).to.equal('.u-img {\n border-radius: 50%;\n}\n');
162+
expect(loadStub.calledOnce).to.be.true;
163+
expect(loadStub.getCall(0).args[0]).to.contain('util.css');
164164
expect(result.css).to.equal('body { color: blue; }');
165165
done();
166166
})
167167
.catch(done);
168168
});
169169

170-
it('should also work with a promise returned from the custom transform function', done => {
170+
it('should also work with a promise returned from the custom load function', done => {
171171
suitcss('@import "./util.css";', {
172172
root: 'test/fixtures',
173173
lint: false,
174174
'postcss-easy-import': {
175-
transform() {
175+
load() {
176176
return Promise.resolve('body { font: red; }');
177177
}
178178
}

0 commit comments

Comments
 (0)