Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for registering webpack build dependencies #517

Merged
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
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ export default async function loader(content, sourceMap, meta) {
this.addDependency(message.file);
}

if (message.type === "build-dependency") {
this.addBuildDependency(message.file);
}

if (message.type === "asset" && message.content && message.file) {
this.emitFile(
message.file,
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 @@ -106,6 +106,10 @@ Warning
]
`;

exports[`loader should register dependencies using the "messages" API: errors 1`] = `Array []`;

exports[`loader should register dependencies using the "messages" API: warnings 1`] = `Array []`;

exports[`loader should reuse PostCSS AST: css 1`] = `
"a {
color: black;
Expand Down
49 changes: 49 additions & 0 deletions test/loader.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from "path";

import postcss from "postcss";
import { NormalModule } from "webpack";

import {
compile,
Expand Down Expand Up @@ -81,6 +82,54 @@ describe("loader", () => {
expect(getErrors(stats)).toMatchSnapshot("errors");
});

it('should register dependencies using the "messages" API', async () => {
const plugin = () => (css, result) => {
result.messages.push({
type: "build-dependency",
file: "build-dep.html",
content: "",
plugin,
});
};

let actualBuildInfo = null;

const postcssPlugin = postcss.plugin("postcss-plugin", plugin);
const compiler = getCompiler(
"./css/index.js",
{
postcssOptions: {
plugins: [postcssPlugin()],
},
},
{
plugins: [
{
/** @param {import("webpack").Compiler} compiler */
apply(wpcompiler) {
wpcompiler.hooks.compilation.tap("plugin", (compilation) => {
NormalModule.getCompilationHooks(compilation).beforeLoaders.tap(
"plugin",
(_1, module) => {
actualBuildInfo = module.buildInfo;
}
);
});
},
},
],
}
);

const stats = await compile(compiler);

const buildDependencies = [...actualBuildInfo.buildDependencies];
expect(buildDependencies).toContain("build-dep.html");

expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});

it("should reuse PostCSS AST", async () => {
const spy = jest.fn();
const compiler = getCompiler(
Expand Down