Repository navigation
Expand file tree
/
Copy pathplugin-resolver.ts
More file actions
207 lines (184 loc) · 7.34 KB
/
Copy pathplugin-resolver.ts
File metadata and controls
207 lines (184 loc) · 7.34 KB
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
import assert from 'assert';
import * as path from 'path';
import resolveFrom from 'resolve-from';
import { PluginError } from './errors';
import { ConfigPlugin, StaticPlugin } from '../Plugin.types';
import { fileExists } from './modules';
// Default plugin entry file name.
export const pluginFileName = 'app.plugin.js';
/**
* Resolve the config plugin from a node module or package.
* If the module or package does not include a config plugin, this function throws a `PluginError`.
* The resolution is done in following order:
* 1. Is the reference a relative file path or an import specifier with file path? e.g. `./file.js`, `pkg/file.js` or `@org/pkg/file.js`?
* - Resolve the config plugin as-is
* 2. If the reference a module? e.g. `expo-font`
* - Resolve the root `app.plugin.js` file within the module, e.g. `expo-font/app.plugin.js`
* 3. Does the module have a valid config plugin in the `main` field?
* - Resolve the `main` entry point as config plugin
*/
export function resolvePluginForModule(
projectRoot: string,
pluginReference: string
): { filePath: string; isPluginFile: boolean } {
if (moduleNameIsDirectFileReference(pluginReference)) {
// Only resolve `./file.js`, `package/file.js`, `@org/package/file.js`
const pluginScriptFile = resolveFrom.silent(projectRoot, pluginReference);
if (pluginScriptFile) {
return {
// NOTE(cedric): `path.sep` is required here, we are resolving the absolute path, not the plugin reference
isPluginFile: pluginScriptFile.endsWith(path.sep + pluginFileName),
filePath: pluginScriptFile,
};
}
} else if (moduleNameIsPackageReference(pluginReference)) {
// Only resolve `package -> package/app.plugin.js`, `@org/package -> @org/package/app.plugin.js`
const pluginPackageFile = resolveFrom.silent(
projectRoot,
`${pluginReference}/${pluginFileName}`
);
if (pluginPackageFile && fileExists(pluginPackageFile)) {
return { isPluginFile: true, filePath: pluginPackageFile };
}
// Try to resole the `main` entry as config plugin
const packageMainEntry = resolveFrom.silent(projectRoot, pluginReference);
if (packageMainEntry) {
return { isPluginFile: false, filePath: packageMainEntry };
}
}
throw new PluginError(
`Failed to resolve plugin for module "${pluginReference}" relative to "${projectRoot}". Do you have node modules installed?`,
'PLUGIN_NOT_FOUND'
);
}
// TODO: Test windows
function pathIsFilePath(name: string): boolean {
// Matches lines starting with: . / ~/
return !!name.match(/^(\.|~\/|\/)/g);
}
export function moduleNameIsDirectFileReference(name: string): boolean {
if (pathIsFilePath(name)) {
return true;
}
const slashCount = name.split('/')?.length;
// Orgs (like @expo/config ) should have more than one slash to be a direct file.
if (name.startsWith('@')) {
return slashCount > 2;
}
// Regular packages should be considered direct reference if they have more than one slash.
return slashCount > 1;
}
export function moduleNameIsPackageReference(name: string): boolean {
const slashCount = name.split('/')?.length;
return name.startsWith('@') ? slashCount === 2 : slashCount === 1;
}
export function normalizeStaticPlugin(plugin: StaticPlugin | ConfigPlugin | string): StaticPlugin {
if (Array.isArray(plugin)) {
assert(
plugin.length > 0 && plugin.length < 3,
`Wrong number of arguments provided for static config plugin, expected either 1 or 2, got ${plugin.length}`
);
return plugin;
}
return [plugin, undefined];
}
export function assertInternalProjectRoot(projectRoot?: string): asserts projectRoot {
assert(
projectRoot,
`Unexpected: Config \`_internal.projectRoot\` isn't defined by expo-cli, this is a bug.`
);
}
// Resolve the module function and assert type
export function resolveConfigPluginFunction(projectRoot: string, pluginReference: string) {
const { plugin } = resolveConfigPluginFunctionWithInfo(projectRoot, pluginReference);
return plugin;
}
// Resolve the module function and assert type
export function resolveConfigPluginFunctionWithInfo(projectRoot: string, pluginReference: string) {
const { filePath: pluginFile, isPluginFile } = resolvePluginForModule(
projectRoot,
pluginReference
);
let result: any;
try {
result = requirePluginFile(pluginFile);
} catch (error) {
const learnMoreLink = 'Learn more: https://docs.expo.dev/guides/config-plugins/';
let underlyingError: string;
let stack: string | undefined;
if (error instanceof Error) {
const errorWithCode = error as Error & { code?: string };
underlyingError = `${errorWithCode.message} ${errorWithCode.code ?? ''}`;
stack = errorWithCode.stack;
} else {
underlyingError = String(error);
}
let errorMessage = `Unable to resolve a valid config plugin for ${pluginReference}.\n`;
if (!isPluginFile) {
errorMessage += `• No "${pluginFileName}" file found in ${pluginReference}: config plugins are typically exported from an "${pluginFileName}" file in the package root.\n`;
}
errorMessage += `• main export of ${pluginReference} does not appear to be a config plugin: the following error was thrown when importing ${pluginFile}: ${underlyingError}\n`;
errorMessage += `Verify that ${pluginReference} includes a config plugin. If it does not, then remove the entry from plugins in your app config file. ${learnMoreLink}`;
const pluginError = new PluginError(errorMessage, 'INVALID_PLUGIN_IMPORT');
if (stack) {
pluginError.stack = stack;
}
throw pluginError;
}
const plugin = resolveConfigPluginExport({
plugin: result,
pluginFile,
pluginReference,
isPluginFile,
});
return { plugin, pluginFile, pluginReference, isPluginFile };
}
/**
* - Resolve the exported contents of an Expo config (be it default or module.exports)
* - Assert no promise exports
* - Return config type
* - Serialize config
*
* @param props.plugin plugin results
* @param props.pluginFile plugin file path
* @param props.pluginReference the string used to reference the plugin
* @param props.isPluginFile is file path from the app.plugin.js module root
*/
export function resolveConfigPluginExport({
plugin,
pluginFile,
pluginReference,
isPluginFile,
}: {
plugin: any;
pluginFile: string;
pluginReference: string;
isPluginFile: boolean;
}): ConfigPlugin<unknown> {
if (plugin.default != null) {
plugin = plugin.default;
}
if (typeof plugin !== 'function') {
const learnMoreLink = `Learn more: https://docs.expo.dev/guides/config-plugins/`;
// If the plugin reference is a node module, and that node module does not export a function then it probably doesn't have a config plugin.
if (!isPluginFile && !moduleNameIsDirectFileReference(pluginReference)) {
throw new PluginError(
`Package "${pluginReference}" does not contain a valid config plugin. Module must export a function from file: ${pluginFile}\n${learnMoreLink}`,
'INVALID_PLUGIN_TYPE'
);
}
throw new PluginError(
`Plugin "${pluginReference}" must export a function from file: ${pluginFile}. ${learnMoreLink}`,
'INVALID_PLUGIN_TYPE'
);
}
return plugin;
}
function requirePluginFile(filePath: string): any {
try {
return require(filePath);
} catch (error) {
// TODO: Improve error messages
throw error;
}
}