fix(react): allow async functions to work with custom webpack config (#11467)

This commit is contained in:
Jack Hsu 2022-08-05 12:13:31 -04:00 committed by GitHub
parent a829270344
commit dec910274b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 89 additions and 42 deletions

View File

@ -1,20 +1,20 @@
import { import {
checkFilesDoNotExist, checkFilesDoNotExist,
checkFilesExist, checkFilesExist,
cleanupProject,
createFile, createFile,
isNotWindows, isNotWindows,
killPorts, killPorts,
newProject, newProject,
readFile, readFile,
removeFile, removeFile,
cleanupProject, rmDist,
runCLI, runCLI,
runCLIAsync, runCLIAsync,
runCypressTests, runCypressTests,
uniq, uniq,
updateFile, updateFile,
updateProjectConfig, updateProjectConfig,
expectJestTestsToPass,
} from '@nrwl/e2e/utils'; } from '@nrwl/e2e/utils';
describe('Web Components Applications', () => { describe('Web Components Applications', () => {
@ -38,7 +38,7 @@ describe('Web Components Applications', () => {
); );
expect(readFile(`dist/apps/${appName}/index.html`)).toContain( expect(readFile(`dist/apps/${appName}/index.html`)).toContain(
`<link rel="stylesheet" href="styles.css">` '<link rel="stylesheet" href="styles.css">'
); );
const testResults = await runCLIAsync(`test ${appName}`); const testResults = await runCLIAsync(`test ${appName}`);
@ -188,8 +188,54 @@ describe('Web Components Applications', () => {
).not.toThrow(); ).not.toThrow();
}, 1000000); }, 1000000);
it('should run default jest tests', async () => { it('should support custom webpackConfig option', async () => {
await expectJestTestsToPass('@nrwl/web:app'); const appName = uniq('app');
runCLI(`generate @nrwl/web:app ${appName} --no-interactive`);
updateProjectConfig(appName, (config) => {
config.targets.build.options.webpackConfig = `apps/${appName}/webpack.config.js`;
return config;
});
// Return sync function
updateFile(
`apps/${appName}/webpack.config.js`,
`
module.exports = (config, context) => {
return config;
};
`
);
runCLI(`build ${appName} --outputHashing none`);
checkFilesExist(`dist/apps/${appName}/main.esm.js`);
rmDist();
// Return async function
updateFile(
`apps/${appName}/webpack.config.js`,
`
module.exports = async (config, context) => {
return config;
};
`
);
runCLI(`build ${appName} --outputHashing none`);
checkFilesExist(`dist/apps/${appName}/main.esm.js`);
rmDist();
// Return promise of function
updateFile(
`apps/${appName}/webpack.config.js`,
`
module.exports = Promise.resolve((config, context) => {
return config;
});
`
);
runCLI(`build ${appName} --outputHashing none`);
checkFilesExist(`dist/apps/${appName}/main.esm.js`);
}, 100000); }, 100000);
}); });
@ -377,7 +423,6 @@ describe('index.html interpolation', () => {
<meta charset="utf-8" /> <meta charset="utf-8" />
<title>BestReactApp</title> <title>BestReactApp</title>
<base href="/" /> <base href="/" />
<meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" type="image/x-icon" href="favicon.ico" /> <link rel="icon" type="image/x-icon" href="favicon.ico" />
</head> </head>

View File

@ -85,7 +85,7 @@ export default async function* devServerExecutor(
customWebpack = await customWebpack; customWebpack = await customWebpack;
} }
webpackConfig = customWebpack(webpackConfig, { webpackConfig = await customWebpack(webpackConfig, {
buildOptions, buildOptions,
configuration: serveOptions.buildTarget.split(':')[2], configuration: serveOptions.buildTarget.split(':')[2],
}); });

View File

@ -101,41 +101,43 @@ async function getWebpackConfigs(
} }
} }
return [ return await Promise.all(
// ESM build for modern browsers. [
getWebConfig( // ESM build for modern browsers.
context.root, getWebConfig(
projectRoot, context.root,
sourceRoot, projectRoot,
options, sourceRoot,
true, options,
isScriptOptimizeOn, true,
context.configurationName isScriptOptimizeOn,
), context.configurationName
// ES5 build for legacy browsers. ),
isScriptOptimizeOn && buildBrowserFeatures.isDifferentialLoadingNeeded() // ES5 build for legacy browsers.
? getWebConfig( isScriptOptimizeOn && buildBrowserFeatures.isDifferentialLoadingNeeded()
context.root, ? getWebConfig(
projectRoot, context.root,
sourceRoot, projectRoot,
options, sourceRoot,
false, options,
isScriptOptimizeOn, false,
context.configurationName isScriptOptimizeOn,
) context.configurationName
: undefined, )
] : undefined,
.filter(Boolean) ]
.map((config) => { .filter(Boolean)
if (customWebpack) { .map(async (config) => {
return customWebpack(config, { if (customWebpack) {
options, return await customWebpack(config, {
configuration: context.configurationName, options,
}); configuration: context.configurationName,
} else { });
return config; } else {
} return config;
}); }
})
);
} }
export async function* run( export async function* run(