From 08d316a1de13ad46335ada8a9b7734260c827d04 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Fri, 14 Feb 2020 22:37:12 +0000 Subject: [PATCH 1/3] cleanup webpack config, remove useless bits, less plugins --- build | 15 +++++---- utils/build.js | 9 ------ utils/env.js | 6 ---- utils/webserver.js | 34 -------------------- webpack.config.js | 78 ++++++++++++++++++++++------------------------ 5 files changed, 46 insertions(+), 96 deletions(-) delete mode 100644 utils/build.js delete mode 100644 utils/env.js delete mode 100644 utils/webserver.js diff --git a/build b/build index 4f4e0de..91fa6bc 100755 --- a/build +++ b/build @@ -5,12 +5,11 @@ from subprocess import check_call from pathlib import Path -TARGETS = ['chrome', 'firefox'] # TODO FIXME copypaste from promnesia # TODO FIXME use it in ci script +TARGETS = ['chrome', 'firefox'] def main(): p = argparse.ArgumentParser() - # TODO rename to --production?? p.add_argument('--release', action='store_true') p.add_argument('--sign', action='store_true') p.add_argument('--lint', action='store_true') @@ -22,6 +21,7 @@ def main(): args = p.parse_args() target = args.target + assert target is not None dist_dir = Path(__file__).absolute().parent / 'dist' def webext(*args, **kwargs): @@ -33,10 +33,9 @@ def main(): *args, ], **kwargs) - maybe_release = {'NODE_ENV': 'production'} if args.release else {} env = { 'TARGET': target, - **maybe_release, + 'RELEASE': 'YES' if args.release else 'NO', **os.environ, } check_call([ @@ -56,11 +55,12 @@ def main(): 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 webext( - # TODO use UUID? https://addons.mozilla.org/en-GB/developers/addon/grasp/edit - # TODO right, wouldn't work because it's listed.. + '--source-dir' , str(src_dir), '--artifacts-dir', str(dist_dir), '--api-key' , API_KEY, '--api-secret', API_SECRET, @@ -69,3 +69,6 @@ def main(): if __name__ == '__main__': main() + +# TODO use UUID? https://addons.mozilla.org/en-GB/developers/addon/grasp/edit +# TODO right, wouldn't work because it's listed.. diff --git a/utils/build.js b/utils/build.js deleted file mode 100644 index 5c4609d..0000000 --- a/utils/build.js +++ /dev/null @@ -1,9 +0,0 @@ -var webpack = require("webpack"), - config = require("../webpack.config"); - -delete config.chromeExtensionBoilerplate; - -webpack( - config, - function (err) { if (err) throw err; } -); diff --git a/utils/env.js b/utils/env.js deleted file mode 100644 index d0aad45..0000000 --- a/utils/env.js +++ /dev/null @@ -1,6 +0,0 @@ -// tiny wrapper with default env vars -module.exports = { - NODE_ENV: (process.env.NODE_ENV || "development"), - PORT: (process.env.PORT || 3000), - TARGET: (process.env.TARGET || 'chrome'), -}; diff --git a/utils/webserver.js b/utils/webserver.js deleted file mode 100644 index 2e1a91f..0000000 --- a/utils/webserver.js +++ /dev/null @@ -1,34 +0,0 @@ -var WebpackDevServer = require("webpack-dev-server"), - webpack = require("webpack"), - config = require("../webpack.config"), - env = require("./env"), - path = require("path"); - -var options = (config.chromeExtensionBoilerplate || {}); -var excludeEntriesToHotReload = (options.notHotReload || []); - -for (var entryName in config.entry) { - if (excludeEntriesToHotReload.indexOf(entryName) === -1) { - config.entry[entryName] = - [ - ("webpack-dev-server/client?http://localhost:" + env.PORT), - "webpack/hot/dev-server" - ].concat(config.entry[entryName]); - } -} - -config.plugins = - [new webpack.HotModuleReplacementPlugin()].concat(config.plugins || []); - -delete config.chromeExtensionBoilerplate; - -var compiler = webpack(config); - -var server = - new WebpackDevServer(compiler, { - hot: true, - contentBase: path.join(__dirname, "../build"), - headers: { "Access-Control-Allow-Origin": "*" } - }); - -server.listen(env.PORT); diff --git a/webpack.config.js b/webpack.config.js index 2df85ed..0826fbd 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,33 +1,33 @@ -var webpack = require("webpack"), - path = require("path"), - fileSystem = require("fs"), - env = require("./utils/env"), - CleanWebpackPlugin = require("clean-webpack-plugin"), - CopyWebpackPlugin = require("copy-webpack-plugin"), - HtmlWebpackPlugin = require("html-webpack-plugin"), - WriteFilePlugin = require("write-file-webpack-plugin"), - WebpackExtensionManifestPlugin = require("webpack-extension-manifest-plugin"); +const webpack = require('webpack'), + path = require('path'), + CleanWebpackPlugin = require('clean-webpack-plugin'), + CopyWebpackPlugin = require('copy-webpack-plugin'), + WebpackExtensionManifestPlugin = require('webpack-extension-manifest-plugin'), + HtmlWebpackPlugin = require('html-webpack-plugin'); +// TODO remove plugins from package.json + +const T = { + CHROME : 'chrome', + FIREFOX: 'firefox', +}; + +const env = { + TARGET : process.env.TARGET, // TODO assert not null? + RELEASE: process.env.RELEASE, +}; const pkg = require('./package.json'); const baseManifest = require('./src/manifest.json'); +const release = env.RELEASE == 'YES' ? true : false; +const dev = !release; // meh. maybe make up my mind? const target = env.TARGET; -const dev = env.NODE_ENV === "development"; -const pkg_name = "grasp" + (dev ? ' [dev]' : ''); - -// TODO make permissions literate -const permissions = [ - "storage", - "notifications", - "activeTab", - "http://localhost/capture", - "https://localhost/capture" -]; +const name = "grasp" + (dev ? ' [dev]' : ''); // Firefox wouldn't let you rebind its default shortcuts most of which use Shift // On the other hand, Chrome wouldn't let you use Alt -const modifier = target === 'chrome' ? 'Shift' : 'Alt'; +const modifier = target === T.CHROME ? 'Shift' : 'Alt'; // ugh. declarative formats are shit. const commandsExtra = { @@ -45,8 +45,18 @@ const commandsExtra = { }, }; +// TODO make permissions literate +const permissions = [ + "storage", + "notifications", + "activeTab", + "http://localhost/capture", + "https://localhost/capture" +]; + + const manifestExtra = { - name: pkg_name, + name: name, version: pkg.version, description: pkg.description, permissions: permissions, @@ -61,32 +71,23 @@ if (dev) { manifestExtra.content_security_policy = "script-src 'self' 'unsafe-eval'; object-src 'self'"; } -if (target === 'chrome') { +if (target === T.CHROME) { manifestExtra.options_ui = {chrome_style: true}; } -if (target === 'firefox') { +if (target === T.FIREFOX) { manifestExtra.options_ui = {browser_style: true}; manifestExtra.browser_action = {browser_style: true}; } -// load the secrets -var alias = {}; - -var secretsPath = path.join(__dirname, ("secrets." + env.NODE_ENV + ".js")); - var fileExtensions = ["jpg", "jpeg", "png", "gif", "eot", "otf", "svg", "ttf", "woff", "woff2"]; -if (fileSystem.existsSync(secretsPath)) { - alias["secrets"] = secretsPath; -} - const build_path = path.join(__dirname, "dist"); // TODO target?? var options = { - mode: 'production', + mode: 'production', // TODO eh?? optimization: { // https://webpack.js.org/configuration/optimization - // don't thing minimize worth it for suck a tiny extension + // don't think minimize worth it for such a tiny extension minimize: false }, entry: { @@ -125,11 +126,7 @@ var options = { } ] }, - resolve: { - alias: alias - }, plugins: [ - // clean the build folder new CleanWebpackPlugin([build_path + "/*"]), new CopyWebpackPlugin([ { from: 'src/img/*.png', flatten: true }, @@ -159,12 +156,11 @@ var options = { filename: "background.html", chunks: ["background"] }), - new WriteFilePlugin() ] }; // TODO https://webpack.js.org/configuration/devtool -if (env.NODE_ENV === "development") { +if (dev) { options.devtool = "cheap-module-eval-source-map"; } From 8a9c64a25ca782d0f1d3d0f378897769d4de9c1f Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Fri, 14 Feb 2020 23:07:50 +0000 Subject: [PATCH 2/3] get rid of HtmlWebpackPlugin --- server/test_with_browser.py | 1 + src/background.html | 9 -------- src/js/options_page.js | 2 +- src/manifest.json | 14 +++++++----- src/options.html | 2 +- src/popup.html | 1 + webpack.config.js | 45 ++++++++----------------------------- 7 files changed, 21 insertions(+), 53 deletions(-) delete mode 100644 src/background.html diff --git a/server/test_with_browser.py b/server/test_with_browser.py index 65ea2b2..c060bca 100755 --- a/server/test_with_browser.py +++ b/server/test_with_browser.py @@ -31,6 +31,7 @@ def get_webdriver(): driver.install_addon(str(addon_path), temporary=True) yield driver finally: + # confirm('ready to continue?') driver.close() diff --git a/src/background.html b/src/background.html deleted file mode 100644 index 384a69f..0000000 --- a/src/background.html +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - diff --git a/src/js/options_page.js b/src/js/options_page.js index 85ee071..8938ac6 100644 --- a/src/js/options_page.js +++ b/src/js/options_page.js @@ -47,6 +47,7 @@ function refreshPermissionValidation(endpoint: string) { } function restoreOptions() { + getSaveButton().addEventListener('click', saveOptions); get_options(opts => { const ep = opts.endpoint; getEndpoint().value = ep; @@ -74,4 +75,3 @@ function saveOptions() { document.addEventListener('DOMContentLoaded', restoreOptions); -getSaveButton().addEventListener('click', saveOptions); diff --git a/src/manifest.json b/src/manifest.json index e01685d..331c235 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -1,14 +1,13 @@ { - "icons": { - "128": "unicorn.png" + "name": "", + "version": "", + "background": { + "scripts": ["background.js"], + "persistent": false }, "options_ui": { "page": "options.html" }, - - "background": { - "page": "background.html" - }, "browser_action": { "default_icon": "unicorn.png", "default_popup": "popup.html", @@ -22,5 +21,8 @@ "description": "Capture page, with extra information" } }, + "icons": { + "128": "unicorn.png" + }, "manifest_version": 2 } diff --git a/src/options.html b/src/options.html index 8b12c4b..d669a1a 100644 --- a/src/options.html +++ b/src/options.html @@ -2,7 +2,7 @@ - +