Skip to content

Commit 26614ee

Browse files
committed
Merge branch 'ertrzyiks-support-global-config-with-load-config-package'
2 parents 7786661 + 201661f commit 26614ee

25 files changed

+330
-139
lines changed

error.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function PostCSSLoaderError(error) {
2+
Error.call(this);
3+
Error.captureStackTrace(this, PostCSSLoaderError);
4+
this.name = 'Syntax Error';
5+
this.error = error.input.source;
6+
this.message = error.reason;
7+
if ( error.line ) {
8+
this.message += ' (' + error.line + ':' + error.column + ')';
9+
}
10+
if ( error.line && error.input.source ) {
11+
this.message += '\n\n' + error.showSourceCode() + '\n';
12+
}
13+
this.hideStack = true;
14+
}
15+
16+
PostCSSLoaderError.prototype = Object.create(Error.prototype);
17+
PostCSSLoaderError.prototype.constructor = PostCSSLoaderError;
18+
19+
module.exports = PostCSSLoaderError;

gulpfile.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
var gulp = require('gulp');
22
var path = require('path');
33

4+
var BUILD_CONFIGS = [
5+
'./test/webpack-default.config',
6+
'./test/webpack-explicit-plugins.config',
7+
'./test/webpack-with-packs.config',
8+
'./test/webpack-incorrect-using-packs.config',
9+
'./test/webpack-custom-parser.config'
10+
];
11+
412
gulp.task('clean', function (done) {
513
var fs = require('fs-extra');
614
fs.remove(path.join(__dirname, 'build'), done);
@@ -17,12 +25,17 @@ gulp.task('lint', function () {
1725
.pipe(eslint.failAfterError());
1826
});
1927

20-
gulp.task('build', ['clean'], function () {
21-
var webpack = require('webpack-stream');
22-
return gulp.src('')
23-
.pipe(webpack(require('./test/webpack.config')))
24-
.pipe(gulp.dest('build/'));
25-
});
28+
BUILD_CONFIGS
29+
.forEach(function (configFile) {
30+
gulp.task(configFile, ['clean'], function () {
31+
var webpack = require('webpack-stream');
32+
return gulp.src('')
33+
.pipe(webpack(require(configFile)))
34+
.pipe(gulp.dest('build/'));
35+
});
36+
});
37+
38+
gulp.task('build', BUILD_CONFIGS);
2639

2740
gulp.task('test', ['build'], function () {
2841
var mocha = require('gulp-mocha');

index.js

Lines changed: 88 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,121 @@
11
var loaderUtils = require('loader-utils');
2+
var loadConfig = require('postcss-load-config');
23
var postcss = require('postcss');
4+
var assign = require('object-assign');
35

4-
function PostCSSLoaderError(error) {
5-
Error.call(this);
6-
Error.captureStackTrace(this, PostCSSLoaderError);
7-
this.name = 'Syntax Error';
8-
this.error = error.input.source;
9-
this.message = error.reason;
10-
if ( error.line ) {
11-
this.message += ' (' + error.line + ':' + error.column + ')';
12-
}
13-
if ( error.line && error.input.source ) {
14-
this.message += '\n\n' + error.showSourceCode() + '\n';
15-
}
16-
this.hideStack = true;
17-
}
18-
19-
PostCSSLoaderError.prototype = Object.create(Error.prototype);
20-
PostCSSLoaderError.prototype.constructor = PostCSSLoaderError;
21-
22-
module.exports = function (source, map) {
23-
if ( this.cacheable ) this.cacheable();
24-
25-
var file = this.resourcePath;
26-
var params = loaderUtils.parseQuery(this.query);
27-
28-
var opts = {
29-
from: file,
30-
to: file,
31-
map: {
32-
inline: params.sourceMap === 'inline',
33-
annotation: false
34-
}
35-
};
36-
37-
if ( typeof map === 'string' ) map = JSON.parse(map);
38-
if ( map && map.mappings ) opts.map.prev = map;
6+
var PostCSSLoaderError = require('./error');
397

40-
var options = params.plugins || this.options.postcss;
8+
function parseOptions(options, pack) {
419
if ( typeof options === 'function' ) {
4210
options = options.call(this, this);
4311
}
4412

4513
var plugins;
46-
var exec;
47-
if ( typeof options === 'undefined' ) {
14+
if ( typeof options === 'undefined') {
4815
plugins = [];
4916
} else if ( Array.isArray(options) ) {
5017
plugins = options;
5118
} else {
5219
plugins = options.plugins || options.defaults;
53-
opts.stringifier = options.stringifier;
54-
opts.parser = options.parser;
55-
opts.syntax = options.syntax;
56-
exec = options.exec;
5720
}
58-
if ( params.pack ) {
59-
plugins = options[params.pack];
21+
22+
if ( pack ) {
23+
plugins = options[pack];
6024
if ( !plugins ) {
6125
throw new Error('PostCSS plugin pack is not defined in options');
6226
}
6327
}
6428

65-
if ( params.syntax ) {
66-
opts.syntax = require(params.syntax);
67-
}
68-
if ( params.parser ) {
69-
opts.parser = require(params.parser);
70-
}
71-
if ( params.stringifier ) {
72-
opts.stringifier = require(params.stringifier);
73-
}
74-
if ( params.exec ) {
75-
exec = params.exec;
29+
var opts = { };
30+
if ( typeof options !== 'undefined' ) {
31+
opts.stringifier = options.stringifier;
32+
opts.parser = options.parser;
33+
opts.syntax = options.syntax;
7634
}
7735

36+
var exec = options && options.exec;
37+
return Promise.resolve({ options: opts, plugins: plugins, exec: exec });
38+
}
39+
40+
module.exports = function (source, map) {
41+
if ( this.cacheable ) this.cacheable();
42+
43+
var file = this.resourcePath;
44+
var params = loaderUtils.parseQuery(this.query);
45+
46+
var options = params.plugins || this.options.postcss;
47+
var pack = params.pack;
7848
var loader = this;
7949
var callback = this.async();
8050

81-
if ( params.parser === 'postcss-js' || exec ) {
82-
source = this.exec(source, this.resource);
83-
}
51+
Promise.resolve().then(function () {
52+
if ( typeof options !== 'undefined' ) {
53+
return parseOptions(options, pack);
54+
} else {
55+
if ( pack ) {
56+
throw new Error('PostCSS plugin pack is supported ' +
57+
'only when config is passed explicitly');
58+
}
59+
return loadConfig(pack);
60+
}
61+
}).then(function (config) {
62+
var plugins = config.plugins;
8463

85-
// Allow plugins to add or remove postcss plugins
86-
if ( this._compilation ) {
87-
plugins = this._compilation.applyPluginsWaterfall(
88-
'postcss-loader-before-processing',
89-
[].concat(plugins),
90-
params
91-
);
92-
} else {
93-
loader.emitWarning(
94-
'this._compilation is not available thus ' +
95-
'`postcss-loader-before-processing` is not supported'
96-
);
97-
}
64+
var opts = assign({}, config.options, {
65+
from: file,
66+
to: file,
67+
map: {
68+
inline: params.sourceMap === 'inline',
69+
annotation: false
70+
}
71+
});
9872

99-
postcss(plugins).process(source, opts)
100-
.then(function (result) {
73+
if ( typeof map === 'string' ) map = JSON.parse(map);
74+
if ( map && map.mappings ) opts.map.prev = map;
75+
76+
if ( params.syntax ) {
77+
opts.syntax = require(params.syntax);
78+
}
79+
if ( params.parser ) {
80+
opts.parser = require(params.parser);
81+
}
82+
if ( params.stringifier ) {
83+
opts.stringifier = require(params.stringifier);
84+
}
85+
86+
var exec = params.exec || config.exec;
87+
if ( params.parser === 'postcss-js' || exec ) {
88+
source = loader.exec(source, loader.resource);
89+
}
90+
91+
// Allow plugins to add or remove postcss plugins
92+
if ( loader._compilation ) {
93+
plugins = loader._compilation.applyPluginsWaterfall(
94+
'postcss-loader-before-processing',
95+
[].concat(plugins),
96+
params
97+
);
98+
} else {
99+
loader.emitWarning(
100+
'this._compilation is not available thus ' +
101+
'`postcss-loader-before-processing` is not supported'
102+
);
103+
}
104+
105+
return postcss(plugins).process(source, opts).then(function (result) {
101106
result.warnings().forEach(function (msg) {
102107
loader.emitWarning(msg.toString());
103108
});
104-
callback(null, result.css, result.map ? result.map.toJSON() : null);
109+
110+
var resultMap = result.map ? result.map.toJSON() : null;
111+
callback(null, result.css, resultMap);
105112
return null;
106-
})
107-
.catch(function (error) {
108-
if ( error.name === 'CssSyntaxError' ) {
109-
callback(new PostCSSLoaderError(error));
110-
} else {
111-
callback(error);
112-
}
113113
});
114+
}).catch(function (error) {
115+
if ( error.name === 'CssSyntaxError' ) {
116+
callback(new PostCSSLoaderError(error));
117+
} else {
118+
callback(error);
119+
}
120+
});
114121
};

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
"license": "MIT",
88
"repository": "postcss/postcss-loader",
99
"dependencies": {
10-
"loader-utils": "^0.2.16",
11-
"postcss": "^5.2.4"
10+
"postcss-load-config": "^1.0.0-alpha4",
11+
"object-assign": "^4.1.0",
12+
"loader-utils": "^0.2.16",
13+
"postcss": "^5.2.4"
1214
},
1315
"devDependencies": {
1416
"eslint-config-postcss": "2.0.2",
@@ -20,6 +22,7 @@
2022
"postcss-js": "0.1.3",
2123
"gulp-mocha": "3.0.1",
2224
"fs-extra": "0.30.0",
25+
"sugarss": "0.2.0",
2326
"chai": "3.5.0",
2427
"gulp": "3.9.1"
2528
},

postcss.config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var config = {
2+
plugins: {}
3+
}
4+
5+
config.plugins[require.resolve(__dirname + '/test/support/plugins/blue')] = false;
6+
7+
module.exports = config

test/plugins/red.js

Lines changed: 0 additions & 14 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)