|
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'); |
6 | 3 |
|
7 |
| -var PostCSSLoaderError = require('./error'); |
| 4 | +const postcss = require('postcss'); |
| 5 | +const postcssrc = require('postcss-load-config'); |
8 | 6 |
|
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); |
12 | 12 | }
|
13 | 13 |
|
14 |
| - var plugins; |
15 |
| - if ( typeof options === 'undefined') { |
| 14 | + console.log('params', params); |
| 15 | + |
| 16 | + let plugins; |
| 17 | + |
| 18 | + if (typeof params === 'undefined') { |
16 | 19 | plugins = [];
|
17 |
| - } else if ( Array.isArray(options) ) { |
18 |
| - plugins = options; |
| 20 | + } else if (Array.isArray(params)) { |
| 21 | + plugins = params; |
19 | 22 | } else {
|
20 |
| - plugins = options.plugins || options.defaults; |
| 23 | + plugins = params.plugins || params.defaults; |
21 | 24 | }
|
22 | 25 |
|
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 = {}; |
29 | 29 |
|
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; |
35 | 34 | }
|
36 | 35 |
|
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 }); |
39 | 43 | }
|
40 | 44 |
|
41 |
| -module.exports = function (source, map) { |
42 |
| - if ( this.cacheable ) this.cacheable(); |
| 45 | +module.exports = function (css, map) { |
| 46 | + if (this.cacheable) this.cacheable(); |
43 | 47 |
|
44 |
| - var loader = this; |
45 |
| - var file = loader.resourcePath; |
46 |
| - var params = loaderUtils.getOptions(loader) || {}; |
| 48 | + const loader = this; |
47 | 49 |
|
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) || {}; |
51 | 52 |
|
52 |
| - var configPath; |
| 53 | + const settings = params.plugins || loader.options.postcss; |
53 | 54 |
|
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(); |
63 | 56 |
|
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); |
73 | 72 | }
|
74 |
| - }).then(function (config) { |
75 |
| - if ( !config ) config = { }; |
76 | 73 |
|
77 |
| - if ( config.file ) loader.addDependency(config.file); |
| 74 | + return postcssrc(context, rc, { argv: false }); |
| 75 | + }).then((config) => { |
| 76 | + if (!config) config = {}; |
78 | 77 |
|
79 |
| - var plugins = config.plugins || []; |
| 78 | + if (config.file) loader.addDependency(config.file); |
80 | 79 |
|
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, { |
82 | 88 | from: file,
|
83 |
| - to: file, |
| 89 | + to: file, |
84 | 90 | map: {
|
85 |
| - inline: params.sourceMap === 'inline', |
| 91 | + inline: params.sourceMap === 'inline', |
86 | 92 | annotation: false
|
87 | 93 | }
|
88 | 94 | });
|
89 | 95 |
|
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; |
92 | 98 |
|
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); |
99 | 101 | }
|
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); |
106 | 105 | }
|
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); |
113 | 109 | }
|
114 | 110 |
|
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); |
118 | 117 | }
|
119 | 118 |
|
120 | 119 | // Allow plugins to add or remove postcss plugins
|
121 | 120 | if ( loader._compilation ) {
|
122 | 121 | plugins = loader._compilation.applyPluginsWaterfall(
|
123 | 122 | 'postcss-loader-before-processing',
|
124 | 123 | [].concat(plugins),
|
125 |
| - params |
| 124 | + options |
126 | 125 | );
|
127 | 126 | }
|
128 | 127 |
|
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)) : |
148 | 152 | callback(error);
|
149 |
| - } |
150 | 153 | });
|
151 | 154 | };
|
0 commit comments