From 73fb8aa3dddbaa039510c5d96acd443652af066a Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Mon, 9 Dec 2019 20:43:38 +0000 Subject: [PATCH] Add nicer build script --- .gitignore | 2 +- README.md | 17 +++++++------- build | 58 +++++++++++++++++++++++++++++++++++++++++++++++ webpack.config.js | 17 +++++++++----- 4 files changed, 79 insertions(+), 15 deletions(-) create mode 100755 build diff --git a/.gitignore b/.gitignore index 6c2c4a7..b34538a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ node_modules -build +dist secrets.*.js diff --git a/README.md b/README.md index f6def9c..6e5d92d 100644 --- a/README.md +++ b/README.md @@ -65,22 +65,23 @@ The most up-to-date instructions should be in [CI config](./.circleci/config.yml You're gonna need `npm` for building extension. npm install - npm run build + ./build --target # e.g. ./build --target chrome or ./build --target firefox -After that you can find the extension in `build` directory and 'Load unpacked** if necessary. There is also Flow and Eslint set up. +After that you can find the extension in `dist` directory and 'Load unpacked** if necessary. There is also Flow and Eslint set up. ## building any host version -If you do need unresticted url permissions, build the extensions like that: `ANY_HOST=yes npm run build`. +If you do need unresticted url permissions, build the extensions like that: `ANY_HOST=yes ./build`. -## building for Firefox -Default target is Chrome. use `TARGET=firefox npm run build` to build for firefox. The code is actually same, the only differences are minor appearance adjustments in manifest. - - -# Development +## Development Check [CI config](./.circleci/config.yml) to figure out all the checks I'm doing. The only test(s) that don't run on CI at the moment (e.g. due to lack of X server) are marked with `@skip_if_ci`. You can run them manually though. +Extra tests (not integrated in CI yet): + +- `(cd dist && web-ext lint)` +- `scripts/test_with_browser.py` + # Credits * Icon made by Freepik from www.flaticon.com, licensed by CC 3.0 BY diff --git a/build b/build new file mode 100755 index 0000000..d6a9b9a --- /dev/null +++ b/build @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 +import argparse +import os +from subprocess import check_call +from pathlib import Path + + +TARGETS = ['chrome', 'firefox'] +# TODO FIXME copypaste from promnesia +# TODO FIXME use it in ci script +def main(): + p = argparse.ArgumentParser() + # TODO rename to --production?? + p.add_argument('--release', action='store_true') + p.add_argument('--sign', action='store_true') + # TODO --lint? + + tg = p.add_mutually_exclusive_group() + tg.add_argument('--target', type=str, choices=TARGETS) + for b in TARGETS: + tg.add_argument('--' + b, action='store_const', const=b, dest='target') + args = p.parse_args() + target = args.target + + assert target is not None + + maybe_release = {'NODE_ENV': 'production'} if args.release else {} + + env = { + 'TARGET': target, + **maybe_release, + **os.environ, + } + check_call([ + 'npm', 'run', 'build', + ], env=env, cwd=str(Path(__file__).absolute().parent)) + + if args.sign: + assert args.release + if target not in ['firefox', 'firefox-mobile']: + raise RuntimeError("{target} is not supported for signing yet".format(target=target)) + + + dist_dir = Path(__file__).absolute().parent / 'dist' + src_dir = dist_dir / target + + from firefox_dev_secrets import API_KEY, API_SECRET + check_call([ + 'web-ext', + '--source-dir' , str(src_dir), + '--artifacts-dir', str(dist_dir), + '--api-key' , API_KEY, + '--api-secret' , API_SECRET, + 'sign', + ]) + +if __name__ == '__main__': + main() diff --git a/webpack.config.js b/webpack.config.js index b7f4a45..8609495 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -14,6 +14,7 @@ const baseManifest = require('./src/manifest.json'); const target = env.TARGET; const pkg_name = env.ANY_HOST ? "grasp (any host)" : "grasp"; +// TODO make permissions literate const permissions = [ "storage", "notifications", @@ -74,16 +75,19 @@ if (fileSystem.existsSync(secretsPath)) { alias["secrets"] = secretsPath; } +const build_path = path.join(__dirname, "dist"); // TODO target?? + var options = { - mode: 'development', + mode: 'production', entry: { - popup: path.join(__dirname, "src", "js", "popup.js"), - options: path.join(__dirname, "src", "js", "options_page.js"), + popup : path.join(__dirname, "src", "js", "popup.js"), + options : path.join(__dirname, "src", "js", "options_page.js"), background: path.join(__dirname, "src", "js", "background.js") }, output: { - path: path.join(__dirname, "build"), - filename: "[name].bundle.js" + path: build_path, + // TODO why bundle??? + filename: "[name].bundle.js", }, module: { rules: [ @@ -116,7 +120,7 @@ var options = { }, plugins: [ // clean the build folder - new CleanWebpackPlugin(["build/*"]), + new CleanWebpackPlugin([build_path + "/*"]), new CopyWebpackPlugin([ { from: 'src/img/*.png', flatten: true }, ]), @@ -149,6 +153,7 @@ var options = { ] }; +// TODO https://webpack.js.org/configuration/devtool if (env.NODE_ENV === "development") { options.devtool = "cheap-module-eval-source-map"; }