Skip to content

Commit

Permalink
fix(best-build): Allow custom plugins (#84)
Browse files Browse the repository at this point in the history
* fix(best-build): Allowing custom plugins
* Fix messager, allow custom plugins
* Fixing tests
  • Loading branch information
diervo authored Feb 16, 2018
1 parent 21cc9b5 commit 8f77b1e
Show file tree
Hide file tree
Showing 8 changed files with 206,127 additions and 206,058 deletions.
412,097 changes: 206,065 additions & 206,032 deletions examples/simple_benchmark/src/__benchmarks__/mock_data.js

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions examples/simple_lwc_benchmark/best.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
module.exports = {
projectName: 'lwc-examples',
plugins: {
'rollup-plugin-lwc-compiler': {
plugins: [
'<rootDir>/custom-rollup-transformer/empty-example.js',
['rollup-plugin-lwc-compiler', {
rootDir: '<rootDir>/src/',
mode: 'prod', // We don't really need prod here since this is for test best itself
},
},
}]
],
benchmarkOnClient: false,
benchmarkRunner: '@best/runner-headless',

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// This is just an example
module.exports = function () {
return {
resolveId(importee, importer) {
// console.log('resolving: ', importee, importer);
}
};
};
10 changes: 4 additions & 6 deletions packages/best-build/src/__tests__/best-build.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const TEMP_DIR_PREFIX = 'best-test-';
const MOCK_MESSAGER = {
onBenchmarkBuildStart() {},
onBenchmarkBuildEnd() {},
logState() {}
};

function tempDir() {
Expand Down Expand Up @@ -51,6 +52,7 @@ describe('buildBenchmark', () => {
const messager = {
onBenchmarkBuildStart: jest.fn(),
onBenchmarkBuildEnd: jest.fn(),
logState: jest.fn
};

await buildBenchmark(
Expand Down Expand Up @@ -90,9 +92,7 @@ describe('buildBenchmark', () => {
path.resolve(__dirname, 'fixtures', 'single-file', 'single-file.js'),
{
cacheDirectory: tempDir(),
plugins: {
'build-plugin-opts': PLUGIN_OPTIONS,
},
plugins: [['build-plugin-opts', PLUGIN_OPTIONS]],
},
{},
MOCK_MESSAGER,
Expand Down Expand Up @@ -127,9 +127,7 @@ describe('buildBenchmark', () => {
path.resolve(__dirname, 'fixtures', 'single-file', 'single-file.js'),
{
cacheDirectory: tempDir(),
plugins: {
'build-plugin-hooks': true,
},
plugins: ['build-plugin-hooks'],
},
{},
MOCK_MESSAGER,
Expand Down
18 changes: 15 additions & 3 deletions packages/best-build/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ function addResolverPlugins({ plugins }) {
return [];
}

const pluginNames = Object.keys(plugins);
return pluginNames.map(pluginName => {
return require(pluginName)(plugins[pluginName]);
return plugins.map((plugin) => {
if (typeof plugin === 'string') {
return require(plugin)();
} else if (Array.isArray(plugin)) {
return require(plugin[0])(plugin[1]);
}

return [];
});
}

Expand All @@ -40,11 +45,15 @@ export async function buildBenchmark(entry, projectConfig, globalConfig, message
plugins: [benchmarkRollup(), ...addResolverPlugins(projectConfig)],
});

messager.logState('Bundling benchmark files...');

const bundle = await rollup(inputOptions);
const outputOptions = Object.assign({}, BASE_ROLLUP_OUTPUT, {
file: path.join(benchmarkFolder, benchmarkJSFileName),
});

messager.logState('Generating artifacts...');

const { code } = await bundle.generate(outputOptions);
await bundle.write(outputOptions);

Expand All @@ -53,6 +62,9 @@ export async function buildBenchmark(entry, projectConfig, globalConfig, message
benchmarkJS: `./${benchmarkJSFileName}`,
benchmarkName,
});

messager.logState('Saving artifacts...');

fs.writeFileSync(htmlPath, html, 'utf8');

messager.onBenchmarkBuildEnd(entry);
Expand Down
12 changes: 9 additions & 3 deletions packages/best-cli/src/run_best.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,15 @@ async function getBenchmarkTests(configs, globalConfig) {
}

async function buildBundleBenchmarks(benchmarksTests, globalConfig, messager) {
const bundle = await Promise.all(
benchmarksTests.map(async ({ matches, config }) => buildBenchmarks(matches, config, globalConfig, messager)),
);
const bundle = [];
// @dval: We don't paralelize here for now since this wouldn't give us much,
// Unless we do proper spawning on threads
for (const benchmarkTest of benchmarksTests) {
const { matches, config } = benchmarkTest;
const result = await buildBenchmarks(matches, config, globalConfig, messager);
bundle.push(result);
}

// Flatten the per-project benchmarks tests
return bundle.reduce((benchmarks, benchBundle) => {
benchmarks.push(...benchBundle);
Expand Down
17 changes: 9 additions & 8 deletions packages/best-config/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ function normalizeUnmockedModulePathPatterns(options, key) {
return options[key].map(pattern => replacePathSepForRegex(normalizeRootDirPattern(pattern, options.rootDir)));
}

function normalizeObjectPathPatterns(options, { rootDir }) {
function normalizeObjectPathPatterns(options, rootDir) {
return Object.keys(options).reduce((m, key) => {
const value = options[key];
if (typeof value === 'string') {
Expand All @@ -144,14 +144,15 @@ function normalizeObjectPathPatterns(options, { rootDir }) {
}, {});
}

function normalizePlugins(plugins, globalOptions) {
return Object.keys(plugins).reduce((m, plugin) => {
const pluginOptions = plugins[plugin];
if (pluginOptions) {
m[plugin] = normalizeObjectPathPatterns(pluginOptions, globalOptions);
function normalizePlugins(plugins, { rootDir }) {
return plugins.map((plugin) => {
if (typeof plugin === 'string') {
return normalizeRootDirPattern(plugin, rootDir);
} else if (Array.isArray(plugin)) {
return [plugin[0], normalizeObjectPathPatterns(plugin[1], rootDir)];
}
return m;
}, {});
return plugin;
});
}

function normalizeCommits([base, target]) {
Expand Down
14 changes: 12 additions & 2 deletions packages/best-messager/src/messager-build-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ const clearStream = buffer => {
height++;
}
}
return '\r\x1B[K\r\x1B[1A'.repeat(height);

return height > 1 ? '\r\x1B[K\r\x1B[1A'.repeat(height) : '';
};

export default class BuildStateMessager {
constructor(benchmarksBundle, globalConfig, outputStream) {
this._bufferStream = [];
this._currentState = '';

this._out = outputStream.write.bind(outputStream);
this._wrapStream(process.stderr);
Expand Down Expand Up @@ -94,6 +96,12 @@ export default class BuildStateMessager {
onBenchmarkBuildEnd(id) {
const bench = this._state.benchmarks[id];
bench.state = BUILD_STATE.DONE;
this._currentState = '';
this._update();
}

logState(state) {
this._currentState = state;
this._update();
}

Expand Down Expand Up @@ -122,12 +130,14 @@ export default class BuildStateMessager {

_write() {
const benchmarks = this._state.benchmarks;
const buffer = Object.keys(benchmarks).reduce((str, key) => {
let buffer = Object.keys(benchmarks).reduce((str, key) => {
const benchState = benchmarks[key];
str += printState(benchState.state) + printDisplayName(benchState.displayPath) + '\n';
return str;
}, '\n' + INIT_BUILD_TEXT);

buffer += this._currentState ? `\n Status: ${this._currentState} \n` : '\n';

this._state.clear = clearStream(buffer);
this._out(buffer);
}
Expand Down

0 comments on commit 8f77b1e

Please sign in to comment.