forked from jamiebuilds/babel-plugin-import-inspector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
113 lines (93 loc) · 3.01 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
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
113
'use strict';
module.exports = function({types: t, template}) {
function currentModuleFileName(path) {
return t.objectProperty(
t.identifier('currentModuleFileName'),
t.stringLiteral(path.hub.file.opts.filename)
);
}
function importedModulePath(path) {
return t.objectProperty(
t.identifier('importedModulePath'),
path.parent.arguments[0]
);
}
const webpackTemplate = template('() => require.resolveWeak(MODULE)');
function webpackRequireWeakId(path) {
return t.objectProperty(
t.identifier('webpackRequireWeakId'),
webpackTemplate({
MODULE: path.parent.arguments[0]
}).expression
);
}
const serverTemplate = template('PATH.join(__dirname, MODULE)');
const pathId = Symbol('pathId');
function serverSideRequirePath(path) {
if (!path.hub.file[pathId]) {
path.hub.file[pathId] = path.hub.file.addImport('path', 'default', 'path');
}
return t.objectProperty(
t.identifier('serverSideRequirePath'),
serverTemplate({
PATH: path.hub.file[pathId],
MODULE: path.parent.arguments[0]
}).expression
);
}
const timeStartTemplate = template('const START = Date.now()');
const timeStartId = Symbol('timeStartId');
function addTimeStart(path) {
const program = path.hub.file.path;
path.hub.file[timeStartId] = program.scope.generateUidIdentifier('start');
program.unshiftContainer(
'body',
timeStartTemplate({
START: path.hub.file[timeStartId]
})
);
}
const timeToImportTemplate = template('START - Date.now()');
function timeToImport(path) {
if (!path.hub.file[timeStartId]) addTimeStart(path);
return t.objectProperty(
t.identifier('timeToImport'),
timeToImportTemplate({
START: path.hub.file[timeStartId]
}).expression
);
}
const visited = Symbol('visited');
const reportId = Symbol('reportId');
return {
name: 'import-inspector',
visitor: {
Import(path) {
if (path[visited]) return;
path[visited] = true;
let opts = Object.assign({
currentModuleFileName: true,
importedModulePath: true,
serverSideRequirePath: false,
webpackRequireWeakId: false,
timeToImport: false
}, this.opts);
if (!path.hub.file[reportId]) {
path.hub.file[reportId] = path.hub.file.addImport('import-inspector', 'report');
}
let props = [];
if (opts.currentModuleFileName) props.push(currentModuleFileName(path));
if (opts.importedModulePath) props.push(importedModulePath(path));
if (opts.serverSideRequirePath) props.push(serverSideRequirePath(path));
if (opts.webpackRequireWeakId) props.push(webpackRequireWeakId(path));
if (opts.timeToImport) props.push(timeToImport(path));
path.parentPath.replaceWith(
t.callExpression(path.hub.file[reportId], [
path.parent,
t.objectExpression(props)
])
);
}
}
};
};