From 2738776e1593d9947e6582e5cadffef65262a21a Mon Sep 17 00:00:00 2001 From: Jordan Pittman Date: Fri, 5 Mar 2021 06:13:51 -0500 Subject: [PATCH 1/3] feat: support registering webpack build dependencies (#517) --- src/index.js | 4 +++ test/__snapshots__/loader.test.js.snap | 4 +++ test/loader.test.js | 49 ++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/src/index.js b/src/index.js index 9d359720..c6d37e72 100644 --- a/src/index.js +++ b/src/index.js @@ -124,6 +124,10 @@ export default async function loader(content, sourceMap, meta) { 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, diff --git a/test/__snapshots__/loader.test.js.snap b/test/__snapshots__/loader.test.js.snap index e63f9610..e26bce3e 100644 --- a/test/__snapshots__/loader.test.js.snap +++ b/test/__snapshots__/loader.test.js.snap @@ -106,6 +106,10 @@ Warning ] `; +exports[`loader should register dependencies using the "messages" API: errors 1`] = `Array []`; + +exports[`loader should register dependencies using the "messages" API: warnings 1`] = `Array []`; + exports[`loader should reuse PostCSS AST: css 1`] = ` "a { color: black; diff --git a/test/loader.test.js b/test/loader.test.js index 832d3880..10d41eee 100644 --- a/test/loader.test.js +++ b/test/loader.test.js @@ -1,6 +1,7 @@ import path from "path"; import postcss from "postcss"; +import { NormalModule } from "webpack"; import { compile, @@ -81,6 +82,54 @@ describe("loader", () => { expect(getErrors(stats)).toMatchSnapshot("errors"); }); + 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, + }); + }; + + let actualBuildInfo = null; + + const postcssPlugin = postcss.plugin("postcss-plugin", plugin); + 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"); + + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); + }); + it("should reuse PostCSS AST", async () => { const spy = jest.fn(); const compiler = getCompiler( From 9ce4972c6740c8dd82463fff0934987695ccf7d2 Mon Sep 17 00:00:00 2001 From: Alexey Lavinsky Date: Fri, 5 Mar 2021 16:16:53 +0300 Subject: [PATCH 2/3] feat: added support for registering `context` and `missing` dependencies (#518) --- README.md | 4 +-- src/index.js | 38 ++++++++++++++--------- test/loader.test.js | 75 +++++++++++++++++++++++---------------------- 3 files changed, 64 insertions(+), 53 deletions(-) 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"); From 55630b8ae5a4fa2547cc6b8c4f54b001c1e24b1a Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Fri, 5 Mar 2021 16:18:41 +0300 Subject: [PATCH 3/3] chore(release): 5.1.0 --- CHANGELOG.md | 7 +++++++ package-lock.json | 2 +- package.json | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 036fdaf9..4b801fa6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +## [5.1.0](https://github.com/webpack-contrib/postcss-loader/compare/v5.0.0...v5.1.0) (2021-03-05) + + +### Features + +* added support for registering `context`, `build` and `missing` dependencies ([#518](https://github.com/webpack-contrib/postcss-loader/issues/518)) ([9ce4972](https://github.com/webpack-contrib/postcss-loader/commit/9ce4972c6740c8dd82463fff0934987695ccf7d2)), please read [docs](https://github.com/webpack-contrib/postcss-loader#add-dependencies-contextdependencies-builddependencies-missingdependencies) + ## [5.0.0](https://github.com/webpack-contrib/postcss-loader/compare/v4.2.0...v5.0.0) (2021-02-02) diff --git a/package-lock.json b/package-lock.json index 4ff52c3d..e1fa0dbb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "postcss-loader", - "version": "5.0.0", + "version": "5.1.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index c9113aef..591b4b65 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "postcss-loader", - "version": "5.0.0", + "version": "5.1.0", "description": "PostCSS loader for webpack", "license": "MIT", "repository": "webpack-contrib/postcss-loader",