-
-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(index): add ctx, ctx.file, ctx.options
- Loading branch information
1 parent
d591cdf
commit 0dceb2c
Showing
2 changed files
with
116 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
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); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters