From 0c6328255681a6ebc66b32d446132a7bb31dee64 Mon Sep 17 00:00:00 2001 From: Pierre-Marie Dartus Date: Tue, 30 Jun 2020 17:43:09 +0200 Subject: [PATCH] fix: Fail compilation is bare module specifier is missing (#233) --- .../builder/src/__tests__/best-build.spec.ts | 16 ++++++++++++ .../error-missing-external.js | 1 + packages/@best/builder/src/build-benchmark.ts | 25 +++++++++++-------- 3 files changed, 32 insertions(+), 10 deletions(-) create mode 100644 packages/@best/builder/src/__tests__/fixtures/error-missing-external/error-missing-external.js diff --git a/packages/@best/builder/src/__tests__/best-build.spec.ts b/packages/@best/builder/src/__tests__/best-build.spec.ts index 3337f82d..40d02c56 100644 --- a/packages/@best/builder/src/__tests__/best-build.spec.ts +++ b/packages/@best/builder/src/__tests__/best-build.spec.ts @@ -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/) + ) + }); }); diff --git a/packages/@best/builder/src/__tests__/fixtures/error-missing-external/error-missing-external.js b/packages/@best/builder/src/__tests__/fixtures/error-missing-external/error-missing-external.js new file mode 100644 index 00000000..9f9b3435 --- /dev/null +++ b/packages/@best/builder/src/__tests__/fixtures/error-missing-external/error-missing-external.js @@ -0,0 +1 @@ +import 'x/missing'; diff --git a/packages/@best/builder/src/build-benchmark.ts b/packages/@best/builder/src/build-benchmark.ts index cd1eed31..42b78db6 100644 --- a/packages/@best/builder/src/build-benchmark.ts +++ b/packages/@best/builder/src/build-benchmark.ts @@ -6,7 +6,7 @@ */ 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"; @@ -14,7 +14,7 @@ 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) { @@ -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