/
loadParcelPlugin.js
202 lines (192 loc) Β· 6.01 KB
/
loadParcelPlugin.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
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
// @flow
import type {FilePath, PackageName, Semver, SemverRange} from '@parcel/types';
import type {ParcelOptions} from './types';
import path from 'path';
import semver from 'semver';
import logger from '@parcel/logger';
import nullthrows from 'nullthrows';
import ThrowableDiagnostic, {
generateJSONCodeHighlights,
md,
} from '@parcel/diagnostic';
import {
findAlternativeNodeModules,
loadConfig,
resolveConfig,
} from '@parcel/utils';
import {type ProjectPath, toProjectPath} from './projectPath';
import {version as PARCEL_VERSION} from '../package.json';
const NODE_MODULES = `${path.sep}node_modules${path.sep}`;
const CONFIG = Symbol.for('parcel-plugin-config');
export default async function loadPlugin<T>(
pluginName: PackageName,
configPath: FilePath,
keyPath?: string,
options: ParcelOptions,
): Promise<{|
plugin: T,
version: Semver,
resolveFrom: ProjectPath,
range: ?SemverRange,
|}> {
let resolveFrom = configPath;
let range;
if (resolveFrom.includes(NODE_MODULES)) {
let configPkg = await loadConfig(
options.inputFS,
resolveFrom,
['package.json'],
options.projectRoot,
);
if (
configPkg != null &&
configPkg.config.dependencies?.[pluginName] == null
) {
// If not in the config's dependencies, the plugin will be auto installed with
// the version declared in "parcelDependencies".
range = configPkg.config.parcelDependencies?.[pluginName];
if (range == null) {
let contents = await options.inputFS.readFile(
configPkg.files[0].filePath,
'utf8',
);
throw new ThrowableDiagnostic({
diagnostic: {
message: md`Could not determine version of ${pluginName} in ${path.relative(
process.cwd(),
resolveFrom,
)}. Either include it in "dependencies" or "parcelDependencies".`,
origin: '@parcel/core',
codeFrames:
configPkg.config.dependencies ||
configPkg.config.parcelDependencies
? [
{
filePath: configPkg.files[0].filePath,
language: 'json5',
code: contents,
codeHighlights: generateJSONCodeHighlights(contents, [
{
key: configPkg.config.parcelDependencies
? '/parcelDependencies'
: '/dependencies',
type: 'key',
},
]),
},
]
: undefined,
},
});
}
// Resolve from project root if not in the config's dependencies.
resolveFrom = path.join(options.projectRoot, 'index');
}
}
let resolved, pkg;
try {
({resolved, pkg} = await options.packageManager.resolve(
pluginName,
resolveFrom,
{
shouldAutoInstall: options.shouldAutoInstall,
range,
},
));
} catch (err) {
if (err.code !== 'MODULE_NOT_FOUND') {
throw err;
}
let configContents = await options.inputFS.readFile(configPath, 'utf8');
let alternatives = await findAlternativeNodeModules(
options.inputFS,
pluginName,
path.dirname(resolveFrom),
);
throw new ThrowableDiagnostic({
diagnostic: {
message: md`Cannot find Parcel plugin "${pluginName}"`,
origin: '@parcel/core',
codeFrames: keyPath
? [
{
filePath: configPath,
language: 'json5',
code: configContents,
codeHighlights: generateJSONCodeHighlights(configContents, [
{
key: keyPath,
type: 'value',
message: md`Cannot find module "${pluginName}"${
alternatives[0]
? `, did you mean "${alternatives[0]}"?`
: ''
}`,
},
]),
},
]
: undefined,
},
});
}
// Validate the engines.parcel field in the plugin's package.json
let parcelVersionRange = pkg && pkg.engines && pkg.engines.parcel;
if (!parcelVersionRange) {
logger.warn({
origin: '@parcel/core',
message: `The plugin "${pluginName}" needs to specify a \`package.json#engines.parcel\` field with the supported Parcel version range.`,
});
}
if (
parcelVersionRange &&
!semver.satisfies(PARCEL_VERSION, parcelVersionRange)
) {
let pkgFile = nullthrows(
await resolveConfig(
options.inputFS,
resolved,
['package.json'],
options.projectRoot,
),
);
let pkgContents = await options.inputFS.readFile(pkgFile, 'utf8');
throw new ThrowableDiagnostic({
diagnostic: {
message: md`The plugin "${pluginName}" is not compatible with the current version of Parcel. Requires "${parcelVersionRange}" but the current version is "${PARCEL_VERSION}".`,
origin: '@parcel/core',
codeFrames: [
{
filePath: pkgFile,
language: 'json5',
code: pkgContents,
codeHighlights: generateJSONCodeHighlights(pkgContents, [
{
key: '/engines/parcel',
},
]),
},
],
},
});
}
let plugin = await options.packageManager.require(pluginName, resolveFrom, {
shouldAutoInstall: options.shouldAutoInstall,
});
plugin = plugin.default ? plugin.default : plugin;
if (!plugin) {
throw new Error(`Plugin ${pluginName} has no exports.`);
}
plugin = plugin[CONFIG];
if (!plugin) {
throw new Error(
`Plugin ${pluginName} is not a valid Parcel plugin, should export an instance of a Parcel plugin ex. "export default new Reporter({ ... })".`,
);
}
return {
plugin,
version: nullthrows(pkg).version,
resolveFrom: toProjectPath(options.projectRoot, resolveFrom),
range,
};
}