Skip to content

Commit

Permalink
fix: Fail compilation is bare module specifier is missing (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmdartus authored Jun 30, 2020
1 parent e90fb40 commit 0c63282
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
16 changes: 16 additions & 0 deletions packages/@best/builder/src/__tests__/best-build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,20 @@ describe('buildBenchmark', () => {
expect(loaded.some(file => file === entry)).toBe(true);
expect(transformed.some(file => file === entry)).toBe(true);
});

test(`throw if bare module specifiers can't be resolved`, async () => {
await expect(() => buildBenchmark(
path.resolve(__dirname, 'fixtures', 'error-missing-external', 'error-missing-external.js'),
{
benchmarkOutput: tempDir(),
projectName,
rootDir
},
GLOBAL_CONFIG,
MOCK_MESSAGER,
)).rejects.toHaveProperty(
'message',
expect.stringMatching(/'x\/missing' is imported by .*, but could not be resolved – treating it as an external dependency/)
)
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import 'x/missing';
25 changes: 15 additions & 10 deletions packages/@best/builder/src/build-benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
*/

import fs from 'fs';
import { rollup, ModuleFormat } from 'rollup';
import { rollup, OutputOptions } from 'rollup';
import path from 'path';
import crypto from 'crypto';
import mkdirp from "mkdirp";
import benchmarkRollup from './rollup-plugin-benchmark-import';
import generateHtml from './html-templating';
import { FrozenGlobalConfig, FrozenProjectConfig, ProjectConfigPlugin, BuildConfig } from '@best/types';

const BASE_ROLLUP_OUTPUT = { format: 'iife' as ModuleFormat };
const BASE_ROLLUP_OUTPUT: OutputOptions = { format: 'iife' };
const ROLLUP_CACHE = new Map();

function md5(data: string) {
Expand Down Expand Up @@ -59,20 +59,25 @@ export async function buildBenchmark(entry: string, projectConfig: FrozenProject
const benchmarkJSFileName = benchmarkName + ext;
const benchmarkProjectFolder = path.join(benchmarkOutput, projectName);

const rollupInputOpts = {
buildLogStream.log('Bundling benchmark files...');
const bundle = await rollup({
input: entry,
plugins: [benchmarkRollup(), ...addResolverPlugins(projectConfig.plugins)],
cache: ROLLUP_CACHE.get(projectName),
manualChunks: function () { /* guarantee one chunk */ return 'main_chunk'; }
};

buildLogStream.log('Bundling benchmark files...');
const bundle = await rollup(rollupInputOpts);
manualChunks: function () { /* guarantee one chunk */ return 'main_chunk'; },
onwarn(warning, warn) {
// Make compilation fail, if any bare module can't be resolved.
if (typeof warning === 'object' && warning.code === 'UNRESOLVED_IMPORT') {
throw new Error(warning.message);
}

warn(warning);
}
});
ROLLUP_CACHE.set(projectName, bundle.cache);

buildLogStream.log('Generating benchmark artifacts...');
const rollupOutputOpts = { ...BASE_ROLLUP_OUTPUT };
const { output } = await bundle.generate(rollupOutputOpts);
const { output } = await bundle.generate(BASE_ROLLUP_OUTPUT);
const benchmarkSource = output[0].code; // We don't do code splitting so the first one will be the one we want

// Benchmark artifacts vars
Expand Down

0 comments on commit 0c63282

Please sign in to comment.