Skip to content

Commit

Permalink
fix(rspack): fix withNx
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 committed Nov 11, 2024
1 parent b5591f5 commit f74f840
Show file tree
Hide file tree
Showing 10 changed files with 368 additions and 18 deletions.
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
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
4 changes: 2 additions & 2 deletions packages/rspack/src/plugins/utils/apply-base-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,8 @@ function applyNxDependentConfig(
test: /\.([jt])sx?$/,
loader: 'builtin:swc-loader',
exclude: /node_modules/,

type: 'javascript/auto',
options: {
jsc: {
parser: {
Expand All @@ -407,8 +409,6 @@ function applyNxDependentConfig(
useBuiltins: false,
},
},
type: 'javascript/auto',
loose: true,
},
},
},
Expand Down
3 changes: 3 additions & 0 deletions packages/rspack/src/plugins/utils/apply-web-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ export function applyWebConfig(
template: options.index,
sri: options.subresourceIntegrity ? 'sha256' : undefined,
...(options.baseHref ? { base: { href: options.baseHref } } : {}),
...(config.output?.scriptType === 'module'
? { scriptLoading: 'module' }
: {}),
})
);
}
Expand Down
Loading

0 comments on commit f74f840

Please sign in to comment.