Skip to content

Commit

Permalink
feat: emit asset from messages (#443)
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed Aug 17, 2020
1 parent 9100905 commit e966ab9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,18 @@ export default async function loader(content, sourceMap, meta = {}) {
this.emitWarning(new Warning(warning));
});

messages.forEach((msg) => {
if (msg.type === 'dependency') {
this.addDependency(msg.file);
messages.forEach((message) => {
if (message.type === 'dependency') {
this.addDependency(message.file);
}

if (message.type === 'asset' && message.content && message.file) {
this.emitFile(
message.file,
message.content,
message.sourceMap,
message.info
);
}
});

Expand Down
4 changes: 4 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ SyntaxError

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

exports[`loader should emit asset: errors 1`] = `Array []`;

exports[`loader should emit asset: warnings 1`] = `Array []`;

exports[`loader should emit warning: css 1`] = `
"a { color: black }
"
Expand Down
24 changes: 24 additions & 0 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,28 @@ describe('loader', () => {
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('should emit asset', async () => {
const plugin = () => (css, result) => {
result.messages.push({
type: 'asset',
file: 'sprite.svg',
content: '<svg>...</svg>',
plugin,
});
};

const postcssPlugin = postcss.plugin('postcss-assets', plugin);

const compiler = getCompiler('./css/index.js', {
plugins: [postcssPlugin()],
config: false,
});
const stats = await compile(compiler);

// eslint-disable-next-line no-underscore-dangle
expect(stats.compilation.assets['sprite.svg']).toBeDefined();
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});
});

0 comments on commit e966ab9

Please sign in to comment.