Skip to content

Commit 4a47eed

Browse files
author
Andrey Kuzmin
committed
Allow to set config location
1 parent 2ee6a31 commit 4a47eed

File tree

2 files changed

+80
-6
lines changed

2 files changed

+80
-6
lines changed

index.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,24 @@ function withConfigLoader(cb) {
110110
})
111111
} else {
112112
var postcssLoadConfig = require('postcss-load-config')
113+
var contextOptions = plugins || {}
113114
return cb(function(file) {
114-
return postcssLoadConfig({
115-
file: file
116-
, options: plugins
117-
})
115+
var configPath
116+
if (contextOptions.config) {
117+
if (path.isAbsolute(contextOptions.config)) {
118+
configPath = contextOptions.config
119+
} else {
120+
configPath = path.join(file.base, contextOptions.config)
121+
}
122+
} else {
123+
configPath = file.dirname
124+
}
125+
return postcssLoadConfig(
126+
{ file: file
127+
, options: contextOptions
128+
},
129+
configPath
130+
)
118131
})
119132
}
120133
}

test.js

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ describe('PostCSS Guidelines', function () {
168168
postcssStub.use(plugins)
169169
return postcssStub
170170
}
171-
, 'postcss-load-config': function (ctx) {
172-
return postcssLoadConfigStub(ctx)
171+
, 'postcss-load-config': function (ctx, configPath) {
172+
return postcssLoadConfigStub(ctx, configPath)
173173
}
174174
, 'vinyl-sourcemaps-apply': function () {
175175
return {}
@@ -303,6 +303,67 @@ describe('PostCSS Guidelines', function () {
303303

304304
})
305305

306+
it('should point the config location to file directory', function (cb) {
307+
var cssPath = __dirname + '/fixture.css'
308+
var stream = postcss()
309+
postcssLoadConfigStub.returns(Promise.resolve({ plugins: [] }))
310+
postcssStub.process.returns(Promise.resolve({
311+
css: ''
312+
, warnings: function () {
313+
return []
314+
}
315+
}))
316+
stream.on('data', function () {
317+
assert.deepEqual(postcssLoadConfigStub.getCall(0).args[1], __dirname)
318+
cb()
319+
})
320+
stream.end(new gutil.File({
321+
contents: new Buffer('a {}')
322+
, path: cssPath
323+
}))
324+
})
325+
326+
it('should set the config location from option', function (cb) {
327+
var cssPath = __dirname + '/fixture.css'
328+
var stream = postcss({ config: '/absolute/path' })
329+
postcssLoadConfigStub.returns(Promise.resolve({ plugins: [] }))
330+
postcssStub.process.returns(Promise.resolve({
331+
css: ''
332+
, warnings: function () {
333+
return []
334+
}
335+
}))
336+
stream.on('data', function () {
337+
assert.deepEqual(postcssLoadConfigStub.getCall(0).args[1], '/absolute/path')
338+
cb()
339+
})
340+
stream.end(new gutil.File({
341+
contents: new Buffer('a {}')
342+
, path: cssPath
343+
}))
344+
})
345+
346+
it('should set the config location from option relative to the base dir', function (cb) {
347+
var cssPath = __dirname + '/src/fixture.css'
348+
var stream = postcss({ config: './relative/path' })
349+
postcssLoadConfigStub.returns(Promise.resolve({ plugins: [] }))
350+
postcssStub.process.returns(Promise.resolve({
351+
css: ''
352+
, warnings: function () {
353+
return []
354+
}
355+
}))
356+
stream.on('data', function () {
357+
assert.deepEqual(postcssLoadConfigStub.getCall(0).args[1], __dirname + '/relative/path')
358+
cb()
359+
})
360+
stream.end(new gutil.File({
361+
contents: new Buffer('a {}')
362+
, path: cssPath
363+
, base: __dirname
364+
}))
365+
})
366+
306367
it('should not override `from` and `map` if using gulp-sourcemaps', function (cb) {
307368
var stream = postcss([ doubler ], { from: 'overriden', map: 'overriden' })
308369
var cssPath = __dirname + '/fixture.css'

0 commit comments

Comments
 (0)