Skip to content

Commit 0dceb2c

Browse files
feat(index): add ctx, ctx.file, ctx.options
1 parent d591cdf commit 0dceb2c

File tree

2 files changed

+116
-114
lines changed

2 files changed

+116
-114
lines changed

index.js

+108-105
Original file line numberDiff line numberDiff line change
@@ -1,151 +1,154 @@
1-
var loaderUtils = require('loader-utils');
2-
var loadConfig = require('postcss-load-config');
3-
var postcss = require('postcss');
4-
var assign = require('object-assign');
5-
var path = require('path');
1+
const path = require('path');
2+
const loaderUtils = require('loader-utils');
63

7-
var PostCSSLoaderError = require('./error');
4+
const postcss = require('postcss');
5+
const postcssrc = require('postcss-load-config');
86

9-
function parseOptions(options, pack) {
10-
if ( typeof options === 'function' ) {
11-
options = options.call(this, this);
7+
const PostCSSLoaderError = require('./error');
8+
9+
function parseOptions(params) {
10+
if (typeof params === 'function') {
11+
params = params.call(this, this);
1212
}
1313

14-
var plugins;
15-
if ( typeof options === 'undefined') {
14+
console.log('params', params);
15+
16+
let plugins;
17+
18+
if (typeof params === 'undefined') {
1619
plugins = [];
17-
} else if ( Array.isArray(options) ) {
18-
plugins = options;
20+
} else if (Array.isArray(params)) {
21+
plugins = params;
1922
} else {
20-
plugins = options.plugins || options.defaults;
23+
plugins = params.plugins || params.defaults;
2124
}
2225

23-
if ( pack ) {
24-
plugins = options[pack];
25-
if ( !plugins ) {
26-
throw new Error('PostCSS plugin pack is not defined in options');
27-
}
28-
}
26+
console.log('plugins', plugins);
27+
28+
let options = {};
2929

30-
var opts = { };
31-
if ( typeof options !== 'undefined' ) {
32-
opts.stringifier = options.stringifier;
33-
opts.parser = options.parser;
34-
opts.syntax = options.syntax;
30+
if (typeof params !== 'undefined') {
31+
options.parser = params.parser;
32+
options.syntax = params.syntax;
33+
options.stringifier = params.stringifier;
3534
}
3635

37-
var exec = options && options.exec;
38-
return Promise.resolve({ options: opts, plugins: plugins, exec: exec });
36+
// console.log('options', options);
37+
38+
let exec = params && params.exec;
39+
40+
// console.log('exec', exec);
41+
42+
return Promise.resolve({ options: options, plugins: plugins, exec: exec });
3943
}
4044

41-
module.exports = function (source, map) {
42-
if ( this.cacheable ) this.cacheable();
45+
module.exports = function (css, map) {
46+
if (this.cacheable) this.cacheable();
4347

44-
var loader = this;
45-
var file = loader.resourcePath;
46-
var params = loaderUtils.getOptions(loader) || {};
48+
const loader = this;
4749

48-
var options = params.plugins || loader.options.postcss;
49-
var pack = params.pack;
50-
var callback = loader.async();
50+
const file = loader.resourcePath;
51+
const params = loaderUtils.getOptions(loader) || {};
5152

52-
var configPath;
53+
const settings = params.plugins || loader.options.postcss;
5354

54-
if (params.config) {
55-
if (path.isAbsolute(params.config)) {
56-
configPath = params.config;
57-
} else {
58-
configPath = path.join(process.cwd(), params.config);
59-
}
60-
} else {
61-
configPath = path.dirname(file);
62-
}
55+
const callback = loader.async();
6356

64-
Promise.resolve().then(function () {
65-
if ( typeof options !== 'undefined' ) {
66-
return parseOptions.call(loader, options, pack);
67-
} else {
68-
if ( pack ) {
69-
throw new Error('PostCSS plugin pack is supported ' +
70-
'only when use plugins in webpack config');
71-
}
72-
return loadConfig({ webpack: loader }, configPath, { argv: false });
57+
let rc;
58+
let context = {
59+
extname: path.extname(file),
60+
dirname: path.dirname(file),
61+
basename: path.basename(file),
62+
webpack: { watch: loader.addDependency }
63+
};
64+
65+
params.config ?
66+
rc = path.resolve(params.config) :
67+
rc = path.dirname(file);
68+
69+
Promise.resolve().then(() => {
70+
if (typeof settings !== 'undefined') {
71+
return parseOptions.call(loader, settings);
7372
}
74-
}).then(function (config) {
75-
if ( !config ) config = { };
7673

77-
if ( config.file ) loader.addDependency(config.file);
74+
return postcssrc(context, rc, { argv: false });
75+
}).then((config) => {
76+
if (!config) config = {};
7877

79-
var plugins = config.plugins || [];
78+
if (config.file) loader.addDependency(config.file);
8079

81-
var opts = assign({}, config.options, {
80+
console.log('Config Plugins', config.plugins);
81+
82+
let plugins = config.plugins || [];
83+
84+
console.log('Plugins', plugins);
85+
console.log('webpack Version', process.env.WEBPACK_VERSION);
86+
87+
let options = Object.assign({}, config.options, {
8288
from: file,
83-
to: file,
89+
to: file,
8490
map: {
85-
inline: params.sourceMap === 'inline',
91+
inline: params.sourceMap === 'inline',
8692
annotation: false
8793
}
8894
});
8995

90-
if ( typeof map === 'string' ) map = JSON.parse(map);
91-
if ( map && map.mappings ) opts.map.prev = map;
96+
if (typeof map === 'string') map = JSON.parse(map);
97+
if (map && map.mappings) options.map.prev = map;
9298

93-
if ( params.syntax ) {
94-
if ( typeof params.syntax === 'string' ) {
95-
opts.syntax = require(params.syntax);
96-
} else {
97-
opts.syntax = params.syntax;
98-
}
99+
if (typeof options.syntax === 'string') {
100+
options.syntax = require(options.syntax);
99101
}
100-
if ( params.parser ) {
101-
if ( typeof params.parser === 'string' ) {
102-
opts.parser = require(params.parser);
103-
} else {
104-
opts.parser = params.parser;
105-
}
102+
103+
if (typeof options.parser === 'string') {
104+
options.parser = require(options.parser);
106105
}
107-
if ( params.stringifier ) {
108-
if ( typeof params.stringifier === 'string' ) {
109-
opts.stringifier = require(params.stringifier);
110-
} else {
111-
opts.stringifier = params.stringifier;
112-
}
106+
107+
if (typeof options.stringifier === 'string') {
108+
options.stringifier = require(options.stringifier);
113109
}
114110

115-
var exec = params.exec || config.exec;
116-
if ( params.parser === 'postcss-js' || exec ) {
117-
source = loader.exec(source, loader.resource);
111+
// console.log('Options', options);
112+
113+
let exec = options.exec || config.exec;
114+
115+
if (options.parser === 'postcss-js' || exec) {
116+
css = loader.exec(css, loader.resource);
118117
}
119118

120119
// Allow plugins to add or remove postcss plugins
121120
if ( loader._compilation ) {
122121
plugins = loader._compilation.applyPluginsWaterfall(
123122
'postcss-loader-before-processing',
124123
[].concat(plugins),
125-
params
124+
options
126125
);
127126
}
128127

129-
return postcss(plugins).process(source, opts).then(function (result) {
130-
result.warnings().forEach(function (msg) {
131-
loader.emitWarning(msg.toString());
132-
});
133-
134-
result.messages.forEach(function (msg) {
135-
if ( msg.type === 'dependency' ) {
136-
loader.addDependency(msg.file);
137-
}
138-
});
139-
140-
var resultMap = result.map ? result.map.toJSON() : null;
141-
callback(null, result.css, resultMap);
142-
return null;
143-
});
144-
}).catch(function (error) {
145-
if ( error.name === 'CssSyntaxError' ) {
146-
callback(new PostCSSLoaderError(error));
147-
} else {
128+
return postcss(plugins)
129+
.process(css, options)
130+
.then((result) => {
131+
result.warnings().forEach((msg) => {
132+
loader.emitWarning(msg.toString());
133+
});
134+
135+
result.messages.forEach((msg) => {
136+
if (msg.type === 'dependency') {
137+
loader.addDependency(msg.file);
138+
}
139+
});
140+
141+
callback(
142+
null, result.css, result.map ? result.map.toJSON() : null
143+
);
144+
145+
// console.log('Index', loader.loaderIndex);
146+
147+
return null;
148+
});
149+
}).catch((error) => {
150+
return error.name === 'CssSyntaxError' ?
151+
callback(new PostCSSLoaderError(error)) :
148152
callback(error);
149-
}
150153
});
151154
};

package.json

+8-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.3.3",
44
"description": "PostCSS loader for webpack",
55
"engines": {
6-
"node": ">=0.12"
6+
"node": ">= 4"
77
},
88
"keywords": [
99
"webpack",
@@ -17,30 +17,29 @@
1717
"repository": "postcss/postcss-loader",
1818
"dependencies": {
1919
"loader-utils": "^1.0.2",
20-
"object-assign": "^4.1.1",
2120
"postcss": "^5.2.15",
2221
"postcss-load-config": "^1.2.0"
2322
},
2423
"devDependencies": {
2524
"eslint": "^3.16.1",
2625
"eslint-config-postcss": "^2.0.2",
2726
"fs-extra": "^2.0.0",
28-
"gulp": "^3.9.1",
29-
"gulp-eslint": "^3.0.1",
30-
"gulp-jest": "^1.0.0",
31-
"jest-cli": "^19.0.2",
27+
"jest": "^19.0.2",
3228
"json-loader": "^0.5.4",
3329
"lint-staged": "^3.3.1",
3430
"postcss-js": "^0.3.0",
3531
"postcss-safe-parser": "^2.0.0",
3632
"pre-commit": "^1.2.2",
3733
"raw-loader": "^0.5.1",
3834
"sugarss": "^0.2.0",
39-
"webpack-stream": "^3.2.0"
35+
"webpack": "^2.2.1"
4036
},
4137
"scripts": {
4238
"lint-staged": "lint-staged",
43-
"test": "gulp"
39+
"lint": "eslint *.js test/*.js",
40+
"build": "node test/index.js",
41+
"pretest": "npm run lint && npm run build",
42+
"test": "jest --verbose --coverage"
4443
},
4544
"eslintConfig": {
4645
"extends": "eslint-config-postcss/es5",
@@ -49,7 +48,7 @@
4948
}
5049
},
5150
"lint-staged": {
52-
"*.js": "eslint"
51+
"*.js": "npm run lint"
5352
},
5453
"pre-commit": [
5554
"lint-staged"

0 commit comments

Comments
 (0)