Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fail compilation is bare module specifier is missing #233

Merged
merged 1 commit into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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