Skip to content
This repository was archived by the owner on Jun 24, 2024. It is now read-only.

Commit c274514

Browse files
committed
fix: re-emit error when bundling
1 parent ecbb456 commit c274514

File tree

4 files changed

+42
-9
lines changed

4 files changed

+42
-9
lines changed

lib/writer.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,17 @@ module.exports = class Writer extends EventEmitter {
6969
return cssStream;
7070
}
7171

72-
return cssStream.pipe(
73-
new Transform({
74-
objectMode: true,
75-
transform(chunk, enc, next) {
76-
next(null, chunk.content);
77-
},
78-
})
72+
const transformStream = new Transform({
73+
objectMode: true,
74+
transform(chunk, enc, next) {
75+
next(null, chunk.content);
76+
},
77+
});
78+
79+
cssStream.on('error', (...args) =>
80+
transformStream.emit('error', ...args)
7981
);
82+
83+
return cssStream.pipe(transformStream);
8084
}
8185
};

test/__snapshots__/writer.test.js.snap

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,10 @@ exports[`new Writer([filePath]) ensures valid filePath provided 1`] = `"Expected
1313
exports[`new Writer([filePath]) ensures valid filePaths provided in array 1`] = `"Expected 'file' (fake.css) to be an absolute path to a file but it was not"`;
1414

1515
exports[`new Writer(filePath) relative paths throw error 1`] = `"Expected 'file' (./test-assets/my-module-1/main.css) to be an absolute path to a file but it was not"`;
16+
17+
exports[`underlying writer error bubbles up 1`] = `
18+
"Failed to find 'some-random-file.css'
19+
in [
20+
<root>/test-assets
21+
]"
22+
`;

test/test-assets/bogus.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "~some-random-file.css";

test/writer.test.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,18 @@ const { hasher } = require('@asset-pipe/common');
66
const { identifyCssModule, bundleCssModule } = require('../lib/util.js');
77
const Writer = require('..');
88

9-
beforeEach(() => {
10-
jest.clearAllMocks();
9+
afterEach(() => {
10+
jest.unmock('../lib/util.js');
1111
jest.resetModules();
1212
});
1313

14+
function clean(message) {
15+
return message
16+
.split('\n')
17+
.map(line => line.replace(__dirname, '<root>'))
18+
.join('\n');
19+
}
20+
1421
test('identifyCssModule(filePath)', async () => {
1522
expect.assertions(1);
1623
const filePath = path.join(__dirname, 'test-assets/my-module-1/main.css');
@@ -262,6 +269,20 @@ test('writer emits error', done => {
262269
writer.on('data', () => {});
263270
});
264271

272+
test('underlying writer error bubbles up', done => {
273+
expect.assertions(1);
274+
const CssWriter = require('..');
275+
const filePath = path.join(__dirname, 'test-assets/bogus.css');
276+
277+
const writer = new CssWriter(filePath, true).bundle();
278+
279+
writer.on('error', error => {
280+
expect(clean(error.message)).toMatchSnapshot();
281+
done();
282+
});
283+
writer.on('data', () => {});
284+
});
285+
265286
test('new Writer() emits nothing but does not break', done => {
266287
const writer = new Writer().bundle();
267288
const items = [];

0 commit comments

Comments
 (0)