Skip to content

Commit 0933990

Browse files
feat: emit asset from messages
1 parent 9100905 commit 0933990

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

src/index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,13 @@ export default async function loader(content, sourceMap, meta = {}) {
191191
this.emitWarning(new Warning(warning));
192192
});
193193

194-
messages.forEach((msg) => {
195-
if (msg.type === 'dependency') {
196-
this.addDependency(msg.file);
194+
messages.forEach((message) => {
195+
if (message.type === 'dependency') {
196+
this.addDependency(message.file);
197+
}
198+
199+
if (message.type === 'asset' && message.content && message.file) {
200+
this.emitFile(message.file, message.content);
197201
}
198202
});
199203

test/__snapshots__/loader.test.js.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ SyntaxError
1616

1717
exports[`loader should emit Syntax Error: warnings 1`] = `Array []`;
1818

19+
exports[`loader should emit asset: errors 1`] = `Array []`;
20+
21+
exports[`loader should emit asset: warnings 1`] = `Array []`;
22+
1923
exports[`loader should emit warning: css 1`] = `
2024
"a { color: black }
2125
"

test/loader.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,28 @@ describe('loader', () => {
5555
expect(getWarnings(stats)).toMatchSnapshot('warnings');
5656
expect(getErrors(stats)).toMatchSnapshot('errors');
5757
});
58+
59+
it('should emit asset', async () => {
60+
const plugin = () => (css, result) => {
61+
result.messages.push({
62+
type: 'asset',
63+
file: 'sprite.svg',
64+
content: '<svg>...</svg>',
65+
plugin,
66+
});
67+
};
68+
69+
const postcssPlugin = postcss.plugin('postcss-assets', plugin);
70+
71+
const compiler = getCompiler('./css/index.js', {
72+
plugins: [postcssPlugin()],
73+
config: false,
74+
});
75+
const stats = await compile(compiler);
76+
77+
// eslint-disable-next-line no-underscore-dangle
78+
expect(stats.compilation.assets['sprite.svg']).toBeDefined();
79+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
80+
expect(getErrors(stats)).toMatchSnapshot('errors');
81+
});
5882
});

0 commit comments

Comments
 (0)