diff --git a/.gitignore b/.gitignore index d96680ec27..ffa60dc99d 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,3 @@ node_modules /server_manager/install_scripts/do_install_script.ts /server_manager/install_scripts/gcp_install_script.ts /output -/build diff --git a/client/build/get_build_parameters.mjs b/client/build/get_build_parameters.mjs new file mode 100644 index 0000000000..6f331f40e2 --- /dev/null +++ b/client/build/get_build_parameters.mjs @@ -0,0 +1,59 @@ +// Copyright 2022 The Outline Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import minimist from 'minimist'; + +const VALID_PLATFORMS = ['linux', 'windows', 'ios', 'macos', 'maccatalyst', 'android', 'browser']; +const VALID_BUILD_MODES = ['debug', 'release']; + +const MS_PER_HOUR = 1000 * 60 * 60; + +/* + Inputs: + => cliParameters: the list of action arguments passed in + Outputs: + => an object containing the specificed platform and buildMode. +*/ +export function getBuildParameters(cliArguments) { + const { + _: [platform = 'browser'], + buildMode = 'debug', + verbose = false, + versionName = '0.0.0', + sentryDsn = process.env.SENTRY_DSN, + } = minimist(cliArguments); + + if (platform && !VALID_PLATFORMS.includes(platform)) { + throw new TypeError( + `Platform "${platform}" is not a valid target for Outline Client. Must be one of ${VALID_PLATFORMS.join(', ')}` + ); + } + + if (buildMode && !VALID_BUILD_MODES.includes(buildMode)) { + throw new TypeError( + `Build mode "${buildMode}" is not a valid build mode for Outline Client. Must be one of ${VALID_BUILD_MODES.join( + ', ' + )}` + ); + } + + return { + platform, + buildMode, + verbose, + versionName: buildMode === 'release' ? versionName : `${versionName}-${buildMode}`, + sentryDsn, + buildNumber: Math.floor(Date.now() / MS_PER_HOUR), + }; +} diff --git a/client/build/get_webpack_build_mode.mjs b/client/build/get_webpack_build_mode.mjs new file mode 100644 index 0000000000..7e86cd98d9 --- /dev/null +++ b/client/build/get_webpack_build_mode.mjs @@ -0,0 +1,30 @@ +// Copyright 2022 The Outline Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/* + Inputs: + => buildMode: the outline build mode + Outputs: + => the appropriate webpack mode for this type of build +*/ +export function getWebpackBuildMode(buildMode) { + switch (buildMode) { + case 'debug': + return 'development'; + case 'release': + return 'production'; + default: + throw new TypeError('get_webpack_mode requires a buildMode argument of debug or release'); + } +} diff --git a/client/build/run_webpack.mjs b/client/build/run_webpack.mjs new file mode 100644 index 0000000000..0f45a78a20 --- /dev/null +++ b/client/build/run_webpack.mjs @@ -0,0 +1,36 @@ +// Copyright 2022 The Outline Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import webpack from 'webpack'; + +export const runWebpack = webpackConfig => + new Promise((resolve, reject) => { + webpack(webpackConfig, (error, stats) => { + if (error || stats.hasErrors()) { + reject( + error || + stats + .toJson() + ?.errors.reduce( + (errorMessages, {message}) => (message ? `${errorMessages}\n${message}` : errorMessages), + '' + ) || + 'Unknown Webpack error.' + ); + } + + resolve(stats); + }); + }); + \ No newline at end of file diff --git a/package.json b/package.json index bd586ad28d..f411f34c71 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "action:help": "npm run action list", "action:list": "npm run action list", "action": "node ./src/build/run_action.mjs", - "clean": "rimraf client/build client/output node_modules client/node_modules client/www client/platforms client/plugins third_party/jsign/*.jar && go run github.com/go-task/task/v3/cmd/task clean", + "clean": "rimraf client/output node_modules client/node_modules client/www client/platforms client/plugins third_party/jsign/*.jar && go run github.com/go-task/task/v3/cmd/task clean", "format:all": "prettier --write \"**/*.{cjs,mjs,html,js,json,md,ts}\"", "format": "pretty-quick --staged --pattern \"**/*.{cjs,mjs,html,js,json,md,ts}\"", "lint:ts": "eslint --ext ts,mjs client",