Skip to content

Commit 1613ab2

Browse files
react-scripts Eject (#1007)
* Eject
1 parent 37cfb84 commit 1613ab2

File tree

8 files changed

+5757
-11473
lines changed

8 files changed

+5757
-11473
lines changed

config/env.js

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/**
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
const fs = require('fs');
20+
const path = require('path');
21+
const paths = require('./paths');
22+
23+
// Make sure that including paths.js after env.js will read .env variables.
24+
delete require.cache[require.resolve('./paths')];
25+
26+
const NODE_ENV = process.env.NODE_ENV;
27+
if (!NODE_ENV) {
28+
throw new Error(
29+
'The NODE_ENV environment variable is required but was not specified.'
30+
);
31+
}
32+
33+
// https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use
34+
const dotenvFiles = [
35+
`${paths.dotenv}.${NODE_ENV}.local`,
36+
// Don't include `.env.local` for `test` environment
37+
// since normally you expect tests to produce the same
38+
// results for everyone
39+
NODE_ENV !== 'test' && `${paths.dotenv}.local`,
40+
`${paths.dotenv}.${NODE_ENV}`,
41+
paths.dotenv,
42+
].filter(Boolean);
43+
44+
// Load environment variables from .env* files. Suppress warnings using silent
45+
// if this file is missing. dotenv will never modify any environment variables
46+
// that have already been set. Variable expansion is supported in .env files.
47+
// https://github.com/motdotla/dotenv
48+
// https://github.com/motdotla/dotenv-expand
49+
dotenvFiles.forEach((dotenvFile) => {
50+
if (fs.existsSync(dotenvFile)) {
51+
require('dotenv-expand')(
52+
require('dotenv').config({
53+
path: dotenvFile,
54+
})
55+
);
56+
}
57+
});
58+
59+
// We support resolving modules according to `NODE_PATH`.
60+
// This lets you use absolute paths in imports inside large monorepos:
61+
// https://github.com/facebook/create-react-app/issues/253.
62+
// It works similar to `NODE_PATH` in Node itself:
63+
// https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
64+
// Note that unlike in Node, only *relative* paths from `NODE_PATH` are honored.
65+
// Otherwise, we risk importing Node.js core modules into an app instead of webpack shims.
66+
// https://github.com/facebook/create-react-app/issues/1023#issuecomment-265344421
67+
// We also resolve them to make sure all tools using them work consistently.
68+
const appDirectory = fs.realpathSync(process.cwd());
69+
process.env.NODE_PATH = (process.env.NODE_PATH || '')
70+
.split(path.delimiter)
71+
.filter((folder) => folder && !path.isAbsolute(folder))
72+
.map((folder) => path.resolve(appDirectory, folder))
73+
.join(path.delimiter);
74+
75+
// Grab NODE_ENV and REACT_APP_* environment variables and prepare them to be
76+
// injected into the application via DefinePlugin in webpack configuration.
77+
const REACT_APP = /^REACT_APP_/i;
78+
79+
function getClientEnvironment(publicUrl) {
80+
const raw = Object.keys(process.env)
81+
.filter((key) => REACT_APP.test(key))
82+
.reduce(
83+
(env, key) => {
84+
env[key] = process.env[key];
85+
return env;
86+
},
87+
{
88+
// Useful for determining whether we’re running in production mode.
89+
// Most importantly, it switches React into the correct mode.
90+
NODE_ENV: process.env.NODE_ENV || 'development',
91+
// Useful for resolving the correct path to static assets in `public`.
92+
// For example, <img src={process.env.PUBLIC_URL + '/img/logo.png'} />.
93+
// This should only be used as an escape hatch. Normally you would put
94+
// images into the `src` and `import` them in code to get their paths.
95+
PUBLIC_URL: publicUrl,
96+
// We support configuring the sockjs pathname during development.
97+
// These settings let a developer run multiple simultaneous projects.
98+
// They are used as the connection `hostname`, `pathname` and `port`
99+
// in webpackHotDevClient. They are used as the `sockHost`, `sockPath`
100+
// and `sockPort` options in webpack-dev-server.
101+
WDS_SOCKET_HOST: process.env.WDS_SOCKET_HOST,
102+
WDS_SOCKET_PATH: process.env.WDS_SOCKET_PATH,
103+
WDS_SOCKET_PORT: process.env.WDS_SOCKET_PORT,
104+
// Whether or not react-refresh is enabled.
105+
// It is defined here so it is available in the webpackHotDevClient.
106+
FAST_REFRESH: process.env.FAST_REFRESH !== 'false',
107+
}
108+
);
109+
// Stringify all values so we can feed into webpack DefinePlugin
110+
const stringified = {
111+
'process.env': Object.keys(raw).reduce((env, key) => {
112+
env[key] = JSON.stringify(raw[key]);
113+
return env;
114+
}, {}),
115+
};
116+
117+
return { raw, stringified };
118+
}
119+
120+
module.exports = getClientEnvironment;

config/jest/babelTransform.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
const babelJest = require('babel-jest').default;
20+
21+
const hasJsxRuntime = (() => {
22+
if (process.env.DISABLE_NEW_JSX_TRANSFORM === 'true') {
23+
return false;
24+
}
25+
26+
try {
27+
require.resolve('react/jsx-runtime');
28+
return true;
29+
} catch (e) {
30+
return false;
31+
}
32+
})();
33+
34+
module.exports = babelJest.createTransformer({
35+
presets: [
36+
[
37+
require.resolve('babel-preset-react-app'),
38+
{
39+
runtime: hasJsxRuntime ? 'automatic' : 'classic',
40+
},
41+
],
42+
],
43+
babelrc: false,
44+
configFile: false,
45+
});

config/jest/cssTransform.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
// This is a custom Jest transformer turning style imports into empty objects.
20+
// http://facebook.github.io/jest/docs/en/webpack.html
21+
22+
module.exports = {
23+
process() {
24+
return 'module.exports = {};';
25+
},
26+
getCacheKey() {
27+
// The output is always the same.
28+
return 'cssTransform';
29+
},
30+
};

config/jest/fileTransform.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
const path = require('path');
20+
const camelcase = require('camelcase');
21+
22+
// This is a custom Jest transformer turning file imports into filenames.
23+
// http://facebook.github.io/jest/docs/en/webpack.html
24+
25+
module.exports = {
26+
process(src, filename) {
27+
const assetFilename = JSON.stringify(path.basename(filename));
28+
29+
if (filename.match(/\.svg$/)) {
30+
// Based on how SVGR generates a component name:
31+
// https://github.com/smooth-code/svgr/blob/01b194cf967347d43d4cbe6b434404731b87cf27/packages/core/src/state.js#L6
32+
const pascalCaseFilename = camelcase(path.parse(filename).name, {
33+
pascalCase: true,
34+
});
35+
const componentName = `Svg${pascalCaseFilename}`;
36+
return `const React = require('react');
37+
module.exports = {
38+
__esModule: true,
39+
default: ${assetFilename},
40+
ReactComponent: React.forwardRef(function ${componentName}(props, ref) {
41+
return {
42+
$$typeof: Symbol.for('react.element'),
43+
type: 'svg',
44+
ref: ref,
45+
key: null,
46+
props: Object.assign({}, props, {
47+
children: ${assetFilename}
48+
})
49+
};
50+
}),
51+
};`;
52+
}
53+
54+
return `module.exports = ${assetFilename};`;
55+
},
56+
};

config/paths.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
const path = require('path');
20+
const fs = require('fs');
21+
const getPublicUrlOrPath = require('react-dev-utils/getPublicUrlOrPath');
22+
23+
// Make sure any symlinks in the project folder are resolved:
24+
// https://github.com/facebook/create-react-app/issues/637
25+
const appDirectory = fs.realpathSync(process.cwd());
26+
const resolveApp = (relativePath) => path.resolve(appDirectory, relativePath);
27+
28+
// We use `PUBLIC_URL` environment variable or "homepage" field to infer
29+
// "public path" at which the app is served.
30+
// webpack needs to know it to put the right <script> hrefs into HTML even in
31+
// single-page apps that may serve index.html for nested URLs like /todos/42.
32+
// We can't use a relative path in HTML because we don't want to load something
33+
// like /todos/42/static/js/bundle.7289d.js. We have to know the root.
34+
const publicUrlOrPath = getPublicUrlOrPath(
35+
process.env.NODE_ENV === 'development',
36+
require(resolveApp('package.json')).homepage,
37+
process.env.PUBLIC_URL
38+
);
39+
40+
const buildPath = process.env.BUILD_PATH || 'build';
41+
42+
const moduleFileExtensions = [
43+
'web.mjs',
44+
'mjs',
45+
'web.js',
46+
'js',
47+
'web.ts',
48+
'ts',
49+
'web.tsx',
50+
'tsx',
51+
'json',
52+
'web.jsx',
53+
'jsx',
54+
];
55+
56+
// Resolve file paths in the same order as webpack
57+
const resolveModule = (resolveFn, filePath) => {
58+
const extension = moduleFileExtensions.find((extension) =>
59+
fs.existsSync(resolveFn(`${filePath}.${extension}`))
60+
);
61+
62+
if (extension) {
63+
return resolveFn(`${filePath}.${extension}`);
64+
}
65+
66+
return resolveFn(`${filePath}.js`);
67+
};
68+
69+
// config after eject: we're in ./config/
70+
module.exports = {
71+
dotenv: resolveApp('.env'),
72+
appPath: resolveApp('.'),
73+
appBuild: resolveApp(buildPath),
74+
appPublic: resolveApp('public'),
75+
appHtml: resolveApp('public/index.html'),
76+
appIndexJs: resolveModule(resolveApp, 'src/index'),
77+
appPackageJson: resolveApp('package.json'),
78+
appSrc: resolveApp('src'),
79+
appTsConfig: resolveApp('tsconfig.json'),
80+
appJsConfig: resolveApp('jsconfig.json'),
81+
yarnLockFile: resolveApp('yarn.lock'),
82+
testsSetup: resolveModule(resolveApp, 'src/setupTests'),
83+
proxySetup: resolveApp('src/setupProxy.js'),
84+
appNodeModules: resolveApp('node_modules'),
85+
appWebpackCache: resolveApp('node_modules/.cache'),
86+
appTsBuildInfoFile: resolveApp('node_modules/.cache/tsconfig.tsbuildinfo'),
87+
swSrc: resolveModule(resolveApp, 'src/service-worker'),
88+
publicUrlOrPath,
89+
};
90+
91+
module.exports.moduleFileExtensions = moduleFileExtensions;

0 commit comments

Comments
 (0)