From e61cb53c7541c79ad113581665c7ee47fc235c66 Mon Sep 17 00:00:00 2001 From: Johnny A <5891646+johnny243@users.noreply.github.com> Date: Thu, 7 May 2020 12:11:44 -0400 Subject: [PATCH 1/7] refactor: webpack configuration (#398) * refactor: webpack configuration * feat: webpack hot reloading Co-authored-by: Johnny Almonte --- package-lock.json | 9 +++++++++ package.json | 12 ++++++------ webpack.config.js | 13 ++----------- webpack.dev.js | 17 +++++++++++++++++ webpack.prod.js | 6 ++++++ 5 files changed, 40 insertions(+), 17 deletions(-) create mode 100644 webpack.dev.js create mode 100644 webpack.prod.js diff --git a/package-lock.json b/package-lock.json index d4ccb0486..9cbd501ab 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11409,6 +11409,15 @@ "uuid": "^3.3.2" } }, + "webpack-merge": { + "version": "4.2.2", + "resolved": "https://registry.npmjs.org/webpack-merge/-/webpack-merge-4.2.2.tgz", + "integrity": "sha512-TUE1UGoTX2Cd42j3krGYqObZbOD+xF7u28WB7tfUordytSjbWTIjK/8V0amkBfTYN4/pB/GIDlJZZ657BGG19g==", + "dev": true, + "requires": { + "lodash": "^4.17.15" + } + }, "webpack-sources": { "version": "1.4.3", "resolved": "https://registry.npmjs.org/webpack-sources/-/webpack-sources-1.4.3.tgz", diff --git a/package.json b/package.json index 279430193..cadb5785c 100644 --- a/package.json +++ b/package.json @@ -7,12 +7,11 @@ "url": "https://github.com/standardnotes/web" }, "scripts": { - "start": "webpack-dev-server --progress", - "watch": "webpack --mode development -w", - "bundle": "webpack --mode production", + "start": "webpack-dev-server --progress --config webpack.dev.js", + "watch": "webpack -w --config webpack.dev.js", + "bundle": "webpack --config webpack.prod.js", "build": "bundle install && npm install && npm run bundle", "submodules": "git submodule update --init --force --remote", - "test": "karma start karma.conf.js --single-run", "lint": "eslint --fix app/assets/javascripts/**/*.js", "tsc": "tsc --project app/assets/javascripts/tsconfig.json" }, @@ -50,6 +49,7 @@ "eslint-plugin-promise": "^4.2.1", "eslint-plugin-standard": "^4.0.1", "file-loader": "^5.1.0", + "lodash": "^4.17.15", "mini-css-extract-plugin": "^0.9.0", "mocha": "^7.1.0", "ng-cache-loader": "0.0.26", @@ -58,14 +58,14 @@ "pug-loader": "^2.4.0", "sass-loader": "^8.0.2", "serve-static": "^1.14.1", - "snjs": "github:standardnotes/snjs#0c306de70c5afffe16096424c6c3326eaa1dd4fd", "sn-stylekit": "2.0.22", + "snjs": "github:standardnotes/snjs#0c306de70c5afffe16096424c6c3326eaa1dd4fd", "ts-loader": "^6.2.2", "typescript": "^3.8.3", "typescript-eslint": "0.0.1-alpha.0", "webpack": "^4.42.0", "webpack-cli": "^3.3.11", "webpack-dev-server": "^3.10.3", - "lodash": "^4.17.15" + "webpack-merge": "^4.2.2" } } diff --git a/webpack.config.js b/webpack.config.js index 25a14bcbd..42ee56f58 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -2,20 +2,12 @@ const path = require('path'); const webpack = require('webpack'); const MiniCssExtractPlugin = require('mini-css-extract-plugin'); const CopyPlugin = require('copy-webpack-plugin'); + module.exports = { entry: './app/assets/javascripts/index.ts', output: { filename: './javascripts/app.js' }, - devServer: { - proxy: { - '/extensions': { - target: 'http://localhost:3001', - pathRewrite: { '^/extensions': '/public/extensions' } - } - }, - port: 3001 - }, plugins: [ new webpack.DefinePlugin({ __VERSION__: JSON.stringify(require('./package.json').version) @@ -27,10 +19,9 @@ module.exports = { new MiniCssExtractPlugin({ // Options similar to the same options in webpackOptions.output filename: './stylesheets/app.css', - ignoreOrder: false // Enable to remove warnings about conflicting order + ignoreOrder: true // Enable to remove warnings about conflicting order }) ], - devtool: 'source-map', resolve: { extensions: ['.ts', '.js'], alias: { diff --git a/webpack.dev.js b/webpack.dev.js new file mode 100644 index 000000000..c7d3091d5 --- /dev/null +++ b/webpack.dev.js @@ -0,0 +1,17 @@ +const merge = require('webpack-merge'); +const config = require('./webpack.config.js'); + +module.exports = merge(config, { + mode: 'development', + devtool: 'source-map', + devServer: { + publicPath: '/dist/', + proxy: { + '/extensions': { + target: 'http://localhost:3001', + pathRewrite: { '^/extensions': '/public/extensions' } + } + }, + port: 3001 + } +}); diff --git a/webpack.prod.js b/webpack.prod.js new file mode 100644 index 000000000..1852a3d46 --- /dev/null +++ b/webpack.prod.js @@ -0,0 +1,6 @@ +const merge = require('webpack-merge'); +const config = require('./webpack.config.js'); + +module.exports = merge(config, { + mode: 'production' +}); From 1916a459a086a31e124f3fa98883e7cd3f64542d Mon Sep 17 00:00:00 2001 From: Baptiste Grob <60621355+baptiste-grob@users.noreply.github.com> Date: Tue, 12 May 2020 13:30:00 +0200 Subject: [PATCH 2/7] perf: improve build performance --- .babelrc | 8 +------- .browserslistrc | 6 ++++++ package-lock.json | 12 ------------ package.json | 2 -- webpack.config.js | 2 +- webpack.dev.js | 1 - webpack.prod.js | 3 ++- 7 files changed, 10 insertions(+), 24 deletions(-) create mode 100644 .browserslistrc diff --git a/.babelrc b/.babelrc index 692a17bc5..a09963d4e 100644 --- a/.babelrc +++ b/.babelrc @@ -1,15 +1,9 @@ { "presets": [ "@babel/typescript", - [ - "@babel/preset-env", - { - "targets": "> 0.25%, not dead" - } - ] + "@babel/preset-env" ], "plugins": [ - "@babel/plugin-transform-runtime", "@babel/plugin-proposal-class-properties", "angularjs-annotate" ] diff --git a/.browserslistrc b/.browserslistrc new file mode 100644 index 000000000..b9145d54f --- /dev/null +++ b/.browserslistrc @@ -0,0 +1,6 @@ +chrome 80 # electron 8.2 +last 2 Firefox versions +last 2 Edge versions +last 2 Safari versions +last 2 Opera versions +Firefox ESR diff --git a/package-lock.json b/package-lock.json index 9cbd501ab..da743c94c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1285,18 +1285,6 @@ "@babel/helper-plugin-utils": "^7.8.3" } }, - "@babel/plugin-transform-runtime": { - "version": "7.8.3", - "resolved": "https://registry.npmjs.org/@babel/plugin-transform-runtime/-/plugin-transform-runtime-7.8.3.tgz", - "integrity": "sha512-/vqUt5Yh+cgPZXXjmaG9NT8aVfThKk7G4OqkVhrXqwsC5soMn/qTCxs36rZ2QFhpfTJcjw4SNDIZ4RUb8OL4jQ==", - "dev": true, - "requires": { - "@babel/helper-module-imports": "^7.8.3", - "@babel/helper-plugin-utils": "^7.8.3", - "resolve": "^1.8.1", - "semver": "^5.5.1" - } - }, "@babel/plugin-transform-shorthand-properties": { "version": "7.8.3", "resolved": "https://registry.npmjs.org/@babel/plugin-transform-shorthand-properties/-/plugin-transform-shorthand-properties-7.8.3.tgz", diff --git a/package.json b/package.json index cadb5785c..d29fcd2d2 100644 --- a/package.json +++ b/package.json @@ -19,10 +19,8 @@ "@babel/cli": "^7.8.4", "@babel/core": "^7.8.7", "@babel/plugin-proposal-class-properties": "^7.8.3", - "@babel/plugin-transform-runtime": "^7.8.3", "@babel/preset-env": "^7.8.7", "@babel/preset-typescript": "^7.8.3", - "@babel/runtime": "^7.8.7", "@types/angular": "^1.7.0", "@types/chai": "^4.2.11", "@types/lodash": "^4.14.149", diff --git a/webpack.config.js b/webpack.config.js index 42ee56f58..966ecb838 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -37,7 +37,7 @@ module.exports = { rules: [ { test: /\.(js|ts)$/, - exclude: /node_modules/, + exclude: /(node_modules|snjs)/, use: { loader: 'babel-loader' } diff --git a/webpack.dev.js b/webpack.dev.js index c7d3091d5..3fe87df80 100644 --- a/webpack.dev.js +++ b/webpack.dev.js @@ -3,7 +3,6 @@ const config = require('./webpack.config.js'); module.exports = merge(config, { mode: 'development', - devtool: 'source-map', devServer: { publicPath: '/dist/', proxy: { diff --git a/webpack.prod.js b/webpack.prod.js index 1852a3d46..4cfcb6189 100644 --- a/webpack.prod.js +++ b/webpack.prod.js @@ -2,5 +2,6 @@ const merge = require('webpack-merge'); const config = require('./webpack.config.js'); module.exports = merge(config, { - mode: 'production' + mode: 'production', + devtool: 'source-map', }); From 49d8ca666d1bb4b07be71b3e93444c6ceec29b65 Mon Sep 17 00:00:00 2001 From: Baptiste Grob <60621355+baptiste-grob@users.noreply.github.com> Date: Wed, 13 May 2020 12:24:28 +0200 Subject: [PATCH 3/7] feat: allow dev server port configuration from CLI --- webpack.dev.js | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/webpack.dev.js b/webpack.dev.js index 3fe87df80..d43339d5f 100644 --- a/webpack.dev.js +++ b/webpack.dev.js @@ -1,16 +1,19 @@ const merge = require('webpack-merge'); const config = require('./webpack.config.js'); -module.exports = merge(config, { - mode: 'development', - devServer: { - publicPath: '/dist/', - proxy: { - '/extensions': { - target: 'http://localhost:3001', - pathRewrite: { '^/extensions': '/public/extensions' } - } - }, - port: 3001 - } -}); +module.exports = (_env, argv) => { + const port = argv.port || 3001; + return merge(config, { + mode: 'development', + devServer: { + publicPath: '/dist/', + proxy: { + '/extensions': { + target: `http://localhost:${port}`, + pathRewrite: { '^/extensions': '/public/extensions' } + } + }, + port, + } + }); +}; From b2d6ea00c8ac5438de29e0bcdc384a0f8526a2bd Mon Sep 17 00:00:00 2001 From: Baptiste Grob <60621355+baptiste-grob@users.noreply.github.com> Date: Wed, 13 May 2020 17:05:04 +0200 Subject: [PATCH 4/7] fix: initialize controller state softly --- .../javascripts/views/abstract/pure_view_ctrl.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/assets/javascripts/views/abstract/pure_view_ctrl.ts b/app/assets/javascripts/views/abstract/pure_view_ctrl.ts index 086a52709..495e2ba3b 100644 --- a/app/assets/javascripts/views/abstract/pure_view_ctrl.ts +++ b/app/assets/javascripts/views/abstract/pure_view_ctrl.ts @@ -17,13 +17,13 @@ export class PureViewCtrl { /* @ngInject */ constructor($timeout: ng.ITimeoutService) { this.$timeout = $timeout; - /* Allow caller constructor to finish setting instance variables */ - setImmediate(() => { - this.state = this.getInitialState(); - }); } $onInit() { + this.state = { + ...this.getInitialState(), + ...this.state, + } this.addAppEventObserver(); this.addAppStateObserver(); } @@ -141,4 +141,4 @@ export class PureViewCtrl { /** Optional override */ } -} \ No newline at end of file +} From d2af65c3378cf506b89f9cdd31dc9135d0d7812c Mon Sep 17 00:00:00 2001 From: Baptiste Grob <60621355+baptiste-grob@users.noreply.github.com> Date: Mon, 1 Jun 2020 18:08:39 +0200 Subject: [PATCH 5/7] feat: add dev-only utilities --- app/assets/javascripts/app.ts | 12 ++++++++++++ app/assets/javascripts/utils.ts | 2 ++ 2 files changed, 14 insertions(+) diff --git a/app/assets/javascripts/app.ts b/app/assets/javascripts/app.ts index a86f6d31a..64fb23e63 100644 --- a/app/assets/javascripts/app.ts +++ b/app/assets/javascripts/app.ts @@ -49,6 +49,7 @@ import { } from './directives/views'; import { trusted } from './filters'; +import { isDev } from './utils'; angular.module('app', ['ngSanitize']); @@ -109,3 +110,14 @@ angular // Services angular.module('app').service('mainApplicationGroup', ApplicationGroup); + +// Debug +if (isDev) { + Object.defineProperties(window, { + application: { + get: () => + (angular.element(document).injector().get('mainApplicationGroup') as any) + .application, + }, + }); +} diff --git a/app/assets/javascripts/utils.ts b/app/assets/javascripts/utils.ts index 73562c79e..e95cd83cb 100644 --- a/app/assets/javascripts/utils.ts +++ b/app/assets/javascripts/utils.ts @@ -1,3 +1,5 @@ +export const isDev = process.env.NODE_ENV === 'development'; + export function getParameterByName(name: string, url: string) { name = name.replace(/[[\]]/g, '\\$&'); var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'); From 36d8b81d2f01c554fdf2a5ea8f37134024c376eb Mon Sep 17 00:00:00 2001 From: Baptiste Grob <60621355+baptiste-grob@users.noreply.github.com> Date: Mon, 1 Jun 2020 18:37:26 +0200 Subject: [PATCH 6/7] fix: clear note change listener on editor vie deinit --- app/assets/javascripts/ui_models/editor.ts | 4 ++++ app/assets/javascripts/views/editor/editor_view.ts | 1 + 2 files changed, 5 insertions(+) diff --git a/app/assets/javascripts/ui_models/editor.ts b/app/assets/javascripts/ui_models/editor.ts index f368618e8..56b68685c 100644 --- a/app/assets/javascripts/ui_models/editor.ts +++ b/app/assets/javascripts/ui_models/editor.ts @@ -82,6 +82,10 @@ export class Editor { } } + public clearNoteChangeListener() { + this._onNoteChange = undefined; + } + /** * Register to be notified when the editor's note's values change * (and thus a new object reference is created) diff --git a/app/assets/javascripts/views/editor/editor_view.ts b/app/assets/javascripts/views/editor/editor_view.ts index 5dc9cdd6d..8d8f21324 100644 --- a/app/assets/javascripts/views/editor/editor_view.ts +++ b/app/assets/javascripts/views/editor/editor_view.ts @@ -144,6 +144,7 @@ class EditorViewCtrl extends PureViewCtrl implements EditorViewScope { } deinit() { + this.editor.clearNoteChangeListener(); this.removeTagsObserver(); this.removeComponentsObserver(); (this.removeTagsObserver as any) = undefined; From 6e485e0e8a195b9365b24b729dca04872aace73d Mon Sep 17 00:00:00 2001 From: Baptiste Grob <60621355+baptiste-grob@users.noreply.github.com> Date: Mon, 1 Jun 2020 18:38:05 +0200 Subject: [PATCH 7/7] fix: keep application reference when on pure view controller destruction --- app/assets/javascripts/views/abstract/pure_view_ctrl.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/app/assets/javascripts/views/abstract/pure_view_ctrl.ts b/app/assets/javascripts/views/abstract/pure_view_ctrl.ts index 495e2ba3b..4b1978dd5 100644 --- a/app/assets/javascripts/views/abstract/pure_view_ctrl.ts +++ b/app/assets/javascripts/views/abstract/pure_view_ctrl.ts @@ -33,7 +33,6 @@ export class PureViewCtrl { this.unsubState(); this.unsubApp = undefined; this.unsubState = undefined; - (this.application as any) = undefined; if (this.stateTimeout) { this.$timeout.cancel(this.stateTimeout); }