@cypress/webpack-dev-server 1.8.3 → 2.1.0
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/README.md +54 -75
- package/dist/CypressCTWebpackPlugin.d.ts +15 -0
- package/dist/CypressCTWebpackPlugin.js +105 -0
- package/dist/aut-runner.js +1 -0
- package/dist/constants.d.ts +1 -0
- package/dist/constants.js +9 -0
- package/dist/createWebpackDevServer.d.ts +26 -0
- package/dist/createWebpackDevServer.js +49 -0
- package/dist/devServer.d.ts +35 -0
- package/dist/devServer.js +106 -0
- package/dist/helpers/angularHandler.d.ts +80 -0
- package/dist/helpers/angularHandler.js +131 -0
- package/dist/helpers/createReactAppHandler.d.ts +8 -0
- package/dist/helpers/createReactAppHandler.js +301 -0
- package/dist/helpers/nextHandler.d.ts +3 -0
- package/dist/helpers/nextHandler.js +237 -0
- package/dist/helpers/nuxtHandler.d.ts +2 -0
- package/dist/helpers/nuxtHandler.js +28 -0
- package/dist/helpers/sourceRelativeWebpackModules.d.ts +46 -0
- package/dist/helpers/sourceRelativeWebpackModules.js +212 -0
- package/dist/helpers/vueCliHandler.d.ts +2 -0
- package/dist/helpers/vueCliHandler.js +24 -0
- package/dist/index.d.ts +3 -15
- package/dist/index.js +4 -63
- package/dist/loader.d.ts +1 -3
- package/dist/loader.js +8 -28
- package/dist/makeDefaultWebpackConfig.d.ts +1 -0
- package/dist/makeDefaultWebpackConfig.js +50 -0
- package/dist/makeWebpackConfig.d.ts +6 -16
- package/dist/makeWebpackConfig.js +139 -93
- package/dist/measureWebpackPerformance.d.ts +1 -2
- package/dist/measureWebpackPerformance.js +11 -8
- package/package.json +34 -25
- package/CHANGELOG.md +0 -181
- package/dist/plugin.d.ts +0 -45
- package/dist/plugin.js +0 -84
- package/dist/startServer.d.ts +0 -13
- package/dist/startServer.js +0 -75
- package/dist/webpack.config.d.ts +0 -2
- package/dist/webpack.config.js +0 -24
- package/dist/webpackDevServerFacts.d.ts +0 -6
- package/dist/webpackDevServerFacts.js +0 -19
- package/index-template.html +0 -12
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toPosix = exports.getAngularJson = exports.getAngularCliModules = exports.getTempDir = exports.generateTsConfig = exports.getAngularBuildOptions = exports.angularHandler = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const fs = (0, tslib_1.__importStar)(require("fs-extra"));
|
|
6
|
+
const os_1 = require("os");
|
|
7
|
+
const path = (0, tslib_1.__importStar)(require("path"));
|
|
8
|
+
const url_1 = require("url");
|
|
9
|
+
const sourceRelativeWebpackModules_1 = require("./sourceRelativeWebpackModules");
|
|
10
|
+
const dynamicImport = new Function('specifier', 'return import(specifier)');
|
|
11
|
+
async function angularHandler(devServerConfig) {
|
|
12
|
+
const webpackConfig = await getAngularCliWebpackConfig(devServerConfig);
|
|
13
|
+
return { frameworkConfig: webpackConfig, sourceWebpackModulesResult: (0, sourceRelativeWebpackModules_1.sourceDefaultWebpackDependencies)(devServerConfig) };
|
|
14
|
+
}
|
|
15
|
+
exports.angularHandler = angularHandler;
|
|
16
|
+
async function getAngularCliWebpackConfig(devServerConfig) {
|
|
17
|
+
const { projectRoot } = devServerConfig.cypressConfig;
|
|
18
|
+
const { generateBrowserWebpackConfigFromContext, getCommonConfig, getStylesConfig, } = await getAngularCliModules(projectRoot);
|
|
19
|
+
const angularJson = await getAngularJson(projectRoot);
|
|
20
|
+
let { defaultProject } = angularJson;
|
|
21
|
+
if (!defaultProject) {
|
|
22
|
+
defaultProject = Object.keys(angularJson.projects).find((name) => angularJson.projects[name].projectType === 'application');
|
|
23
|
+
if (!defaultProject) {
|
|
24
|
+
throw new Error('Could not find a project with projectType "application" in "angular.json"');
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
const defaultProjectConfig = angularJson.projects[defaultProject];
|
|
28
|
+
const tsConfig = await generateTsConfig(devServerConfig, defaultProjectConfig);
|
|
29
|
+
const buildOptions = getAngularBuildOptions(defaultProjectConfig, tsConfig);
|
|
30
|
+
const context = createFakeContext(projectRoot, defaultProject, defaultProjectConfig);
|
|
31
|
+
const { config } = await generateBrowserWebpackConfigFromContext(buildOptions, context, (wco) => [getCommonConfig(wco), getStylesConfig(wco)]);
|
|
32
|
+
delete config.entry.main;
|
|
33
|
+
return config;
|
|
34
|
+
}
|
|
35
|
+
function getAngularBuildOptions(projectConfig, tsConfig) {
|
|
36
|
+
var _a;
|
|
37
|
+
// Default options are derived from the @angular-devkit/build-angular browser builder, with some options from
|
|
38
|
+
// the serve builder thrown in for development.
|
|
39
|
+
// see: https://github.com/angular/angular-cli/blob/main/packages/angular_devkit/build_angular/src/builders/browser/schema.json
|
|
40
|
+
return Object.assign(Object.assign(Object.assign({ outputPath: 'dist/angular-app', assets: [], styles: [], scripts: [], fileReplacements: [], inlineStyleLanguage: 'css', stylePreprocessorOptions: { includePaths: [] }, resourcesOutputPath: undefined, commonChunk: true, baseHref: undefined, deployUrl: undefined, verbose: false, progress: false, i18nMissingTranslation: 'warning', i18nDuplicateTranslation: 'warning', localize: undefined, watch: true, poll: undefined, deleteOutputPath: true, preserveSymlinks: undefined, showCircularDependencies: false, subresourceIntegrity: false, serviceWorker: false, ngswConfigPath: undefined, statsJson: false, webWorkerTsConfig: undefined, crossOrigin: 'none', allowedCommonJsDependencies: [], buildOptimizer: false, optimization: false, vendorChunk: true, extractLicenses: false, sourceMap: true, namedChunks: true }, projectConfig.architect.build.options), ((_a = projectConfig.architect.build.configurations) === null || _a === void 0 ? void 0 : _a.development) || {}), { tsConfig, aot: false, outputHashing: 'none', budgets: undefined });
|
|
41
|
+
}
|
|
42
|
+
exports.getAngularBuildOptions = getAngularBuildOptions;
|
|
43
|
+
async function generateTsConfig(devServerConfig, projectConfig) {
|
|
44
|
+
const { cypressConfig } = devServerConfig;
|
|
45
|
+
const { projectRoot } = cypressConfig;
|
|
46
|
+
const specPattern = Array.isArray(cypressConfig.specPattern) ? cypressConfig.specPattern : [cypressConfig.specPattern];
|
|
47
|
+
const getProjectFilePath = (...fileParts) => (0, exports.toPosix)(path.join(projectRoot, ...fileParts));
|
|
48
|
+
const includePaths = [...specPattern.map((pattern) => getProjectFilePath(pattern))];
|
|
49
|
+
if (cypressConfig.supportFile) {
|
|
50
|
+
includePaths.push((0, exports.toPosix)(cypressConfig.supportFile));
|
|
51
|
+
}
|
|
52
|
+
if (projectConfig.architect.build.options.polyfills) {
|
|
53
|
+
const polyfills = getProjectFilePath(projectConfig.architect.build.options.polyfills);
|
|
54
|
+
includePaths.push(polyfills);
|
|
55
|
+
}
|
|
56
|
+
const cypressTypes = getProjectFilePath('node_modules', 'cypress', 'types', 'index.d.ts');
|
|
57
|
+
includePaths.push(cypressTypes);
|
|
58
|
+
const tsConfigContent = JSON.stringify({
|
|
59
|
+
extends: getProjectFilePath('tsconfig.json'),
|
|
60
|
+
compilerOptions: {
|
|
61
|
+
outDir: getProjectFilePath('out-tsc/cy'),
|
|
62
|
+
allowSyntheticDefaultImports: true,
|
|
63
|
+
skipLibCheck: true,
|
|
64
|
+
},
|
|
65
|
+
include: includePaths,
|
|
66
|
+
});
|
|
67
|
+
const tsConfigPath = path.join(await getTempDir(), 'tsconfig.json');
|
|
68
|
+
await fs.writeFile(tsConfigPath, tsConfigContent);
|
|
69
|
+
return tsConfigPath;
|
|
70
|
+
}
|
|
71
|
+
exports.generateTsConfig = generateTsConfig;
|
|
72
|
+
async function getTempDir() {
|
|
73
|
+
const cypressTempDir = path.join((0, os_1.tmpdir)(), 'cypress-angular-ct');
|
|
74
|
+
await fs.ensureDir(cypressTempDir);
|
|
75
|
+
return cypressTempDir;
|
|
76
|
+
}
|
|
77
|
+
exports.getTempDir = getTempDir;
|
|
78
|
+
async function getAngularCliModules(projectRoot) {
|
|
79
|
+
const [{ generateBrowserWebpackConfigFromContext }, { getCommonConfig }, { getStylesConfig },] = await Promise.all([
|
|
80
|
+
'@angular-devkit/build-angular/src/utils/webpack-browser-config.js',
|
|
81
|
+
'@angular-devkit/build-angular/src/webpack/configs/common.js',
|
|
82
|
+
'@angular-devkit/build-angular/src/webpack/configs/styles.js',
|
|
83
|
+
].map((dep) => {
|
|
84
|
+
try {
|
|
85
|
+
const depPath = require.resolve(dep, { paths: [projectRoot] });
|
|
86
|
+
const url = (0, url_1.pathToFileURL)(depPath).href;
|
|
87
|
+
return dynamicImport(url);
|
|
88
|
+
}
|
|
89
|
+
catch (e) {
|
|
90
|
+
throw new Error(`Could not resolve "${dep}". Do you have "@angular-devkit/build-angular" installed?`);
|
|
91
|
+
}
|
|
92
|
+
}));
|
|
93
|
+
return {
|
|
94
|
+
generateBrowserWebpackConfigFromContext,
|
|
95
|
+
getCommonConfig,
|
|
96
|
+
getStylesConfig,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
exports.getAngularCliModules = getAngularCliModules;
|
|
100
|
+
async function getAngularJson(projectRoot) {
|
|
101
|
+
const { findUp } = await dynamicImport('find-up');
|
|
102
|
+
const angularJsonPath = await findUp('angular.json', { cwd: projectRoot });
|
|
103
|
+
if (!angularJsonPath) {
|
|
104
|
+
throw new Error(`Could not find angular.json. Looked in ${projectRoot} and up.`);
|
|
105
|
+
}
|
|
106
|
+
const angularJson = await fs.readFile(angularJsonPath, 'utf8');
|
|
107
|
+
return JSON.parse(angularJson);
|
|
108
|
+
}
|
|
109
|
+
exports.getAngularJson = getAngularJson;
|
|
110
|
+
function createFakeContext(projectRoot, defaultProject, defaultProjectConfig) {
|
|
111
|
+
const logger = {
|
|
112
|
+
createChild: () => ({}),
|
|
113
|
+
};
|
|
114
|
+
const context = {
|
|
115
|
+
target: {
|
|
116
|
+
project: defaultProject,
|
|
117
|
+
},
|
|
118
|
+
workspaceRoot: projectRoot,
|
|
119
|
+
getProjectMetadata: () => {
|
|
120
|
+
return {
|
|
121
|
+
root: defaultProjectConfig.root,
|
|
122
|
+
sourceRoot: defaultProjectConfig.root,
|
|
123
|
+
projectType: 'application',
|
|
124
|
+
};
|
|
125
|
+
},
|
|
126
|
+
logger,
|
|
127
|
+
};
|
|
128
|
+
return context;
|
|
129
|
+
}
|
|
130
|
+
const toPosix = (filePath) => filePath.split(path.sep).join(path.posix.sep);
|
|
131
|
+
exports.toPosix = toPosix;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Configuration } from 'webpack';
|
|
2
|
+
import type { PresetHandlerResult, WebpackDevServerConfig } from '../devServer';
|
|
3
|
+
/**
|
|
4
|
+
* Sourcing the config for create-react-app
|
|
5
|
+
*/
|
|
6
|
+
export declare function createReactAppHandler(devServerConfig: WebpackDevServerConfig): PresetHandlerResult;
|
|
7
|
+
export declare const cypressGlobals: string[];
|
|
8
|
+
export declare function reactScriptsFiveModifications(webpackConfig: Configuration): void;
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.reactScriptsFiveModifications = exports.cypressGlobals = exports.createReactAppHandler = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const debug_1 = (0, tslib_1.__importDefault)(require("debug"));
|
|
6
|
+
const path_1 = (0, tslib_1.__importDefault)(require("path"));
|
|
7
|
+
const sourceRelativeWebpackModules_1 = require("./sourceRelativeWebpackModules");
|
|
8
|
+
const debug = (0, debug_1.default)('cypress:webpack-dev-server:create-react-app');
|
|
9
|
+
/**
|
|
10
|
+
* Sourcing the config for create-react-app
|
|
11
|
+
*/
|
|
12
|
+
function createReactAppHandler(devServerConfig) {
|
|
13
|
+
const sourceWebpackModulesResult = (0, sourceRelativeWebpackModules_1.sourceDefaultWebpackDependencies)(devServerConfig);
|
|
14
|
+
// this is required because
|
|
15
|
+
// 1) we use our own HMR and we don't need react-refresh transpiling overhead
|
|
16
|
+
// 2) it doesn't work with process.env=test @see https://github.com/cypress-io/cypress-realworld-app/pull/832
|
|
17
|
+
process.env.FAST_REFRESH = 'false';
|
|
18
|
+
const webpackConfig = loadWebpackConfig(devServerConfig);
|
|
19
|
+
addCypressToWebpackEslintRulesInPlace(webpackConfig);
|
|
20
|
+
getTranspileFolders(devServerConfig.cypressConfig).forEach((folder) => {
|
|
21
|
+
allowModuleSourceInPlace(folder, webpackConfig);
|
|
22
|
+
addFolderToBabelLoaderTranspileInPlace(folder, webpackConfig);
|
|
23
|
+
});
|
|
24
|
+
if (sourceWebpackModulesResult.webpack.majorVersion === 5) {
|
|
25
|
+
debug('Modifying configuration for react-scripts@5');
|
|
26
|
+
reactScriptsFiveModifications(webpackConfig);
|
|
27
|
+
}
|
|
28
|
+
return {
|
|
29
|
+
frameworkConfig: webpackConfig,
|
|
30
|
+
sourceWebpackModulesResult,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
exports.createReactAppHandler = createReactAppHandler;
|
|
34
|
+
function loadWebpackConfig(devServerConfig) {
|
|
35
|
+
let webpackConfigPath;
|
|
36
|
+
const envName = 'test';
|
|
37
|
+
process.env.NODE_ENV = envName;
|
|
38
|
+
process.env.BABEL_ENV = envName;
|
|
39
|
+
try {
|
|
40
|
+
// Search for react-scripts webpack config
|
|
41
|
+
webpackConfigPath = require.resolve('react-scripts/config/webpack.config.js', {
|
|
42
|
+
paths: [devServerConfig.cypressConfig.projectRoot],
|
|
43
|
+
});
|
|
44
|
+
debug('Found react-scripts webpack config at %s', webpackConfigPath);
|
|
45
|
+
}
|
|
46
|
+
catch (err) {
|
|
47
|
+
// Might be an ejected cra app, search for common webpack configs
|
|
48
|
+
const ejectedWebpackConfigPath = path_1.default.join(devServerConfig.cypressConfig.projectRoot, 'config', 'webpack.config.js');
|
|
49
|
+
try {
|
|
50
|
+
webpackConfigPath = require.resolve(ejectedWebpackConfigPath);
|
|
51
|
+
}
|
|
52
|
+
catch (err) {
|
|
53
|
+
throw new Error(`Failed to find webpack at ${ejectedWebpackConfigPath}. We looked in this location as we could not find the "react-scripts" dependency and are assuming an ejected create-react-app.`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
try {
|
|
57
|
+
let webpackConfig = require(webpackConfigPath);
|
|
58
|
+
if (webpackConfig.default) {
|
|
59
|
+
// we probably loaded TS file
|
|
60
|
+
debug('loaded webpack options has .default - taking that as the config');
|
|
61
|
+
webpackConfig = webpackConfig.default;
|
|
62
|
+
}
|
|
63
|
+
if (typeof webpackConfig === 'function') {
|
|
64
|
+
debug('calling webpack function with environment "%s"', envName);
|
|
65
|
+
webpackConfig = webpackConfig('development');
|
|
66
|
+
}
|
|
67
|
+
delete webpackConfig.entry;
|
|
68
|
+
return webpackConfig;
|
|
69
|
+
}
|
|
70
|
+
catch (err) {
|
|
71
|
+
throw new Error(`Failed to require webpack config at ${webpackConfigPath} with error: ${err}`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
exports.cypressGlobals = ['cy', 'Cypress', 'before', 'after', 'context'];
|
|
75
|
+
function addCypressToWebpackEslintRulesInPlace(webpackConfig) {
|
|
76
|
+
var _a, _b;
|
|
77
|
+
const eslintPlugin = (_a = webpackConfig.plugins) === null || _a === void 0 ? void 0 : _a.find((plugin) => plugin.constructor.name === 'ESLintWebpackPlugin');
|
|
78
|
+
if (eslintPlugin) {
|
|
79
|
+
const cypressGlobalsRules = exports.cypressGlobals
|
|
80
|
+
.reduce((acc, global) => (Object.assign(Object.assign({}, acc), { [global]: 'writable' })), {});
|
|
81
|
+
eslintPlugin.options.baseConfig = Object.assign(Object.assign({}, eslintPlugin.options.baseConfig), { globals: Object.assign(Object.assign({}, (_b = eslintPlugin.options.baseConfig) === null || _b === void 0 ? void 0 : _b.globals), cypressGlobalsRules) });
|
|
82
|
+
debug('Found ESLintWebpackPlugin, modified eslint config %o', eslintPlugin.options.baseConfig);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function getTranspileFolders(cypressConfig) {
|
|
86
|
+
var _a;
|
|
87
|
+
const rawFolders = (_a = cypressConfig.addTranspiledFolders) !== null && _a !== void 0 ? _a : [];
|
|
88
|
+
const folders = rawFolders.map((folder) => path_1.default.resolve(cypressConfig.projectRoot, folder));
|
|
89
|
+
// ensure path is absolute
|
|
90
|
+
const ensureAbs = (folder) => {
|
|
91
|
+
if (!path_1.default.isAbsolute(folder)) {
|
|
92
|
+
return path_1.default.resolve(folder);
|
|
93
|
+
}
|
|
94
|
+
return folder;
|
|
95
|
+
};
|
|
96
|
+
if (cypressConfig.fixturesFolder) {
|
|
97
|
+
folders.push(ensureAbs(cypressConfig.fixturesFolder));
|
|
98
|
+
}
|
|
99
|
+
if (cypressConfig.supportFolder) {
|
|
100
|
+
folders.push(ensureAbs(cypressConfig.supportFolder));
|
|
101
|
+
}
|
|
102
|
+
// attempt to add directories based on spec pattern
|
|
103
|
+
let componentDirs = cypressConfig.specPattern || '';
|
|
104
|
+
// can be string or array
|
|
105
|
+
if (typeof componentDirs === 'string') {
|
|
106
|
+
componentDirs = [componentDirs];
|
|
107
|
+
}
|
|
108
|
+
const dirsFromSpecPattern = componentDirs.reduce((acc, curr) => {
|
|
109
|
+
// glob
|
|
110
|
+
if (curr.includes('*')) {
|
|
111
|
+
const parts = curr.slice(0, curr.indexOf('*') - 1);
|
|
112
|
+
const joined = parts.split(path_1.default.sep);
|
|
113
|
+
const dir = path_1.default.join(...joined);
|
|
114
|
+
return acc.concat(path_1.default.resolve(cypressConfig.projectRoot, dir));
|
|
115
|
+
}
|
|
116
|
+
return acc;
|
|
117
|
+
}, []);
|
|
118
|
+
return folders.concat(...dirsFromSpecPattern, path_1.default.resolve('cypress'));
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Finds the ModuleScopePlugin plugin and adds given folder
|
|
122
|
+
* to that list. This allows react-scripts to import folders
|
|
123
|
+
* outside of the default "/src" folder.
|
|
124
|
+
* WARNING modifies the input webpack options argument.
|
|
125
|
+
* @see https://github.com/bahmutov/cypress-react-unit-test/issues/289
|
|
126
|
+
* @param {string} folderName Folder to add, should be absolute
|
|
127
|
+
*/
|
|
128
|
+
function allowModuleSourceInPlace(folderName, webpackConfig) {
|
|
129
|
+
if (!folderName) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
if (!webpackConfig.resolve) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
if (!Array.isArray(webpackConfig.resolve.plugins)) {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
const moduleSourcePlugin = webpackConfig.resolve.plugins.find((plugin) => {
|
|
139
|
+
return typeof plugin !== 'string' && Array.isArray(plugin.appSrcs);
|
|
140
|
+
});
|
|
141
|
+
if (!moduleSourcePlugin) {
|
|
142
|
+
debug('cannot find module source plugin');
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
debug('found module source plugin %o', moduleSourcePlugin);
|
|
146
|
+
if (!moduleSourcePlugin.appSrcs.includes(folderName)) {
|
|
147
|
+
moduleSourcePlugin.appSrcs.push(folderName);
|
|
148
|
+
debug('added folder %s to allowed sources', folderName);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Returns true if the provided loader path includes "babel-loader".
|
|
153
|
+
* Uses current OS path separator to split the loader path correctly.
|
|
154
|
+
*/
|
|
155
|
+
const isBabelLoader = (loaderPath) => {
|
|
156
|
+
if (!loaderPath) {
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
const loaderPathParts = loaderPath.split(path_1.default.sep);
|
|
160
|
+
return loaderPathParts.some((pathPart) => pathPart === 'babel-loader');
|
|
161
|
+
};
|
|
162
|
+
const findBabelRule = (webpackConfig) => {
|
|
163
|
+
if (!webpackConfig) {
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
if (!webpackConfig.module) {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
debug('webpackConfig.module %o', webpackConfig.module);
|
|
170
|
+
if (!Array.isArray(webpackConfig.module.rules)) {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
const oneOfRule = webpackConfig.module.rules.find((rule) => {
|
|
174
|
+
return typeof rule !== 'string' && Array.isArray(rule.oneOf);
|
|
175
|
+
});
|
|
176
|
+
if (!oneOfRule || !oneOfRule.oneOf) {
|
|
177
|
+
debug('could not find oneOfRule');
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
debug('looking through oneOf rules');
|
|
181
|
+
debug('oneOfRule.oneOf %o', oneOfRule.oneOf);
|
|
182
|
+
oneOfRule.oneOf.forEach((rule) => debug('rule %o', rule));
|
|
183
|
+
const babelRule = oneOfRule.oneOf.find((rule) => rule.loader && isBabelLoader(rule.loader));
|
|
184
|
+
return babelRule;
|
|
185
|
+
};
|
|
186
|
+
// see https://github.com/bahmutov/find-webpack/issues/7
|
|
187
|
+
const findBabelLoaderRule = (webpackConfig) => {
|
|
188
|
+
debug('looking for babel-loader rule');
|
|
189
|
+
if (!webpackConfig) {
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
if (!webpackConfig.module) {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
debug('webpackConfig.module %o', webpackConfig.module);
|
|
196
|
+
if (!Array.isArray(webpackConfig.module.rules)) {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
debug('webpack module rules');
|
|
200
|
+
webpackConfig.module.rules.forEach((rule) => {
|
|
201
|
+
debug('rule %o', rule);
|
|
202
|
+
});
|
|
203
|
+
const babelRule = webpackConfig.module.rules.find((rule) => typeof rule !== 'string' && rule.loader === 'babel-loader');
|
|
204
|
+
if (!babelRule || !babelRule.test) {
|
|
205
|
+
debug('could not find babel rule');
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
debug('found Babel rule that applies to %s', babelRule.test.toString());
|
|
209
|
+
return babelRule;
|
|
210
|
+
};
|
|
211
|
+
const findBabelLoaderUseRule = (webpackConfig) => {
|
|
212
|
+
var _a;
|
|
213
|
+
debug('looking for babel-loader rule with use property');
|
|
214
|
+
if (!webpackConfig) {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
if (!webpackConfig.module) {
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
debug('webpackConfig.module %o', webpackConfig.module);
|
|
221
|
+
if (!Array.isArray(webpackConfig.module.rules)) {
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
debug('webpack module rules');
|
|
225
|
+
webpackConfig.module.rules.forEach((rule) => {
|
|
226
|
+
debug('rule %o', rule);
|
|
227
|
+
});
|
|
228
|
+
const isBabelLoader = (rule) => { var _a; return ((_a = rule === null || rule === void 0 ? void 0 : rule.use) === null || _a === void 0 ? void 0 : _a.loader) === 'babel-loader'; };
|
|
229
|
+
const isNextBabelLoader = (rule) => { var _a; return ((_a = rule === null || rule === void 0 ? void 0 : rule.use) === null || _a === void 0 ? void 0 : _a.loader) === 'next-babel-loader'; };
|
|
230
|
+
const babelRule = webpackConfig.module.rules.find((rule) => isBabelLoader(rule) || isNextBabelLoader(rule));
|
|
231
|
+
if (!babelRule) {
|
|
232
|
+
debug('could not find babel rule');
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
debug('found Babel use rule that applies to %s', (_a = babelRule.test) === null || _a === void 0 ? void 0 : _a.toString());
|
|
236
|
+
return babelRule.use;
|
|
237
|
+
};
|
|
238
|
+
const findBabelRuleWrap = (webpackConfig) => {
|
|
239
|
+
let babelRule = findBabelRule(webpackConfig);
|
|
240
|
+
if (!babelRule) {
|
|
241
|
+
debug('could not find Babel rule using oneOf');
|
|
242
|
+
babelRule = findBabelLoaderRule(webpackConfig);
|
|
243
|
+
}
|
|
244
|
+
if (!babelRule) {
|
|
245
|
+
debug('could not find Babel rule directly');
|
|
246
|
+
babelRule = findBabelLoaderUseRule(webpackConfig);
|
|
247
|
+
}
|
|
248
|
+
if (!babelRule) {
|
|
249
|
+
debug('could not find Babel rule');
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
return babelRule;
|
|
253
|
+
};
|
|
254
|
+
function addFolderToBabelLoaderTranspileInPlace(folder, webpackConfig) {
|
|
255
|
+
if (!folder) {
|
|
256
|
+
debug('no extra folder to transpile using Babel');
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
debug('trying to transpile additional folder %s using Babel', folder);
|
|
260
|
+
const babelRule = findBabelRuleWrap(webpackConfig);
|
|
261
|
+
if (!babelRule) {
|
|
262
|
+
debug('could not find Babel rule');
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
debug('babel rule %o', babelRule);
|
|
266
|
+
if (!babelRule.include) {
|
|
267
|
+
debug('could not find Babel include condition');
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
if (typeof babelRule.include === 'string') {
|
|
271
|
+
babelRule.include = [babelRule.include];
|
|
272
|
+
}
|
|
273
|
+
if (babelRule.include.includes(folder)) {
|
|
274
|
+
// do not double include the same folder
|
|
275
|
+
debug('babel includes rule for folder %s', folder);
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
babelRule.include.push(folder);
|
|
279
|
+
debug('added folder %s to babel rules', folder);
|
|
280
|
+
}
|
|
281
|
+
function reactScriptsFiveModifications(webpackConfig) {
|
|
282
|
+
var _a;
|
|
283
|
+
// React-Scripts sets the webpack target to ["browserslist"] which tells
|
|
284
|
+
// webpack to target the browsers found within the browserslist config
|
|
285
|
+
// depending on the environment (process.env.NODE_ENV). Since we set
|
|
286
|
+
// process.env.NODE_ENV = "test", webpack is unable to find any browsers and errors.
|
|
287
|
+
// We set BROWSERSLIST_ENV = "development" to override the default NODE_ENV search of browsers.
|
|
288
|
+
if (!process.env.BROWSERSLIST_ENV) {
|
|
289
|
+
process.env.BROWSERSLIST_ENV = 'development';
|
|
290
|
+
}
|
|
291
|
+
// We use the "development" configuration of the react-scripts webpack config.
|
|
292
|
+
// There is a conflict when settings process.env.NODE_ENV = "test" since DefinePlugin
|
|
293
|
+
// uses the "development" configuration and expects process.env.NODE_ENV = "development".
|
|
294
|
+
const definePlugin = (_a = webpackConfig.plugins) === null || _a === void 0 ? void 0 : _a.find((plugin) => plugin.constructor.name === 'DefinePlugin');
|
|
295
|
+
if (definePlugin) {
|
|
296
|
+
const processEnv = definePlugin.definitions['process.env'];
|
|
297
|
+
processEnv.NODE_ENV = JSON.stringify('development');
|
|
298
|
+
debug('Found "DefinePlugin", modified "process.env" definition %o', processEnv);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
exports.reactScriptsFiveModifications = reactScriptsFiveModifications;
|