-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
122 lines (96 loc) · 3.76 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
'use strict';
const {inspect} = require('util');
const CleanCss = require('clean-css');
const inspectWithKind = require('inspect-with-kind');
const isPlainObj = require('is-plain-obj');
const CONSTRUCTOR_ERROR = 'Expected an <Object> to specify clean-css options https://github.com/jakubpawlowicz/clean-css';
const REBASE_TO_ERROR = 'Expected `rebaseTo` option to be a string or undefined';
const createLine = (msg, error, index) => `${msg}\n ${index + 1}. ${error}`;
module.exports = class CleanCssPromise extends CleanCss {
constructor(...args) {
const argLen = args.length;
if (argLen > 1) {
const error = new RangeError(`Expected 0 or 1 argument (<Object>), but got ${argLen} arguments.`);
error.code = 'ERR_TOO_MANY_ARGS';
throw error;
}
if (argLen === 1) {
const [options] = args;
if (!isPlainObj(options)) {
const error = new TypeError(`${CONSTRUCTOR_ERROR}, but got ${
inspectWithKind(options)
}.`);
error.code = 'ERR_INVALID_ARG_TYPE';
throw error;
}
if (options.returnPromise !== undefined) {
throw new Error(`clean-css-promise automatically enables \`returnPromise\` option and it's unconfigurable, but a value ${
inspect(options.returnPromise)
} was provided for it.`);
}
if (options.rebaseTo !== undefined && typeof options.rebaseTo !== 'string') {
throw new TypeError(`${REBASE_TO_ERROR}, but got ${inspect(options.rebaseTo)}.${
options.rebaseTo === null || options.rebaseTo === false ?
' If you want to disable `rebaseTo` option, do not pass any values to `rebaseTo`.' :
''
}`);
}
}
super(...args);
}
async minify(...args) {
const argLen = args.length;
if (argLen === 0) {
const error = new RangeError('Expected 1 or 2 arguments (<string|Object>[, <string>]), but got no arguments.');
error.code = 'ERR_MISSING_ARGS';
throw error;
}
const [input, sourceMap] = args;
if (argLen === 2) {
if (typeof input !== 'string') {
const error = new TypeError(`Expected CleanCssPromise#minify() to receive <string> as its first argument when it takes 2 arguments, but got ${
inspectWithKind(input)
}.`);
error.code = 'ERR_INVALID_ARG_TYPE';
throw error;
}
if (typeof sourceMap !== 'string') {
const error = new TypeError(`Expected CleanCssPromise#minify() to receive <string> as its second argument when it takes 2 arguments, but got ${
inspectWithKind(sourceMap)
}.`);
error.code = 'ERR_INVALID_ARG_TYPE';
throw error;
}
} else if (argLen === 1) {
if (typeof input !== 'string' && !isPlainObj(input)) {
const error = new TypeError(`Expected CleanCssPromise#minify() to receive <string> or <Object> as its first argument, but got ${
inspectWithKind(input)
}.`);
error.code = 'ERR_INVALID_ARG_TYPE';
throw error;
}
} else {
const error = new RangeError(`Expected 1 or 2 arguments (<string|Object>[, <string>]), but got ${argLen} arguments.`);
error.code = 'ERR_TOO_MANY_ARGS';
throw error;
}
return new Promise((resolve, reject) => {
super.minify(...args, (unusedArg, result) => {
const errors = [...result.errors, ...result.warnings];
const errorCount = errors.length;
if (errorCount !== 0) {
const errorMessage = errorCount !== 1 ? errors.reduce(
createLine,
`${errors.length} errors occured while optimizing CSS with clean-css:`
) : `An error occured while optimizing CSS with clean-css: ${errors[0]}`;
reject(new Error(`${errorMessage
}\n\nclean-css dangerously ignores ${
errorCount === 1 ? 'this error' : 'these errors'
} but clean-css-promise doesn't, because it's much more reasonable to update the CSS` +
' to fix all problems than to pretend that you didn\'t see the errors.'));
}
resolve(result);
});
});
}
};