-
Notifications
You must be signed in to change notification settings - Fork 3
/
Config.js
81 lines (69 loc) · 2.35 KB
/
Config.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
import {PluginLogger} from '@parcel/logger';
import path from 'path';
import fs from 'fs';
const PACKAGE_JSON_SECTION = "parcel-namer-rewrite";
export class Config {
rules: NamerRule[]
chain: string
developmentHashing = false
/**
* Disable logging names
*/
silent = false
/**
* Use file name hashes from parcel
*/
useParcelHash = true
constructor() {
this.chain = '@parcel/namer-default';
this.rules = [];
}
loadFromPackageFolder(rootFolder: string, logger: PluginLogger) {
const packageJson = fs.readFileSync(path.join(rootFolder, 'package.json')).toString();
const packageInfo = JSON.parse(packageJson);
const packageSection = packageInfo[PACKAGE_JSON_SECTION];
if (!packageSection) {
logger.warn({
message: `no "${PACKAGE_JSON_SECTION}" section in package.json. Use no-rules config`
})
return;
}
if (packageSection && 'chain' in packageSection) {
this.chain = packageSection.chain;
}
this.silent = packageSection && 'silent' in packageSection && packageSection.silent;
if (packageSection && 'useParcelHash' in packageSection) {
this.useParcelHash = !packageSection.useParcelHash;
}
if (packageSection && 'rules' in packageSection) {
Object.keys(packageSection.rules).forEach(k => {
const ruleData = packageSection.rules[k];
const ruleTo = typeof ruleData === 'string' ? ruleData : null;
if (ruleTo === null) {
logger.warn(`No "to" rule for test "${k}" `);
return;
}
this.rules.push({
test: new RegExp(k),
to: ruleTo
})
})
}
if (packageSection && 'developmentHashing' in packageSection) {
this.developmentHashing = !!packageSection.developmentHashing;
}
}
selectRule(name: string): NamerRule | null {
const matches = this.rules
.map(rule => rule.test.test(name) ? rule : null)
.filter(rule => rule != null);
if (matches.length > 0) {
return matches[0];
}
return null;
}
}
export interface NamerRule {
test: RegExp;
to: string;
}