Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rspack): move logic for withNx to applyBaseConfig and bring in line with webpack #28825

Merged
merged 13 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions e2e/react/src/react.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,15 @@ describe('React Applications', () => {
`
);

runCLI(`build ${appName}`);
runCLI(`build ${appName}`, { verbose: true });

checkFilesExist(`dist/${appName}/index.html`);

if (runE2ETests()) {
// TODO(Colum): investigate why webkit is failing
const e2eResults = runCLI(`e2e ${appName}-e2e -- --project=chromium`);
const e2eResults = runCLI(`e2e ${appName}-e2e -- --project=chromium`, {
verbose: true,
});
expect(e2eResults).toContain('Successfully ran target e2e for project');
expect(await killPorts()).toBeTruthy();
}
Expand Down
8 changes: 5 additions & 3 deletions packages/js/src/utils/buildable-libs-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,8 @@ export function createTmpTsConfig(
workspaceRoot: string,
projectRoot: string,
dependencies: DependentBuildableProjectNode[],
useWorkspaceAsBaseUrl: boolean = false
useWorkspaceAsBaseUrl: boolean = false,
doNotCleanOnExit: boolean = false
) {
const tmpTsConfigPath = join(
workspaceRoot,
Expand All @@ -445,8 +446,9 @@ export function createTmpTsConfig(
tmpTsConfigPath,
dependencies
);
process.on('exit', () => cleanupTmpTsConfigFile(tmpTsConfigPath));

if (!doNotCleanOnExit) {
process.on('exit', () => cleanupTmpTsConfigFile(tmpTsConfigPath));
}
if (useWorkspaceAsBaseUrl) {
parsedTSConfig.compilerOptions ??= {};
parsedTSConfig.compilerOptions.baseUrl = workspaceRoot;
Expand Down
5 changes: 4 additions & 1 deletion packages/rspack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,25 @@
"@rspack/dev-server": "^1.0.4",
"@rspack/plugin-react-refresh": "^1.0.0",
"autoprefixer": "^10.4.9",
"browserslist": "^4.21.4",
"chalk": "~4.1.0",
"css-loader": "^6.4.0",
"enquirer": "~2.3.6",
"express": "^4.19.2",
"fork-ts-checker-webpack-plugin": "7.2.13",
"http-proxy-middleware": "^3.0.3",
"less-loader": "11.1.0",
"license-webpack-plugin": "^4.0.2",
"loader-utils": "^2.0.3",
"sass": "^1.42.1",
"sass-loader": "^12.2.0",
"source-map-loader": "^5.0.0",
"style-loader": "^3.3.0",
"postcss-import": "~14.1.0",
"postcss-loader": "^8.1.1",
"postcss": "^8.4.38",
"tsconfig-paths": "^4.1.2",
"tslib": "^2.3.0",
"webpack-node-externals": "^3.0.0",
"webpack-subresource-integrity": "^5.1.0"
},
"peerDependencies": {
Expand Down
21 changes: 18 additions & 3 deletions packages/rspack/src/executors/dev-server/dev-server.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { createCompiler, isMultiCompiler } from '../../utils/create-compiler';
import { isMode } from '../../utils/mode-utils';
import { getDevServerOptions } from './lib/get-dev-server-config';
import { DevServerExecutorSchema } from './schema';
import { normalizeOptions } from '../rspack/lib/normalize-options';

type DevServer = Configuration['devServer'];
export default async function* runExecutor(
Expand All @@ -30,14 +31,28 @@ export default async function* runExecutor(

const buildOptions = readTargetOptions(buildTarget, context);

process.env.NX_BUILD_LIBS_FROM_SOURCE = `${buildOptions.buildLibsFromSource}`;
process.env.NX_BUILD_TARGET = options.buildTarget;

const metadata = context.projectsConfigurations.projects[context.projectName];
const sourceRoot = metadata.sourceRoot;
const normalizedBuildOptions = normalizeOptions(
buildOptions,
context.root,
metadata.root,
sourceRoot
);
let devServerConfig: DevServer = getDevServerOptions(
context.root,
options,
buildOptions
normalizedBuildOptions
);

const compiler = await createCompiler(
{ ...buildOptions, devServer: devServerConfig, mode: options.mode },
{
...normalizedBuildOptions,
devServer: devServerConfig,
mode: options.mode,
},
context
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function getDevServerOptions(

const config: RspackDevServerConfiguration = {
host: serveOptions.host,
port: serveOptions.port,
port: serveOptions.port ?? 4200,
headers: { 'Access-Control-Allow-Origin': '*' },
historyApiFallback: {
index:
Expand Down
56 changes: 56 additions & 0 deletions packages/rspack/src/executors/rspack/lib/normalize-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { resolve } from 'path';

import {
normalizeAssets,
normalizeFileReplacements,
} from '../../../plugins/utils/plugins/normalize-options';
import type {
RspackExecutorSchema,
NormalizedRspackExecutorSchema,
} from '../schema';

export function normalizeOptions(
options: RspackExecutorSchema,
root: string,
projectRoot: string,
sourceRoot: string
): NormalizedRspackExecutorSchema {
const normalizedOptions = {
...options,
root,
projectRoot,
sourceRoot,
target: options.target ?? 'web',
outputFileName: options.outputFileName ?? 'main.js',
rspackConfig: normalizePluginPath(options.rspackConfig, root),
fileReplacements: normalizeFileReplacements(root, options.fileReplacements),
optimization:
typeof options.optimization !== 'object'
? {
scripts: options.optimization,
styles: options.optimization,
}
: options.optimization,
};
if (options.assets) {
normalizedOptions.assets = normalizeAssets(
options.assets,
root,
sourceRoot,
projectRoot,
false // executor assets are relative to workspace root for consistency
);
}
return normalizedOptions as NormalizedRspackExecutorSchema;
}

export function normalizePluginPath(pluginPath: void | string, root: string) {
if (!pluginPath) {
return '';
}
try {
return require.resolve(pluginPath);
} catch {
return resolve(root, pluginPath);
}
}
23 changes: 20 additions & 3 deletions packages/rspack/src/executors/rspack/rspack.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as path from 'path';
import { createCompiler, isMultiCompiler } from '../../utils/create-compiler';
import { isMode } from '../../utils/mode-utils';
import { RspackExecutorSchema } from './schema';
import { normalizeOptions } from './lib/normalize-options';

export default async function* runExecutor(
options: RspackExecutorSchema,
Expand All @@ -28,8 +29,16 @@ export default async function* runExecutor(
force: true,
recursive: true,
});
const metadata = context.projectsConfigurations.projects[context.projectName];
const sourceRoot = metadata.sourceRoot;
const normalizedOptions = normalizeOptions(
options,
context.root,
metadata.root,
sourceRoot
);

const compiler = await createCompiler(options, context);
const compiler = await createCompiler(normalizedOptions, context);

const iterable = createAsyncIterable<{
success: boolean;
Expand Down Expand Up @@ -58,7 +67,11 @@ export default async function* runExecutor(
}
next({
success: !stats.hasErrors(),
outfile: path.resolve(context.root, options.outputPath, 'main.js'),
outfile: path.resolve(
context.root,
normalizedOptions.outputPath,
'main.js'
),
});
}
);
Expand Down Expand Up @@ -90,7 +103,11 @@ export default async function* runExecutor(
}
next({
success: !stats.hasErrors(),
outfile: path.resolve(context.root, options.outputPath, 'main.js'),
outfile: path.resolve(
context.root,
normalizedOptions.outputPath,
'main.js'
),
});
done();
});
Expand Down
Loading