From 67977d43c1b2fb7517c88285ed3e87357efc5e39 Mon Sep 17 00:00:00 2001 From: sheo13666q <4567934+alexander-akait@users.noreply.github.com> Date: Fri, 20 Nov 2026 09:35:44 +0000 Subject: [PATCH] perf: pass a precompiled options check to getOptions Generate src/options.check.js with `schema-utils precompile --minify` and pass it to `this.getOptions`. Valid options then skip loading and compiling the schema on webpack versions that accept the check; older webpack ignores it. --- .changeset/precompiled-options-check.md | 5 + .cspell.json | 3 +- .prettierignore | 3 +- eslint.config.mjs | 2 + package-lock.json | 297 +++++++----------- package.json | 9 +- src/index.js | 3 +- src/options.check.js | 7 + .../validate-options.test.js.snap | 16 +- test/validate-options.test.js | 33 ++ types/options.check.d.ts | 25 ++ 11 files changed, 212 insertions(+), 191 deletions(-) create mode 100644 .changeset/precompiled-options-check.md create mode 100644 src/options.check.js create mode 100644 types/options.check.d.ts diff --git a/.changeset/precompiled-options-check.md b/.changeset/precompiled-options-check.md new file mode 100644 index 00000000..eade35e0 --- /dev/null +++ b/.changeset/precompiled-options-check.md @@ -0,0 +1,5 @@ +--- +"less-loader": patch +--- + +Pass a precompiled options check to `this.getOptions`, so webpack versions that accept it skip compiling the options schema. diff --git a/.cspell.json b/.cspell.json index dfbea4b5..a4f4ccaf 100644 --- a/.cspell.json +++ b/.cspell.json @@ -25,6 +25,7 @@ "/test/fixtures/generated-3.less", "node_modules", "coverage", - "*.log" + "*.log", + "src/options.check.js" ] } diff --git a/.prettierignore b/.prettierignore index f8f1e312..c02b0e85 100644 --- a/.prettierignore +++ b/.prettierignore @@ -2,4 +2,5 @@ /dist /node_modules /test/fixtures -CHANGELOG.md \ No newline at end of file +CHANGELOG.md +/src/options.check.js diff --git a/eslint.config.mjs b/eslint.config.mjs index 6bf3643f..cd79fa37 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -2,6 +2,8 @@ import { defineConfig } from "eslint/config"; import configs from "eslint-config-webpack/configs.js"; export default defineConfig([ + // Generated by `schema-utils precompile` + { ignores: ["src/options.check.js"] }, { extends: [configs["node-recommended"]], }, diff --git a/package-lock.json b/package-lock.json index 6ac5d25d..1870c567 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "less-loader", - "version": "13.0.0", + "version": "13.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "less-loader", - "version": "13.0.0", + "version": "13.1.0", "license": "MIT", "dependencies": { "@types/less": "^3.0.8" @@ -30,8 +30,9 @@ "memfs": "^4.57.2", "npm-run-all": "^4.1.5", "prettier": "^3.8.3", + "schema-utils": "^4.8.5", "typescript": "^6.0.3", - "webpack": "^5.107.0" + "webpack": "^5.113.0" }, "engines": { "node": ">= 22.11.0" @@ -4359,19 +4360,6 @@ "node": ">=0.4.0" } }, - "node_modules/acorn-import-phases": { - "version": "1.0.4", - "resolved": "https://registry.npmjs.org/acorn-import-phases/-/acorn-import-phases-1.0.4.tgz", - "integrity": "sha512-wKmbr/DDiIXzEOiWrTTUcDm24kQ2vGfZQvM2fwg2vXqR5uW6aapr7ObPtj1th32b9u90/Pf4AItvdTh42fBmVQ==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=10.13.0" - }, - "peerDependencies": { - "acorn": "^8.14.0" - } - }, "node_modules/acorn-jsx": { "version": "5.3.2", "resolved": "https://registry.npmjs.org/acorn-jsx/-/acorn-jsx-5.3.2.tgz", @@ -4400,9 +4388,9 @@ } }, "node_modules/ajv-formats": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/ajv-formats/-/ajv-formats-2.1.1.tgz", - "integrity": "sha512-Wx0Kx52hxE7C18hkMEggYlEifqWZtYaRgouJor+WMdPnQyEK13vgEWyVNup7SoeeoLMsr4kf5h6dOW11I15MUA==", + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/ajv-formats/-/ajv-formats-3.0.1.tgz", + "integrity": "sha512-8iUql50EUR+uUcdRQ3HDqa6EVyo3docL8g5WJ3FNcWmu62IbkGUue/pEyLBW8VGKKucTPgqeks4fIU1DA4yowQ==", "dev": true, "license": "MIT", "dependencies": { @@ -6053,9 +6041,9 @@ "license": "MIT" }, "node_modules/enhanced-resolve": { - "version": "5.21.5", - "resolved": "https://registry.npmjs.org/enhanced-resolve/-/enhanced-resolve-5.21.5.tgz", - "integrity": "sha512-mLCNbrQli11K1ySUmuNt4ZUB3OpGIDq4q2vTBTf5cL2lpsRjI9QKqSD0ndjW8FyvcW/Jj46gMe9syyHAsvMa/A==", + "version": "5.27.0", + "resolved": "https://registry.npmjs.org/enhanced-resolve/-/enhanced-resolve-5.27.0.tgz", + "integrity": "sha512-Do6BB2xzlcc/odFg+XEYyneAx4T/WfywO71yw9ePS2tVfRsf7JVR4mUeELadIeHzixkzbG5DpluiEFPi2WGfYQ==", "dev": true, "license": "MIT", "dependencies": { @@ -6274,13 +6262,6 @@ "node": ">= 0.4" } }, - "node_modules/es-module-lexer": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/es-module-lexer/-/es-module-lexer-2.1.0.tgz", - "integrity": "sha512-n27zTYMjYu1aj4MjCWzSP7G9r75utsaoc8m61weK+W8JMBGGQybd43GstCXZ3WNmSFtGT9wi59qQTW6mhTR5LQ==", - "dev": true, - "license": "MIT" - }, "node_modules/es-object-atoms": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/es-object-atoms/-/es-object-atoms-1.1.1.tgz", @@ -7806,13 +7787,6 @@ "tslib": "2" } }, - "node_modules/glob-to-regexp": { - "version": "0.4.1", - "resolved": "https://registry.npmjs.org/glob-to-regexp/-/glob-to-regexp-0.4.1.tgz", - "integrity": "sha512-lkX1HJXwyMcprw/5YUZc2s7DrpAiHB21/V+E1rHUrVNokkvB6bqMzT0VfV6/86ZNabt1k14YOIaT7nDvOX3Iiw==", - "dev": true, - "license": "BSD-2-Clause" - }, "node_modules/global-directory": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/global-directory/-/global-directory-5.0.0.tgz", @@ -9652,20 +9626,6 @@ "node": ">=4" } }, - "node_modules/loader-runner": { - "version": "4.3.2", - "resolved": "https://registry.npmjs.org/loader-runner/-/loader-runner-4.3.2.tgz", - "integrity": "sha512-DFEqQ3ihfS9blba08cLfYf1NRAIEm+dDjic073DRDc3/JspI/8wYmtDsHwd3+4hwvdxSK7PGaElfTmm0awWJ4w==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=6.11.5" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/webpack" - } - }, "node_modules/locate-path": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-6.0.0.tgz", @@ -10927,6 +10887,78 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/minimizer-webpack-plugin": { + "version": "5.14.0", + "resolved": "https://registry.npmjs.org/minimizer-webpack-plugin/-/minimizer-webpack-plugin-5.14.0.tgz", + "integrity": "sha512-PnvQXCJxhgH2ZVVO7Lj0sM6q8L1XGxnUvd8wVQIRwi8khMCLq5wg0Ngxe44bcuZL2FSN81RdVE7YHEVzTKsQDQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "jest-worker": "^27.4.5", + "schema-utils": "^4.6.0", + "terser": "^5.51.0" + }, + "engines": { + "node": ">= 10.13.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/webpack" + }, + "peerDependencies": { + "webpack": "^5.1.0" + }, + "peerDependenciesMeta": { + "@minify-html/node": { + "optional": true + }, + "@napi-rs/image": { + "optional": true + }, + "@swc/core": { + "optional": true + }, + "@swc/css": { + "optional": true + }, + "@swc/html": { + "optional": true + }, + "clean-css": { + "optional": true + }, + "cssnano": { + "optional": true + }, + "csso": { + "optional": true + }, + "esbuild": { + "optional": true + }, + "html-minifier-terser": { + "optional": true + }, + "imagemin": { + "optional": true + }, + "lightningcss": { + "optional": true + }, + "postcss": { + "optional": true + }, + "sharp": { + "optional": true + }, + "svgo": { + "optional": true + }, + "uglify-js": { + "optional": true + } + } + }, "node_modules/mixin-deep": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/mixin-deep/-/mixin-deep-1.3.2.tgz", @@ -11006,13 +11038,6 @@ "node": ">= 4.4.x" } }, - "node_modules/neo-async": { - "version": "2.6.2", - "resolved": "https://registry.npmjs.org/neo-async/-/neo-async-2.6.2.tgz", - "integrity": "sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==", - "dev": true, - "license": "MIT" - }, "node_modules/nice-try": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/nice-try/-/nice-try-1.0.5.tgz", @@ -12386,23 +12411,34 @@ } }, "node_modules/schema-utils": { - "version": "4.3.3", - "resolved": "https://registry.npmjs.org/schema-utils/-/schema-utils-4.3.3.tgz", - "integrity": "sha512-eflK8wEtyOE6+hsaRVPxvUKYCpRgzLqDTb8krvAsRIwOGlHoSgYLgBXoubGgLd2fT41/OUYdb48v4k4WWHQurA==", + "version": "4.8.5", + "resolved": "https://registry.npmjs.org/schema-utils/-/schema-utils-4.8.5.tgz", + "integrity": "sha512-K1RRP7aTUAY2lGXNzPwjMK6AMMzbETdpF3pTPMEnH/RrpmPx6K1X47/gSebrV+1I9pRcgpwZwqpYBlyZAsliiw==", "dev": true, "license": "MIT", "dependencies": { - "@types/json-schema": "^7.0.9", - "ajv": "^8.9.0", - "ajv-formats": "^2.1.1", + "@types/json-schema": "^7.0.15", + "ajv": "^8.20.0", + "ajv-formats": "^3.0.1", "ajv-keywords": "^5.1.0" }, + "bin": { + "schema-utils": "bin/schema-utils.js" + }, "engines": { "node": ">= 10.13.0" }, "funding": { "type": "opencollective", "url": "https://opencollective.com/webpack" + }, + "peerDependencies": { + "webpack": "^5.0.0" + }, + "peerDependenciesMeta": { + "webpack": { + "optional": true + } } }, "node_modules/semver": { @@ -13293,9 +13329,9 @@ } }, "node_modules/tapable": { - "version": "2.3.3", - "resolved": "https://registry.npmjs.org/tapable/-/tapable-2.3.3.tgz", - "integrity": "sha512-uxc/zpqFg6x7C8vOE7lh6Lbda8eEL9zmVm/PLeTPBRhh1xCgdWaQ+J1CUieGpIfm2HdtsUpRv+HshiasBMcc6A==", + "version": "2.3.4", + "resolved": "https://registry.npmjs.org/tapable/-/tapable-2.3.4.tgz", + "integrity": "sha512-NHFiWnAbMJxNNpBwiO7jJVGDuqMxT9/oV8v57RR+fgBQShkJil5inGD8/xWy0hjLyHQyX1tu27QANhlz3KBqCg==", "dev": true, "license": "MIT", "engines": { @@ -13320,9 +13356,9 @@ } }, "node_modules/terser": { - "version": "5.47.1", - "resolved": "https://registry.npmjs.org/terser/-/terser-5.47.1.tgz", - "integrity": "sha512-tPbLXTI6ohPASb/1YViL428oEHu6/qv1OxqYnfaonVCFHqx4+wCd95pHrQWsL5X4pl90CTyW9piSAsS2L0VoMw==", + "version": "5.51.3", + "resolved": "https://registry.npmjs.org/terser/-/terser-5.51.3.tgz", + "integrity": "sha512-j5jPZZ6Sz0Eg2unusKhdRzUhUlHXJ3Bw2AkIXyJ6AqajWwihUlZOdJS+KSnZzmtVk6mFGdm3hHg0+M2sgyEz5Q==", "dev": true, "license": "BSD-2-Clause", "dependencies": { @@ -13338,67 +13374,6 @@ "node": ">=10" } }, - "node_modules/terser-webpack-plugin": { - "version": "5.6.0", - "resolved": "https://registry.npmjs.org/terser-webpack-plugin/-/terser-webpack-plugin-5.6.0.tgz", - "integrity": "sha512-Eum+5ajkaOhf5KbM26osvv21kLD7BaGqQ1UA4Ami4arYwylmGUQTgHFpHDdmJod1q4QXa66p0to/FBKID+J1vA==", - "dev": true, - "license": "MIT", - "dependencies": { - "@jridgewell/trace-mapping": "^0.3.25", - "jest-worker": "^27.4.5", - "schema-utils": "^4.3.0", - "terser": "^5.31.1" - }, - "engines": { - "node": ">= 10.13.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/webpack" - }, - "peerDependencies": { - "webpack": "^5.1.0" - }, - "peerDependenciesMeta": { - "@minify-html/node": { - "optional": true - }, - "@swc/core": { - "optional": true - }, - "@swc/css": { - "optional": true - }, - "@swc/html": { - "optional": true - }, - "clean-css": { - "optional": true - }, - "cssnano": { - "optional": true - }, - "csso": { - "optional": true - }, - "esbuild": { - "optional": true - }, - "html-minifier-terser": { - "optional": true - }, - "lightningcss": { - "optional": true - }, - "postcss": { - "optional": true - }, - "uglify-js": { - "optional": true - } - } - }, "node_modules/terser/node_modules/commander": { "version": "2.20.3", "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz", @@ -14110,13 +14085,12 @@ "license": "MIT" }, "node_modules/watchpack": { - "version": "2.5.1", - "resolved": "https://registry.npmjs.org/watchpack/-/watchpack-2.5.1.tgz", - "integrity": "sha512-Zn5uXdcFNIA1+1Ei5McRd+iRzfhENPCe7LeABkJtNulSxjma+l7ltNx55BWZkRlwRnpOgHqxnjyaDgJnNXnqzg==", + "version": "2.6.4", + "resolved": "https://registry.npmjs.org/watchpack/-/watchpack-2.6.4.tgz", + "integrity": "sha512-5H3Tce2GfzWbcr9VnSqyak2whFaONw4BdmI5BDnMXold4qSlCFd64XaMQESRh2vkDKmdUkv6rUo17UvZ9f9nXw==", "dev": true, "license": "MIT", "dependencies": { - "glob-to-regexp": "^0.4.1", "graceful-fs": "^4.1.2" }, "engines": { @@ -14131,9 +14105,9 @@ "license": "BSD-2-Clause" }, "node_modules/webpack": { - "version": "5.107.0", - "resolved": "https://registry.npmjs.org/webpack/-/webpack-5.107.0.tgz", - "integrity": "sha512-PSxeHk/dmLYZlnTU+vL1Gej6Evg5RNtl3flhxBresfznFnzxinHMzHKloHnywM/3ouQv7/AlZCswWDIkNSggUA==", + "version": "5.113.0", + "resolved": "https://registry.npmjs.org/webpack/-/webpack-5.113.0.tgz", + "integrity": "sha512-q54iF0LNaAAdLEt3al9qMXC2zARvcx7LVKjHp+ExjCRkGx0M8TmawGHAXykXwofWzxhNzPTAh3kLVXvNg0El4A==", "dev": true, "license": "MIT", "dependencies": { @@ -14142,24 +14116,17 @@ "@webassemblyjs/ast": "^1.14.1", "@webassemblyjs/wasm-edit": "^1.14.1", "@webassemblyjs/wasm-parser": "^1.14.1", - "acorn": "^8.16.0", - "acorn-import-phases": "^1.0.3", "browserslist": "^4.28.1", "chrome-trace-event": "^1.0.2", - "enhanced-resolve": "^5.21.4", - "es-module-lexer": "^2.1.0", - "eslint-scope": "5.1.1", + "enhanced-resolve": "^5.27.0", "events": "^3.2.0", - "glob-to-regexp": "^0.4.1", "graceful-fs": "^4.2.11", - "loader-runner": "^4.3.2", "mime-db": "^1.54.0", - "neo-async": "^2.6.2", - "schema-utils": "^4.3.3", - "tapable": "^2.3.0", - "terser-webpack-plugin": "^5.5.0", - "watchpack": "^2.5.1", - "webpack-sources": "^3.4.1" + "minimizer-webpack-plugin": "^5.14.0", + "schema-utils": "^4.8.2", + "tapable": "^2.3.4", + "watchpack": "^2.6.4", + "webpack-sources": "^3.7.1" }, "bin": { "webpack": "bin/webpack.js" @@ -14178,39 +14145,15 @@ } }, "node_modules/webpack-sources": { - "version": "3.4.1", - "resolved": "https://registry.npmjs.org/webpack-sources/-/webpack-sources-3.4.1.tgz", - "integrity": "sha512-eACpxRN02yaawnt+uUNIF7Qje6A9zArxBbcAJjK1PK3S9Ycg5jIuJ8pW4q8EMnwNZCEGltcjkRx1QzOxOkKD8A==", + "version": "3.7.1", + "resolved": "https://registry.npmjs.org/webpack-sources/-/webpack-sources-3.7.1.tgz", + "integrity": "sha512-mcxs7A2LsEdwsNqQburqS4QHPR2Ca3NGplUakn6J5GPQvnVZ4oD4IM4FJcT3f8AVN2e2EsjuIVxKj4rorHvyuQ==", "dev": true, "license": "MIT", "engines": { "node": ">=10.13.0" } }, - "node_modules/webpack/node_modules/eslint-scope": { - "version": "5.1.1", - "resolved": "https://registry.npmjs.org/eslint-scope/-/eslint-scope-5.1.1.tgz", - "integrity": "sha512-2NxwbF/hZ0KpepYN0cNbo+FN6XoK7GaHlQhgx/hIZl6Va0bF45RQOOwhLIy8lQDbuCiadSLCBnH2CFYquit5bw==", - "dev": true, - "license": "BSD-2-Clause", - "dependencies": { - "esrecurse": "^4.3.0", - "estraverse": "^4.1.1" - }, - "engines": { - "node": ">=8.0.0" - } - }, - "node_modules/webpack/node_modules/estraverse": { - "version": "4.3.0", - "resolved": "https://registry.npmjs.org/estraverse/-/estraverse-4.3.0.tgz", - "integrity": "sha512-39nnKffWz8xN1BU/2c79n9nB9HDzo0niYUqx6xyqUnyoAnQyyWpOTdZEeiCch8BBu515t4wp9ZmgVfVhn9EBpw==", - "dev": true, - "license": "BSD-2-Clause", - "engines": { - "node": ">=4.0" - } - }, "node_modules/whatwg-url": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", diff --git a/package.json b/package.json index 50c127ac..2a1ceb56 100644 --- a/package.json +++ b/package.json @@ -50,14 +50,16 @@ "lint:code": "eslint --cache .", "lint:spelling": "cspell --cache --no-must-find-files --quiet \"**/*.*\"", "lint:types": "tsc --pretty --noEmit", + "lint:schema": "schema-utils precompile --check --minify src/options.json", "fmt": "npm run fmt:check -- --write", "fmt:check": "prettier --list-different --cache --ignore-unknown .", - "fix": "npm run fix:code && npm run fmt", + "fix": "npm run fix:schema && npm run fix:code && npm run fmt", "fix:code": "npm run lint:code -- --fix", + "fix:schema": "schema-utils precompile --minify src/options.json", "pretest": "npm run lint", "test": "npm run test:base", "test:base": "node --import ./test/setup-snapshots.js --test --test-force-exit --test-reporter=dot --test-reporter-destination=stdout --experimental-test-module-mocks", - "test:coverage": "npm run test:base -- --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=coverage.lcov --test-coverage-include=\"src/**/*.js\"", + "test:coverage": "npm run test:base -- --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=coverage.lcov --test-coverage-include=\"src/**/*.js\" --test-coverage-exclude=\"src/options.check.js\"", "prepare": "husky && npm run build", "version": "changeset version", "release": "npm run build && changeset publish" @@ -84,8 +86,9 @@ "memfs": "^4.57.2", "npm-run-all": "^4.1.5", "prettier": "^3.8.3", + "schema-utils": "^4.8.5", "typescript": "^6.0.3", - "webpack": "^5.107.0" + "webpack": "^5.113.0" }, "peerDependencies": { "@rspack/core": "0.x || ^1.0.0 || ^2.0.0-0", diff --git a/src/index.js b/src/index.js index 8172669b..08c6db9b 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,6 @@ import path from "node:path"; +import check from "./options.check.js"; import schema from "./options.json" with { type: "json" }; import { errorFactory, @@ -22,7 +23,7 @@ import { * @returns {Promise} loader result */ async function lessLoader(content) { - const options = this.getOptions(/** @type {Schema} */ (schema)); + const options = this.getOptions(/** @type {Schema} */ (schema), check); const callback = this.async(); let implementation; diff --git a/src/options.check.js b/src/options.check.js new file mode 100644 index 00000000..68228e11 --- /dev/null +++ b/src/options.check.js @@ -0,0 +1,7 @@ +/* eslint-disable */ +// @ts-nocheck +/* + * This file was automatically generated by "schema-utils". + * DO NOT MODIFY BY HAND. + */ +export const validate=e;export default e;let r={type:"object",properties:{lessOptions:{anyOf:[{type:"object",additionalProperties:!0},{instanceof:"Function"}]},additionalData:{anyOf:[{type:"string"},{instanceof:"Function"}]},sourceMap:{type:"boolean"},webpackImporter:{anyOf:[{type:"boolean"},{type:"string",enum:["only"]}]},implementation:{anyOf:[{type:"string"},{type:"object"}]},lessLogAsWarnOrErr:{type:"boolean"}},additionalProperties:!1,$id:"options.json"};function e(s,{instancePath="",parentData,parentDataProperty,rootData=s}={}){let a=null,o=0;if(o===0)if(s&&"object"==typeof s&&!Array.isArray(s)){let d=o;for(let r in s)if(!(r==="lessOptions"||r==="additionalData"||r==="sourceMap"||r==="webpackImporter"||r==="implementation"||r==="lessLogAsWarnOrErr"))return e.errors=[{params:{additionalProperty:r}}],!1;if(d===o){if(s.lessOptions!==void 0){let r=s.lessOptions,i=o,n=o,v=!1,d=o;if(o===d)if(!(r&&"object"==typeof r&&!Array.isArray(r))){let r={params:{type:"object"}};a===null?a=[r]:a.push(r),o++}var t=d===o;if(!(v=v||t)){let e=o;if(!(r instanceof Function)){let r={params:{}};a===null?a=[r]:a.push(r),o++}var t=e===o;v=v||t}if(!v){let r={params:{}};a===null?a=[r]:a.push(r),o++,e.errors=a;return!1}o=n;if(a!==null)n?a.length=n:a=null;var l=i===o}else var l=!0;if(l){if(s.additionalData!==void 0){let r=s.additionalData,t=o,n=o,v=!1,d=o;if("string"!=typeof r){let r={params:{type:"string"}};a===null?a=[r]:a.push(r),o++}var i=d===o;if(!(v=v||i)){let e=o;if(!(r instanceof Function)){let r={params:{}};a===null?a=[r]:a.push(r),o++}var i=e===o;v=v||i}if(!v){let r={params:{}};a===null?a=[r]:a.push(r),o++,e.errors=a;return!1}o=n;if(a!==null)n?a.length=n:a=null;var l=t===o}else var l=!0;if(l){if(s.sourceMap!==void 0){let r=o;if("boolean"!=typeof s.sourceMap)return e.errors=[{params:{type:"boolean"}}],!1;var l=r===o}else var l=!0;if(l){if(s.webpackImporter!==void 0){let t=s.webpackImporter,i=o,v=o,d=!1,f=o;if("boolean"!=typeof t){let r={params:{type:"boolean"}};a===null?a=[r]:a.push(r),o++}var n=f===o;if(!(d=d||n)){let e=o;if("string"!=typeof t){let r={params:{type:"string"}};a===null?a=[r]:a.push(r),o++}if(t!=="only"){let e={params:{allowedValues:r.properties.webpackImporter.anyOf[1].enum}};a===null?a=[e]:a.push(e),o++}var n=e===o;d=d||n}if(!d){let r={params:{}};a===null?a=[r]:a.push(r),o++,e.errors=a;return!1}o=v;if(a!==null)v?a.length=v:a=null;var l=i===o}else var l=!0;if(l){if(s.implementation!==void 0){let r=s.implementation,t=o,i=o,n=!1,d=o;if("string"!=typeof r){let r={params:{type:"string"}};a===null?a=[r]:a.push(r),o++}var v=d===o;if(!(n=n||v)){let e=o;if(!(r&&"object"==typeof r&&!Array.isArray(r))){let r={params:{type:"object"}};a===null?a=[r]:a.push(r),o++}var v=e===o;n=n||v}if(!n){let r={params:{}};a===null?a=[r]:a.push(r),o++,e.errors=a;return!1}o=i;if(a!==null)i?a.length=i:a=null;var l=t===o}else var l=!0;if(l)if(s.lessLogAsWarnOrErr!==void 0){let r=o;if("boolean"!=typeof s.lessLogAsWarnOrErr)return e.errors=[{params:{type:"boolean"}}],!1;var l=r===o}else var l=!0}}}}}}else return e.errors=[{params:{type:"object"}}],!1;return e.errors=a,o===0} diff --git a/test/__snapshots__/validate-options.test.js.snap b/test/__snapshots__/validate-options.test.js.snap index d70a0eb2..71e82636 100644 --- a/test/__snapshots__/validate-options.test.js.snap +++ b/test/__snapshots__/validate-options.test.js.snap @@ -67,35 +67,35 @@ exports[`validate options > should throw an error on the \"sourceMap\" option wi `; exports[`validate options > should throw an error on the \"unknown\" option with \"/test/\" value 1`] = ` -"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.\\n - options has an unknown property 'unknown'. These properties are valid:\\n object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation?, lessLogAsWarnOrErr? }" +"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.\\n - options has an unknown property 'unknown'. These properties are valid:\\n object { lessOptions?, additionalData?, sourceMap?: boolean, webpackImporter?, implementation?, lessLogAsWarnOrErr?: boolean }" `; exports[`validate options > should throw an error on the \"unknown\" option with \"1\" value 1`] = ` -"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.\\n - options has an unknown property 'unknown'. These properties are valid:\\n object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation?, lessLogAsWarnOrErr? }" +"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.\\n - options has an unknown property 'unknown'. These properties are valid:\\n object { lessOptions?, additionalData?, sourceMap?: boolean, webpackImporter?, implementation?, lessLogAsWarnOrErr?: boolean }" `; exports[`validate options > should throw an error on the \"unknown\" option with \"[]\" value 1`] = ` -"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.\\n - options has an unknown property 'unknown'. These properties are valid:\\n object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation?, lessLogAsWarnOrErr? }" +"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.\\n - options has an unknown property 'unknown'. These properties are valid:\\n object { lessOptions?, additionalData?, sourceMap?: boolean, webpackImporter?, implementation?, lessLogAsWarnOrErr?: boolean }" `; exports[`validate options > should throw an error on the \"unknown\" option with \"false\" value 1`] = ` -"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.\\n - options has an unknown property 'unknown'. These properties are valid:\\n object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation?, lessLogAsWarnOrErr? }" +"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.\\n - options has an unknown property 'unknown'. These properties are valid:\\n object { lessOptions?, additionalData?, sourceMap?: boolean, webpackImporter?, implementation?, lessLogAsWarnOrErr?: boolean }" `; exports[`validate options > should throw an error on the \"unknown\" option with \"test\" value 1`] = ` -"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.\\n - options has an unknown property 'unknown'. These properties are valid:\\n object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation?, lessLogAsWarnOrErr? }" +"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.\\n - options has an unknown property 'unknown'. These properties are valid:\\n object { lessOptions?, additionalData?, sourceMap?: boolean, webpackImporter?, implementation?, lessLogAsWarnOrErr?: boolean }" `; exports[`validate options > should throw an error on the \"unknown\" option with \"true\" value 1`] = ` -"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.\\n - options has an unknown property 'unknown'. These properties are valid:\\n object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation?, lessLogAsWarnOrErr? }" +"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.\\n - options has an unknown property 'unknown'. These properties are valid:\\n object { lessOptions?, additionalData?, sourceMap?: boolean, webpackImporter?, implementation?, lessLogAsWarnOrErr?: boolean }" `; exports[`validate options > should throw an error on the \"unknown\" option with \"{\"foo\":\"bar\"}\" value 1`] = ` -"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.\\n - options has an unknown property 'unknown'. These properties are valid:\\n object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation?, lessLogAsWarnOrErr? }" +"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.\\n - options has an unknown property 'unknown'. These properties are valid:\\n object { lessOptions?, additionalData?, sourceMap?: boolean, webpackImporter?, implementation?, lessLogAsWarnOrErr?: boolean }" `; exports[`validate options > should throw an error on the \"unknown\" option with \"{}\" value 1`] = ` -"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.\\n - options has an unknown property 'unknown'. These properties are valid:\\n object { lessOptions?, additionalData?, sourceMap?, webpackImporter?, implementation?, lessLogAsWarnOrErr? }" +"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.\\n - options has an unknown property 'unknown'. These properties are valid:\\n object { lessOptions?, additionalData?, sourceMap?: boolean, webpackImporter?, implementation?, lessLogAsWarnOrErr?: boolean }" `; exports[`validate options > should throw an error on the \"webpackImporter\" option with \"string\" value 1`] = ` diff --git a/test/validate-options.test.js b/test/validate-options.test.js index 53858232..4da0e49d 100644 --- a/test/validate-options.test.js +++ b/test/validate-options.test.js @@ -2,11 +2,39 @@ import assert from "node:assert"; import { createRequire } from "node:module"; import { describe, it } from "node:test"; +import { validate } from "schema-utils"; + +import check from "../src/options.check.js"; +import schema from "../src/options.json" with { type: "json" }; + import { compile, getCompiler } from "./helpers/index.js"; const require = createRequire(import.meta.url); +/** + * @param {EXPECTED_ANY} options loader options + * @returns {boolean} whether the schema accepts them + */ +function isValid(options) { + try { + validate(/** @type {EXPECTED_ANY} */ (schema), options); + + return true; + } catch { + return false; + } +} + describe("validate options", () => { + // Options that are not an object never pass the check. `validate` takes an + // array as a list of options objects, so it accepts `[]`; the check failing + // there only means webpack falls back to the full validation. + for (const value of [[], "invalid", 42, null]) { + it(`should not pass the check with ${JSON.stringify(value)}`, () => { + assert.strictEqual(check(value), false); + }); + } + const tests = { lessOptions: { success: [ @@ -68,6 +96,11 @@ describe("validate options", () => { it(`should ${ type === "success" ? "successfully validate" : "throw an error on" } the "${key}" option with "${stringifyValue(value)}" value`, async (t) => { + // webpack trusts a check that passes and skips the schema, so the two + // must agree on every value + assert.strictEqual(check({ [key]: value }), isValid({ [key]: value })); + assert.strictEqual(check({ [key]: value }), type === "success"); + const compiler = getCompiler("./basic.less", { [key]: value, }); diff --git a/types/options.check.d.ts b/types/options.check.d.ts new file mode 100644 index 00000000..c5270637 --- /dev/null +++ b/types/options.check.d.ts @@ -0,0 +1,25 @@ +export function validate( + s: any, + { + instancePath, + parentData, + parentDataProperty, + rootData, + }?: { + instancePath?: string | undefined; + rootData?: any; + }, +): any; +export default e; +declare function e( + s: any, + { + instancePath, + parentData, + parentDataProperty, + rootData, + }?: { + instancePath?: string | undefined; + rootData?: any; + }, +): any;