Skip to content

Commit 9ce4972

Browse files
feat: added support for registering context and missing dependencies (#518)
1 parent 2738776 commit 9ce4972

File tree

3 files changed

+64
-53
lines changed

3 files changed

+64
-53
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,7 @@ module.exports = {
934934
};
935935
```
936936

937-
### Add dependencies
937+
### Add dependencies, contextDependencies, buildDependencies, missingDependencies
938938

939939
The dependencies are necessary for webpack to understand when it needs to run recompilation on the changed files.
940940

@@ -944,7 +944,7 @@ There are two way to add dependencies:
944944

945945
The message should contain the following fields:
946946

947-
- `type` = `dependency` - Message type (require, should be equal `dependency`)
947+
- `type` = `dependency` - Message type (require, should be equal `dependency`, `context-dependency`, `build-dependency` or `missing-dependency`)
948948
- `file` - absolute file path (require)
949949

950950
**webpack.config.js**

src/index.js

+23-15
Original file line numberDiff line numberDiff line change
@@ -120,21 +120,29 @@ export default async function loader(content, sourceMap, meta) {
120120
}
121121

122122
for (const message of result.messages) {
123-
if (message.type === "dependency") {
124-
this.addDependency(message.file);
125-
}
126-
127-
if (message.type === "build-dependency") {
128-
this.addBuildDependency(message.file);
129-
}
130-
131-
if (message.type === "asset" && message.content && message.file) {
132-
this.emitFile(
133-
message.file,
134-
message.content,
135-
message.sourceMap,
136-
message.info
137-
);
123+
// eslint-disable-next-line default-case
124+
switch (message.type) {
125+
case "dependency":
126+
this.addDependency(message.file);
127+
break;
128+
case "build-dependency":
129+
this.addBuildDependency(message.file);
130+
break;
131+
case "missing-dependency":
132+
this.addMissingDependency(message.file);
133+
break;
134+
case "context-dependency":
135+
this.addContextDependency(message.file);
136+
break;
137+
case "asset":
138+
if (message.content && message.file) {
139+
this.emitFile(
140+
message.file,
141+
message.content,
142+
message.sourceMap,
143+
message.info
144+
);
145+
}
138146
}
139147
}
140148

test/loader.test.js

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

33
import postcss from "postcss";
4-
import { NormalModule } from "webpack";
54

65
import {
76
compile,
@@ -84,47 +83,51 @@ describe("loader", () => {
8483

8584
it('should register dependencies using the "messages" API', async () => {
8685
const plugin = () => (css, result) => {
87-
result.messages.push({
88-
type: "build-dependency",
89-
file: "build-dep.html",
90-
content: "",
91-
plugin,
92-
});
86+
result.messages.push(
87+
{
88+
type: "build-dependency",
89+
file: path.resolve(__dirname, "fixtures", "build-dep.html"),
90+
content: "",
91+
plugin,
92+
},
93+
{
94+
type: "missing-dependency",
95+
file: path.resolve(__dirname, "fixtures", "missing-dep.html"),
96+
content: "",
97+
plugin,
98+
},
99+
{
100+
type: "context-dependency",
101+
file: path.resolve(__dirname, "fixtures", "deps"),
102+
content: "",
103+
plugin,
104+
}
105+
);
93106
};
94107

95-
let actualBuildInfo = null;
96-
97108
const postcssPlugin = postcss.plugin("postcss-plugin", plugin);
98-
const compiler = getCompiler(
99-
"./css/index.js",
100-
{
101-
postcssOptions: {
102-
plugins: [postcssPlugin()],
103-
},
109+
const compiler = getCompiler("./css/index.js", {
110+
postcssOptions: {
111+
plugins: [postcssPlugin()],
104112
},
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-
);
113+
});
123114

124115
const stats = await compile(compiler);
125-
126-
const buildDependencies = [...actualBuildInfo.buildDependencies];
127-
expect(buildDependencies).toContain("build-dep.html");
116+
const {
117+
contextDependencies,
118+
missingDependencies,
119+
buildDependencies,
120+
} = stats.compilation;
121+
122+
expect(contextDependencies).toContain(
123+
path.resolve(__dirname, "fixtures", "deps")
124+
);
125+
expect(missingDependencies).toContain(
126+
path.resolve(__dirname, "fixtures", "missing-dep.html")
127+
);
128+
expect(buildDependencies).toContain(
129+
path.resolve(__dirname, "fixtures", "build-dep.html")
130+
);
128131

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

0 commit comments

Comments
 (0)