-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (58 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
58
59
60
61
const path = require('path');
const transform = require('@babel/core').transformSync;
const loaderUtils = require('loader-utils');
const getRoutePathList = require('./lib/getRoutePathList');
const genRoute = require('./lib/genRoute');
const genReducer = require('./lib/genReducer');
const genSaga = require('./lib/genSaga');
const babelTypeNamePlugin = require('./lib/babel-plugin-type-name')
const routerId = '__react_router__';
const reducerId = '__redux_reducer__';
const sagaId = '__redux_saga__';
module.exports = function reactRouterLoader(codeStr) {
const ctx = this;
const loaderQuery = loaderUtils.getOptions(this) || {};
const {
componentDir, loadablePath,
reducerName,
typeName,
sagaName,
} = loaderQuery;
const cwd = path.join(process.cwd(), 'src', componentDir);
let result = codeStr;
const routePathList = getRoutePathList(cwd, reducerName, typeName, sagaName);
// 自动生成route
if (codeStr.indexOf(routerId) > 0) {
const [imp, routes] = genRoute(routePathList, loadablePath);
result = imp + codeStr;
result = result.replace(routerId, routes);
}
// 自动导入reducer
if (codeStr.indexOf(reducerId) > 0) {
const [importList, reducers] = genReducer(routePathList);
result = importList + codeStr;
result = result.replace(reducerId, reducers);
}
// 自动导入saga
if (codeStr.indexOf(sagaId) > 0) {
debugger
const [importList, sagas] = genSaga(routePathList);
result = importList + codeStr;
result = result.replace(sagaId, sagas);
}
// 用路径名补全type
if (
ctx.resourcePath.endsWith(`/${typeName}.js`) &&
routePathList.find(item => item.typePath === ctx.resourcePath)
) {
debugger
const namespace = path.relative(cwd, path.dirname(ctx.resourcePath));
const { code } = transform(codeStr, {
plugins: [[babelTypeNamePlugin, { namespace }]]
})
result = code;
}
ctx.addContextDependency(componentDir);
ctx.cacheable();
return result;
}