-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
110 lines (96 loc) · 3.11 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
const path = require("path");
const fs = require("fs").promises;
const less = require("less");
const importRegex = /@import(?:\s+\((.*)\))?\s+['"](.*)['"]/;
const globalImportRegex = /@import(?:\s+\((.*)\))?\s+['"](.*)['"]/g;
const importCommentRegex = /(?:\/\*(?:[\s\S]*?)\*\/)|(\/\/(?:.*)$)/gm;
const extWhitelist = [".css", ".less"];
/** Recursively get .less/.css imports from file */
function getLessImports(filePath) {
try {
const dir = path.dirname(filePath);
const content = fs.readFileSync(filePath).toString("utf8");
const cleanContent = content.replace(importCommentRegex, "");
const match = cleanContent.match(globalImportRegex) ?? [];
const fileImports = match
.map((el) => {
const match = el.match(importRegex);
return match[2];
})
.filter((el) => !!el)
// NOTE: According to the docs, extensionless imports are interpreted as '.less' files.
// http://lesscss.org/features/#import-atrules-feature-file-extensions
// https://github.com/iam-medvedev/esbuild-plugin-less/issues/13
.map((el) => path.resolve(dir, path.extname(el) ? el : `${el}.less`));
const recursiveImports = fileImports.reduce((result, el) => {
return [...result, ...getLessImports(el)];
}, fileImports);
const result = recursiveImports.filter((el) =>
extWhitelist.includes(path.extname(el).toLowerCase())
);
return result;
} catch (e) {
return [];
}
}
/** Convert less error into esbuild error */
function convertLessError(error) {
const sourceLine = error.extract.filter((line) => line);
const lineText = sourceLine.length === 3 ? sourceLine[1] : sourceLine[0];
return {
text: error.message,
location: {
namespace: "file",
file: error.filename,
line: error.line,
column: error.column,
lineText,
},
};
}
module.exports = (options = {}) => {
return {
name: "css-file",
setup(buildArg) {
buildArg.onResolve({ filter: /\.less$/, namespace: "file" }, (args) => {
const filePath = path.resolve(
process.cwd(),
path.relative(process.cwd(), args.resolveDir),
args.path
);
return {
path: filePath,
watchFiles: !!buildArg.initialOptions.watch
? [filePath, ...getLessImports(filePath)]
: undefined,
};
});
buildArg.onLoad(
{ filter: /\.less$/, namespace: "file" },
async (args) => {
const content = await fs.readFile(args.path, "utf-8");
const dir = path.dirname(args.path);
const filename = path.basename(args.path);
try {
const result = await less.render(content, {
filename,
rootpath: dir,
...options,
paths: [...(options.paths ?? []), dir],
});
return {
contents: result.css,
loader: "text",
resolveDir: dir,
};
} catch (e) {
return {
errors: [convertLessError(e)],
resolveDir: dir,
};
}
}
);
},
};
};