@parcel/transformer-babel 2.0.0-beta.2 → 2.0.0-nightly.1004
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/BabelTransformer.js +70 -32
- package/lib/babel7.js +31 -28
- package/lib/babelErrorUtils.js +17 -0
- package/lib/config.js +219 -129
- package/lib/constants.js +3 -3
- package/lib/flow.js +29 -2
- package/lib/jsx.js +14 -48
- package/lib/remapAstLocations.js +59 -0
- package/lib/utils.js +20 -26
- package/package.json +11 -18
- package/src/BabelTransformer.js +61 -12
- package/src/babel7.js +37 -5
- package/src/babelErrorUtils.js +30 -0
- package/src/config.js +273 -106
- package/src/constants.js +1 -1
- package/src/flow.js +36 -5
- package/src/jsx.js +16 -52
- package/src/remapAstLocations.js +70 -0
- package/src/utils.js +17 -14
- package/lib/astConverter.js +0 -114
- package/lib/env.js +0 -88
- package/lib/getBabelTargets.js +0 -104
- package/lib/typescript.js +0 -29
- package/src/astConverter.js +0 -102
- package/src/env.js +0 -69
- package/src/getBabelTargets.js +0 -97
- package/src/typescript.js +0 -25
package/src/config.js
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
// @flow
|
|
2
2
|
|
|
3
|
-
import type {Config, PluginOptions} from '@parcel/types';
|
|
3
|
+
import type {Config, PluginOptions, PluginLogger} from '@parcel/types';
|
|
4
|
+
import typeof * as BabelCore from '@babel/core';
|
|
5
|
+
import type {Diagnostic} from '@parcel/diagnostic';
|
|
4
6
|
import type {BabelConfig} from './types';
|
|
5
|
-
import type {PluginLogger} from '@parcel/logger';
|
|
6
7
|
|
|
8
|
+
import json5 from 'json5';
|
|
7
9
|
import path from 'path';
|
|
8
|
-
import
|
|
9
|
-
import {
|
|
10
|
+
import {hashObject, relativePath, resolveConfig} from '@parcel/utils';
|
|
11
|
+
import {md, generateJSONCodeHighlights} from '@parcel/diagnostic';
|
|
12
|
+
import {BABEL_CORE_RANGE} from './constants';
|
|
10
13
|
|
|
11
|
-
import
|
|
12
|
-
import getJSXOptions from './jsx';
|
|
14
|
+
import isJSX from './jsx';
|
|
13
15
|
import getFlowOptions from './flow';
|
|
14
|
-
import getTypescriptOptions from './typescript';
|
|
15
16
|
import {enginesToBabelTargets} from './utils';
|
|
16
17
|
|
|
17
|
-
const TYPESCRIPT_EXTNAME_RE =
|
|
18
|
-
const BABEL_TRANSFORMER_DIR = path.dirname(__dirname);
|
|
18
|
+
const TYPESCRIPT_EXTNAME_RE = /\.tsx?$/;
|
|
19
19
|
const JS_EXTNAME_RE = /^\.(js|cjs|mjs)$/;
|
|
20
20
|
const BABEL_CONFIG_FILENAMES = [
|
|
21
21
|
'.babelrc',
|
|
@@ -30,16 +30,61 @@ const BABEL_CONFIG_FILENAMES = [
|
|
|
30
30
|
'babel.config.cjs',
|
|
31
31
|
];
|
|
32
32
|
|
|
33
|
+
type BabelConfigResult = {|
|
|
34
|
+
internal: boolean,
|
|
35
|
+
config: BabelConfig,
|
|
36
|
+
targets?: mixed,
|
|
37
|
+
syntaxPlugins?: mixed,
|
|
38
|
+
|};
|
|
39
|
+
|
|
33
40
|
export async function load(
|
|
34
41
|
config: Config,
|
|
35
42
|
options: PluginOptions,
|
|
36
43
|
logger: PluginLogger,
|
|
37
|
-
): Promise
|
|
44
|
+
): Promise<?BabelConfigResult> {
|
|
38
45
|
// Don't transpile inside node_modules
|
|
39
46
|
if (!config.isSource) {
|
|
40
47
|
return;
|
|
41
48
|
}
|
|
42
49
|
|
|
50
|
+
// Invalidate when any babel config file is added.
|
|
51
|
+
for (let fileName of BABEL_CONFIG_FILENAMES) {
|
|
52
|
+
config.invalidateOnFileCreate({
|
|
53
|
+
fileName,
|
|
54
|
+
aboveFilePath: config.searchPath,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Do nothing if we cannot resolve any babel config filenames. Checking using our own
|
|
59
|
+
// config resolution (which is cached) is much faster than relying on babel.
|
|
60
|
+
if (
|
|
61
|
+
!(await resolveConfig(
|
|
62
|
+
options.inputFS,
|
|
63
|
+
config.searchPath,
|
|
64
|
+
BABEL_CONFIG_FILENAMES,
|
|
65
|
+
options.projectRoot,
|
|
66
|
+
))
|
|
67
|
+
) {
|
|
68
|
+
return buildDefaultBabelConfig(options, config);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const babelCore: BabelCore = await options.packageManager.require(
|
|
72
|
+
'@babel/core',
|
|
73
|
+
config.searchPath,
|
|
74
|
+
{
|
|
75
|
+
range: BABEL_CORE_RANGE,
|
|
76
|
+
saveDev: true,
|
|
77
|
+
shouldAutoInstall: options.shouldAutoInstall,
|
|
78
|
+
},
|
|
79
|
+
);
|
|
80
|
+
config.addDevDependency({
|
|
81
|
+
specifier: '@babel/core',
|
|
82
|
+
resolveFrom: config.searchPath,
|
|
83
|
+
range: BABEL_CORE_RANGE,
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
config.invalidateOnEnvChange('BABEL_ENV');
|
|
87
|
+
config.invalidateOnEnvChange('NODE_ENV');
|
|
43
88
|
let babelOptions = {
|
|
44
89
|
filename: config.searchPath,
|
|
45
90
|
cwd: options.projectRoot,
|
|
@@ -57,36 +102,33 @@ export async function load(
|
|
|
57
102
|
[string]: any,
|
|
58
103
|
|} = await babelCore.loadPartialConfigAsync(babelOptions);
|
|
59
104
|
|
|
60
|
-
// Invalidate when any babel config file is added.
|
|
61
|
-
for (let fileName of BABEL_CONFIG_FILENAMES) {
|
|
62
|
-
config.invalidateOnFileCreate({
|
|
63
|
-
fileName,
|
|
64
|
-
aboveFilePath: config.searchPath,
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
|
|
68
105
|
let addIncludedFile = file => {
|
|
69
106
|
if (JS_EXTNAME_RE.test(path.extname(file))) {
|
|
70
107
|
// We need to invalidate on startup in case the config is non-static,
|
|
71
108
|
// e.g. uses unknown environment variables, reads from the filesystem, etc.
|
|
72
109
|
logger.warn({
|
|
73
|
-
message: `It looks like you're using a JavaScript Babel config file. This means the config cannot be watched for changes, and Babel transformations cannot be cached. You'll need to restart Parcel for changes to this config to take effect. Try using a ${
|
|
74
|
-
file,
|
|
75
|
-
|
|
76
|
-
) + '.json'} file instead.`,
|
|
110
|
+
message: `It looks like you're using a JavaScript Babel config file. This means the config cannot be watched for changes, and Babel transformations cannot be cached. You'll need to restart Parcel for changes to this config to take effect. Try using a ${
|
|
111
|
+
path.basename(file, path.extname(file)) + '.json'
|
|
112
|
+
} file instead.`,
|
|
77
113
|
});
|
|
78
|
-
config.
|
|
114
|
+
config.invalidateOnStartup();
|
|
79
115
|
|
|
80
116
|
// But also add the config as a dev dependency so we can at least attempt invalidation in watch mode.
|
|
81
117
|
config.addDevDependency({
|
|
82
|
-
|
|
118
|
+
specifier: relativePath(options.projectRoot, file),
|
|
83
119
|
resolveFrom: path.join(options.projectRoot, 'index'),
|
|
84
|
-
// Also invalidate @
|
|
120
|
+
// Also invalidate @babel/core when the config or a dependency updates.
|
|
85
121
|
// This ensures that the caches in @babel/core are also invalidated.
|
|
86
|
-
|
|
122
|
+
additionalInvalidations: [
|
|
123
|
+
{
|
|
124
|
+
specifier: '@babel/core',
|
|
125
|
+
resolveFrom: config.searchPath,
|
|
126
|
+
range: BABEL_CORE_RANGE,
|
|
127
|
+
},
|
|
128
|
+
],
|
|
87
129
|
});
|
|
88
130
|
} else {
|
|
89
|
-
config.
|
|
131
|
+
config.invalidateOnFileChange(file);
|
|
90
132
|
}
|
|
91
133
|
};
|
|
92
134
|
|
|
@@ -95,7 +137,7 @@ export async function load(
|
|
|
95
137
|
message:
|
|
96
138
|
'You are using an old version of @babel/core which does not support the necessary features for Parcel to cache and watch babel config files safely. You may need to restart Parcel for config changes to take effect. Please upgrade to @babel/core 7.12.0 or later to resolve this issue.',
|
|
97
139
|
});
|
|
98
|
-
config.
|
|
140
|
+
config.invalidateOnStartup();
|
|
99
141
|
};
|
|
100
142
|
|
|
101
143
|
// Old versions of @babel/core return null from loadPartialConfig when the file should explicitly not be run through babel (ignore/exclude)
|
|
@@ -130,11 +172,16 @@ export async function load(
|
|
|
130
172
|
) {
|
|
131
173
|
return;
|
|
132
174
|
} else if (partialConfig.hasFilesystemConfig()) {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
175
|
+
// Determine what syntax plugins we need to enable
|
|
176
|
+
let syntaxPlugins = [];
|
|
177
|
+
if (TYPESCRIPT_EXTNAME_RE.test(config.searchPath)) {
|
|
178
|
+
syntaxPlugins.push('typescript');
|
|
179
|
+
if (config.searchPath.endsWith('.tsx')) {
|
|
180
|
+
syntaxPlugins.push('jsx');
|
|
181
|
+
}
|
|
182
|
+
} else if (await isJSX(options, config)) {
|
|
183
|
+
syntaxPlugins.push('jsx');
|
|
184
|
+
}
|
|
138
185
|
|
|
139
186
|
// If the config has plugins loaded with require(), or inline plugins in the config,
|
|
140
187
|
// we can't cache the result of the compilation because we don't know where they came from.
|
|
@@ -144,81 +191,52 @@ export async function load(
|
|
|
144
191
|
'It looks like you are using `require` to configure Babel plugins or presets. This means Babel transformations cannot be cached and will run on each build. Please use strings to configure Babel instead.',
|
|
145
192
|
});
|
|
146
193
|
|
|
147
|
-
config.
|
|
148
|
-
config.
|
|
194
|
+
config.setCacheKey(JSON.stringify(Date.now()));
|
|
195
|
+
config.invalidateOnStartup();
|
|
149
196
|
} else {
|
|
150
|
-
|
|
151
|
-
config
|
|
197
|
+
await warnOnRedundantPlugins(options.inputFS, partialConfig, logger);
|
|
198
|
+
definePluginDependencies(config, partialConfig.options, options);
|
|
199
|
+
config.setCacheKey(hashObject(partialConfig.options));
|
|
152
200
|
}
|
|
201
|
+
|
|
202
|
+
return {
|
|
203
|
+
internal: false,
|
|
204
|
+
config: partialConfig.options,
|
|
205
|
+
targets: enginesToBabelTargets(config.env),
|
|
206
|
+
syntaxPlugins,
|
|
207
|
+
};
|
|
153
208
|
} else {
|
|
154
|
-
|
|
209
|
+
return buildDefaultBabelConfig(options, config);
|
|
155
210
|
}
|
|
156
211
|
}
|
|
157
212
|
|
|
158
|
-
async function buildDefaultBabelConfig(
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
jsxOptions?.pragmaFrag,
|
|
167
|
-
);
|
|
168
|
-
} else {
|
|
169
|
-
babelOptions = await getFlowOptions(config, options);
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
let babelTargets;
|
|
173
|
-
let envOptions = await getEnvOptions(config);
|
|
174
|
-
if (envOptions != null) {
|
|
175
|
-
babelTargets = envOptions.targets;
|
|
176
|
-
babelOptions = mergeOptions(babelOptions, envOptions.config);
|
|
177
|
-
}
|
|
178
|
-
babelOptions = mergeOptions(babelOptions, jsxOptions?.config);
|
|
179
|
-
|
|
180
|
-
if (babelOptions != null) {
|
|
181
|
-
let _babelOptions = babelOptions; // For Flow
|
|
182
|
-
_babelOptions.presets = (_babelOptions.presets || []).map(preset =>
|
|
183
|
-
babelCore.createConfigItem(preset, {
|
|
184
|
-
type: 'preset',
|
|
185
|
-
dirname: BABEL_TRANSFORMER_DIR,
|
|
186
|
-
}),
|
|
187
|
-
);
|
|
188
|
-
_babelOptions.plugins = (_babelOptions.plugins || []).map(plugin =>
|
|
189
|
-
babelCore.createConfigItem(plugin, {
|
|
190
|
-
type: 'plugin',
|
|
191
|
-
dirname: BABEL_TRANSFORMER_DIR,
|
|
192
|
-
}),
|
|
193
|
-
);
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
config.setResult({
|
|
197
|
-
internal: true,
|
|
198
|
-
config: babelOptions,
|
|
199
|
-
targets: babelTargets,
|
|
200
|
-
});
|
|
201
|
-
definePluginDependencies(config, options);
|
|
202
|
-
}
|
|
213
|
+
async function buildDefaultBabelConfig(
|
|
214
|
+
options: PluginOptions,
|
|
215
|
+
config: Config,
|
|
216
|
+
): Promise<?BabelConfigResult> {
|
|
217
|
+
// If this is a .ts or .tsx file, we don't need to enable flow.
|
|
218
|
+
if (TYPESCRIPT_EXTNAME_RE.test(config.searchPath)) {
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
203
221
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
(!config.plugins || config.plugins.length === 0))
|
|
209
|
-
) {
|
|
210
|
-
return result;
|
|
222
|
+
// Detect flow. If not enabled, babel doesn't need to run at all.
|
|
223
|
+
let babelOptions = await getFlowOptions(config, options);
|
|
224
|
+
if (babelOptions == null) {
|
|
225
|
+
return;
|
|
211
226
|
}
|
|
212
227
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
} else {
|
|
218
|
-
result = config;
|
|
228
|
+
// When flow is enabled, we may also need to enable JSX so it parses properly.
|
|
229
|
+
let syntaxPlugins = [];
|
|
230
|
+
if (await isJSX(options, config)) {
|
|
231
|
+
syntaxPlugins.push('jsx');
|
|
219
232
|
}
|
|
220
233
|
|
|
221
|
-
|
|
234
|
+
definePluginDependencies(config, babelOptions, options);
|
|
235
|
+
return {
|
|
236
|
+
internal: true,
|
|
237
|
+
config: babelOptions,
|
|
238
|
+
syntaxPlugins,
|
|
239
|
+
};
|
|
222
240
|
}
|
|
223
241
|
|
|
224
242
|
function hasRequire(options) {
|
|
@@ -226,26 +244,175 @@ function hasRequire(options) {
|
|
|
226
244
|
return configItems.some(item => !item.file);
|
|
227
245
|
}
|
|
228
246
|
|
|
229
|
-
function definePluginDependencies(config, options) {
|
|
230
|
-
let babelConfig = config.result.config;
|
|
247
|
+
function definePluginDependencies(config, babelConfig: ?BabelConfig, options) {
|
|
231
248
|
if (babelConfig == null) {
|
|
232
249
|
return;
|
|
233
250
|
}
|
|
234
251
|
|
|
235
|
-
let configItems = [
|
|
252
|
+
let configItems = [
|
|
253
|
+
...(babelConfig.presets || []),
|
|
254
|
+
...(babelConfig.plugins || []),
|
|
255
|
+
];
|
|
236
256
|
for (let configItem of configItems) {
|
|
237
257
|
// FIXME: this uses a relative path from the project root rather than resolving
|
|
238
258
|
// from the config location because configItem.file.request can be a shorthand
|
|
239
259
|
// rather than a full package name.
|
|
240
260
|
config.addDevDependency({
|
|
241
|
-
|
|
242
|
-
options.projectRoot,
|
|
243
|
-
configItem.file.resolved,
|
|
244
|
-
),
|
|
261
|
+
specifier: relativePath(options.projectRoot, configItem.file.resolved),
|
|
245
262
|
resolveFrom: path.join(options.projectRoot, 'index'),
|
|
246
|
-
// Also invalidate @
|
|
263
|
+
// Also invalidate @babel/core when the plugin or a dependency updates.
|
|
247
264
|
// This ensures that the caches in @babel/core are also invalidated.
|
|
248
|
-
|
|
265
|
+
additionalInvalidations: [
|
|
266
|
+
{
|
|
267
|
+
specifier: '@babel/core',
|
|
268
|
+
resolveFrom: config.searchPath,
|
|
269
|
+
range: BABEL_CORE_RANGE,
|
|
270
|
+
},
|
|
271
|
+
],
|
|
249
272
|
});
|
|
250
273
|
}
|
|
251
274
|
}
|
|
275
|
+
|
|
276
|
+
const redundantPresets = new Set([
|
|
277
|
+
'@babel/preset-env',
|
|
278
|
+
'@babel/preset-react',
|
|
279
|
+
'@babel/preset-typescript',
|
|
280
|
+
'@parcel/babel-preset-env',
|
|
281
|
+
]);
|
|
282
|
+
|
|
283
|
+
async function warnOnRedundantPlugins(fs, babelConfig, logger) {
|
|
284
|
+
if (babelConfig == null) {
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
let configPath = babelConfig.config ?? babelConfig.babelrc;
|
|
289
|
+
if (!configPath) {
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
let presets = babelConfig.options.presets || [];
|
|
294
|
+
let plugins = babelConfig.options.plugins || [];
|
|
295
|
+
let foundRedundantPresets = new Set();
|
|
296
|
+
|
|
297
|
+
let filteredPresets = presets.filter(preset => {
|
|
298
|
+
if (redundantPresets.has(preset.file.request)) {
|
|
299
|
+
foundRedundantPresets.add(preset.file.request);
|
|
300
|
+
return false;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
return true;
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
let filePath = path.relative(process.cwd(), configPath);
|
|
307
|
+
let diagnostics: Array<Diagnostic> = [];
|
|
308
|
+
|
|
309
|
+
if (
|
|
310
|
+
filteredPresets.length === 0 &&
|
|
311
|
+
foundRedundantPresets.size > 0 &&
|
|
312
|
+
plugins.length === 0
|
|
313
|
+
) {
|
|
314
|
+
diagnostics.push({
|
|
315
|
+
message: md`Parcel includes transpilation by default. Babel config __${filePath}__ contains only redundant presets. Deleting it may significantly improve build performance.`,
|
|
316
|
+
codeFrames: [
|
|
317
|
+
{
|
|
318
|
+
filePath: configPath,
|
|
319
|
+
codeHighlights: await getCodeHighlights(
|
|
320
|
+
fs,
|
|
321
|
+
configPath,
|
|
322
|
+
foundRedundantPresets,
|
|
323
|
+
),
|
|
324
|
+
},
|
|
325
|
+
],
|
|
326
|
+
hints: [md`Delete __${filePath}__`],
|
|
327
|
+
documentationURL:
|
|
328
|
+
'https://parceljs.org/languages/javascript/#default-presets',
|
|
329
|
+
});
|
|
330
|
+
} else if (foundRedundantPresets.size > 0) {
|
|
331
|
+
diagnostics.push({
|
|
332
|
+
message: md`Parcel includes transpilation by default. Babel config __${filePath}__ includes the following redundant presets: ${[
|
|
333
|
+
...foundRedundantPresets,
|
|
334
|
+
].map(p =>
|
|
335
|
+
md.underline(p),
|
|
336
|
+
)}. Removing these may improve build performance.`,
|
|
337
|
+
codeFrames: [
|
|
338
|
+
{
|
|
339
|
+
filePath: configPath,
|
|
340
|
+
codeHighlights: await getCodeHighlights(
|
|
341
|
+
fs,
|
|
342
|
+
configPath,
|
|
343
|
+
foundRedundantPresets,
|
|
344
|
+
),
|
|
345
|
+
},
|
|
346
|
+
],
|
|
347
|
+
hints: [md`Remove the above presets from __${filePath}__`],
|
|
348
|
+
documentationURL:
|
|
349
|
+
'https://parceljs.org/languages/javascript/#default-presets',
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
if (foundRedundantPresets.has('@babel/preset-env')) {
|
|
354
|
+
diagnostics.push({
|
|
355
|
+
message:
|
|
356
|
+
"@babel/preset-env does not support Parcel's targets, which will likely result in unnecessary transpilation and larger bundle sizes.",
|
|
357
|
+
codeFrames: [
|
|
358
|
+
{
|
|
359
|
+
filePath: babelConfig.config ?? babelConfig.babelrc,
|
|
360
|
+
codeHighlights: await getCodeHighlights(
|
|
361
|
+
fs,
|
|
362
|
+
babelConfig.config ?? babelConfig.babelrc,
|
|
363
|
+
new Set(['@babel/preset-env']),
|
|
364
|
+
),
|
|
365
|
+
},
|
|
366
|
+
],
|
|
367
|
+
hints: [
|
|
368
|
+
`Either remove __@babel/preset-env__ to use Parcel's builtin transpilation, or replace with __@parcel/babel-preset-env__`,
|
|
369
|
+
],
|
|
370
|
+
documentationURL:
|
|
371
|
+
'https://parceljs.org/languages/javascript/#custom-plugins',
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
if (diagnostics.length > 0) {
|
|
376
|
+
logger.warn(diagnostics);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
async function getCodeHighlights(fs, filePath, redundantPresets) {
|
|
381
|
+
let ext = path.extname(filePath);
|
|
382
|
+
if (ext !== '.js' && ext !== '.cjs' && ext !== '.mjs') {
|
|
383
|
+
let contents = await fs.readFile(filePath, 'utf8');
|
|
384
|
+
let json = json5.parse(contents);
|
|
385
|
+
|
|
386
|
+
let presets = json.presets || [];
|
|
387
|
+
let pointers = [];
|
|
388
|
+
for (let i = 0; i < presets.length; i++) {
|
|
389
|
+
if (Array.isArray(presets[i]) && redundantPresets.has(presets[i][0])) {
|
|
390
|
+
pointers.push({type: 'value', key: `/presets/${i}/0`});
|
|
391
|
+
} else if (redundantPresets.has(presets[i])) {
|
|
392
|
+
pointers.push({type: 'value', key: `/presets/${i}`});
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
if (pointers.length > 0) {
|
|
397
|
+
try {
|
|
398
|
+
return generateJSONCodeHighlights(contents, pointers);
|
|
399
|
+
} catch {
|
|
400
|
+
// TODO: support code highlights for json5 sources.
|
|
401
|
+
// Babel supports json5 syntax, but json-source-map does not.
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
return [
|
|
407
|
+
{
|
|
408
|
+
start: {
|
|
409
|
+
line: 1,
|
|
410
|
+
column: 1,
|
|
411
|
+
},
|
|
412
|
+
end: {
|
|
413
|
+
line: 1,
|
|
414
|
+
column: 1,
|
|
415
|
+
},
|
|
416
|
+
},
|
|
417
|
+
];
|
|
418
|
+
}
|
package/src/constants.js
CHANGED
package/src/flow.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
// @flow
|
|
2
2
|
|
|
3
|
-
import type {Config, PluginOptions} from '@parcel/types';
|
|
3
|
+
import type {Config, PluginOptions, PackageJSON} from '@parcel/types';
|
|
4
4
|
import type {BabelConfig} from './types';
|
|
5
|
+
import typeof * as BabelCore from '@babel/core';
|
|
6
|
+
|
|
7
|
+
import {BABEL_CORE_RANGE} from './constants';
|
|
8
|
+
import path from 'path';
|
|
5
9
|
|
|
6
10
|
/**
|
|
7
11
|
* Generates a babel config for stripping away Flow types.
|
|
@@ -15,9 +19,10 @@ export default async function getFlowOptions(
|
|
|
15
19
|
}
|
|
16
20
|
|
|
17
21
|
// Only add flow plugin if `flow-bin` is listed as a dependency in the root package.json
|
|
18
|
-
let conf = await config.getConfigFrom(
|
|
19
|
-
|
|
20
|
-
|
|
22
|
+
let conf = await config.getConfigFrom<PackageJSON>(
|
|
23
|
+
options.projectRoot + '/index',
|
|
24
|
+
['package.json'],
|
|
25
|
+
);
|
|
21
26
|
let pkg = conf?.contents;
|
|
22
27
|
if (
|
|
23
28
|
!pkg ||
|
|
@@ -27,9 +32,35 @@ export default async function getFlowOptions(
|
|
|
27
32
|
return null;
|
|
28
33
|
}
|
|
29
34
|
|
|
35
|
+
const babelCore: BabelCore = await options.packageManager.require(
|
|
36
|
+
'@babel/core',
|
|
37
|
+
config.searchPath,
|
|
38
|
+
{
|
|
39
|
+
range: BABEL_CORE_RANGE,
|
|
40
|
+
saveDev: true,
|
|
41
|
+
shouldAutoInstall: options.shouldAutoInstall,
|
|
42
|
+
},
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
await options.packageManager.require(
|
|
46
|
+
'@babel/plugin-transform-flow-strip-types',
|
|
47
|
+
config.searchPath,
|
|
48
|
+
{
|
|
49
|
+
range: '^7.0.0',
|
|
50
|
+
saveDev: true,
|
|
51
|
+
shouldAutoInstall: options.shouldAutoInstall,
|
|
52
|
+
},
|
|
53
|
+
);
|
|
54
|
+
|
|
30
55
|
return {
|
|
31
56
|
plugins: [
|
|
32
|
-
|
|
57
|
+
babelCore.createConfigItem(
|
|
58
|
+
['@babel/plugin-transform-flow-strip-types', {requireDirective: true}],
|
|
59
|
+
{
|
|
60
|
+
type: 'plugin',
|
|
61
|
+
dirname: path.dirname(config.searchPath),
|
|
62
|
+
},
|
|
63
|
+
),
|
|
33
64
|
],
|
|
34
65
|
};
|
|
35
66
|
}
|
package/src/jsx.js
CHANGED
|
@@ -1,76 +1,40 @@
|
|
|
1
1
|
// @flow strict-local
|
|
2
2
|
|
|
3
3
|
import type {Config, PluginOptions} from '@parcel/types';
|
|
4
|
-
import type {BabelConfig} from './types';
|
|
5
4
|
|
|
6
5
|
import path from 'path';
|
|
7
6
|
|
|
8
|
-
const JSX_EXTENSIONS =
|
|
9
|
-
|
|
10
|
-
'.tsx': true,
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
const JSX_PRAGMA = {
|
|
14
|
-
react: {
|
|
15
|
-
pragma: 'React.createElement',
|
|
16
|
-
pragmaFrag: 'React.Fragment',
|
|
17
|
-
},
|
|
18
|
-
preact: {
|
|
19
|
-
pragma: 'h',
|
|
20
|
-
pragmaFrag: 'Fragment',
|
|
21
|
-
},
|
|
22
|
-
nervjs: {
|
|
23
|
-
pragma: 'Nerv.createElement',
|
|
24
|
-
pragmaFrag: undefined,
|
|
25
|
-
},
|
|
26
|
-
hyperapp: {
|
|
27
|
-
pragma: 'h',
|
|
28
|
-
pragmaFrag: undefined,
|
|
29
|
-
},
|
|
30
|
-
};
|
|
7
|
+
const JSX_EXTENSIONS = new Set(['.jsx', '.tsx']);
|
|
8
|
+
const JSX_LIBRARIES = ['react', 'preact', 'nervejs', 'hyperapp'];
|
|
31
9
|
|
|
32
10
|
/**
|
|
33
|
-
*
|
|
34
|
-
*
|
|
11
|
+
* Returns whether an asset is likely JSX. Attempts to detect react or react-like libraries
|
|
12
|
+
* along with
|
|
35
13
|
*/
|
|
36
|
-
export default async function
|
|
14
|
+
export default async function isJSX(
|
|
37
15
|
options: PluginOptions,
|
|
38
16
|
config: Config,
|
|
39
|
-
): Promise
|
|
17
|
+
): Promise<boolean> {
|
|
40
18
|
if (!config.isSource) {
|
|
41
|
-
return
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (JSX_EXTENSIONS.has(path.extname(config.searchPath))) {
|
|
23
|
+
return true;
|
|
42
24
|
}
|
|
43
25
|
|
|
44
26
|
let pkg = await config.getPackage();
|
|
45
|
-
let reactLib;
|
|
46
27
|
if (pkg?.alias && pkg.alias['react']) {
|
|
47
28
|
// e.g.: `{ alias: { "react": "preact/compat" } }`
|
|
48
|
-
|
|
29
|
+
return true;
|
|
49
30
|
} else {
|
|
50
|
-
// Find a dependency that
|
|
51
|
-
|
|
31
|
+
// Find a dependency that implies JSX syntax.
|
|
32
|
+
return JSX_LIBRARIES.some(
|
|
52
33
|
libName =>
|
|
53
34
|
pkg &&
|
|
54
35
|
((pkg.dependencies && pkg.dependencies[libName]) ||
|
|
55
|
-
(pkg.devDependencies && pkg.devDependencies[libName])
|
|
36
|
+
(pkg.devDependencies && pkg.devDependencies[libName]) ||
|
|
37
|
+
(pkg.peerDependencies && pkg.peerDependencies[libName])),
|
|
56
38
|
);
|
|
57
39
|
}
|
|
58
|
-
|
|
59
|
-
let pragma = reactLib ? JSX_PRAGMA[reactLib].pragma : undefined;
|
|
60
|
-
let pragmaFrag = reactLib ? JSX_PRAGMA[reactLib].pragmaFrag : undefined;
|
|
61
|
-
|
|
62
|
-
if (pragma || JSX_EXTENSIONS[path.extname(config.searchPath)]) {
|
|
63
|
-
return {
|
|
64
|
-
config: {
|
|
65
|
-
presets: [
|
|
66
|
-
[
|
|
67
|
-
'@babel/preset-react',
|
|
68
|
-
{pragma, pragmaFrag, development: options.mode === 'development'},
|
|
69
|
-
],
|
|
70
|
-
],
|
|
71
|
-
},
|
|
72
|
-
pragma,
|
|
73
|
-
pragmaFrag,
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
40
|
}
|