diff --git a/README.md b/README.md index d7e08807..3a4496b2 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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** diff --git a/src/index.js b/src/index.js index c6d37e72..f582df1e 100644 --- a/src/index.js +++ b/src/index.js @@ -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 + ); + } } } diff --git a/test/loader.test.js b/test/loader.test.js index 10d41eee..1c04f144 100644 --- a/test/loader.test.js +++ b/test/loader.test.js @@ -1,7 +1,6 @@ import path from "path"; import postcss from "postcss"; -import { NormalModule } from "webpack"; import { compile, @@ -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");