Skip to content

feat: allow setting custom webpack config file #5216

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

Merged
merged 2 commits into from
Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 9 additions & 1 deletion lib/definitions/project.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ interface INsConfig {
appResourcesPath?: string;
shared?: boolean;
previewAppSchema?: string;
overridePods?: string
overridePods?: string;
webpackConfigPath?: string;
}

interface IProjectData extends ICreateProjectData {
Expand Down Expand Up @@ -106,6 +107,13 @@ interface IProjectData extends ICreateProjectData {
*/
previewAppSchema: string;

/**
* Defines the path to the configuration file passed to webpack process.
* By default this is the webpack.config.js at the root of the application.
* The value can be changed by setting `webpackConfigPath` in nsconfig.json.
*/
webpackConfigPath: string;

/**
* Initializes project data with the given project directory. If none supplied defaults to --path option or cwd.
* @param {string} projectDir Project root directory.
Expand Down
2 changes: 2 additions & 0 deletions lib/project-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export class ProjectData implements IProjectData {
public podfilePath: string;
public isShared: boolean;
public previewAppSchema: string;
public webpackConfigPath: string;

constructor(private $fs: IFileSystem,
private $errors: IErrors,
Expand Down Expand Up @@ -145,6 +146,7 @@ export class ProjectData implements IProjectData {
this.podfilePath = path.join(this.appResourcesDirectoryPath, this.$devicePlatformsConstants.iOS, constants.PODFILE_NAME);
this.isShared = !!(this.nsConfig && this.nsConfig.shared);
this.previewAppSchema = this.nsConfig && this.nsConfig.previewAppSchema;
this.webpackConfigPath = (this.nsConfig && this.nsConfig.webpackConfigPath) ? path.resolve(this.projectDir, this.nsConfig.webpackConfigPath) : path.join(this.projectDir, "webpack.config.js");
return;
}

Expand Down
8 changes: 6 additions & 2 deletions lib/services/webpack/webpack-compiler-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class WebpackCompilerService extends EventEmitter implements IWebpackComp
constructor(
private $errors: IErrors,
private $childProcess: IChildProcess,
public $fs: IFileSystem,
public $hooksService: IHooksService,
public $hostInfo: IHostInfo,
private $logger: ILogger,
Expand Down Expand Up @@ -153,15 +154,18 @@ export class WebpackCompilerService extends EventEmitter implements IWebpackComp

@performanceLog()
private async startWebpackProcess(platformData: IPlatformData, projectData: IProjectData, prepareData: IPrepareData): Promise<child_process.ChildProcess> {
if (!this.$fs.exists(projectData.webpackConfigPath)) {
this.$errors.fail(`The webpack configuration file ${projectData.webpackConfigPath} does not exist. Ensure you have such file or set correct path in nsconfig.json`);
}

const envData = this.buildEnvData(platformData.platformNameLowerCase, projectData, prepareData);
const envParams = await this.buildEnvCommandLineParams(envData, platformData, projectData, prepareData);
const additionalNodeArgs = semver.major(process.version) <= 8 ? ["--harmony"] : [];

const args = [
...additionalNodeArgs,
"--preserve-symlinks",
path.join(projectData.projectDir, "node_modules", "webpack", "bin", "webpack.js"),
`--config=${path.join(projectData.projectDir, "webpack.config.js")}`,
`--config=${projectData.webpackConfigPath}`,
...envParams
];

Expand Down
2 changes: 1 addition & 1 deletion npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "nativescript",
"preferGlobal": true,
"version": "6.3.3",
"version": "6.4.0",
"author": "Telerik <[email protected]>",
"description": "Command-line interface for building NativeScript projects",
"bin": {
Expand Down
24 changes: 22 additions & 2 deletions test/project-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ describe("projectData", () => {
return testInjector;
};

const prepareTest = (opts?: { packageJsonData?: { dependencies?: IStringDictionary, devDependencies: IStringDictionary }, nsconfigData?: { shared: boolean } }): IProjectData => {
const projectDir = "projectDir";
const prepareTest = (opts?: { packageJsonData?: { dependencies?: IStringDictionary, devDependencies: IStringDictionary }, nsconfigData?: { shared?: boolean, webpackConfigPath?: string } }): IProjectData => {
const testInjector = createTestInjector();
const fs = testInjector.resolve("fs");
fs.exists = (filePath: string) => filePath && (path.basename(filePath) === "package.json" || (path.basename(filePath) === "nsconfig.json" && opts && opts.nsconfigData));
Expand All @@ -64,7 +65,7 @@ describe("projectData", () => {
};

const projectHelper: IProjectHelper = testInjector.resolve("projectHelper");
projectHelper.projectDir = "projectDir";
projectHelper.projectDir = projectDir;

const projectData: IProjectData = testInjector.resolve("projectData");
projectData.initializeProjectData();
Expand Down Expand Up @@ -142,4 +143,23 @@ describe("projectData", () => {
assert.isTrue(projectData.isShared);
});
});

describe("webpackConfigPath", () => {
it("default path to webpack.config.js is set when nsconfig.json does not set value", () => {
const projectData = prepareTest();
assert.equal(projectData.webpackConfigPath, path.join(projectDir, "webpack.config.js"));
});

it("returns correct path when full path is set in nsconfig.json", () => {
const pathToConfig = path.resolve(path.join("/testDir", "innerDir", "mywebpack.config.js"));
const projectData = prepareTest({ nsconfigData: { webpackConfigPath: pathToConfig } });
assert.equal(projectData.webpackConfigPath, pathToConfig);
});

it("returns correct path when relative path is set in nsconfig.json", () => {
const pathToConfig = path.resolve(path.join("projectDir", "innerDir", "mywebpack.config.js"));
const projectData = prepareTest({ nsconfigData: { webpackConfigPath: path.join("./innerDir", "mywebpack.config.js") } });
assert.equal(projectData.webpackConfigPath, pathToConfig);
});
});
});
24 changes: 23 additions & 1 deletion test/services/webpack/webpack-compiler-service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Yok } from "../../../lib/common/yok";
import { WebpackCompilerService } from "../../../lib/services/webpack/webpack-compiler-service";
import { assert } from "chai";
import { ErrorsStub } from "../../stubs";

const iOSPlatformName = "ios";
const androidPlatformName = "android";
Expand All @@ -17,10 +18,13 @@ function createTestInjector(): IInjector {
testInjector.register("hooksService", {});
testInjector.register("hostInfo", {});
testInjector.register("logger", {});
testInjector.register("errors", {});
testInjector.register("errors", ErrorsStub);
testInjector.register("packageInstallationManager", {});
testInjector.register("mobileHelper", {});
testInjector.register("cleanupService", {});
testInjector.register("fs", {
exists: (filePath: string) => true
});

return testInjector;
}
Expand Down Expand Up @@ -87,4 +91,22 @@ describe("WebpackCompilerService", () => {
assert.deepEqual(androidResult.emittedFiles, ["bundle.hash6.hot-update.js", "hash6.hot-update.json"]);
});
});

describe("compileWithWatch", () => {
it("fails when the value set for webpackConfigPath is not existant file", async () => {
const webpackConfigPath = "some path.js";
testInjector.resolve("fs").exists = (filePath: string) => filePath !== webpackConfigPath;
await assert.isRejected(webpackCompilerService.compileWithWatch(<any>{ platformNameLowerCase: "android" }, <any>{ webpackConfigPath }, <any>{}),
`The webpack configuration file ${webpackConfigPath} does not exist. Ensure you have such file or set correct path in nsconfig.json`);
});
});

describe("compileWithoutWatch", () => {
it("fails when the value set for webpackConfigPath is not existant file", async () => {
const webpackConfigPath = "some path.js";
testInjector.resolve("fs").exists = (filePath: string) => filePath !== webpackConfigPath;
await assert.isRejected(webpackCompilerService.compileWithoutWatch(<any>{ platformNameLowerCase: "android" }, <any>{ webpackConfigPath }, <any>{}),
`The webpack configuration file ${webpackConfigPath} does not exist. Ensure you have such file or set correct path in nsconfig.json`);
});
});
});
1 change: 1 addition & 0 deletions test/stubs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ export class NodePackageManagerStub implements INodePackageManager {
export class ProjectDataStub implements IProjectData {
projectDir: string;
projectName: string;
webpackConfigPath: string;
get platformsDir(): string {
return this.platformsDirCache || (this.projectDir && join(this.projectDir, "platforms")) || "";
}
Expand Down