diff --git a/_scripts/ProcessLocalesPlugin.js b/_scripts/ProcessLocalesPlugin.js index b96a5823..4d471cd2 100644 --- a/_scripts/ProcessLocalesPlugin.js +++ b/_scripts/ProcessLocalesPlugin.js @@ -34,7 +34,7 @@ class ProcessLocalesPlugin { }, async (_assets) => { const promises = [] - + for (const { locale, data } of this.locales) { promises.push(new Promise((resolve) => { if (Object.prototype.hasOwnProperty.call(data, 'Locale Name')) { @@ -60,8 +60,6 @@ class ProcessLocalesPlugin { } await Promise.all(promises) - - this.locales = null }) }) } diff --git a/_scripts/dev-runner.js b/_scripts/dev-runner.js index c48daa56..bedcb7f2 100644 --- a/_scripts/dev-runner.js +++ b/_scripts/dev-runner.js @@ -1,5 +1,6 @@ process.env.NODE_ENV = 'development' +const open = require('open') const electron = require('electron') const webpack = require('webpack') const WebpackDevServer = require('webpack-dev-server') @@ -10,13 +11,14 @@ const { spawn } = require('child_process') const mainConfig = require('./webpack.main.config') const rendererConfig = require('./webpack.renderer.config') +const webConfig = require('./webpack.web.config') const workersConfig = require('./webpack.workers.config') let electronProcess = null let manualRestart = null -const remoteDebugging = !!( - process.argv[2] && process.argv[2] === '--remote-debug' -) + +const remoteDebugging = process.argv.indexOf('--remote-debug') !== -1 +const web = process.argv.indexOf('--web') !== -1 if (remoteDebugging) { // disable dvtools open in electron @@ -87,7 +89,6 @@ function startMain() { manualRestart = true await restartElectron() - setTimeout(() => { manualRestart = false }, 2500) @@ -135,4 +136,38 @@ function startRenderer(callback) { }) } -startRenderer(startMain) +function startWeb (callback) { + const compiler = webpack(webConfig) + const { name } = compiler + + compiler.hooks.afterEmit.tap('afterEmit', () => { + console.log(`\nCompiled ${name} script!`) + console.log(`\nWatching file changes for ${name} script...`) + }) + + const server = new WebpackDevServer({ + static: { + directory: path.join(process.cwd(), 'dist/web/static'), + watch: { + ignored: [ + /(dashFiles|storyboards)\/*/, + '/**/.DS_Store', + ] + } + }, + port + }, compiler) + + server.startCallback(err => { + if (err) console.error(err) + + callback({ port: server.options.port }) + }) +} +if (!web) { + startRenderer(startMain) +} else { + startWeb(({ port }) => { + open(`http://localhost:${port}`) + }) +} diff --git a/_scripts/webpack.web.config.js b/_scripts/webpack.web.config.js index 5e28947d..79276116 100644 --- a/_scripts/webpack.web.config.js +++ b/_scripts/webpack.web.config.js @@ -24,7 +24,9 @@ const config = { filename: '[name].js', }, externals: { - electron: '{}' + electron: '{}', + ytpl: '{}', + ytsr: '{}' }, module: { rules: [ @@ -168,41 +170,42 @@ if (isDevMode) { __static: `"${path.join(__dirname, '../static').replace(/\\/g, '\\\\')}"`, }) ) -} else { - const processLocalesPlugin = new ProcessLocalesPlugin({ - compress: false, - inputDir: path.join(__dirname, '../static/locales'), - outputDir: 'static/locales', - }) - - config.plugins.push( - processLocalesPlugin, - new webpack.DefinePlugin({ - 'process.env.LOCALE_NAMES': JSON.stringify(processLocalesPlugin.localeNames) - }), - new CopyWebpackPlugin({ - patterns: [ - { - from: path.join(__dirname, '../static/pwabuilder-sw.js'), - to: path.join(__dirname, '../dist/web/pwabuilder-sw.js'), - }, - { - from: path.join(__dirname, '../static'), - to: path.join(__dirname, '../dist/web/static'), - globOptions: { - dot: true, - ignore: ['**/.*', '**/locales/**', '**/pwabuilder-sw.js', '**/dashFiles/**', '**/storyboards/**'], - }, - }, - ] - }), - // webpack doesn't get rid of js-yaml even though it isn't used in the production builds - // so we need to manually tell it to ignore any imports for `js-yaml` - new webpack.IgnorePlugin({ - resourceRegExp: /^js-yaml$/, - contextRegExp: /i18n$/ - }) - ) } +const processLocalesPlugin = new ProcessLocalesPlugin({ + compress: false, + inputDir: path.join(__dirname, '../static/locales'), + outputDir: 'static/locales', +}) + +config.plugins.push( + processLocalesPlugin, + new webpack.DefinePlugin({ + 'process.env.LOCALE_NAMES': JSON.stringify(processLocalesPlugin.localeNames) + }), + new CopyWebpackPlugin({ + patterns: [ + { + from: path.join(__dirname, '../static/pwabuilder-sw.js'), + to: path.join(__dirname, '../dist/web/pwabuilder-sw.js'), + }, + { + from: path.join(__dirname, '../static'), + to: path.join(__dirname, '../dist/web/static'), + globOptions: { + dot: true, + ignore: ['**/.*', '**/locales/**', '**/pwabuilder-sw.js', '**/dashFiles/**', '**/storyboards/**'], + }, + }, + ] + }), + // webpack doesn't get rid of js-yaml even though it isn't used in the production builds + // so we need to manually tell it to ignore any imports for `js-yaml` + new webpack.IgnorePlugin({ + resourceRegExp: /^js-yaml$/, + contextRegExp: /i18n$/ + }) +) + + module.exports = config diff --git a/package.json b/package.json index 551ac46a..025005bd 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "debug": "run-s rebuild:electron debug-runner", "debug-runner": "node _scripts/dev-runner.js --remote-debug", "dev": "run-s rebuild:electron dev-runner", + "dev:web": "node _scripts/dev-runner.js --web", "dev-runner": "node _scripts/dev-runner.js", "lint-fix": "eslint --fix --ext .js,.vue ./", "lint": "eslint --ext .js,.vue ./", diff --git a/src/renderer/components/general-settings/general-settings.js b/src/renderer/components/general-settings/general-settings.js index 96bcbe64..d7332c93 100644 --- a/src/renderer/components/general-settings/general-settings.js +++ b/src/renderer/components/general-settings/general-settings.js @@ -117,7 +117,7 @@ export default Vue.extend({ this.$t('Settings.General Settings.System Default') ] - if (process.env.NODE_ENV === 'development') { + if (process.env.NODE_ENV === 'development' && process.env.IS_ELECTRON) { Object.entries(this.$i18n.messages).forEach(([locale, localeData]) => { const localeName = localeData['Locale Name'] if (typeof localeName !== 'undefined') { diff --git a/src/renderer/i18n/index.js b/src/renderer/i18n/index.js index 6b0495de..3ad3d603 100644 --- a/src/renderer/i18n/index.js +++ b/src/renderer/i18n/index.js @@ -9,8 +9,9 @@ const isDev = process.env.NODE_ENV === 'development' const messages = {} -if (isDev) { +if (isDev && process.env.IS_ELECTRON) { const { load } = require('js-yaml') + // Take active locales and load respective YAML file activeLocales.forEach((locale) => { try { @@ -30,45 +31,44 @@ class CustomVueI18n extends VueI18n { } async loadLocale(locale) { - // we only lazy load locales for producation builds - if (!isDev) { - // don't need to load it if it's already loaded - if (this.availableLocales.includes(locale)) { - return - } - if (!this.allLocales.includes(locale)) { - console.error(`Unable to load unknown locale: "${locale}"`) - } + // we don't lazy load locales in development in electron + if (isDev && process.env.IS_ELECTRON) { return } + // don't need to load it if it's already loaded + if (this.availableLocales.includes(locale)) { + return + } + if (!this.allLocales.includes(locale)) { + console.error(`Unable to load unknown locale: "${locale}"`) + } - if (process.env.IS_ELECTRON) { - const { brotliDecompressSync } = require('zlib') - // locales are only compressed in our production Electron builds - try { - // decompress brotli compressed json file and then load it - // eslint-disable-next-line node/no-path-concat - const compressed = fs.readFileSync(`${__dirname}/static/locales/${locale}.json.br`) - const data = JSON.parse(brotliDecompressSync(compressed).toString()) - this.setLocaleMessage(locale, data) - } catch (err) { - console.error(locale, err) - } - } else { - const url = new URL(window.location.href) - url.hash = '' - if (url.pathname.endsWith('index.html')) { - url.pathname = url.pathname.replace(/index\.html$/, '') - } - - if (url.pathname) { - url.pathname += `${!url.pathname.endsWith('/') ? '/' : ''}static/locales/${locale}.json` - } else { - url.pathname = `/static/locales/${locale}.json` - } - - const response = await fetch(url) - const data = await response.json() + if (process.env.IS_ELECTRON) { + const { brotliDecompressSync } = require('zlib') + // locales are only compressed in our production Electron builds + try { + // decompress brotli compressed json file and then load it + // eslint-disable-next-line node/no-path-concat + const compressed = fs.readFileSync(`${__dirname}/static/locales/${locale}.json.br`) + const data = JSON.parse(brotliDecompressSync(compressed).toString()) this.setLocaleMessage(locale, data) + } catch (err) { + console.error(locale, err) } + } else { + const url = new URL(window.location.href) + url.hash = '' + if (url.pathname.endsWith('index.html')) { + url.pathname = url.pathname.replace(/index\.html$/, '') + } + + if (url.pathname) { + url.pathname += `${!url.pathname.endsWith('/') ? '/' : ''}static/locales/${locale}.json` + } else { + url.pathname = `/static/locales/${locale}.json` + } + + const response = await fetch(url) + const data = await response.json() + this.setLocaleMessage(locale, data) } } } @@ -81,7 +81,7 @@ const i18n = new CustomVueI18n({ messages }) -if (!isDev) { +if (!isDev || !process.env.IS_ELECTRON) { i18n.loadLocale('en-US') } diff --git a/src/renderer/store/modules/settings.js b/src/renderer/store/modules/settings.js index 1d0aeeb4..04286b29 100644 --- a/src/renderer/store/modules/settings.js +++ b/src/renderer/store/modules/settings.js @@ -318,7 +318,7 @@ const stateWithSideEffects = { } } - if (process.env.NODE_ENV !== 'development') { + if (process.env.NODE_ENV !== 'development' || !process.env.IS_ELECTRON) { await i18n.loadLocale(targetLocale) }