-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
112 lines (100 loc) · 3.14 KB
/
index.js
File metadata and controls
112 lines (100 loc) · 3.14 KB
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
import { promises as fs } from 'fs';
import path from 'path';
import postcss from 'postcss';
import { compileString } from 'sass-embedded';
function getFilePriority(fileName) {
if (fileName.includes('themes')) return 0;
if (fileName.includes('design-system')) return 1;
return 2;
}
export function compareFiles(a, b) {
const priorityDiff = getFilePriority(a.id) - getFilePriority(b.id);
if (priorityDiff !== 0) {
return priorityDiff;
}
const nameA = path.basename(a.id);
const nameB = path.basename(b.id);
return nameA.localeCompare(nameB);
}
function copyFontFolders(emitFile) {
return async function (fontImport) {
const fontDir = path.join(path.dirname(fontImport.id), 'files');
const fontFiles = await fs.readdir(fontDir);
await Promise.all(fontFiles.map(async file => {
const filePath = path.join(fontDir, file);
emitFile({
type: 'asset',
fileName: path.join(path.basename(fontDir), file),
source: await fs.readFile(filePath)
});
}));
}
}
export default function bundleSass({ output, noOutput = false, copyFonts = false, exclusive = true, scssOnly = false, postfixOptions = {} } = {}) {
const files = new Set();
const bundleExt = "scss";
const {
plugins = [],
use = [],
} = postfixOptions;
return {
name: 'bundle-sass',
transform(source, id) {
if (/\.s?[ac]ss$/.test(id)) {
if (/\.sass$/.test(id)) {
bundleExt = "sass";
}
files.add({ id, content: source });
if (exclusive) {
return { code: `export default ${JSON.stringify(source)}` };
}
}
return null;
},
async generateBundle(opts) {
if (noOutput) {
return;
}
const outputName = output || `${opts.file ? path.parse(opts.file).name : 'index'}.${bundleExt}`;
const outputPath = path.resolve(
opts.file ? path.dirname(opts.file) : opts.dir,
outputName,
);
await fs.mkdir(path.dirname(outputPath), { recursive: true });
const uniqueFiles = Array.from(files);
uniqueFiles.sort(compareFiles);
const bundledContent = use.join('\n')
+ uniqueFiles.map((file) => file.content).join('\n');
//await fs.writeFile(outputPath, bundledContent);
this.emitFile({
type: 'asset',
fileName: outputName,
source: bundledContent
});
if (scssOnly) {
return;
}
if (copyFonts) {
const fontSourceFiles = uniqueFiles.filter((file) => file.id.includes("@fontsource"));
await Promise.all(fontSourceFiles.map(copyFontFolders(this.emitFile)));
}
const cssResult = await compileString(bundledContent, {});
const outputName2 = `${outputName.replace(/\.s?[ac]ss$/, '.css')}`;
const validPlugins = plugins.filter(Boolean);
const r = await postcss(validPlugins)
.process(cssResult.css);
this.emitFile({
type: 'asset',
fileName: outputName2,
source: r.css
});
if (r.map) {
this.emitFile({
type: 'asset',
fileName: `${outputName2}.map`,
source: r.map.toString()
});
}
},
};
}