@parcel/transformer-babel 2.0.0-nightly.142 → 2.0.0-nightly.1421

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.
@@ -1,102 +0,0 @@
1
- import traverse from '@babel/traverse';
2
-
3
- // Convert between babel 7 and babel 6 AST
4
- // More info on the AST Changes: https://babeljs.io/docs/en/v7-migration-api#ast-changes
5
- export function babel7toBabel6(ast) {
6
- const visitor = {
7
- ArrowFunctionExpression: node => {
8
- node.expression = node.body.type !== 'BlockStatement';
9
- },
10
- ExistsTypeAnnotation: node => {
11
- node.type = 'ExistentialTypeParam';
12
- },
13
- NumberLiteralTypeAnnotation: node => {
14
- node.type = 'NumericLiteralTypeAnnotation';
15
- },
16
- ObjectTypeIndexer: node => {
17
- node.end++;
18
- node.loc.end.column++;
19
- },
20
- ForOfStatement: node => {
21
- node.type = 'ForAwaitStatement';
22
- delete node.await;
23
- },
24
- SpreadElement: (node, path) => {
25
- if (
26
- path.parentPath.isObjectExpression() ||
27
- path.parentPath.isArrayExpression()
28
- ) {
29
- node.type = 'SpreadProperty';
30
- }
31
- },
32
- RestElement: (node, path) => {
33
- if (
34
- path.parentPath.isObjectPattern() ||
35
- path.parentPath.isArrayPattern()
36
- ) {
37
- node.type = 'RestProperty';
38
- }
39
- },
40
- };
41
-
42
- traverse(ast, {
43
- enter(path) {
44
- if (path.node.variance && path.node.variance.type === 'Variance') {
45
- path.node.variance = path.node.variance.kind;
46
- }
47
-
48
- let visitorFunc = visitor[path.node.type];
49
- if (visitorFunc) {
50
- visitorFunc(path.node, path);
51
- }
52
- },
53
- });
54
-
55
- return ast;
56
- }
57
-
58
- export function babel6toBabel7(ast) {
59
- const visitor = {
60
- ArrowFunctionExpression: node => {
61
- delete node.expression;
62
- },
63
- ExistentialTypeParam: node => {
64
- node.type = 'ExistsTypeAnnotation';
65
- },
66
- NumericLiteralTypeAnnotation: node => {
67
- node.type = 'NumberLiteralTypeAnnotation';
68
- },
69
- ObjectTypeIndexer: node => {
70
- node.end--;
71
- node.loc.end.column--;
72
- },
73
- ForAwaitStatement: node => {
74
- node.type = 'ForOfStatement';
75
- node.await = true;
76
- },
77
- SpreadProperty: node => {
78
- node.type = 'SpreadElement';
79
- },
80
- RestProperty: node => {
81
- node.type = 'RestElement';
82
- },
83
- };
84
-
85
- traverse(ast, {
86
- enter(path) {
87
- if (path.node.variance && typeof path.node.variance === 'string') {
88
- path.node.variance = {
89
- type: 'VarianceNode',
90
- kind: path.node.variance,
91
- };
92
- }
93
-
94
- let visitorFunc = visitor[path.node.type];
95
- if (visitorFunc) {
96
- visitorFunc(path.node);
97
- }
98
- },
99
- });
100
-
101
- return ast;
102
- }
package/src/babel6.js DELETED
@@ -1,52 +0,0 @@
1
- // @flow
2
-
3
- import type {MutableAsset, AST, PluginOptions} from '@parcel/types';
4
-
5
- import {babel6toBabel7} from './astConverter';
6
-
7
- export default async function babel6(
8
- asset: MutableAsset,
9
- options: PluginOptions,
10
- babelOptions: any,
11
- ): Promise<?AST> {
12
- let babel = await options.packageManager.require(
13
- 'babel-core',
14
- asset.filePath,
15
- );
16
-
17
- let config = babelOptions.config;
18
- config.code = false;
19
- config.ast = true;
20
- config.filename = asset.filePath;
21
- config.babelrc = false;
22
- config.parserOpts = Object.assign({}, config.parserOpts, {
23
- allowReturnOutsideFunction: true,
24
- allowHashBang: true,
25
- ecmaVersion: Infinity,
26
- strictMode: false,
27
- sourceType: 'module',
28
- locations: true,
29
- });
30
-
31
- // Passing a list of plugins as part of parserOpts seems to override any custom
32
- // syntax plugins a user might have added (e.g. decorators). We add dynamicImport
33
- // using a plugin instead.
34
- config.plugins = (config.plugins || []).concat(dynamicImport);
35
-
36
- let res = babel.transform(await asset.getCode(), config);
37
- if (res.ast) {
38
- return {
39
- type: 'babel',
40
- version: '7.0.0',
41
- program: babel6toBabel7(res.ast),
42
- };
43
- }
44
- }
45
-
46
- function dynamicImport() {
47
- return {
48
- manipulateOptions(opts, parserOpts) {
49
- parserOpts.plugins.push('dynamicImport');
50
- },
51
- };
52
- }
package/src/env.js DELETED
@@ -1,70 +0,0 @@
1
- // @flow
2
-
3
- import type {Config} from '@parcel/types';
4
- import type {BabelTargets} from './types';
5
-
6
- import presetEnv from '@babel/preset-env';
7
-
8
- import getBabelTargets from './getBabelTargets';
9
- import {enginesToBabelTargets} from './utils';
10
-
11
- /**
12
- * Generates a @babel/preset-env config for an asset.
13
- * This is done by finding the source module's target engines, and the app's
14
- * target engines, and doing a diff to include only the necessary plugins.
15
- */
16
- export default async function getEnvOptions(config: Config) {
17
- // Load the target engines for the app and generate a @babel/preset-env config
18
- let appBabelTargets = enginesToBabelTargets(config.env);
19
-
20
- // If this is the app module, the source and target will be the same, so just compile everything.
21
- // Otherwise, load the source engines and generate a babel-present-env config.
22
- if (!config.isSource) {
23
- let sourceBabelTargets = await getBabelTargets(config);
24
-
25
- if (
26
- !sourceBabelTargets ||
27
- !shouldCompileFurther(sourceBabelTargets, appBabelTargets)
28
- ) {
29
- return null;
30
- }
31
- }
32
-
33
- return {
34
- targets: appBabelTargets,
35
- presets: [
36
- [
37
- '@parcel/babel-preset-env',
38
- {
39
- useBuiltIns: 'entry',
40
- corejs: 3,
41
- shippedProposals: true,
42
- ignoreBrowserslistConfig: true,
43
- targets: appBabelTargets,
44
- },
45
- ],
46
- ],
47
- };
48
- }
49
-
50
- function getNeededPlugins(targets: BabelTargets): Array<mixed> {
51
- return presetEnv(
52
- {assertVersion: () => true},
53
- {targets: targets},
54
- ).plugins.filter(p => p[0]);
55
- }
56
-
57
- function shouldCompileFurther(
58
- sourceBabelTargets: BabelTargets,
59
- appBabelTargets: BabelTargets,
60
- ): boolean {
61
- let sourcePlugins = new Set(getNeededPlugins(sourceBabelTargets));
62
- let appPlugins = getNeededPlugins(appBabelTargets);
63
-
64
- // If there is any app plugin present that was not used to compile the source,
65
- // then the asset was built to a higher target and will need to be compiled
66
- // further
67
- return appPlugins.some(plugin => {
68
- return !sourcePlugins.has(plugin);
69
- });
70
- }
@@ -1,97 +0,0 @@
1
- // @flow
2
-
3
- import type {Config} from '@parcel/types';
4
- import type {BabelTargets} from './types';
5
-
6
- import browserslist from 'browserslist';
7
- import semver from 'semver';
8
-
9
- const BROWSER_CONTEXT = new Set(['browser', 'web-worker', 'service-worker']);
10
-
11
- // https://github.com/browserslist/browserslist/blob/04d7b3cc6ebeb62baf6d62aeb90f1bfd0c11117c/test/config.test.js#L23
12
- type BrowserslistConfig = {|[string]: string | Array<string>|};
13
-
14
- /**
15
- * Loads target node and browser versions from the following locations:
16
- * - package.json engines field
17
- * - package.json browserslist field
18
- * - browserslist or .browserslistrc files
19
- *
20
- * "Targets" is the name @babel/preset-env uses for what Parcel calls engines.
21
- * This should not be confused with Parcel's own targets.
22
- * Unlike Parcel's engines, @babel/preset-env expects to work with minimum
23
- * versions, not semver ranges, of its targets.
24
- */
25
- export default async function getBabelTargets(
26
- config: Config,
27
- ): Promise<?BabelTargets> {
28
- let targets = {};
29
- let compileTarget = BROWSER_CONTEXT.has(config.env.context)
30
- ? 'browsers'
31
- : config.env.context;
32
- let pkg = await config.getPackage();
33
- let packageEngines = pkg?.engines;
34
-
35
- if (compileTarget === 'node') {
36
- let nodeVersion = packageEngines?.node;
37
- // Use package.engines.node by default if we are compiling for node.
38
- if (typeof nodeVersion === 'string') {
39
- try {
40
- //$FlowFixMe catch error when minVersion() returned null
41
- targets.node = semver.minVersion(nodeVersion).version;
42
- } catch (e) {
43
- throw new Error("Expected 'node' engine to be a valid Semver Range");
44
- }
45
- }
46
- } else {
47
- let browsers;
48
- if (
49
- packageEngines &&
50
- (typeof packageEngines.browsers === 'string' ||
51
- Array.isArray(packageEngines.browsers))
52
- ) {
53
- browsers = packageEngines.browsers;
54
- } else if (pkg && pkg.browserslist) {
55
- browsers = pkg.browserslist;
56
- } else {
57
- let browserslist = await loadBrowserslist(config);
58
- if (browserslist) {
59
- browsers = browserslist;
60
- }
61
- }
62
-
63
- // Parse browser targets
64
- if (
65
- typeof browsers === 'object' &&
66
- browsers != null &&
67
- !Array.isArray(browsers)
68
- ) {
69
- let env = process.env.NODE_ENV || 'development';
70
- browsers = browsers[env] || browsers.defaults;
71
- }
72
-
73
- if (browsers) {
74
- targets.browsers = browserslist(browsers).sort();
75
- }
76
- }
77
-
78
- // Dont compile if we couldn't find any targets
79
- if (Object.keys(targets).length === 0) {
80
- return null;
81
- }
82
-
83
- return targets;
84
- }
85
-
86
- async function loadBrowserslist(config): Promise<?BrowserslistConfig> {
87
- let browserslistConfig = await config.getConfig(
88
- ['browserslist', '.browserslistrc'],
89
- {
90
- parse: false,
91
- },
92
- );
93
-
94
- if (browserslistConfig) {
95
- return browserslist.parseConfig(browserslistConfig);
96
- }
97
- }
package/src/typescript.js DELETED
@@ -1,16 +0,0 @@
1
- // @flow
2
- import path from 'path';
3
-
4
- import type {Config} from '@parcel/types';
5
- import type {BabelConfig} from './types';
6
-
7
- export default function getTypescriptOptions(config: Config): BabelConfig {
8
- return {
9
- plugins: [
10
- [
11
- '@babel/plugin-transform-typescript',
12
- {isTSX: path.extname(config.searchPath) === '.tsx'},
13
- ],
14
- ],
15
- };
16
- }