mirror of
https://github.com/bitwarden/browser
synced 2025-12-06 00:13:28 +00:00
build(cli): integrate nx (#16648)
* build(cli): integrate nx * refactor(project.json): rename "bit" builds to "commercial" * refactor(webpack.base): implement DEFAULT_PARAMS * refactor(webpack.base): move DEFAULT_PARAMS out of buildConfig
This commit is contained in:
86
apps/cli/project.json
Normal file
86
apps/cli/project.json
Normal file
@@ -0,0 +1,86 @@
|
||||
{
|
||||
"$schema": "../../node_modules/nx/schemas/project-schema.json",
|
||||
"name": "cli",
|
||||
"projectType": "application",
|
||||
"sourceRoot": "apps/cli/src",
|
||||
"tags": ["scope:cli", "type:app"],
|
||||
"targets": {
|
||||
"build": {
|
||||
"executor": "@nx/webpack:webpack",
|
||||
"outputs": ["{options.outputPath}"],
|
||||
"defaultConfiguration": "oss-dev",
|
||||
"options": {
|
||||
"outputPath": "dist/apps/cli",
|
||||
"webpackConfig": "apps/cli/webpack.config.js",
|
||||
"tsConfig": "apps/cli/tsconfig.json",
|
||||
"main": "apps/cli/src/bw.ts",
|
||||
"target": "node",
|
||||
"compiler": "tsc"
|
||||
},
|
||||
"configurations": {
|
||||
"oss": {
|
||||
"mode": "production",
|
||||
"outputPath": "dist/apps/cli/oss"
|
||||
},
|
||||
"oss-dev": {
|
||||
"mode": "development",
|
||||
"outputPath": "dist/apps/cli/oss-dev"
|
||||
},
|
||||
"commercial": {
|
||||
"mode": "production",
|
||||
"outputPath": "dist/apps/cli/commercial",
|
||||
"webpackConfig": "bitwarden_license/bit-cli/webpack.config.js",
|
||||
"main": "bitwarden_license/bit-cli/src/bw.ts",
|
||||
"tsConfig": "bitwarden_license/bit-cli/tsconfig.json"
|
||||
},
|
||||
"commercial-dev": {
|
||||
"mode": "development",
|
||||
"outputPath": "dist/apps/cli/commercial-dev",
|
||||
"webpackConfig": "bitwarden_license/bit-cli/webpack.config.js",
|
||||
"main": "bitwarden_license/bit-cli/src/bw.ts",
|
||||
"tsConfig": "bitwarden_license/bit-cli/tsconfig.json"
|
||||
}
|
||||
}
|
||||
},
|
||||
"serve": {
|
||||
"executor": "@nx/webpack:webpack",
|
||||
"defaultConfiguration": "oss-dev",
|
||||
"options": {
|
||||
"outputPath": "dist/apps/cli",
|
||||
"webpackConfig": "apps/cli/webpack.config.js",
|
||||
"tsConfig": "apps/cli/tsconfig.json",
|
||||
"main": "apps/cli/src/bw.ts",
|
||||
"target": "node",
|
||||
"compiler": "tsc",
|
||||
"watch": true
|
||||
},
|
||||
"configurations": {
|
||||
"oss-dev": {
|
||||
"mode": "development",
|
||||
"outputPath": "dist/apps/cli/oss-dev"
|
||||
},
|
||||
"commercial-dev": {
|
||||
"mode": "development",
|
||||
"outputPath": "dist/apps/cli/commercial-dev",
|
||||
"webpackConfig": "bitwarden_license/bit-cli/webpack.config.js",
|
||||
"main": "bitwarden_license/bit-cli/src/bw.ts",
|
||||
"tsConfig": "bitwarden_license/bit-cli/tsconfig.json"
|
||||
}
|
||||
}
|
||||
},
|
||||
"test": {
|
||||
"executor": "@nx/jest:jest",
|
||||
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
|
||||
"options": {
|
||||
"jestConfig": "apps/cli/jest.config.js"
|
||||
}
|
||||
},
|
||||
"lint": {
|
||||
"executor": "@nx/eslint:lint",
|
||||
"outputs": ["{options.outputFile}"],
|
||||
"options": {
|
||||
"lintFilePatterns": ["apps/cli/**/*.ts"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,16 +10,32 @@ module.exports.getEnv = function getEnv() {
|
||||
return { ENV };
|
||||
};
|
||||
|
||||
const DEFAULT_PARAMS = {
|
||||
localesPath: "./src/locales",
|
||||
modulesPath: [path.resolve("../../node_modules")],
|
||||
externalsModulesDir: "../../node_modules",
|
||||
outputPath: path.resolve(__dirname, "build"),
|
||||
watch: false,
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {{
|
||||
* configName: string;
|
||||
* entry: string;
|
||||
* tsConfig: string;
|
||||
* outputPath?: string;
|
||||
* mode?: string;
|
||||
* env?: string;
|
||||
* modulesPath?: string[];
|
||||
* localesPath?: string;
|
||||
* externalsModulesDir?: string;
|
||||
* watch?: boolean;
|
||||
* }} params
|
||||
*/
|
||||
module.exports.buildConfig = function buildConfig(params) {
|
||||
const { ENV } = module.exports.getEnv();
|
||||
params = { ...DEFAULT_PARAMS, ...params };
|
||||
const ENV = params.env || module.exports.getEnv().ENV;
|
||||
|
||||
const envConfig = config.load(ENV);
|
||||
config.log(`Building CLI - ${params.configName} version`);
|
||||
@@ -35,7 +51,7 @@ module.exports.buildConfig = function buildConfig(params) {
|
||||
|
||||
const plugins = [
|
||||
new CopyWebpackPlugin({
|
||||
patterns: [{ from: "./src/locales", to: "locales" }],
|
||||
patterns: [{ from: params.localesPath, to: "locales" }],
|
||||
}),
|
||||
new webpack.DefinePlugin({
|
||||
"process.env.BWCLI_ENV": JSON.stringify(ENV),
|
||||
@@ -61,7 +77,7 @@ module.exports.buildConfig = function buildConfig(params) {
|
||||
];
|
||||
|
||||
const webpackConfig = {
|
||||
mode: ENV,
|
||||
mode: params.mode || ENV,
|
||||
target: "node",
|
||||
devtool: ENV === "development" ? "eval-source-map" : "source-map",
|
||||
node: {
|
||||
@@ -77,19 +93,19 @@ module.exports.buildConfig = function buildConfig(params) {
|
||||
resolve: {
|
||||
extensions: [".ts", ".js"],
|
||||
symlinks: false,
|
||||
modules: [path.resolve("../../node_modules")],
|
||||
modules: params.modulesPath,
|
||||
plugins: [new TsconfigPathsPlugin({ configFile: params.tsConfig })],
|
||||
},
|
||||
output: {
|
||||
filename: "[name].js",
|
||||
path: path.resolve(__dirname, "build"),
|
||||
path: path.resolve(params.outputPath),
|
||||
clean: true,
|
||||
},
|
||||
module: { rules: moduleRules },
|
||||
plugins: plugins,
|
||||
externals: [
|
||||
nodeExternals({
|
||||
modulesDir: "../../node_modules",
|
||||
modulesDir: params.externalsModulesDir,
|
||||
allowlist: [/@bitwarden/],
|
||||
}),
|
||||
],
|
||||
@@ -97,6 +113,12 @@ module.exports.buildConfig = function buildConfig(params) {
|
||||
asyncWebAssembly: true,
|
||||
},
|
||||
};
|
||||
|
||||
if (params.watch) {
|
||||
webpackConfig.watch = true;
|
||||
webpackConfig.watchOptions = {
|
||||
ignored: /node_modules/,
|
||||
poll: 1000,
|
||||
};
|
||||
}
|
||||
return webpackConfig;
|
||||
};
|
||||
|
||||
@@ -1,7 +1,48 @@
|
||||
const path = require("path");
|
||||
const { buildConfig } = require("./webpack.base");
|
||||
|
||||
module.exports = buildConfig({
|
||||
configName: "OSS",
|
||||
entry: "./src/bw.ts",
|
||||
tsConfig: "./tsconfig.json",
|
||||
});
|
||||
module.exports = (webpackConfig, context) => {
|
||||
// Detect if called by Nx (context parameter exists)
|
||||
const isNxBuild = context && context.options;
|
||||
|
||||
if (isNxBuild) {
|
||||
// Nx build configuration
|
||||
const mode = context.options.mode || "development";
|
||||
if (process.env.NODE_ENV == null) {
|
||||
process.env.NODE_ENV = mode;
|
||||
}
|
||||
const ENV = (process.env.ENV = process.env.NODE_ENV);
|
||||
|
||||
return buildConfig({
|
||||
configName: "OSS",
|
||||
entry: context.options.main || "apps/cli/src/bw.ts",
|
||||
tsConfig: "tsconfig.base.json",
|
||||
outputPath: path.resolve(context.context.root, context.options.outputPath),
|
||||
mode: mode,
|
||||
env: ENV,
|
||||
modulesPath: [path.resolve("node_modules")],
|
||||
localesPath: "apps/cli/src/locales",
|
||||
externalsModulesDir: "node_modules",
|
||||
watch: context.options.watch || false,
|
||||
});
|
||||
} else {
|
||||
// npm build configuration
|
||||
if (process.env.NODE_ENV == null) {
|
||||
process.env.NODE_ENV = "development";
|
||||
}
|
||||
const ENV = (process.env.ENV = process.env.NODE_ENV);
|
||||
const mode = ENV;
|
||||
|
||||
return buildConfig({
|
||||
configName: "OSS",
|
||||
entry: "./src/bw.ts",
|
||||
tsConfig: "./tsconfig.json",
|
||||
outputPath: path.resolve(__dirname, "build"),
|
||||
mode: mode,
|
||||
env: ENV,
|
||||
modulesPath: [path.resolve("../../node_modules")],
|
||||
localesPath: "./src/locales",
|
||||
externalsModulesDir: "../../node_modules",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user