Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/frank-cougars-go.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@callstack/repack": patch
---

Handle previous compiler errors and abort bundling in `RepackOutputPlugin`
7 changes: 7 additions & 0 deletions packages/repack/src/plugins/OutputPlugin/OutputPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@ export class OutputPlugin {
);

compiler.hooks.done.tapPromise('RepackOutputPlugin', async (stats) => {
if (stats.hasErrors()) {
throw new Error(
'[RepackOutputPlugin] Compilation failed:\n' +
stats.toString('errors-only')
);
}

const compilationStats = stats.toJson({
all: false,
assets: true,
Expand Down
69 changes: 69 additions & 0 deletions packages/repack/src/plugins/__tests__/OutputPlugin.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,69 @@
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import {
type EntryNormalized,
ModuleFilenameHelpers,
type StatsChunk,
rspack,
} from '@rspack/core';
import RspackVirtualModulePlugin from 'rspack-plugin-virtual-module';
import {
OutputPlugin,
type OutputPluginConfig,
} from '../OutputPlugin/index.js';

async function testCompile(code: string, expectThrow?: string) {
// Create a temporary directory to write files to.
const tempDir = await fs.mkdtemp(
path.join(os.tmpdir(), 'output-plugin-test-')
);

// And then run the compiler with the provided code.
try {
const compiler = rspack({
context: tempDir,
mode: 'production',
devtool: false,
entry: 'index.js',
output: {
filename: 'index.bundle',
path: path.join(tempDir, 'out'),
},
plugins: [
new OutputPlugin({
context: tempDir,
platform: 'ios',
output: {},
}),
new RspackVirtualModulePlugin({
'index.js': code,
}),
],
});

const promise = new Promise<void>((resolve, reject) =>
compiler.run((error, _stats) => {
if (error) {
reject(error);
} else {
resolve();
}
})
);

// Depending on whether we expect an error or not, assert accordingly.
if (expectThrow) {
await expect(promise).rejects.toThrow(expectThrow);
} else {
await expect(promise).resolves.not.toThrow();
}
} finally {
// Delete the temporary directory after the test
await fs.rm(tempDir, { recursive: true, force: true });
}
}

const makeChunk = ({
name,
entry = false,
Expand Down Expand Up @@ -204,4 +260,17 @@ describe('OutputPlugin', () => {
});
});
});

describe('apply', () => {
it('should throw an error when compilation has errors', async () => {
await testCompile(
'const x = {{{',
'[RepackOutputPlugin] Compilation failed:'
);
});

it('should complete successfully when compilation succeeds', async () => {
await testCompile('const x = {};');
});
});
});