/
index.js
132 lines (107 loc) · 3.24 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
import path from "path";
import schema from "./options.json";
import {
getLessOptions,
isUnsupportedUrl,
normalizeSourceMap,
getLessImplementation,
errorFactory,
} from "./utils";
async function lessLoader(source) {
const options = this.getOptions(schema);
const callback = this.async();
let implementation;
try {
implementation = getLessImplementation(this, options.implementation);
} catch (error) {
callback(error);
return;
}
if (!implementation) {
callback(
new Error(
`The Less implementation "${options.implementation}" not found`,
),
);
return;
}
const lessOptions = getLessOptions(this, options, implementation);
const useSourceMap =
typeof options.sourceMap === "boolean" ? options.sourceMap : this.sourceMap;
if (useSourceMap) {
lessOptions.sourceMap = {
outputSourceFiles: true,
};
}
let data = source;
if (typeof options.additionalData !== "undefined") {
data =
typeof options.additionalData === "function"
? `${await options.additionalData(data, this)}`
: `${options.additionalData}\n${data}`;
}
const logger = this.getLogger("less-loader");
const loaderContext = this;
const loggerListener = {
error(message) {
// TODO enable by default in the next major release
if (options.lessLogAsWarnOrErr) {
loaderContext.emitError(new Error(message));
} else {
logger.error(message);
}
},
warn(message) {
// TODO enable by default in the next major release
if (options.lessLogAsWarnOrErr) {
loaderContext.emitWarning(new Error(message));
} else {
logger.warn(message);
}
},
info(message) {
logger.log(message);
},
debug(message) {
logger.debug(message);
},
};
implementation.logger.addListener(loggerListener);
let result;
try {
result = await implementation.render(data, lessOptions);
} catch (error) {
if (error.filename) {
// `less` returns forward slashes on windows when `webpack` resolver return an absolute windows path in `WebpackFileManager`
// Ref: https://github.com/webpack-contrib/less-loader/issues/357
this.addDependency(path.normalize(error.filename));
}
callback(errorFactory(error));
return;
} finally {
// Fix memory leaks in `less`
implementation.logger.removeListener(loggerListener);
delete lessOptions.pluginManager.webpackLoaderContext;
delete lessOptions.pluginManager;
}
const { css, imports } = result;
imports.forEach((item) => {
if (isUnsupportedUrl(item)) {
return;
}
// `less` return forward slashes on windows when `webpack` resolver return an absolute windows path in `WebpackFileManager`
// Ref: https://github.com/webpack-contrib/less-loader/issues/357
const normalizedItem = path.normalize(item);
// Custom `importer` can return only `contents` so item will be relative
if (path.isAbsolute(normalizedItem)) {
this.addDependency(normalizedItem);
}
});
let map =
typeof result.map === "string" ? JSON.parse(result.map) : result.map;
if (map && useSourceMap) {
map = normalizeSourceMap(map, this.rootContext);
}
callback(null, css, map);
}
export default lessLoader;