diff --git a/.gitignore b/.gitignore index 375c251..cecdd34 100644 --- a/.gitignore +++ b/.gitignore @@ -10,7 +10,6 @@ node_modules /tests/**/*.js /tests/**/*.js.map /frontend -/index.html # managed by application /database diff --git a/.prettierrc.yml b/.prettierrc.yml index 70efcb0..52e8718 100644 --- a/.prettierrc.yml +++ b/.prettierrc.yml @@ -9,3 +9,6 @@ overrides: - files: '*.svelte' options: parser: html + - files: '*.handlebars' + options: + parser: html diff --git a/forge.config.js b/forge.config.js index 56ad5c3..1462d9f 100644 --- a/forge.config.js +++ b/forge.config.js @@ -7,6 +7,7 @@ const ignoreList = [ /^\/node_modules\/\.cache($|\/)/, /^\/src\/.*\.(ts|js\.map)/, /^\/store($|\/)/, + /^\/templates($|\/)/, /^\/tests($|\/)/, /^\/\.editorconfig/, /^\/\.eslintignore/, @@ -17,7 +18,6 @@ const ignoreList = [ /^\/\.prettierrc\.yml/, /^\/forge.config\.js/, /^\/gulpfile\.js/, - /^\/index\..*\.html/, /^\/package-lock\.json/, /^\/tsconfig\.json/, /^\/tslint\.json/, diff --git a/gulpfile.js b/gulpfile.js index 03d5b6f..4d2d0b6 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -6,6 +6,7 @@ const minimist = require('minimist'); const tsConfig = require('./tsconfig.json'); const webpack = require('webpack-stream'); const webpackConfig = require('./webpack.config'); +const templating = require('./templates'); const argv = minimist(process.argv); @@ -23,12 +24,15 @@ function buildFrontend() { .pipe(dest(config.output.path)); } -function copyIndexHtml(cb) { - let indexHtml = 'index.prod.html'; +function buildIndexHtml(cb) { + let isDev = false; if (argv.dev) { - indexHtml = 'index.dev.html'; + isDev = true; } - fs.copyFileSync(indexHtml, 'index.html'); + const html = templating.compile(isDev); + + fs.writeFileSync(path.resolve(webpackConfig.output.path, 'index.html'), html); + cb(); } @@ -48,10 +52,10 @@ function transpileTypescript(cb) { function build(cb) { if (argv.watch) { watch(tsConfig.include, { ignoreInitial: false }, transpileTypescript); - watch('index.*.html', { ignoreInitial: false }, copyIndexHtml); + watch('./templates/**/*', { ignoreInitial: false }, buildIndexHtml); buildFrontend(); } else { - parallel(transpileTypescript, copyIndexHtml, buildFrontend)(cb); + parallel(transpileTypescript, buildIndexHtml, buildFrontend)(cb); } } build.flags = { diff --git a/index.prod.html b/index.prod.html deleted file mode 100644 index 0553f58..0000000 --- a/index.prod.html +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - Renai - - - - diff --git a/package-lock.json b/package-lock.json index 6510456..b0540c8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10297,9 +10297,9 @@ "dev": true }, "uglify-js": { - "version": "3.6.1", - "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.6.1.tgz", - "integrity": "sha512-+dSJLJpXBb6oMHP+Yvw8hUgElz4gLTh82XuX68QiJVTXaE5ibl6buzhNkQdYhBlIhozWOC9ge16wyRmjG4TwVQ==", + "version": "3.6.2", + "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.6.2.tgz", + "integrity": "sha512-+gh/xFte41GPrgSMJ/oJVq15zYmqr74pY9VoM69UzMzq9NFk4YDylclb1/bhEzZSaUQjbW5RvniHeq1cdtRYjw==", "dev": true, "optional": true, "requires": { diff --git a/package.json b/package.json index 6ea903c..057b182 100644 --- a/package.json +++ b/package.json @@ -20,8 +20,8 @@ "eslint": "eslint .", "tslint-check": "tslint-config-prettier-check ./tslint.json", "tslint": "tslint -t stylish -p tsconfig.json", - "prettier": "prettier --ignore-path .gitignore -c **/*.{html,json,{c,sc,sa,le}ss,yml,svelte,md,ts,js}", - "prettier:write": "prettier --ignore-path .gitignore --write **/*.{html,json,{c,sc,sa,le}ss,yml,svelte,md,ts,js}", + "prettier": "prettier --ignore-path .gitignore -c **/*.{html,handlebars,json,{c,sc,sa,le}ss,yml,svelte,md,ts,js}", + "prettier:write": "prettier --ignore-path .gitignore --write **/*.{html,handlebars,json,{c,sc,sa,le}ss,yml,svelte,md,ts,js}", "forge:start": "electron-forge start", "forge:make": "electron-forge --platform win32 --arch x64 make", "forge": "npm run build && npm run forge:make", @@ -56,6 +56,7 @@ "fast-check": "^1.17.0", "gulp": "^4.0.2", "gulp-cli": "^2.2.0", + "handlebars": "^4.4.3", "husky": "^3.0.8", "minimist": "^1.2.0", "mocha": "^6.2.1", diff --git a/src/main.ts b/src/main.ts index 268d271..0464c53 100644 --- a/src/main.ts +++ b/src/main.ts @@ -38,7 +38,7 @@ async function createWindow(): Promise { mainWindow = new BrowserWindow(options); // and load the index.html of the app. - await mainWindow.loadFile('index.html'); + await mainWindow.loadFile('frontend/index.html'); // Open the DevTools. mainWindow.webContents.openDevTools(); diff --git a/templates/data.json b/templates/data.json new file mode 100644 index 0000000..d85708e --- /dev/null +++ b/templates/data.json @@ -0,0 +1,17 @@ +{ + "prod": { + "csp": { + "default-src": ["self"], + "style-src": ["unsafe-inline"] + }, + "appBundle": "./bundle.js" + }, + "dev": { + "csp": { + "default-src": ["self"], + "style-src": ["unsafe-inline"], + "script-src": ["self", "unsafe-eval"] + }, + "appBundle": "./bundle.js" + } +} diff --git a/index.dev.html b/templates/index.html.handlebars similarity index 57% rename from index.dev.html rename to templates/index.html.handlebars index 84c6cce..cccdb04 100644 --- a/index.dev.html +++ b/templates/index.html.handlebars @@ -2,12 +2,14 @@ + {{#if csp}} + {{/if}} Renai - + diff --git a/templates/index.js b/templates/index.js new file mode 100644 index 0000000..1049858 --- /dev/null +++ b/templates/index.js @@ -0,0 +1,20 @@ +const handlebars = require('handlebars'); +const path = require('path'); +const fs = require('fs'); +const data = require('./data'); + +function compile(isDevMode = false) { + const templatePath = path.resolve(__dirname, 'index.html.handlebars'); + const template = fs.readFileSync(templatePath).toString(); + const compiled = handlebars.compile(template); + let result = ''; + if (isDevMode) { + result = compiled(data.dev); + } else { + result = compiled(data.prod); + } + + return result; +} + +exports.compile = compile;