From d976b621b8e9bf20545b5484be010968a1c4f914 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Sat, 26 Jan 2019 10:14:15 -0800 Subject: [PATCH] chore: move inline-script to src (#917) --- .gitignore | 2 +- bin/build-inline-script.js | 6 +++--- bin/build-template-html.js | 2 +- inline-script.js => src/inline-script/inline-script.js | 10 +++++----- src/server.js | 2 +- 5 files changed, 11 insertions(+), 11 deletions(-) rename inline-script.js => src/inline-script/inline-script.js (86%) diff --git a/.gitignore b/.gitignore index a93c9ba3..41dce00b 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,4 @@ node_modules /static/robots.txt /static/inline-script.js.map /static/emoji-mart-all.json -/inline-script-checksum.json +/src/inline-script/checksum.js diff --git a/bin/build-inline-script.js b/bin/build-inline-script.js index ba8cbaf0..bd296a73 100644 --- a/bin/build-inline-script.js +++ b/bin/build-inline-script.js @@ -13,7 +13,7 @@ const writeFile = promisify(fs.writeFile) const themeColors = fromPairs(themes.map(_ => ([_.name, _.color]))) export async function buildInlineScript () { - let inlineScriptPath = path.join(__dirname, '../inline-script.js') + let inlineScriptPath = path.join(__dirname, '../src/inline-script/inline-script.js') let bundle = await rollup({ input: inlineScriptPath, @@ -38,8 +38,8 @@ export async function buildInlineScript () { let fullCode = `${code}//# sourceMappingURL=/inline-script.js.map` let checksum = crypto.createHash('sha256').update(fullCode).digest('base64') - await writeFile(path.resolve(__dirname, '../inline-script-checksum.json'), - JSON.stringify({ checksum }), 'utf8') + await writeFile(path.resolve(__dirname, '../src/inline-script/checksum.js'), + `module.exports = ${JSON.stringify(checksum)}`, 'utf8') await writeFile(path.resolve(__dirname, '../static/inline-script.js.map'), map.toString(), 'utf8') diff --git a/bin/build-template-html.js b/bin/build-template-html.js index 2ee9ae63..c3ab5e8d 100644 --- a/bin/build-template-html.js +++ b/bin/build-template-html.js @@ -19,7 +19,7 @@ const builders = [ rebuild: buildSass }, { - watch: 'inline-script.js', + watch: 'src/inline-script/inline-script.js', comment: '', rebuild: buildInlineScript }, diff --git a/inline-script.js b/src/inline-script/inline-script.js similarity index 86% rename from inline-script.js rename to src/inline-script/inline-script.js index 1e7bab09..3d9d4aad 100644 --- a/inline-script.js +++ b/src/inline-script/inline-script.js @@ -1,12 +1,12 @@ // For perf reasons, this script is run inline to quickly set certain styles. // To allow CSP to work correctly, we also calculate a sha256 hash during -// the build process and write it to inline-script-checksum.json. +// the build process and write it to checksum.js. -import { testHasLocalStorageOnce } from './src/routes/_utils/testStorage' -import { switchToTheme } from './src/routes/_utils/themeEngine' -import { basename } from './src/routes/_api/utils' -import { onUserIsLoggedOut } from './src/routes/_actions/onUserIsLoggedOut' +import { testHasLocalStorageOnce } from '../routes/_utils/testStorage' +import { switchToTheme } from '../routes/_utils/themeEngine' +import { basename } from '../routes/_api/utils' +import { onUserIsLoggedOut } from '../routes/_actions/onUserIsLoggedOut' window.__themeColors = process.env.THEME_COLORS diff --git a/src/server.js b/src/server.js index c8bed2a0..2069b47c 100644 --- a/src/server.js +++ b/src/server.js @@ -6,7 +6,7 @@ const app = express() const helmet = require('helmet') const uuidv4 = require('uuid/v4') -const headScriptChecksum = require('../inline-script-checksum').checksum +const headScriptChecksum = require('./inline-script/checksum') const { PORT = 4002 } = process.env