Skip to content

Commit 5b80974

Browse files
authored
test: bundler and bundlerConfigPath (#5841)
1 parent 247f961 commit 5b80974

File tree

2 files changed

+91
-16
lines changed

2 files changed

+91
-16
lines changed

lib/project-data.ts

+5-9
Original file line numberDiff line numberDiff line change
@@ -207,21 +207,17 @@ export class ProjectData implements IProjectData {
207207
constants.PODFILE_NAME,
208208
);
209209
this.isShared = !!(this.nsConfig && this.nsConfig.shared);
210-
this.webpackConfigPath =
210+
211+
const webpackConfigPath =
211212
this.nsConfig && this.nsConfig.webpackConfigPath
212213
? path.resolve(this.projectDir, this.nsConfig.webpackConfigPath)
213214
: path.join(this.projectDir, "webpack.config.js");
215+
this.webpackConfigPath = webpackConfigPath;
214216
this.bundlerConfigPath =
215217
this.nsConfig && this.nsConfig.bundlerConfigPath
216218
? path.resolve(this.projectDir, this.nsConfig.bundlerConfigPath)
217-
: null;
218-
this.bundler =
219-
this.nsConfig && this.nsConfig.bundler
220-
? (path.resolve(
221-
this.projectDir,
222-
this.nsConfig.bundler,
223-
) as BundlerType)
224-
: "webpack";
219+
: webpackConfigPath;
220+
this.bundler = this?.nsConfig?.bundler ?? "webpack";
225221
return;
226222
}
227223

test/project-data.ts

+86-7
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,16 @@ describe("projectData", () => {
5656
configData?: {
5757
shared?: boolean;
5858
webpackConfigPath?: string;
59+
bundlerConfigPath?: string;
5960
projectName?: string;
61+
bundler?: string;
6062
};
6163
}): IProjectData => {
6264
const testInjector = createTestInjector();
6365
const fs = testInjector.resolve("fs");
6466
testInjector.register(
6567
"projectConfigService",
66-
stubs.ProjectConfigServiceStub.initWithConfig(opts?.configData)
68+
stubs.ProjectConfigServiceStub.initWithConfig(opts?.configData),
6769
);
6870

6971
fs.exists = (filePath: string) => {
@@ -98,7 +100,7 @@ describe("projectData", () => {
98100
const assertProjectType = (
99101
dependencies: any,
100102
devDependencies: any,
101-
expectedProjecType: string
103+
expectedProjecType: string,
102104
) => {
103105
const projectData = prepareTest({
104106
packageJsonData: {
@@ -125,7 +127,7 @@ describe("projectData", () => {
125127
assertProjectType(
126128
{ "nativescript-vue": "*" },
127129
{ typescript: "*" },
128-
"Vue.js"
130+
"Vue.js",
129131
);
130132
});
131133

@@ -141,7 +143,7 @@ describe("projectData", () => {
141143
assertProjectType(
142144
null,
143145
{ "nativescript-dev-typescript": "*" },
144-
"Pure TypeScript"
146+
"Pure TypeScript",
145147
);
146148
});
147149

@@ -195,13 +197,13 @@ describe("projectData", () => {
195197
const projectData = prepareTest();
196198
assert.equal(
197199
projectData.webpackConfigPath,
198-
path.join(projectDir, "webpack.config.js")
200+
path.join(projectDir, "webpack.config.js"),
199201
);
200202
});
201203

202204
it("returns correct path when full path is set in nsconfig.json", () => {
203205
const pathToConfig = path.resolve(
204-
path.join("/testDir", "innerDir", "mywebpack.config.js")
206+
path.join("/testDir", "innerDir", "mywebpack.config.js"),
205207
);
206208
const projectData = prepareTest({
207209
configData: { webpackConfigPath: pathToConfig },
@@ -211,7 +213,7 @@ describe("projectData", () => {
211213

212214
it("returns correct path when relative path is set in nsconfig.json", () => {
213215
const pathToConfig = path.resolve(
214-
path.join("projectDir", "innerDir", "mywebpack.config.js")
216+
path.join("projectDir", "innerDir", "mywebpack.config.js"),
215217
);
216218
const projectData = prepareTest({
217219
configData: {
@@ -221,4 +223,81 @@ describe("projectData", () => {
221223
assert.equal(projectData.webpackConfigPath, pathToConfig);
222224
});
223225
});
226+
227+
describe("bundlerConfigPath", () => {
228+
it("default path to webpack.config.js is set when nsconfig.json does not set value", () => {
229+
const projectData = prepareTest();
230+
assert.equal(
231+
projectData.bundlerConfigPath,
232+
path.join(projectDir, "webpack.config.js"),
233+
);
234+
});
235+
236+
it("should use webpackConfigPath property when bundlerConfigPath is not defined", () => {
237+
const pathToConfig = path.resolve(
238+
path.join("/testDir", "innerDir", "mywebpack.config.js"),
239+
);
240+
const projectData = prepareTest({
241+
configData: { webpackConfigPath: pathToConfig },
242+
});
243+
assert.equal(projectData.bundlerConfigPath, pathToConfig);
244+
});
245+
246+
it("returns correct path when full path is set in nsconfig.json", () => {
247+
const pathToConfig = path.resolve(
248+
path.join("/testDir", "innerDir", "mywebpack.config.js"),
249+
);
250+
const projectData = prepareTest({
251+
configData: { bundlerConfigPath: pathToConfig },
252+
});
253+
assert.equal(projectData.bundlerConfigPath, pathToConfig);
254+
});
255+
256+
it("returns correct path when relative path is set in nsconfig.json", () => {
257+
const pathToConfig = path.resolve(
258+
path.join("projectDir", "innerDir", "mywebpack.config.js"),
259+
);
260+
const projectData = prepareTest({
261+
configData: {
262+
bundlerConfigPath: path.join("./innerDir", "mywebpack.config.js"),
263+
},
264+
});
265+
assert.equal(projectData.bundlerConfigPath, pathToConfig);
266+
});
267+
268+
it("should use bundlerConfigPath instead of webpackConfigPath if both are defined.", () => {
269+
const pathToConfig = path.resolve(
270+
path.join("projectDir", "innerDir", "myrspack.config.js"),
271+
);
272+
const projectData = prepareTest({
273+
configData: {
274+
webpackConfigPath: path.join("./innerDir", "mywebpack.config.js"),
275+
bundlerConfigPath: path.join("./innerDir", "myrspack.config.js"),
276+
},
277+
});
278+
assert.equal(projectData.bundlerConfigPath, pathToConfig);
279+
});
280+
});
281+
282+
describe("bundler", () => {
283+
it("sets bundler to 'webpack' by default when nsconfig.json does not specify a bundler", () => {
284+
const projectData = prepareTest();
285+
assert.equal(projectData.bundler, "webpack");
286+
});
287+
288+
it("sets bundler to 'webpack' when explicitly defined in nsconfig.json", () => {
289+
const projectData = prepareTest({ configData: { bundler: "webpack" } });
290+
assert.equal(projectData.bundler, "webpack");
291+
});
292+
293+
it("sets bundler to 'rspack' when explicitly defined in nsconfig.json", () => {
294+
const projectData = prepareTest({ configData: { bundler: "rspack" } });
295+
assert.equal(projectData.bundler, "rspack");
296+
});
297+
298+
it("sets bundler to 'vite' when explicitly defined in nsconfig.json", () => {
299+
const projectData = prepareTest({ configData: { bundler: "vite" } });
300+
assert.equal(projectData.bundler, "vite");
301+
});
302+
});
224303
});

0 commit comments

Comments
 (0)