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

Feat: added support for registering context and missing dependencies #518

Merged
merged 2 commits into from
Mar 5, 2021
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: 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