From d75015279a80c2284105e0625fe4631298fca7bb Mon Sep 17 00:00:00 2001 From: Vinson Chuong Date: Wed, 28 Aug 2019 15:53:55 -0700 Subject: [PATCH] feat(precompile): Experimental first pass at precompiling --- .flowconfig | 2 + lib/precompile/compile-api/index.js | 18 ++++++ lib/precompile/index.js | 2 + package.json | 6 +- src/cli/pass-notes/bin.js | 18 ++++-- src/cli/precompile/bin.js | 26 ++++++++ yarn.lock | 94 +++++++++++++++++++++++++++++ 7 files changed, 159 insertions(+), 7 deletions(-) create mode 100644 lib/precompile/compile-api/index.js create mode 100644 lib/precompile/index.js create mode 100755 src/cli/precompile/bin.js diff --git a/.flowconfig b/.flowconfig index 482fc11..92597b8 100644 --- a/.flowconfig +++ b/.flowconfig @@ -9,3 +9,5 @@ [options] module.name_mapper='^passing-notes$' -> '' module.name_mapper='^passing-notes\/\(.*\)$' -> '/\1' +sharedmemory.hash_table_pow=21 +module.ignore_non_literal_requires=true diff --git a/lib/precompile/compile-api/index.js b/lib/precompile/compile-api/index.js new file mode 100644 index 0000000..e3f75b3 --- /dev/null +++ b/lib/precompile/compile-api/index.js @@ -0,0 +1,18 @@ +/* @flow */ +import * as path from 'path' +import { rollup } from 'rollup' +import resolve from 'rollup-plugin-node-resolve' +import commonjs from 'rollup-plugin-commonjs' +import filenamify from 'filenamify' + +export default async function(entry: string): Promise { + const bundle = await rollup({ + input: entry, + plugins: [resolve(), commonjs({ include: /node_modules/ })] + }) + + await bundle.write({ + file: path.resolve('dist', filenamify(entry), 'index.js'), + format: 'cjs' + }) +} diff --git a/lib/precompile/index.js b/lib/precompile/index.js new file mode 100644 index 0000000..acbfa55 --- /dev/null +++ b/lib/precompile/index.js @@ -0,0 +1,2 @@ +/* @flow */ +export { default as compileApi } from './compile-api' diff --git a/package.json b/package.json index 19be21b..5837378 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,8 @@ "author": "Vinson Chuong ", "main": "./src/index.js", "bin": { - "pass-notes": "./src/cli/pass-notes/bin.js" + "pass-notes": "./src/cli/pass-notes/bin.js", + "precompile": "./src/cli/precompile/bin.js" }, "browser": { "./src/index.js": "./src/index.browser.js", @@ -46,6 +47,9 @@ "parcel-bundler": "^1.12.3", "pretty-error": "^2.1.1", "promisify-event": "^1.0.0", + "rollup": "^1.20.3", + "rollup-plugin-commonjs": "^10.1.0", + "rollup-plugin-node-resolve": "^5.2.0", "url-parse": "^1.4.7", "ws": "^7.1.2" }, diff --git a/src/cli/pass-notes/bin.js b/src/cli/pass-notes/bin.js index f473bb4..f701431 100644 --- a/src/cli/pass-notes/bin.js +++ b/src/cli/pass-notes/bin.js @@ -9,20 +9,26 @@ import { hotRequireAndCompile } from 'passing-notes/lib/require' import { printLog } from 'passing-notes/lib/log' +import { pathExists } from 'fs-extra' +import filenamify from 'filenamify' async function run() { - const applicationPath = path.resolve(process.argv[2] || 'server.js') + const applicationPath = process.argv[2] || 'server.js' + const precompilePath = path.resolve('dist', filenamify(applicationPath)) const port = await getPort() + await pathExists(precompilePath) const application = process.env.NODE_ENV === 'production' - ? requireAndCompile({ - modulePath: applicationPath, - log: printLog - }) || {} + ? (await pathExists(precompilePath)) + ? requireAndCompile({ + modulePath: path.resolve(applicationPath), + log: printLog + }) || {} + : require(precompilePath) : hotRequireAndCompile({ baseDirectory: path.resolve(), - modulePath: applicationPath, + modulePath: path.resolve(applicationPath), log: printLog }) diff --git a/src/cli/precompile/bin.js b/src/cli/precompile/bin.js new file mode 100755 index 0000000..41667e7 --- /dev/null +++ b/src/cli/precompile/bin.js @@ -0,0 +1,26 @@ +#!/usr/bin/env node +/* @flow */ +import 'dotenv/config' +import * as path from 'path' +import { compileApi } from 'passing-notes/lib/precompile' +import { printLog } from 'passing-notes/lib/log' +import { readJson } from 'fs-extra' + +async function run() { + if (process.env.NODE_ENV !== 'production') { + return + } + + const packageJson = await readJson(path.resolve('package.json')) + const [, entry] = packageJson.scripts.start.match(/pass-notes (.*)/) + + const endLog = printLog({ type: 'CLI', message: 'Compiling API...' }) + try { + await compileApi(entry) + endLog({ type: 'CLI', message: 'Finished' }) + } catch (error) { + endLog({ type: 'CLI', message: 'Failed', error }) + } +} + +run() diff --git a/yarn.lock b/yarn.lock index ca2a0b1..4fd0bec 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1565,6 +1565,11 @@ dependencies: defer-to-connect "^1.0.1" +"@types/estree@0.0.39": + version "0.0.39" + resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.39.tgz#e177e699ee1b8c22d23174caaa7422644389509f" + integrity sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw== + "@types/events@*": version "3.0.0" resolved "https://registry.yarnpkg.com/@types/events/-/events-3.0.0.tgz#2862f3f58a9a7f7c3e78d79f130dd4d71c25c2a7" @@ -1589,6 +1594,11 @@ resolved "https://registry.yarnpkg.com/@types/node/-/node-11.13.7.tgz#85dbb71c510442d00c0631f99dae957ce44fd104" integrity sha512-suFHr6hcA9mp8vFrZTgrmqW2ZU3mbWsryQtQlY/QvwTISCw7nw/j+bCQPPohqmskhmqa5wLNuMHTTsc+xf1MQg== +"@types/node@^12.7.2": + version "12.7.2" + resolved "https://registry.yarnpkg.com/@types/node/-/node-12.7.2.tgz#c4e63af5e8823ce9cc3f0b34f7b998c2171f0c44" + integrity sha512-dyYO+f6ihZEtNPDcWNR1fkoTDf3zAK3lAABDze3mz6POyIercH0lEUawUFXlG8xaQZmm1yEBON/4TsYv/laDYg== + "@types/normalize-package-data@^2.4.0": version "2.4.0" resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz#e486d0d97396d79beedd0a6e33f4534ff6b4973e" @@ -1599,6 +1609,13 @@ resolved "https://registry.yarnpkg.com/@types/q/-/q-1.5.2.tgz#690a1475b84f2a884fd07cd797c00f5f31356ea8" integrity sha512-ce5d3q03Ex0sy4R14722Rmt6MT07Ua+k4FwDfdcToYJcMKNtRVQvJ6JCAPdAmAnbRb6CsX6aYb9m96NGod9uTw== +"@types/resolve@0.0.8": + version "0.0.8" + resolved "https://registry.yarnpkg.com/@types/resolve/-/resolve-0.0.8.tgz#f26074d238e02659e323ce1a13d041eee280e194" + integrity sha512-auApPaJf3NPfe18hSoJkp8EbZzer2ISk7o8mCC3M9he/a04+gbMF97NkpD2S8riMGvm4BMRI59/SZQSaLTKpsQ== + dependencies: + "@types/node" "*" + JSONStream@^1.0.4, JSONStream@^1.3.4: version "1.3.5" resolved "https://registry.yarnpkg.com/JSONStream/-/JSONStream-1.3.5.tgz#3208c1f08d3a4d99261ab64f92302bc15e111ca0" @@ -1645,6 +1662,11 @@ acorn@^6.0.1, acorn@^6.0.7: resolved "https://registry.yarnpkg.com/acorn/-/acorn-6.1.1.tgz#7d25ae05bb8ad1f9b699108e1094ecd7884adc1f" integrity sha512-jPTiwtOxaHNaAPg/dmrJ/beuzLRnXtB0kQPQ8JpotKJgTB6rX6c8mlf315941pyjBSaPg8NHXS9fhP4u17DpGA== +acorn@^7.0.0: + version "7.0.0" + resolved "https://registry.yarnpkg.com/acorn/-/acorn-7.0.0.tgz#26b8d1cd9a9b700350b71c0905546f64d1284e7a" + integrity sha512-PaF/MduxijYYt7unVGRuds1vBC9bFxbNf+VWqhOClfdgy7RlVkQqt610ig1/yxTgsDIfW1cWDel5EBbOy3jdtQ== + agent-base@4, agent-base@^4.1.0, agent-base@~4.2.1: version "4.2.1" resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-4.2.1.tgz#d89e5999f797875674c07d87f260fc41e83e8ca9" @@ -2527,6 +2549,11 @@ builtin-modules@^1.0.0: resolved "https://registry.yarnpkg.com/builtin-modules/-/builtin-modules-1.1.1.tgz#270f076c5a72c02f5b65a47df94c5fe3a278892f" integrity sha1-Jw8HbFpywC9bZaR9+Uxf46J4iS8= +builtin-modules@^3.1.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/builtin-modules/-/builtin-modules-3.1.0.tgz#aad97c15131eb76b65b50ef208e7584cd76a7484" + integrity sha512-k0KL0aWZuBt2lrxrcASWDfwOLMnodeQjodT/1SxEQAXsHANgo6ZC/VEaSEHCXt7aSTZ4/4H5LKa+tBXmW7Vtvw== + builtin-status-codes@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/builtin-status-codes/-/builtin-status-codes-3.0.0.tgz#85982878e21b98e1c66425e03d0174788f569ee8" @@ -4570,6 +4597,11 @@ estraverse@^4.0.0, estraverse@^4.1.0, estraverse@^4.1.1, estraverse@^4.2.0: resolved "https://registry.yarnpkg.com/estraverse/-/estraverse-4.2.0.tgz#0dee3fed31fcd469618ce7342099fc1afa0bdb13" integrity sha1-De4/7TH81GlhjOc0IJn8GvoL2xM= +estree-walker@^0.6.1: + version "0.6.1" + resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-0.6.1.tgz#53049143f40c6eb918b23671d1fe3219f3a1b362" + integrity sha512-SqmZANLWS0mnatqbSfRP5g8OXZC12Fgg1IwNtLsyHDzJizORW4khDfjPqJZsemPWBB2uqykUah5YpQ6epsqC/w== + esutils@^2.0.0, esutils@^2.0.2: version "2.0.2" resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.2.tgz#0abf4f1caa5bcb1f7a9d8acc6dea4faaa04bac9b" @@ -6157,6 +6189,11 @@ is-installed-globally@^0.1.0: global-dirs "^0.1.0" is-path-inside "^1.0.0" +is-module@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/is-module/-/is-module-1.0.0.tgz#3258fb69f78c14d5b815d664336b4cffb6441591" + integrity sha1-Mlj7afeMFNW4FdZkM2tM/7ZEFZE= + is-npm@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/is-npm/-/is-npm-1.0.0.tgz#f2fb63a65e4905b406c86072765a1a4dc793b9f4" @@ -6254,6 +6291,13 @@ is-redirect@^1.0.0: resolved "https://registry.yarnpkg.com/is-redirect/-/is-redirect-1.0.0.tgz#1d03dded53bd8db0f30c26e4f95d36fc7c87dc24" integrity sha1-HQPd7VO9jbDzDCbk+V02/HyH3CQ= +is-reference@^1.1.2: + version "1.1.3" + resolved "https://registry.yarnpkg.com/is-reference/-/is-reference-1.1.3.tgz#e99059204b66fdbe09305cfca715a29caa5c8a51" + integrity sha512-W1iHHv/oyBb2pPxkBxtaewxa1BC58Pn5J0hogyCdefwUIvb6R+TGbAcIa4qPNYLqLhb3EnOgUf2MQkkF76BcKw== + dependencies: + "@types/estree" "0.0.39" + is-regex@^1.0.4: version "1.0.4" resolved "https://registry.yarnpkg.com/is-regex/-/is-regex-1.0.4.tgz#5517489b547091b0930e095654ced25ee97e9491" @@ -6953,6 +6997,13 @@ magic-string@^0.22.4: dependencies: vlq "^0.2.2" +magic-string@^0.25.2: + version "0.25.3" + resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.3.tgz#34b8d2a2c7fec9d9bdf9929a3fd81d271ef35be9" + integrity sha512-6QK0OpF/phMz0Q2AxILkX2mFhi7m+WMwTRg0LQKq/WBB0cDP4rYH3Wp4/d3OTXlrPLVJT/RFqj8tFeAR4nk8AA== + dependencies: + sourcemap-codec "^1.4.4" + make-dir@^1.0.0: version "1.3.0" resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-1.3.0.tgz#79c1033b80515bd6d24ec9933e860ca75ee27f0c" @@ -9987,6 +10038,44 @@ ripemd160@^2.0.0, ripemd160@^2.0.1: hash-base "^3.0.0" inherits "^2.0.1" +rollup-plugin-commonjs@^10.1.0: + version "10.1.0" + resolved "https://registry.yarnpkg.com/rollup-plugin-commonjs/-/rollup-plugin-commonjs-10.1.0.tgz#417af3b54503878e084d127adf4d1caf8beb86fb" + integrity sha512-jlXbjZSQg8EIeAAvepNwhJj++qJWNJw1Cl0YnOqKtP5Djx+fFGkp3WRh+W0ASCaFG5w1jhmzDxgu3SJuVxPF4Q== + dependencies: + estree-walker "^0.6.1" + is-reference "^1.1.2" + magic-string "^0.25.2" + resolve "^1.11.0" + rollup-pluginutils "^2.8.1" + +rollup-plugin-node-resolve@^5.2.0: + version "5.2.0" + resolved "https://registry.yarnpkg.com/rollup-plugin-node-resolve/-/rollup-plugin-node-resolve-5.2.0.tgz#730f93d10ed202473b1fb54a5997a7db8c6d8523" + integrity sha512-jUlyaDXts7TW2CqQ4GaO5VJ4PwwaV8VUGA7+km3n6k6xtOEacf61u0VXwN80phY/evMcaS+9eIeJ9MOyDxt5Zw== + dependencies: + "@types/resolve" "0.0.8" + builtin-modules "^3.1.0" + is-module "^1.0.0" + resolve "^1.11.1" + rollup-pluginutils "^2.8.1" + +rollup-pluginutils@^2.8.1: + version "2.8.1" + resolved "https://registry.yarnpkg.com/rollup-pluginutils/-/rollup-pluginutils-2.8.1.tgz#8fa6dd0697344938ef26c2c09d2488ce9e33ce97" + integrity sha512-J5oAoysWar6GuZo0s+3bZ6sVZAC0pfqKz68De7ZgDi5z63jOVZn1uJL/+z1jeKHNbGII8kAyHF5q8LnxSX5lQg== + dependencies: + estree-walker "^0.6.1" + +rollup@^1.20.3: + version "1.20.3" + resolved "https://registry.yarnpkg.com/rollup/-/rollup-1.20.3.tgz#6243f6c118ca05f56b2d9433112400cd834a1eb8" + integrity sha512-/OMCkY0c6E8tleeVm4vQVDz24CkVgvueK3r8zTYu2AQNpjrcaPwO9hE+pWj5LTFrvvkaxt4MYIp2zha4y0lRvg== + dependencies: + "@types/estree" "0.0.39" + "@types/node" "^12.7.2" + acorn "^7.0.0" + run-async@^2.2.0: version "2.3.0" resolved "https://registry.yarnpkg.com/run-async/-/run-async-2.3.0.tgz#0371ab4ae0bdd720d4166d7dfda64ff7a445a6c0" @@ -10441,6 +10530,11 @@ source-map@^0.5.0, source-map@^0.5.3, source-map@^0.5.6: resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.5.7.tgz#8a039d2d1021d22d1ea14c80d8ea468ba2ef3fcc" integrity sha1-igOdLRAh0i0eoUyA2OpGi6LvP8w= +sourcemap-codec@^1.4.4: + version "1.4.6" + resolved "https://registry.yarnpkg.com/sourcemap-codec/-/sourcemap-codec-1.4.6.tgz#e30a74f0402bad09807640d39e971090a08ce1e9" + integrity sha512-1ZooVLYFxC448piVLBbtOxFcXwnymH9oUF8nRd3CuYDVvkRBxRl6pB4Mtas5a4drtL+E8LDgFkQNcgIw6tc8Hg== + spawn-error-forwarder@~1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/spawn-error-forwarder/-/spawn-error-forwarder-1.0.0.tgz#1afd94738e999b0346d7b9fc373be55e07577029"