-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
resolve-module-exports.ts
240 lines (211 loc) · 6.74 KB
/
resolve-module-exports.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import * as fs from "fs-extra"
import * as t from "@babel/types"
import traverse from "@babel/traverse"
import { codeFrameColumns, SourceLocation } from "@babel/code-frame"
import report from "gatsby-cli/lib/reporter"
import { babelParseToAst } from "../utils/babel-parse-to-ast"
import { testImportError } from "../utils/test-import-error"
import { resolveModule, ModuleResolver } from "../utils/module-resolver"
import { maybeAddFileProtocol, resolveJSFilepath } from "./resolve-js-file-path"
import { preferDefault } from "./prefer-default"
const staticallyAnalyzeExports = (
modulePath: string,
resolver = resolveModule
): Array<string> => {
let absPath: string | undefined
const exportNames: Array<string> = []
try {
absPath = resolver(modulePath) as string
} catch (err) {
return exportNames // doesn't exist
}
const code = fs.readFileSync(absPath, `utf8`) // get file contents
let ast
try {
ast = babelParseToAst(code, absPath)
} catch (err) {
if (err instanceof SyntaxError) {
// Pretty print syntax errors
const codeFrame = codeFrameColumns(
code,
{
start: (err as unknown as { loc: SourceLocation["start"] }).loc,
},
{
highlightCode: true,
}
)
report.panic(
`Syntax error in "${absPath}":\n${err.message}\n${codeFrame}`
)
} else {
// if it's not syntax error, just throw it
throw err
}
}
let isCommonJS = false
let isES6 = false
// extract names of exports from file
traverse(ast, {
// Check if the file is using ES6 imports
ImportDeclaration: function ImportDeclaration() {
isES6 = true
},
ExportNamedDeclaration: function ExportNamedDeclaration(astPath) {
const declaration = astPath.node.declaration
// get foo from `export const foo = bar`
if (
declaration?.type === `VariableDeclaration` &&
declaration.declarations[0]?.id.type === `Identifier`
) {
isES6 = true
exportNames.push(declaration.declarations[0].id.name)
}
// get foo from `export function foo()`
if (
declaration?.type === `FunctionDeclaration` &&
declaration.id?.type === `Identifier`
) {
isES6 = true
exportNames.push(declaration.id.name)
}
},
// get foo from `export { foo } from 'bar'`
// get foo from `export { foo }`
ExportSpecifier: function ExportSpecifier(astPath) {
isES6 = true
const exp = astPath?.node?.exported
if (!exp) {
return
}
if (exp.type === `Identifier`) {
const exportName = exp.name
if (exportName) {
exportNames.push(exportName)
}
}
},
// export default () => {}
// export default function() {}
// export default function foo() {}
// const foo = () => {}; export default foo
ExportDefaultDeclaration: function ExportDefaultDeclaration(astPath) {
const declaration = astPath.node.declaration
if (
!t.isIdentifier(declaration) &&
!t.isArrowFunctionExpression(declaration) &&
!t.isFunctionDeclaration(declaration)
) {
return
}
let name = ``
if (t.isIdentifier(declaration)) {
name = declaration.name
} else if (t.isFunctionDeclaration(declaration) && declaration.id) {
name = declaration.id.name
}
const exportName = `export default${name ? ` ${name}` : ``}`
isES6 = true
exportNames.push(exportName)
},
AssignmentExpression: function AssignmentExpression(astPath) {
const nodeLeft = astPath.node.left
if (!t.isMemberExpression(nodeLeft)) {
return
}
// ignore marker property `__esModule`
if (
t.isIdentifier(nodeLeft.property) &&
nodeLeft.property.name === `__esModule`
) {
return
}
// get foo from `exports.foo = bar`
if (
t.isIdentifier(nodeLeft.object) &&
nodeLeft.object.name === `exports`
) {
isCommonJS = true
exportNames.push((nodeLeft.property as t.Identifier).name)
}
// get foo from `module.exports.foo = bar`
if (t.isMemberExpression(nodeLeft.object)) {
const exp: t.MemberExpression = nodeLeft.object
if (
t.isIdentifier(exp.object) &&
t.isIdentifier(exp.property) &&
exp.object.name === `module` &&
exp.property.name === `exports`
) {
isCommonJS = true
exportNames.push((nodeLeft.property as t.Identifier).name)
}
}
},
})
if (isES6 && isCommonJS && process.env.NODE_ENV !== `test`) {
report.panic(
`This plugin file is using both CommonJS and ES6 module systems together which we don't support.
You'll need to edit the file to use just one or the other.
plugin: ${modulePath}.js
This didn't cause a problem in Gatsby v1 so you might want to review the migration doc for this:
https://gatsby.dev/no-mixed-modules
`
)
}
return exportNames
}
interface IResolveModuleExportsOptions {
mode?: `analysis` | `import`
resolver?: ModuleResolver
rootDir?: string
}
/**
* Given a path to a module, return an array of the module's exports.
*
* It can run in two modes:
* 1. `analysis` mode gets exports via static analysis by traversing the file's AST with babel
* 2. `import` mode gets exports by directly importing the module and accessing its properties
*
* At the time of writing, analysis mode is used for files that can be jsx (e.g. gatsby-browser, gatsby-ssr)
* and import mode is used for files that can be js or mjs.
*
* Returns [] for invalid paths and modules without exports.
*/
export async function resolveModuleExports(
modulePath: string,
{
mode = `analysis`,
resolver = resolveModule,
rootDir = process.cwd(),
}: IResolveModuleExportsOptions = {}
): Promise<Array<string>> {
if (mode === `import`) {
try {
const moduleFilePath = await resolveJSFilepath({
rootDir,
filePath: modulePath,
})
if (!moduleFilePath) {
return []
}
const rawImportedModule = await import(
maybeAddFileProtocol(moduleFilePath)
)
// If the module is cjs, the properties we care about are nested under a top-level `default` property
const importedModule = preferDefault(rawImportedModule)
return Object.keys(importedModule).filter(
exportName => exportName !== `__esModule`
)
} catch (error) {
if (!testImportError(modulePath, error)) {
// if module exists, but requiring it cause errors,
// show the error to the user and terminate build
report.panic(`Error in "${modulePath}":`, error)
}
}
} else {
return staticallyAnalyzeExports(modulePath, resolver)
}
return []
}