1 | "use strict";
|
2 |
|
3 | var _path = _interopRequireDefault(require("path"));
|
4 |
|
5 | var _loaderUtils = _interopRequireDefault(require("loader-utils"));
|
6 |
|
7 | var _schemaUtils = require("schema-utils");
|
8 |
|
9 | var _minify = _interopRequireDefault(require("./minify"));
|
10 |
|
11 | var _interpolateName = _interopRequireDefault(require("./utils/interpolate-name"));
|
12 |
|
13 | var _loaderOptions = _interopRequireDefault(require("./loader-options.json"));
|
14 |
|
15 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
16 |
|
17 | module.exports = async function loader(content) {
|
18 | const options = _loaderUtils.default.getOptions(this);
|
19 |
|
20 | (0, _schemaUtils.validate)(_loaderOptions.default, options, {
|
21 | name: 'Image Minimizer Plugin Loader',
|
22 | baseDataPath: 'options'
|
23 | });
|
24 | const callback = this.async();
|
25 |
|
26 | const name = _path.default.relative(this.rootContext, this.resourcePath);
|
27 |
|
28 | if (options.filter && !options.filter(content, name)) {
|
29 | callback(null, content);
|
30 | return;
|
31 | }
|
32 |
|
33 | const input = content;
|
34 | const {
|
35 | severityError,
|
36 | minimizerOptions
|
37 | } = options;
|
38 | const minifyOptions = {
|
39 | input,
|
40 | filename: name,
|
41 | severityError,
|
42 | minimizerOptions,
|
43 | isProductionMode: this.mode === 'production' || !this.mode
|
44 | };
|
45 | const output = await (0, _minify.default)(minifyOptions);
|
46 |
|
47 | if (output.errors && output.errors.length > 0) {
|
48 | output.errors.forEach(warning => {
|
49 | this.emitError(warning);
|
50 | });
|
51 | callback(null, content);
|
52 | return;
|
53 | }
|
54 |
|
55 | output.source = output.output;
|
56 |
|
57 | if (output.warnings && output.warnings.length > 0) {
|
58 | output.warnings.forEach(warning => {
|
59 | this.emitWarning(warning);
|
60 | });
|
61 | }
|
62 |
|
63 | const {
|
64 | source
|
65 | } = output;
|
66 | const newName = (0, _interpolateName.default)(name, options.filename || '[path][name][ext]');
|
67 | const isNewAsset = name !== newName;
|
68 |
|
69 | if (isNewAsset) {
|
70 | this.emitFile(newName, source, null, {
|
71 | minimized: true
|
72 | });
|
73 |
|
74 | if (options.deleteOriginalAssets) {
|
75 | }
|
76 |
|
77 | callback(null, content);
|
78 | return;
|
79 | }
|
80 |
|
81 | callback(null, source);
|
82 | };
|
83 |
|
84 | module.exports.raw = true; |
\ | No newline at end of file |