-
-
Notifications
You must be signed in to change notification settings - Fork 210
/
Copy pathindex.js
151 lines (129 loc) · 4.54 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
var loaderUtils = require('loader-utils');
var loadConfig = require('postcss-load-config');
var postcss = require('postcss');
var assign = require('object-assign');
var path = require('path');
var PostCSSLoaderError = require('./error');
function parseOptions(options, pack) {
if ( typeof options === 'function' ) {
options = options.call(this, this);
}
var plugins;
if ( typeof options === 'undefined') {
plugins = [];
} else if ( Array.isArray(options) ) {
plugins = options;
} else {
plugins = options.plugins || options.defaults;
}
if ( pack ) {
plugins = options[pack];
if ( !plugins ) {
throw new Error('PostCSS plugin pack is not defined in options');
}
}
var opts = { };
if ( typeof options !== 'undefined' ) {
opts.stringifier = options.stringifier;
opts.parser = options.parser;
opts.syntax = options.syntax;
}
var exec = options && options.exec;
return Promise.resolve({ options: opts, plugins: plugins, exec: exec });
}
module.exports = function (source, map) {
if ( this.cacheable ) this.cacheable();
var loader = this;
var file = loader.resourcePath;
var params = loaderUtils.getOptions(loader) || {};
var options = params.plugins || loader.options.postcss;
var pack = params.pack;
var callback = loader.async();
var configPath;
if (params.config) {
if (path.isAbsolute(params.config)) {
configPath = params.config;
} else {
configPath = path.join(process.cwd(), params.config);
}
} else {
configPath = path.dirname(file);
}
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 });
}
}).then(function (config) {
if ( !config ) config = { };
if ( config.file ) loader.addDependency(config.file);
var plugins = config.plugins || [];
var opts = assign({}, config.options, {
from: file,
to: file,
map: {
inline: params.sourceMap === 'inline',
annotation: false
}
});
if ( typeof map === 'string' ) map = JSON.parse(map);
if ( map && map.mappings ) opts.map.prev = map;
if ( params.syntax ) {
if ( typeof params.syntax === 'string' ) {
opts.syntax = require(params.syntax);
} else {
opts.syntax = params.syntax;
}
}
if ( params.parser ) {
if ( typeof params.parser === 'string' ) {
opts.parser = require(params.parser);
} else {
opts.parser = params.parser;
}
}
if ( params.stringifier ) {
if ( typeof params.stringifier === 'string' ) {
opts.stringifier = require(params.stringifier);
} else {
opts.stringifier = params.stringifier;
}
}
var exec = params.exec || config.exec;
if ( params.parser === 'postcss-js' || exec ) {
source = loader.exec(source, loader.resource);
}
// Allow plugins to add or remove postcss plugins
if ( loader._compilation ) {
plugins = loader._compilation.applyPluginsWaterfall(
'postcss-loader-before-processing',
[].concat(plugins),
params
);
}
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 {
callback(error);
}
});
};