Skip to content

Commit 2738776

Browse files
feat: support registering webpack build dependencies (#517)
1 parent 6de5ef8 commit 2738776

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

src/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ export default async function loader(content, sourceMap, meta) {
124124
this.addDependency(message.file);
125125
}
126126

127+
if (message.type === "build-dependency") {
128+
this.addBuildDependency(message.file);
129+
}
130+
127131
if (message.type === "asset" && message.content && message.file) {
128132
this.emitFile(
129133
message.file,

test/__snapshots__/loader.test.js.snap

+4
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ Warning
106106
]
107107
`;
108108
109+
exports[`loader should register dependencies using the "messages" API: errors 1`] = `Array []`;
110+
111+
exports[`loader should register dependencies using the "messages" API: warnings 1`] = `Array []`;
112+
109113
exports[`loader should reuse PostCSS AST: css 1`] = `
110114
"a {
111115
color: black;

test/loader.test.js

+49
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from "path";
22

33
import postcss from "postcss";
4+
import { NormalModule } from "webpack";
45

56
import {
67
compile,
@@ -81,6 +82,54 @@ describe("loader", () => {
8182
expect(getErrors(stats)).toMatchSnapshot("errors");
8283
});
8384

85+
it('should register dependencies using the "messages" API', async () => {
86+
const plugin = () => (css, result) => {
87+
result.messages.push({
88+
type: "build-dependency",
89+
file: "build-dep.html",
90+
content: "",
91+
plugin,
92+
});
93+
};
94+
95+
let actualBuildInfo = null;
96+
97+
const postcssPlugin = postcss.plugin("postcss-plugin", plugin);
98+
const compiler = getCompiler(
99+
"./css/index.js",
100+
{
101+
postcssOptions: {
102+
plugins: [postcssPlugin()],
103+
},
104+
},
105+
{
106+
plugins: [
107+
{
108+
/** @param {import("webpack").Compiler} compiler */
109+
apply(wpcompiler) {
110+
wpcompiler.hooks.compilation.tap("plugin", (compilation) => {
111+
NormalModule.getCompilationHooks(compilation).beforeLoaders.tap(
112+
"plugin",
113+
(_1, module) => {
114+
actualBuildInfo = module.buildInfo;
115+
}
116+
);
117+
});
118+
},
119+
},
120+
],
121+
}
122+
);
123+
124+
const stats = await compile(compiler);
125+
126+
const buildDependencies = [...actualBuildInfo.buildDependencies];
127+
expect(buildDependencies).toContain("build-dep.html");
128+
129+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
130+
expect(getErrors(stats)).toMatchSnapshot("errors");
131+
});
132+
84133
it("should reuse PostCSS AST", async () => {
85134
const spy = jest.fn();
86135
const compiler = getCompiler(

0 commit comments

Comments
 (0)