-
Notifications
You must be signed in to change notification settings - Fork 34
/
index.ts
72 lines (66 loc) · 2.89 KB
/
index.ts
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
import path from 'path';
import {logger, isMonorepo, normalizeRuleMatch} from '@reskript/core';
import {SettingsPlugin} from '@reskript/settings';
import {Options, LocalPackageInfo} from './interface.js';
import {
resolveParticipant,
findSiblingPackages,
buildPackageInfo,
buildPeerAlias,
checkDependencyGraph,
} from './utils.js';
export default (options: Options = {}): SettingsPlugin => async (settings, {cwd}) => {
const isWorkspace = await isMonorepo(cwd);
if (!isWorkspace) {
logger.error('Current project is not a monorepo workspace');
process.exit(24);
}
const self = await buildPackageInfo(cwd);
const siblings = await findSiblingPackages(cwd, self);
const isDependencyOfSelf = ({name}: LocalPackageInfo) => !!(self.dependencies[name] ?? self.devDependencies[name]);
const includedSiblings = resolveParticipant(siblings.filter(isDependencyOfSelf), options);
const dependencyGraphChecked = checkDependencyGraph(includedSiblings, self);
if (!dependencyGraphChecked) {
process.exit(24);
}
const incomingBabelFilter = normalizeRuleMatch(cwd, settings.build.script.babel);
const incomingModulesFilter = normalizeRuleMatch(cwd, settings.build.style.modules);
const includedSiblingDirectories = includedSiblings.map(v => v.directory);
return {
...settings,
build: {
...settings.build,
style: options.styles
? {
...settings.build.style,
modules: (resource: string) => {
const shouldProcess = incomingModulesFilter(resource);
return shouldProcess || includedSiblingDirectories.some(v => resource.startsWith(v));
},
}
: settings.build.style,
script: {
...settings.build.script,
babel: (resource: string) => {
const shouldProcess = incomingBabelFilter(resource);
return shouldProcess || includedSiblingDirectories.some(v => resource.startsWith(v));
},
},
finalize: (config, env, internals) => {
const previous = settings.build.finalize(config, env, internals);
// 因为`peerDependencies`里也会包含本地的包,所以要先处理这些东西,再用下面的`for`循环把本地包的规则覆盖上去就对了
Object.assign(
previous.resolve?.alias,
buildPeerAlias(cwd, includedSiblings)
);
for (const {name, directory} of includedSiblings) {
Object.assign(
previous.resolve?.alias,
{[`${name}`]: path.join(directory, 'src')}
);
}
return previous;
},
},
};
};