Skip to content

Commit 6176de0

Browse files
authored
fix(bundler): resolve the configured bundler package and pass buildPath (#6127)
Projects overriding `webpackPackageName` (such as @akylas/nativescript-webpack) fell back to raw webpack/bin/webpack.js, which rejects the `--env.x` flags the CLI emits. Resolve the configured package so the modern bin is used instead. [skip ci]
1 parent 8992b24 commit 6176de0

5 files changed

Lines changed: 37 additions & 4 deletions

File tree

lib/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export const BUNDLE_DIR = "bundle";
6767
export const RESOURCES_DIR = "res";
6868
export const CONFIG_NS_FILE_NAME = "nsconfig.json";
6969
export const CONFIG_NS_APP_RESOURCES_ENTRY = "appResourcesPath";
70+
export const CONFIG_NS_BUILD_ENTRY = "buildPath";
7071
export const CONFIG_NS_APP_ENTRY = "appPath";
7172
export const CONFIG_FILE_NAME_DISPLAY = "nativescript.config.(js|ts)";
7273
export const CONFIG_FILE_NAME_JS = "nativescript.config.js";

lib/contracts/project-data.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,6 @@ export abstract class ProjectData {
8989
abstract getAppResourcesDirectoryPath(projectDir?: string): string;
9090

9191
abstract getAppResourcesRelativeDirectoryPath(): string;
92+
93+
abstract getBuildRelativeDirectoryPath(): string;
9294
}

lib/project-data.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,14 @@ export class ProjectData implements IProjectData {
282282
return this.resolveToProjectDir(appRelativePath, projectDir);
283283
}
284284

285+
public getBuildRelativeDirectoryPath(): string {
286+
if (this.nsConfig && this.nsConfig[constants.CONFIG_NS_BUILD_ENTRY]) {
287+
return this.nsConfig[constants.CONFIG_NS_BUILD_ENTRY];
288+
}
289+
290+
return constants.PLATFORMS_DIR_NAME;
291+
}
292+
285293
public getAppDirectoryRelativePath(): string {
286294
if (this.nsConfig && this.nsConfig[constants.CONFIG_NS_APP_ENTRY]) {
287295
return this.nsConfig[constants.CONFIG_NS_APP_ENTRY];

lib/services/bundler/bundler-compiler-service.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -822,12 +822,14 @@ export class BundlerCompilerService
822822
const appId = projectData.projectIdentifiers[platform];
823823
const appPath = projectData.getAppDirectoryRelativePath();
824824
const appResourcesPath = projectData.getAppResourcesRelativeDirectoryPath();
825+
const buildPath = projectData.getBuildRelativeDirectoryPath();
825826

826827
Object.assign(
827828
envData,
828829
appId && { appId },
829830
appPath && { appPath },
830831
appResourcesPath && { appResourcesPath },
832+
buildPath && { buildPath },
831833
{
832834
nativescriptLibPath: path.resolve(
833835
__dirname,
@@ -1097,7 +1099,7 @@ export class BundlerCompilerService
10971099
return path.resolve(packagePath, "bin", "vite.js");
10981100
}
10991101
} else if (this.isModernBundler(projectData)) {
1100-
const packagePath = resolvePackagePath(`@nativescript/${bundler}`, {
1102+
const packagePath = resolvePackagePath(this.getBundlerPackageName(), {
11011103
paths: [projectData.projectDir],
11021104
});
11031105

@@ -1117,15 +1119,31 @@ export class BundlerCompilerService
11171119
return path.resolve(packagePath, "bin", "webpack.js");
11181120
}
11191121

1122+
// Forks such as @akylas/nativescript-webpack replace the default package.
1123+
private getBundlerPackageName(): string {
1124+
const bundler = this.getBundler();
1125+
if (bundler !== "webpack") {
1126+
return `@nativescript/${bundler}`;
1127+
}
1128+
1129+
return this.$projectConfigService.getValue(
1130+
"webpackPackageName",
1131+
WEBPACK_PLUGIN_NAME,
1132+
);
1133+
}
1134+
11201135
private isModernBundler(projectData: IProjectData): boolean {
11211136
const bundler = this.getBundler();
11221137
switch (bundler) {
11231138
case "rspack":
11241139
return true;
11251140
default:
1126-
const packageJSONPath = resolvePackageJSONPath(WEBPACK_PLUGIN_NAME, {
1127-
paths: [projectData.projectDir],
1128-
});
1141+
const packageJSONPath = resolvePackageJSONPath(
1142+
this.getBundlerPackageName(),
1143+
{
1144+
paths: [projectData.projectDir],
1145+
},
1146+
);
11291147

11301148
if (packageJSONPath) {
11311149
const packageData = this.$fs.readJson(packageJSONPath);

test/stubs.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,10 @@ export class ProjectDataStub implements IProjectData {
720720
return "";
721721
}
722722

723+
public getBuildRelativeDirectoryPath(): string {
724+
return "platforms";
725+
}
726+
723727
public getAppDirectoryPath(projectDir?: string): string {
724728
if (!projectDir) {
725729
projectDir = this.projectDir;

0 commit comments

Comments
 (0)