-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathindex.ts
95 lines (83 loc) · 3.3 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import * as path from 'path';
import * as fse from 'fs-extra';
import { IPlugin } from '@alib/build-scripts';
import { IRouterOptions } from './types/router';
import walker from './collector/walker';
// compatible with $ice/routes
const TEM_ROUTER_COMPATIBLE = '$ice/routes';
const TEM_ROUTER_SETS = [TEM_ROUTER_COMPATIBLE];
const plugin: IPlugin = ({ context, onGetWebpackConfig, modifyUserConfig, getValue, applyMethod, registerUserConfig }) => {
const { rootDir, userConfig, command } = context;
// [enum] js or ts
const projectType = getValue('PROJECT_TYPE');
// .tmp path
const iceTempPath = getValue('ICE_TEMP');
const routerOptions = (userConfig.router || {}) as IRouterOptions;
const { configPath } = routerOptions;
const { mpa: isMpa } = userConfig;
const routesTempPath = path.join(iceTempPath, `routes.${projectType}`);
const { routesPath, isConfigRoutes } = applyMethod('getRoutes', {
rootDir,
tempDir: iceTempPath,
configPath,
projectType,
isMpa
});
// add babel plugins for ice lazy
modifyUserConfig('babelPlugins',
[
...(userConfig.babelPlugins as [] || []),
[
require.resolve('./babelPluginLazy'),
{ routesPath }
]
]);
// copy templates and export react-router-dom/history apis to ice
const routerTemplatesPath = path.join(__dirname, '../templates');
const routerTargetPath = path.join(iceTempPath, 'router');
fse.ensureDirSync(routerTargetPath);
fse.copySync(routerTemplatesPath, routerTargetPath);
applyMethod('addIceExport', { source: './router' });
// copy types
fse.copySync(path.join(__dirname, '../src/types/index.ts'), path.join(iceTempPath, 'router/types/index.ts'));
fse.copySync(path.join(__dirname, '../src/types/base.ts'), path.join(iceTempPath, 'router/types/base.ts'));
// set IAppRouterProps to IAppConfig
applyMethod('addIceAppConfigTypes', { source: './router/types', specifier: '{ IAppRouterProps }', exportName: 'router?: IAppRouterProps' });
// export IRouterConfig to the public
applyMethod('addIceTypesExport', { source: './router/types' });
// modify webpack config
onGetWebpackConfig((config) => {
// add alias
TEM_ROUTER_SETS.forEach(i => {
config.resolve.alias.set(i, routesPath);
});
// alias for runtime/Router
config.resolve.alias.set('$ice/Router', path.join(__dirname, 'runtime/Router'));
// alias for runtime/history
config.resolve.alias.set('$ice/history', path.join(__dirname, '../templates/history'));
// alias for react-router-dom
const routerName = 'react-router-dom';
config.resolve.alias.set(routerName, require.resolve(routerName));
// config historyApiFallback for router type browser
config.devServer.set('historyApiFallback', true);
});
// register router in build.json
registerUserConfig({
name: 'router',
validation: 'object',
});
// do not watch folder pages when route config is exsits
if (!isConfigRoutes) {
const routerMatch = 'src/pages';
const pagesDir = path.join(rootDir, routerMatch);
const walkerOptions = { rootDir, routerOptions, routesTempPath, pagesDir };
walker(walkerOptions);
if (command === 'start') {
// watch folder change when dev
applyMethod('watchFileChange', routerMatch, () => {
walker(walkerOptions);
});
}
}
};
export default plugin;