Skip to content

Commit

Permalink
fix: update static frontend (#190)
Browse files Browse the repository at this point in the history
This closes #187.
  • Loading branch information
jasonsilberman authored Jul 29, 2019
1 parent 16a3000 commit c1e7ff0
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 34 deletions.
1 change: 1 addition & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ module.exports = {
plugins: [
"@babel/plugin-proposal-class-properties",
"@babel/transform-modules-commonjs",
"@babel/plugin-syntax-dynamic-import"
]
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
},
"devDependencies": {
"@babel/core": "^7.5.5",
"@babel/plugin-syntax-dynamic-import": "^7.2.0",
"@babel/preset-typescript": "~7.3.3",
"@commitlint/cli": "^8.0.0",
"@commitlint/config-conventional": "^8.0.0",
Expand Down
1 change: 0 additions & 1 deletion packages/@best/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"@best/compare": "4.0.0-alpha4",
"@best/config": "4.0.0-alpha4",
"@best/console-stream": "4.0.0-alpha4",
"@best/frontend": "4.0.0-alpha4",
"@best/github-integration": "4.0.0-alpha4",
"@best/runner": "4.0.0-alpha4",
"@best/store": "4.0.0-alpha4",
Expand Down
12 changes: 8 additions & 4 deletions packages/@best/cli/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import { logError } from "@best/utils";
import { runBest } from '../run_best';
import { runCompare } from '../run_compare';
import { getConfigs } from "@best/config";
import { ProjectConfigs, FrozenProjectConfig, CliConfig, BenchmarkResultsSnapshot } from '@best/types';
import { buildStaticFrontend } from '@best/frontend';
import { ProjectConfigs, FrozenProjectConfig, CliConfig } from '@best/types';

export function buildArgs(maybeArgv?: string[]): CliConfig {
const parsedArgs = yargs(maybeArgv || process.argv.slice(2))
Expand Down Expand Up @@ -103,8 +102,13 @@ export async function runCLI(argsCLI: CliConfig, projects: string[]) {
output.report(results);

if (argsCLI.generateHTML) {
const benchmarkResults: BenchmarkResultsSnapshot[] = results;
await buildStaticFrontend(benchmarkResults, globalConfig, process.stdout);
try {
const { buildStaticFrontend } = await import('@best/frontend');
const projectConfig = configs[0];
await buildStaticFrontend(results, globalConfig, projectConfig, process.stdout);
} catch (err) {
throw new Error('You passed the `--generateHTML` flag, but `@best/frontend` is not a dependency. Make sure you include it as a dependency.');
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/@best/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
},
"files": [
"build/**/*.js",
"dist/**/*"
"dist/**/*",
"src/**/*"
]
}
2 changes: 2 additions & 0 deletions packages/@best/frontend/server/static/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export const buildMockedDataFromApi = async (options: MockerOptions): Promise<{
} | null> => {
const db = loadDbFromConfig(options.config);

await db.migrate();

const allProjects = await db.fetchProjects();
const projects = allProjects.filter((proj): boolean => options.projectNames.includes(proj.name));

Expand Down
11 changes: 6 additions & 5 deletions packages/@best/frontend/server/static/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ import path from 'path'
import fs from 'fs'
import { promisify } from 'util'
import { rollup } from 'rollup'
import { BenchmarkResultsSnapshot, FrozenGlobalConfig } from '@best/types';
import { BenchmarkResultsSnapshot, FrozenGlobalConfig, FrozenProjectConfig } from '@best/types';
import { OutputStream } from '@best/console-stream';

import rollupConfig from './rollup.config'
import { buildRollupConfig } from './rollup.config'
import html from './static-template.html'

const asyncWrite = promisify(fs.writeFile);


export const buildStaticFrontend = async (results: BenchmarkResultsSnapshot[], globalConfig: FrozenGlobalConfig, stream: NodeJS.WriteStream): Promise<boolean> => {
export const buildStaticFrontend = async (results: BenchmarkResultsSnapshot[], globalConfig: FrozenGlobalConfig, projectConfig: FrozenProjectConfig, stream: NodeJS.WriteStream): Promise<boolean> => {
if (! globalConfig.apiDatabase) { throw new Error('No database configured') }

const outputStream = new OutputStream(stream, globalConfig.isInteractive);
Expand All @@ -26,12 +25,14 @@ export const buildStaticFrontend = async (results: BenchmarkResultsSnapshot[], g
config: { apiDatabase: globalConfig.apiDatabase }
}

const rollupConfig = buildRollupConfig(projectConfig);

try {
const build = await rollup(rollupConfig.inputOptions(options));
await build.generate(rollupConfig.outputOptions());
await build.write(rollupConfig.outputOptions());

const distDir = path.resolve(__dirname, '../../dist/static');
const distDir = path.resolve(projectConfig.benchmarkOutput, 'static');

const indexPath = path.resolve(distDir, 'index.html');
await asyncWrite(indexPath, html);
Expand Down
45 changes: 23 additions & 22 deletions packages/@best/frontend/server/static/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,29 @@ import replace from 'rollup-plugin-replace';
import { terser } from 'rollup-plugin-terser';
import { bestMocker, MockerOptions } from './mocker'
import * as rollup from 'rollup';
import { FrozenProjectConfig } from '@best/types';

const rollupConfig: {
export const buildRollupConfig = (projectConfig: FrozenProjectConfig): {
inputOptions: (options: MockerOptions) => rollup.InputOptions;
outputOptions: () => rollup.OutputOptions;
} = {
inputOptions: (options): rollup.InputOptions => ({
input: path.resolve(__dirname, '../../src/index.js'),
plugins: [
bestMocker(options),
replace({
'process.env.NODE_ENV': JSON.stringify('production')
}),
lwc(),
resolve(),
commonjs(),
terser()
]
}),
outputOptions: (): rollup.OutputOptions => ({
file: path.resolve(__dirname, '../../dist/static/bundle.js'),
format: 'iife'
})
}

export default rollupConfig;
} => {
return {
inputOptions: (options): rollup.InputOptions => ({
input: path.resolve(__dirname, '../../src/index.js'),
plugins: [
bestMocker(options),
replace({
'process.env.NODE_ENV': JSON.stringify('production')
}),
lwc(),
resolve(),
commonjs(),
terser()
]
}),
outputOptions: (): rollup.OutputOptions => ({
file: path.resolve(projectConfig.benchmarkOutput, 'static/bundle.js'),
format: 'iife'
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ export default `<!DOCTYPE html>
<script src="bundle.js"></script>
</head>
<body>
<my-app></my-app>
<view-app></view-app>
</body>
</html>`
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,13 @@
dependencies:
"@babel/helper-plugin-utils" "^7.0.0"

"@babel/plugin-syntax-dynamic-import@^7.2.0":
version "7.2.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-dynamic-import/-/plugin-syntax-dynamic-import-7.2.0.tgz#69c159ffaf4998122161ad8ebc5e6d1f55df8612"
integrity sha512-mVxuJ0YroI/h/tbFTPGZR8cv6ai+STMKNBq0f8hFxsxWjl94qqhsb+wXbpNMDPU3cfR1TIsVFzU3nXyZMqyK4w==
dependencies:
"@babel/helper-plugin-utils" "^7.0.0"

"@babel/plugin-syntax-object-rest-spread@^7.0.0", "@babel/plugin-syntax-object-rest-spread@^7.2.0":
version "7.2.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-object-rest-spread/-/plugin-syntax-object-rest-spread-7.2.0.tgz#3b7a3e733510c57e820b9142a6579ac8b0dfad2e"
Expand Down

0 comments on commit c1e7ff0

Please sign in to comment.