Skip to content

Commit

Permalink
feat: added support for registering context and missing dependenc…
Browse files Browse the repository at this point in the history
…ies (#518)
  • Loading branch information
cap-Bernardito committed Mar 5, 2021
1 parent 2738776 commit 9ce4972
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 53 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ module.exports = {
};
```

### Add dependencies
### Add dependencies, contextDependencies, buildDependencies, missingDependencies

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

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

The message should contain the following fields:

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

**webpack.config.js**
Expand Down
38 changes: 23 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,29 @@ export default async function loader(content, sourceMap, meta) {
}

for (const message of result.messages) {
if (message.type === "dependency") {
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,
message.content,
message.sourceMap,
message.info
);
// eslint-disable-next-line default-case
switch (message.type) {
case "dependency":
this.addDependency(message.file);
break;
case "build-dependency":
this.addBuildDependency(message.file);
break;
case "missing-dependency":
this.addMissingDependency(message.file);
break;
case "context-dependency":
this.addContextDependency(message.file);
break;
case "asset":
if (message.content && message.file) {
this.emitFile(
message.file,
message.content,
message.sourceMap,
message.info
);
}
}
}

Expand Down
75 changes: 39 additions & 36 deletions test/loader.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import path from "path";

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

import {
compile,
Expand Down Expand Up @@ -84,47 +83,51 @@ describe("loader", () => {

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,
});
result.messages.push(
{
type: "build-dependency",
file: path.resolve(__dirname, "fixtures", "build-dep.html"),
content: "",
plugin,
},
{
type: "missing-dependency",
file: path.resolve(__dirname, "fixtures", "missing-dep.html"),
content: "",
plugin,
},
{
type: "context-dependency",
file: path.resolve(__dirname, "fixtures", "deps"),
content: "",
plugin,
}
);
};

let actualBuildInfo = null;

const postcssPlugin = postcss.plugin("postcss-plugin", plugin);
const compiler = getCompiler(
"./css/index.js",
{
postcssOptions: {
plugins: [postcssPlugin()],
},
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");
const {
contextDependencies,
missingDependencies,
buildDependencies,
} = stats.compilation;

expect(contextDependencies).toContain(
path.resolve(__dirname, "fixtures", "deps")
);
expect(missingDependencies).toContain(
path.resolve(__dirname, "fixtures", "missing-dep.html")
);
expect(buildDependencies).toContain(
path.resolve(__dirname, "fixtures", "build-dep.html")
);

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

0 comments on commit 9ce4972

Please sign in to comment.