babel-preset-react-app-new 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2013-present, Facebook, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # babel-preset-react-app
2
+
3
+ This package includes the Babel preset used by [Create React App](https://github.com/facebook/create-react-app).<br>
4
+ Please refer to its documentation:
5
+
6
+ - [Getting Started](https://facebook.github.io/create-react-app/docs/getting-started) – How to create a new app.
7
+ - [User Guide](https://facebook.github.io/create-react-app/) – How to develop apps bootstrapped with Create React App.
8
+
9
+ ## Usage in Create React App Projects
10
+
11
+ The easiest way to use this configuration is with [Create React App](https://github.com/facebook/create-react-app), which includes it by default. **You don’t need to install it separately in Create React App projects.**
12
+
13
+ ## Usage Outside of Create React App
14
+
15
+ If you want to use this Babel preset in a project not built with Create React App, you can install it with the following steps.
16
+
17
+ First, [install Babel](https://babeljs.io/docs/setup/).
18
+
19
+ Then install babel-preset-react-app.
20
+
21
+ ```sh
22
+ npm install babel-preset-react-app --save-dev
23
+ ```
24
+
25
+ Then create a file named `.babelrc` with following contents in the root folder of your project:
26
+
27
+ ```json
28
+ {
29
+ "presets": ["react-app"]
30
+ }
31
+ ```
32
+
33
+ This preset uses the `useBuiltIns` option with [transform-object-rest-spread](https://babeljs.io/docs/plugins/transform-object-rest-spread/) and [transform-react-jsx](https://babeljs.io/docs/plugins/transform-react-jsx/), which assumes that `Object.assign` is available or polyfilled.
34
+
35
+ ## Usage with Flow
36
+
37
+ Make sure you have a `.flowconfig` file at the root directory. You can also use the `flow` option on `.babelrc`:
38
+
39
+ ```json
40
+ {
41
+ "presets": [["react-app", { "flow": true, "typescript": false }]]
42
+ }
43
+ ```
44
+
45
+ ## Usage with TypeScript
46
+
47
+ Make sure you have a `tsconfig.json` file at the root directory. You can also use the `typescript` option on `.babelrc`:
48
+
49
+ ```json
50
+ {
51
+ "presets": [["react-app", { "flow": false, "typescript": true }]]
52
+ }
53
+ ```
54
+
55
+ ## Absolute Runtime Paths
56
+
57
+ Absolute paths are enabled by default for imports. To use relative paths instead, set the `absoluteRuntime` option in `.babelrc` to `false`:
58
+
59
+ ```
60
+ {
61
+ "presets": [["react-app", { "absoluteRuntime": false }]]
62
+ }
63
+ ```
package/create.js ADDED
@@ -0,0 +1,203 @@
1
+ /**
2
+ * Copyright (c) 2015-present, Facebook, Inc.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ 'use strict';
8
+
9
+ const path = require('path');
10
+
11
+ const validateBoolOption = (name, value, defaultValue) => {
12
+ if (typeof value === 'undefined') {
13
+ value = defaultValue;
14
+ }
15
+
16
+ if (typeof value !== 'boolean') {
17
+ throw new Error(`Preset react-app: '${name}' option must be a boolean.`);
18
+ }
19
+
20
+ return value;
21
+ };
22
+
23
+ module.exports = function (api, opts, env) {
24
+ if (!opts) {
25
+ opts = {};
26
+ }
27
+
28
+ var isEnvDevelopment = env === 'development';
29
+ var isEnvProduction = env === 'production';
30
+ var isEnvTest = env === 'test';
31
+
32
+ var useESModules = validateBoolOption('useESModules', opts.useESModules, isEnvDevelopment || isEnvProduction);
33
+ var isFlowEnabled = validateBoolOption('flow', opts.flow, true);
34
+ var isTypeScriptEnabled = validateBoolOption('typescript', opts.typescript, true);
35
+ var areHelpersEnabled = validateBoolOption('helpers', opts.helpers, true);
36
+ var useAbsoluteRuntime = validateBoolOption('absoluteRuntime', opts.absoluteRuntime, true);
37
+
38
+ var absoluteRuntimePath = undefined;
39
+ if (useAbsoluteRuntime) {
40
+ absoluteRuntimePath = path.dirname(require.resolve('@babel/runtime/package.json'));
41
+ }
42
+
43
+ var corejsVerson = '3.30';
44
+
45
+ try {
46
+ corejsVerson = require('core-js/package.json').version;
47
+ } catch (err) {}
48
+
49
+ if (!isEnvDevelopment && !isEnvProduction && !isEnvTest) {
50
+ throw new Error(
51
+ 'Using `babel-preset-react-app` requires that you specify `NODE_ENV` or ' +
52
+ '`BABEL_ENV` environment variables. Valid values are "development", ' +
53
+ '"test", and "production". Instead, received: ' +
54
+ JSON.stringify(env) +
55
+ '.'
56
+ );
57
+ }
58
+
59
+ return {
60
+ presets: [
61
+ isEnvTest && [
62
+ // ES features necessary for user's Node version
63
+ require('@babel/preset-env').default,
64
+ {
65
+ targets: {
66
+ node: 'current'
67
+ }
68
+ }
69
+ ],
70
+ (isEnvProduction || isEnvDevelopment) && [
71
+ // Latest stable ECMAScript features
72
+ require('@babel/preset-env').default,
73
+ Object.assign(
74
+ {
75
+ // Allow importing core-js in entrypoint and use browserlist to select polyfills
76
+ useBuiltIns: 'entry',
77
+ // Set the corejs version we are using to avoid warnings in console
78
+ corejs: corejsVerson,
79
+ // Exclude transforms that make all code slower
80
+ exclude: ['transform-typeof-symbol']
81
+ },
82
+ opts.presetEnvOptions
83
+ )
84
+ ],
85
+ [
86
+ require('@babel/preset-react').default,
87
+ {
88
+ // Adds component stack to warning messages
89
+ // Adds __self attribute to JSX which React will use for some warnings
90
+ development: isEnvDevelopment || isEnvTest,
91
+ // Will use the native built-in instead of trying to polyfill
92
+ // behavior for any plugins that require one.
93
+ ...(opts.runtime !== 'automatic' ? { useBuiltIns: true } : {}),
94
+ runtime: opts.runtime || 'classic'
95
+ }
96
+ ],
97
+ isTypeScriptEnabled && [require('@babel/preset-typescript').default]
98
+ ].filter(Boolean),
99
+ plugins: [
100
+ // Strip flow types before any other transform, emulating the behavior
101
+ // order as-if the browser supported all of the succeeding features
102
+ // https://github.com/facebook/create-react-app/pull/5182
103
+ // We will conditionally enable this plugin below in overrides as it clashes with
104
+ // @babel/plugin-proposal-decorators when using TypeScript.
105
+ // https://github.com/facebook/create-react-app/issues/5741
106
+ isFlowEnabled && [require('@babel/plugin-transform-flow-strip-types').default, false],
107
+ // Experimental macros support. Will be documented after it's had some time
108
+ // in the wild.
109
+ require('babel-plugin-macros'),
110
+ // Disabled as it's handled automatically by preset-env, and `selectiveLoose` isn't
111
+ // yet merged into babel: https://github.com/babel/babel/pull/9486
112
+ // Related: https://github.com/facebook/create-react-app/pull/8215
113
+ // [
114
+ // require('@babel/plugin-transform-destructuring').default,
115
+ // {
116
+ // // Use loose mode for performance:
117
+ // // https://github.com/facebook/create-react-app/issues/5602
118
+ // loose: false,
119
+ // selectiveLoose: [
120
+ // 'useState',
121
+ // 'useEffect',
122
+ // 'useContext',
123
+ // 'useReducer',
124
+ // 'useCallback',
125
+ // 'useMemo',
126
+ // 'useRef',
127
+ // 'useImperativeHandle',
128
+ // 'useLayoutEffect',
129
+ // 'useDebugValue',
130
+ // ],
131
+ // },
132
+ // ],
133
+ // Turn on legacy decorators for TypeScript files
134
+ isTypeScriptEnabled && [require('@babel/plugin-proposal-decorators').default, false],
135
+ // class { handleClick = () => { } }
136
+ // Enable loose mode to use assignment instead of defineProperty
137
+ // See discussion in https://github.com/facebook/create-react-app/issues/4263
138
+ // Note:
139
+ // 'loose' mode configuration must be the same for
140
+ // * @babel/plugin-proposal-class-properties
141
+ // * @babel/plugin-proposal-private-methods
142
+ // * @babel/plugin-proposal-private-property-in-object
143
+ // (when they are enabled)
144
+ [
145
+ require('@babel/plugin-proposal-class-properties').default,
146
+ {
147
+ loose: true
148
+ }
149
+ ],
150
+ [
151
+ require('@babel/plugin-proposal-private-methods').default,
152
+ {
153
+ loose: true
154
+ }
155
+ ],
156
+ [
157
+ require('@babel/plugin-proposal-private-property-in-object').default,
158
+ {
159
+ loose: true
160
+ }
161
+ ],
162
+ // Polyfills the runtime needed for async/await, generators, and friends
163
+ // https://babeljs.io/docs/en/babel-plugin-transform-runtime
164
+ [
165
+ require('@babel/plugin-transform-runtime').default,
166
+ {
167
+ corejs: false,
168
+ helpers: areHelpersEnabled,
169
+ // By default, babel assumes babel/runtime version 7.0.0-beta.0,
170
+ // explicitly resolving to match the provided helper functions.
171
+ // https://github.com/babel/babel/issues/10261
172
+ version: require('@babel/runtime/package.json').version,
173
+ regenerator: true,
174
+ // https://babeljs.io/docs/en/babel-plugin-transform-runtime#useesmodules
175
+ // We should turn this on once the lowest version of Node LTS
176
+ // supports ES Modules.
177
+ useESModules,
178
+ // Undocumented option that lets us encapsulate our runtime, ensuring
179
+ // the correct version is used
180
+ // https://github.com/babel/babel/blob/090c364a90fe73d36a30707fc612ce037bdbbb24/packages/babel-plugin-transform-runtime/src/index.js#L35-L42
181
+ absoluteRuntime: absoluteRuntimePath
182
+ }
183
+ ],
184
+ isEnvProduction && [
185
+ // Remove PropTypes from production build
186
+ require('babel-plugin-transform-react-remove-prop-types').default,
187
+ {
188
+ removeImport: true
189
+ }
190
+ ]
191
+ ].filter(Boolean),
192
+ overrides: [
193
+ isFlowEnabled && {
194
+ exclude: /\.tsx?$/,
195
+ plugins: [require('@babel/plugin-transform-flow-strip-types').default]
196
+ },
197
+ isTypeScriptEnabled && {
198
+ test: /\.tsx?$/,
199
+ plugins: [[require('@babel/plugin-proposal-decorators').default, { legacy: true }]]
200
+ }
201
+ ].filter(Boolean)
202
+ };
203
+ };
@@ -0,0 +1,143 @@
1
+ /**
2
+ * Copyright (c) 2015-present, Facebook, Inc.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ 'use strict';
8
+
9
+ const path = require('path');
10
+
11
+ const validateBoolOption = (name, value, defaultValue) => {
12
+ if (typeof value === 'undefined') {
13
+ value = defaultValue;
14
+ }
15
+
16
+ if (typeof value !== 'boolean') {
17
+ throw new Error(`Preset react-app: '${name}' option must be a boolean.`);
18
+ }
19
+
20
+ return value;
21
+ };
22
+
23
+ module.exports = function (api, opts) {
24
+ if (!opts) {
25
+ opts = {};
26
+ }
27
+
28
+ // This is similar to how `env` works in Babel:
29
+ // https://babeljs.io/docs/usage/babelrc/#env-option
30
+ // We are not using `env` because it’s ignored in versions > babel-core@6.10.4:
31
+ // https://github.com/babel/babel/issues/4539
32
+ // https://github.com/facebook/create-react-app/issues/720
33
+ // It’s also nice that we can enforce `NODE_ENV` being specified.
34
+ var env = process.env.BABEL_ENV || process.env.NODE_ENV;
35
+ var isEnvDevelopment = env === 'development';
36
+ var isEnvProduction = env === 'production';
37
+ var isEnvTest = env === 'test';
38
+
39
+ var areHelpersEnabled = validateBoolOption('helpers', opts.helpers, false);
40
+ var useAbsoluteRuntime = validateBoolOption(
41
+ 'absoluteRuntime',
42
+ opts.absoluteRuntime,
43
+ true
44
+ );
45
+
46
+ var absoluteRuntimePath = undefined;
47
+ if (useAbsoluteRuntime) {
48
+ absoluteRuntimePath = path.dirname(
49
+ require.resolve('@babel/runtime/package.json')
50
+ );
51
+ }
52
+
53
+ if (!isEnvDevelopment && !isEnvProduction && !isEnvTest) {
54
+ throw new Error(
55
+ 'Using `babel-preset-react-app` requires that you specify `NODE_ENV` or ' +
56
+ '`BABEL_ENV` environment variables. Valid values are "development", ' +
57
+ '"test", and "production". Instead, received: ' +
58
+ JSON.stringify(env) +
59
+ '.'
60
+ );
61
+ }
62
+
63
+ return {
64
+ // Babel assumes ES Modules, which isn't safe until CommonJS
65
+ // dies. This changes the behavior to assume CommonJS unless
66
+ // an `import` or `export` is present in the file.
67
+ // https://github.com/webpack/webpack/issues/4039#issuecomment-419284940
68
+ sourceType: 'unambiguous',
69
+ presets: [
70
+ isEnvTest && [
71
+ // ES features necessary for user's Node version
72
+ require('@babel/preset-env').default,
73
+ {
74
+ targets: {
75
+ node: 'current',
76
+ },
77
+ // Exclude transforms that make all code slower
78
+ exclude: ['transform-typeof-symbol'],
79
+ },
80
+ ],
81
+ (isEnvProduction || isEnvDevelopment) && [
82
+ // Latest stable ECMAScript features
83
+ require('@babel/preset-env').default,
84
+ {
85
+ // Allow importing core-js in entrypoint and use browserlist to select polyfills
86
+ useBuiltIns: 'entry',
87
+ // Set the corejs version we are using to avoid warnings in console
88
+ // This will need to change once we upgrade to corejs@3
89
+ corejs: 3,
90
+ // Exclude transforms that make all code slower
91
+ exclude: ['transform-typeof-symbol'],
92
+ },
93
+ ],
94
+ ].filter(Boolean),
95
+ plugins: [
96
+ // Disabled as it's handled automatically by preset-env, and `selectiveLoose` isn't
97
+ // yet merged into babel: https://github.com/babel/babel/pull/9486
98
+ // Related: https://github.com/facebook/create-react-app/pull/8215
99
+ // [
100
+ // require('@babel/plugin-transform-destructuring').default,
101
+ // {
102
+ // // Use loose mode for performance:
103
+ // // https://github.com/facebook/create-react-app/issues/5602
104
+ // loose: false,
105
+ // selectiveLoose: [
106
+ // 'useState',
107
+ // 'useEffect',
108
+ // 'useContext',
109
+ // 'useReducer',
110
+ // 'useCallback',
111
+ // 'useMemo',
112
+ // 'useRef',
113
+ // 'useImperativeHandle',
114
+ // 'useLayoutEffect',
115
+ // 'useDebugValue',
116
+ // ],
117
+ // },
118
+ // ],
119
+ // Polyfills the runtime needed for async/await, generators, and friends
120
+ // https://babeljs.io/docs/en/babel-plugin-transform-runtime
121
+ [
122
+ require('@babel/plugin-transform-runtime').default,
123
+ {
124
+ corejs: false,
125
+ helpers: areHelpersEnabled,
126
+ // By default, babel assumes babel/runtime version 7.0.0-beta.0,
127
+ // explicitly resolving to match the provided helper functions.
128
+ // https://github.com/babel/babel/issues/10261
129
+ version: require('@babel/runtime/package.json').version,
130
+ regenerator: true,
131
+ // https://babeljs.io/docs/en/babel-plugin-transform-runtime#useesmodules
132
+ // We should turn this on once the lowest version of Node LTS
133
+ // supports ES Modules.
134
+ useESModules: isEnvDevelopment || isEnvProduction,
135
+ // Undocumented option that lets us encapsulate our runtime, ensuring
136
+ // the correct version is used
137
+ // https://github.com/babel/babel/blob/090c364a90fe73d36a30707fc612ce037bdbbb24/packages/babel-plugin-transform-runtime/src/index.js#L35-L42
138
+ absoluteRuntime: absoluteRuntimePath,
139
+ },
140
+ ],
141
+ ].filter(Boolean),
142
+ };
143
+ };
package/dev.js ADDED
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Copyright (c) 2015-present, Facebook, Inc.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ 'use strict';
8
+
9
+ const create = require('./create');
10
+
11
+ module.exports = function (api, opts) {
12
+ return create(api, Object.assign({ helpers: false }, opts), 'development');
13
+ };
package/index.js ADDED
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Copyright (c) 2015-present, Facebook, Inc.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ 'use strict';
8
+
9
+ const create = require('./create');
10
+
11
+ module.exports = function (api, opts) {
12
+ // This is similar to how `env` works in Babel:
13
+ // https://babeljs.io/docs/usage/babelrc/#env-option
14
+ // We are not using `env` because it’s ignored in versions > babel-core@6.10.4:
15
+ // https://github.com/babel/babel/issues/4539
16
+ // https://github.com/facebook/create-react-app/issues/720
17
+ // It’s also nice that we can enforce `NODE_ENV` being specified.
18
+ const env = process.env.BABEL_ENV || process.env.NODE_ENV;
19
+ return create(api, opts, env);
20
+ };
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "babel-preset-react-app-new",
3
+ "version": "0.0.1",
4
+ "description": "Babel preset used by Create React App",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/facebook/create-react-app.git",
8
+ "directory": "packages/babel-preset-react-app"
9
+ },
10
+ "license": "MIT",
11
+ "bugs": {
12
+ "url": "https://github.com/facebook/create-react-app/issues"
13
+ },
14
+ "files": [
15
+ "create.js",
16
+ "dependencies.js",
17
+ "dev.js",
18
+ "index.js",
19
+ "webpack-overrides.js",
20
+ "prod.js",
21
+ "test.js"
22
+ ],
23
+ "dependencies": {
24
+ "@babel/core": "^7.16.0",
25
+ "@babel/plugin-proposal-class-properties": "^7.16.0",
26
+ "@babel/plugin-proposal-decorators": "^7.16.4",
27
+ "@babel/plugin-proposal-nullish-coalescing-operator": "^7.16.0",
28
+ "@babel/plugin-proposal-numeric-separator": "^7.16.0",
29
+ "@babel/plugin-proposal-optional-chaining": "^7.16.0",
30
+ "@babel/plugin-proposal-private-methods": "^7.16.0",
31
+ "@babel/plugin-transform-flow-strip-types": "^7.16.0",
32
+ "@babel/plugin-transform-react-display-name": "^7.16.0",
33
+ "@babel/plugin-transform-runtime": "^7.16.4",
34
+ "@babel/preset-env": "^7.16.4",
35
+ "@babel/preset-react": "^7.16.0",
36
+ "@babel/preset-typescript": "^7.16.0",
37
+ "@babel/runtime": "^7.16.3",
38
+ "babel-plugin-macros": "^3.1.0",
39
+ "babel-plugin-transform-react-remove-prop-types": "^0.4.24"
40
+ }
41
+ }
package/prod.js ADDED
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Copyright (c) 2015-present, Facebook, Inc.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ 'use strict';
8
+
9
+ const create = require('./create');
10
+
11
+ module.exports = function (api, opts) {
12
+ return create(api, Object.assign({ helpers: false }, opts), 'production');
13
+ };
package/test.js ADDED
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Copyright (c) 2015-present, Facebook, Inc.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ 'use strict';
8
+
9
+ const create = require('./create');
10
+
11
+ module.exports = function (api, opts) {
12
+ return create(api, Object.assign({ helpers: false }, opts), 'test');
13
+ };
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Copyright (c) 2015-present, Facebook, Inc.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ 'use strict';
8
+
9
+ const crypto = require('crypto');
10
+
11
+ const macroCheck = new RegExp('[./]macro');
12
+
13
+ module.exports = function () {
14
+ return {
15
+ // This function transforms the Babel configuration on a per-file basis
16
+ config(config, { source }) {
17
+ // Babel Macros are notoriously hard to cache, so they shouldn't be
18
+ // https://github.com/babel/babel/issues/8497
19
+ // We naively detect macros using their package suffix and add a random token
20
+ // to the caller, a valid option accepted by Babel, to compose a one-time
21
+ // cacheIdentifier for the file. We cannot tune the loader options on a per
22
+ // file basis.
23
+ if (macroCheck.test(source)) {
24
+ return Object.assign({}, config.options, {
25
+ caller: Object.assign({}, config.options.caller, {
26
+ craInvalidationToken: crypto.randomBytes(32).toString('hex'),
27
+ }),
28
+ });
29
+ }
30
+ return config.options;
31
+ },
32
+ };
33
+ };