UNPKG

1.38 kBJavaScriptView Raw
1'use strict';
2
3// Dependencies
4var path = require('path');
5var arrify = require('arrify');
6var assign = require('object-assign');
7var formatter = require('stylelint').formatters.string;
8
9// Modules
10var runCompilation = require('./lib/run-compilation');
11
12function apply(options, compiler) {
13 options = options || {};
14 var context = options.context || compiler.context;
15
16 options = assign({
17 configFile: '.stylelintrc',
18 formatter: formatter,
19 quiet: false
20 }, options, {
21 // Default Glob is any directory level of scss and/or sass file,
22 // under webpack's context and specificity changed via globbing patterns
23 files: arrify(options.files || '**/*.s?(c|a)ss').map(function (file) {
24 return path.join(context, '/', file);
25 }),
26 context: context
27 });
28
29 var runner = runCompilation.bind(this, options);
30
31 compiler.plugin('run', runner);
32 compiler.plugin('watch-run', function onWatchRun(watcher, callback) {
33 runner(watcher.compiler, callback);
34 });
35}
36
37/**
38 * Pass options to the plugin that get checked and updated before running
39 * ref: https://webpack.github.io/docs/plugins.html#the-compiler-instance
40 * @param options - from webpack config, see defaults in `apply` function.
41 * @return {Object} the bound apply function
42 */
43module.exports = function stylelintWebpackPlugin(options) {
44 return {
45 apply: apply.bind(this, options)
46 };
47};