-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.js
57 lines (50 loc) · 1.95 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
const fs = require("fs-extra");
const postcss = require("postcss");
const util = require("util");
const tmp = require("tmp");
const path = require("path");
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
const ensureDir = util.promisify(fs.ensureDir);
module.exports = (options = { plugins: [] }) => ({
name: "postcss",
setup: function (build) {
const { rootDir = options.rootDir || process.cwd() } = options;
const tmpDirPath = tmp.dirSync().name;
build.onResolve(
{ filter: /.\.(css)$/, namespace: "file" },
async (args) => {
// use esbuild path resolution for node_modules, typescript paths, etc.
// https://esbuild.github.io/plugins/#resolve
const resolution = await build.resolve(args.path, {
resolveDir: args.resolveDir,
kind: args.kind,
});
if (resolution.errors.length > 0) {
return { errors: result.errors }
}
const sourceFullPath = resolution.path;
const sourceExt = path.extname(sourceFullPath);
const sourceBaseName = path.basename(sourceFullPath, sourceExt);
const sourceDir = path.dirname(sourceFullPath);
const sourceRelDir = path.relative(path.dirname(rootDir), sourceDir);
const tmpDir = path.resolve(tmpDirPath, sourceRelDir);
const tmpFilePath = path.resolve(tmpDir, `${sourceBaseName}.css`);
await ensureDir(tmpDir);
const css = await readFile(sourceFullPath);
const result = await postcss(options.plugins).process(css, {
from: sourceFullPath,
to: tmpFilePath,
});
// Write the result file
await writeFile(tmpFilePath, result.css);
// https://esbuild.github.io/plugins/#on-resolve-results
return {
path: tmpFilePath,
// watch for changes to the original input for automatic rebuilds
watchFiles: [ sourceFullPath ],
};
}
);
},
});