8000 v2.0.0 by michael-ciniawsky · Pull Request #191 · webpack/postcss-loader · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat(index): add ctx, ctx.file, ctx.options
  • Loading branch information
michael-ciniawsky committed Mar 28, 2017
commit 0dceb2c2dc827306533e44017e537d1862084c1a
213 changes: 108 additions & 105 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,151 +1,154 @@
var loaderUtils = require('loader-utils');
var loadConfig = require('postcss-load-config');
var postcss = require('postcss');
var assign = require('object-assign');
var path = require('path');
const path = require('path');
const loaderUtils = require('loader-utils');

var PostCSSLoaderError = require('./error');
const postcss = require('postcss');
const postcssrc = require('postcss-load-config');

function parseOptions(options, pack) {
if ( typeof options === 'function' ) {
options = options.call(this, this);
const PostCSSLoaderError = require('./error');

function parseOptions(params) {
if (typeof params === 'function') {
params = params.call(this, this);
}

var plugins;
if ( typeof options === 'undefined') {
console.log('params', params);

let plugins;

if (typeof params === 'undefined') {
plugins = [];
} else if ( Array.isArray(options) ) {
plugins = options;
} else if (Array.isArray(params)) {
plugins = params;
} else {
plugins = options.plugins || options.defaults;
plugins = params.plugins || params.defaults;
}

if ( pack ) {
plugins = options[pack];
if ( !plugins ) {
throw new Error('PostCSS plugin pack is not defined in options');
}
}
console.log('plugins', plugins);

let options = {};

var opts = { };
if ( typeof options !== 'undefined' ) {
opts.stringifier = options.stringifier;
opts.parser = options.parser;
opts.syntax = options.syntax;
if (typeof params !== 'undefined') {
options.parser = params.parser;
options.syntax = params.syntax;
options.stringifier = params.stringifier;
}

var exec = options && options.exec;
return Promise.resolve({ options: opts, plugins: plugins, exec: exec });
// console.log('options', options);

let exec = params && params.exec;

// console.log('exec', exec);

return Promise.resolve({ options: options, plugins: plugins, exec: exec });
}

module.exports = function (source, map) {
if ( this.cacheable ) this.cacheable();
module.exports = function (css, map) {
if (this.cacheable) this.cacheable();

var loader = this;
< 8000 /td> var file = loader.resourcePath;
var params = loaderUtils.getOptions(loader) || {};
const loader = this;

var options = params.plugins || loader.options.postcss;
var pack = params.pack;
var callback = loader.async();
const file = loader.resourcePath;
const params = loaderUtils.getOptions(loader) || {};

var configPath;
const settings = params.plugins || loader.options.postcss;

if (params.config) {
if (path.isAbsolute(params.config)) {
configPath = params.config;
} else {
configPath = path.join(process.cwd(), params.config);
}
} else {
configPath = path.dirname(file);
}
const callback = loader.async();

Promise.resolve().then(function () {
if ( typeof options !== 'undefined' ) {
return parseOptions.call(loader, options, pack);
} else {
if ( pack ) {
throw new Error('PostCSS plugin pack is supported ' +
'only when use plugins in webpack config');
}
return loadConfig({ webpack: loader }, configPath, { argv: false });
let rc;
let context = {
extname: path.extname(file),
dirname: path.dirname(file),
basename: path.basename(file),
webpack: { watch: loader.addDependency }
};

params.config ?
rc = path.resolve(params.config) :
rc = path.dirname(file);

Promise.resolve().then(() => {
if (typeof settings !== 'undefined') {
return parseOptions.call(loader, settings);
}
}).then(function (config) {
if ( !config ) config = { };

if ( config.file ) loader.addDependency(config.file);
return postcssrc(context, rc, { argv: false });
}).then((config) => {
if (!config) config = {};

var plugins = config.plugins || [];
if (config.file) loader.addDependency(config.file);

var opts = assign({}, config.options, {
console.log('Config Plugins', config.plugins);

let plugins = config.plugins || [];

console.log('Plugins', plugins);
console.log('webpack Version', process.env.WEBPACK_VERSION);

let options = Object.assign({}, config.options, {
from: file,
to: file,
to: file,
map: {
inline: params.sourceMap === 'inline',
inline: params.sourceMap === 'inline',
annotation: false
}
});

if ( typeof map === 'string' ) map = JSON.parse(map);
if ( map && map.mappings ) opts.map.prev = map;
if (typeof map === 'string') map = JSON.parse(map);
if (map && map.mappings) options.map.prev = map;

if ( params.syntax ) {
if ( typeof params.syntax === 'string' ) {
opts.syntax = require(params.syntax);
} else {
opts.syntax = params.syntax;
}
if (typeof options.syntax === 'string') {
options.syntax = require(options.syntax);
}
if ( params.parser ) {
if ( typeof params.parser === 'string' ) {
opts.parser = require(params.parser);
} else {
opts.parser = params.parser;
}

if (typeof options.parser === 'string') {
options.parser = require(options.parser);
}
if ( params.stringifier ) {
if ( typeof params.stringifier === 'string' ) {
opts.stringifier = require(params.stringifier);
} else {
opts.stringifier = params.stringifier;
}

if (typeof options.stringifier === 'string') {
options.stringifier = require(options.stringifier);
}

var exec = params.exec || config.exec;
if ( params.parser === 'postcss-js' || exec ) {
source = loader.exec(source, loader.resource);
// console.log('Options', options);

let exec = options.exec || config.exec;

if (options.parser === 'postcss-js' || exec) {
css = loader.exec(css, loader.resource);
}

// Allow plugins to add or remove postcss plugins
if ( loader._compilation ) {
plugins = loader._compilation.applyPluginsWaterfall(
'postcss-loader-before-processing',
[].concat(plugins),
params
options
);
}

return postcss(plugins).process(source, opts).then(function (result) {
result.warnings().forEach(function (msg) {
loader.emitWarning(msg.toString());
});

result.messages.forEach(function (msg) {
if ( msg.type === 'dependency' ) {
loader.addDependency(msg.file);
}
});

var resultMap = result.map ? result.map.toJSON() : null;
callback(null, result.css, resultMap);
return null;
});
}).catch(function (error) {
if ( error.name === 'CssSyntaxError' ) {
callback(new PostCSSLoaderError(error));
} else {
return postcss(plugins)
.process(css, options)
.then((result) => {
result.warnings().forEach((msg) => {
loader.emitWarning(msg.toString());
});

result.messages.forEach((msg) => {
if (msg.type === 'dependency') {
loader.addDependency(msg.file);
}
});

callback(
null, result.css, result.map ? result.map.toJSON() : null
);

// console.log('Index', loader.loaderIndex);

return null;
});
}).catch((error) => {
return error.name === 'CssSyntaxError' ?
callback(new PostCSSLoaderError(error)) :
callback(error);
}
});
};
17 changes: 8 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.3.3",
"description": "PostCSS loader for webpack",
"engines": {
"node": ">=0.12"
"node": ">= 4"
},
"keywords": [
"webpack",
Expand All @@ -17,30 +17,29 @@
"repository": "postcss/postcss-loader",
"dependencies": {
"loader-utils": "^1.0.2",
"object-assign": "^4.1.1",
"postcss": "^5.2.15",
"postcss-load-config": "^1.2.0"
},
"devDependencies": {
"eslint": "^3.16.1",
"eslint-config-postcss": "^2.0.2",
"fs-extra": "^2.0.0",
"gulp": "^3.9.1",
"gulp-eslint": "^3.0.1",
"gulp-jest": "^1.0.0",
"jest-cli": "^19.0.2",
"jest": "^19.0.2",
"json-loader": "^0.5.4",
"lint-staged": "^3.3.1",
"postcss-js": "^0.3.0",
"postcss-safe-parser": "^2.0.0",
"pre-commit": "^1.2.2",
"raw-loader": "^0.5.1",
"sugarss": "^0.2.0",
"webpack-stream": "^3.2.0"
"webpack": "^2.2.1"
},
"scripts": {
"lint-staged": "lint-staged",
"test": "gulp"
"lint": "eslint *.js test/*.js",
"build": "node test/index.js",
"pretest": "npm run lint && npm run build",
"test": "jest --verbose --coverage"
},
"eslintConfig": {
"extends": "eslint-config-postcss/es5",
Expand All @@ -49,7 +48,7 @@
}
},
"lint-staged": {
"*.js": "eslint"
"*.js": "npm run lint"
},
"pre-commit": [
"lint-staged"
Expand Down