From 89ea7a478f1a7f4ced2b91bd3ba6830e4f5e82e0 Mon Sep 17 00:00:00 2001 From: agrozyme Date: Thu, 3 Dec 2020 13:48:10 +0800 Subject: [PATCH 1/2] add support @nuxt/typescript-runtime@2.0.0 --- .gitignore | 4 + examples/_template/ts/main/background.ts | 9 +-- .../ts/main/helpers/create-window.ts | 74 +++++++++---------- .../ts/main/helpers/exit-on-change.ts | 2 +- examples/_template/ts/main/helpers/index.ts | 5 +- examples/with-typescript/package.json | 13 ++-- .../with-typescript/renderer/assets/README.md | 7 ++ .../renderer/components/Logo.vue | 26 +++++++ .../renderer/components/README.md | 7 ++ .../renderer/layouts/README.md | 7 ++ .../renderer/layouts/default.vue | 59 +++++++++++++++ .../renderer/middleware/README.md | 8 ++ .../with-typescript/renderer/nuxt.config.js | 10 --- .../with-typescript/renderer/nuxt.config.ts | 15 ++++ .../with-typescript/renderer/pages/README.md | 6 ++ .../with-typescript/renderer/pages/about.vue | 44 +++++------ .../with-typescript/renderer/pages/home.vue | 42 ++++++----- .../renderer/plugins/README.md | 7 ++ .../with-typescript/renderer/static/README.md | 11 +++ .../with-typescript/renderer/store/README.md | 10 +++ .../with-typescript/renderer/tsconfig.json | 36 +++++++++ lib/nuxtron-build.ts | 30 +++++--- lib/nuxtron-dev.ts | 40 +++++----- lib/nuxtron-init.ts | 12 +-- lib/nuxtron-list.ts | 8 +- lib/nuxtron.ts | 32 +++++--- lib/webpack/build.production.ts | 8 +- lib/webpack/helpers.ts | 20 ++--- lib/webpack/webpack.config.ts | 35 +++++---- package.json | 28 +++---- 30 files changed, 422 insertions(+), 193 deletions(-) create mode 100644 examples/with-typescript/renderer/assets/README.md create mode 100644 examples/with-typescript/renderer/components/Logo.vue create mode 100644 examples/with-typescript/renderer/components/README.md create mode 100644 examples/with-typescript/renderer/layouts/README.md create mode 100644 examples/with-typescript/renderer/layouts/default.vue create mode 100644 examples/with-typescript/renderer/middleware/README.md delete mode 100644 examples/with-typescript/renderer/nuxt.config.js create mode 100644 examples/with-typescript/renderer/nuxt.config.ts create mode 100644 examples/with-typescript/renderer/pages/README.md create mode 100644 examples/with-typescript/renderer/plugins/README.md create mode 100644 examples/with-typescript/renderer/static/README.md create mode 100644 examples/with-typescript/renderer/store/README.md create mode 100644 examples/with-typescript/renderer/tsconfig.json diff --git a/.gitignore b/.gitignore index a7bac68..6f71286 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,7 @@ pnpm-lock.yaml # nuxtron dev output bin workspace + +# ide +.vscode/ +.idea/ diff --git a/examples/_template/ts/main/background.ts b/examples/_template/ts/main/background.ts index 29ac34a..01965a9 100644 --- a/examples/_template/ts/main/background.ts +++ b/examples/_template/ts/main/background.ts @@ -1,11 +1,8 @@ import { app } from 'electron'; import serve from 'electron-serve'; -import { - createWindow, - exitOnChange, -} from './helpers'; +import { createWindow, exitOnChange } from './helpers'; -const isProd: boolean = process.env.NODE_ENV === 'production'; +const isProd: boolean = 'production' === process.env.NODE_ENV; if (isProd) { serve({ directory: 'app' }); @@ -19,7 +16,7 @@ if (isProd) { const mainWindow = createWindow('main', { width: 1000, - height: 600, + height: 600 }); if (isProd) { diff --git a/examples/_template/ts/main/helpers/create-window.ts b/examples/_template/ts/main/helpers/create-window.ts index 2713e99..1b846da 100644 --- a/examples/_template/ts/main/helpers/create-window.ts +++ b/examples/_template/ts/main/helpers/create-window.ts @@ -1,79 +1,79 @@ -import { - screen, - BrowserWindow, - BrowserWindowConstructorOptions, - Rectangle, -} from 'electron'; +import { BrowserWindow, BrowserWindowConstructorOptions, Rectangle, screen } from 'electron'; import Store from 'electron-store'; export default (windowName: string, options: BrowserWindowConstructorOptions): BrowserWindow => { const key = 'window-state'; const name = `window-state-${windowName}`; const store = new Store({ name }); - const defaultSize = { - width: options.width, - height: options.height, + const bounds = screen.getPrimaryDisplay().bounds; + const defaultSize: Rectangle = { + width: options.width ?? bounds.width, + height: options.height ?? bounds.height, + x: options.x ?? bounds.x, + y: options.y ?? bounds.y }; - let state = {}; + let win: BrowserWindow; - const restore = () => store.get(key, defaultSize); + const restore = () => store.get(key, defaultSize); - const getCurrentPosition = () => { + const getCurrentPosition = (): Rectangle => { const position = win.getPosition(); const size = win.getSize(); return { x: position[0], y: position[1], width: size[0], - height: size[1], + height: size[1] }; }; const windowWithinBounds = (windowState: Rectangle, bounds: Rectangle) => { - return ( - windowState.x >= bounds.x && - windowState.y >= bounds.y && - windowState.x + windowState.width <= bounds.x + bounds.width && + let test = true; + + const items = [ + windowState.x >= bounds.x, + windowState.y >= bounds.y, + windowState.x + windowState.width <= bounds.x + bounds.width, windowState.y + windowState.height <= bounds.y + bounds.height - ); + ]; + + items.forEach(item => { + test = test && item; + }); + + return test; }; - const resetToDefaults = () => { + const resetToDefaults = (): Rectangle => { const bounds = screen.getPrimaryDisplay().bounds; return Object.assign({}, defaultSize, { x: (bounds.width - defaultSize.width) / 2, - y: (bounds.height - defaultSize.height) / 2, + y: (bounds.height - defaultSize.height) / 2 }); }; const ensureVisibleOnSomeDisplay = (windowState: Rectangle) => { - const visible = screen.getAllDisplays().some(display => { - return windowWithinBounds(windowState, display.bounds); - }); - if (!visible) { - // Window is partially or fully not visible now. - // Reset it to safe defaults. - return resetToDefaults(); - } - return windowState; + const visible = screen.getAllDisplays().some(display => windowWithinBounds(windowState, display.bounds)); + + // Window is partially or fully not visible now. + // Reset it to safe defaults. + return visible ? windowState : resetToDefaults(); }; const saveState = () => { - if (!win.isMinimized() && !win.isMaximized()) { - Object.assign(state, getCurrentPosition()); - } - store.set(key, state); + const target = (!win.isMinimized() && !win.isMaximized()) ? Object.assign(state, getCurrentPosition()) : state; + store.set(key, target); }; - state = ensureVisibleOnSomeDisplay(restore()); + const state = ensureVisibleOnSomeDisplay(restore()); const browserOptions: BrowserWindowConstructorOptions = { - ...options, - ...state, + ...options, ...state, webPreferences: { nodeIntegration: true, - }, + contextIsolation: true + } }; win = new BrowserWindow(browserOptions); diff --git a/examples/_template/ts/main/helpers/exit-on-change.ts b/examples/_template/ts/main/helpers/exit-on-change.ts index e831994..a9072e8 100644 --- a/examples/_template/ts/main/helpers/exit-on-change.ts +++ b/examples/_template/ts/main/helpers/exit-on-change.ts @@ -1,6 +1,6 @@ +import { app } from 'electron'; import { watchFile } from 'fs'; import { join } from 'path'; -import { app } from 'electron'; export default function exitOnChange(): void { watchFile(join(process.cwd(), 'app/background.js'), () => { diff --git a/examples/_template/ts/main/helpers/index.ts b/examples/_template/ts/main/helpers/index.ts index 36f451b..5ddd8dd 100644 --- a/examples/_template/ts/main/helpers/index.ts +++ b/examples/_template/ts/main/helpers/index.ts @@ -1,7 +1,4 @@ import createWindow from './create-window'; import exitOnChange from './exit-on-change'; -export { - createWindow, - exitOnChange, -}; +export { createWindow, exitOnChange }; diff --git a/examples/with-typescript/package.json b/examples/with-typescript/package.json index 4154d85..191326c 100644 --- a/examples/with-typescript/package.json +++ b/examples/with-typescript/package.json @@ -14,12 +14,13 @@ "electron-store": "^6.0.1" }, "devDependencies": { - "@nuxt/typescript": "^2.8.1", - "@types/node": "^12.7.12", - "electron": "^10.1.5", + "@nuxt/types": "^2.14.9", + "@nuxt/typescript-build": "^2.0.3", + "@nuxt/typescript-runtime": "^2.0.0", + "core-js": "^3.8.0", + "electron": "^11.0.3", "electron-builder": "^22.9.1", - "nuxt": "^2.14.7", - "nuxtron": "^0.3.1", - "typescript": "^3.6.4" + "nuxt": "^2.14.9", + "nuxtron": "^0.3.1" } } diff --git a/examples/with-typescript/renderer/assets/README.md b/examples/with-typescript/renderer/assets/README.md new file mode 100644 index 0000000..34766f9 --- /dev/null +++ b/examples/with-typescript/renderer/assets/README.md @@ -0,0 +1,7 @@ +# ASSETS + +**This directory is not required, you can delete it if you don't want to use it.** + +This directory contains your un-compiled assets such as LESS, SASS, or JavaScript. + +More information about the usage of this directory in [the documentation](https://nuxtjs.org/guide/assets#webpacked). diff --git a/examples/with-typescript/renderer/components/Logo.vue b/examples/with-typescript/renderer/components/Logo.vue new file mode 100644 index 0000000..543ddcd --- /dev/null +++ b/examples/with-typescript/renderer/components/Logo.vue @@ -0,0 +1,26 @@ + + + diff --git a/examples/with-typescript/renderer/components/README.md b/examples/with-typescript/renderer/components/README.md new file mode 100644 index 0000000..a079f10 --- /dev/null +++ b/examples/with-typescript/renderer/components/README.md @@ -0,0 +1,7 @@ +# COMPONENTS + +**This directory is not required, you can delete it if you don't want to use it.** + +The components directory contains your Vue.js Components. + +_Nuxt.js doesn't supercharge these components._ diff --git a/examples/with-typescript/renderer/layouts/README.md b/examples/with-typescript/renderer/layouts/README.md new file mode 100644 index 0000000..cad1ad5 --- /dev/null +++ b/examples/with-typescript/renderer/layouts/README.md @@ -0,0 +1,7 @@ +# LAYOUTS + +**This directory is not required, you can delete it if you don't want to use it.** + +This directory contains your Application Layouts. + +More information about the usage of this directory in [the documentation](https://nuxtjs.org/guide/views#layouts). diff --git a/examples/with-typescript/renderer/layouts/default.vue b/examples/with-typescript/renderer/layouts/default.vue new file mode 100644 index 0000000..570aba2 --- /dev/null +++ b/examples/with-typescript/renderer/layouts/default.vue @@ -0,0 +1,59 @@ + + + diff --git a/examples/with-typescript/renderer/middleware/README.md b/examples/with-typescript/renderer/middleware/README.md new file mode 100644 index 0000000..01595de --- /dev/null +++ b/examples/with-typescript/renderer/middleware/README.md @@ -0,0 +1,8 @@ +# MIDDLEWARE + +**This directory is not required, you can delete it if you don't want to use it.** + +This directory contains your application middleware. +Middleware let you define custom functions that can be run before rendering either a page or a group of pages. + +More information about the usage of this directory in [the documentation](https://nuxtjs.org/guide/routing#middleware). diff --git a/examples/with-typescript/renderer/nuxt.config.js b/examples/with-typescript/renderer/nuxt.config.js deleted file mode 100644 index 8410978..0000000 --- a/examples/with-typescript/renderer/nuxt.config.js +++ /dev/null @@ -1,10 +0,0 @@ -export default { - head: { - title: 'Nuxtron (with-typescript)', - }, - build: { - extend: (config) => { - config.target = 'electron-renderer'; - }, - }, -} diff --git a/examples/with-typescript/renderer/nuxt.config.ts b/examples/with-typescript/renderer/nuxt.config.ts new file mode 100644 index 0000000..de9f184 --- /dev/null +++ b/examples/with-typescript/renderer/nuxt.config.ts @@ -0,0 +1,15 @@ +import { NuxtConfig } from '@nuxt/types'; + +const config: NuxtConfig = { + head: { + title: 'Nuxtron (with-typescript)' + }, + buildModules: ['@nuxt/typescript-build'], + build: { + extend: (config) => { + config.target = 'electron-renderer'; + } + } +}; + +export default config; diff --git a/examples/with-typescript/renderer/pages/README.md b/examples/with-typescript/renderer/pages/README.md new file mode 100644 index 0000000..1d5d48b --- /dev/null +++ b/examples/with-typescript/renderer/pages/README.md @@ -0,0 +1,6 @@ +# PAGES + +This directory contains your Application Views and Routes. +The framework reads all the `*.vue` files inside this directory and creates the router of your application. + +More information about the usage of this directory in [the documentation](https://nuxtjs.org/guide/routing). diff --git a/examples/with-typescript/renderer/pages/about.vue b/examples/with-typescript/renderer/pages/about.vue index 6b11125..53c5771 100644 --- a/examples/with-typescript/renderer/pages/about.vue +++ b/examples/with-typescript/renderer/pages/about.vue @@ -1,40 +1,42 @@ -