diff --git a/babel.config.js b/babel.config.js index b65dedb..106a962 100644 --- a/babel.config.js +++ b/babel.config.js @@ -12,6 +12,7 @@ module.exports = (api) => { targets: { node: "18.12.0", }, + exclude: ["transform-dynamic-import"], }, ], ], diff --git a/package-lock.json b/package-lock.json index ede1aeb..83d0844 100644 --- a/package-lock.json +++ b/package-lock.json @@ -33,7 +33,7 @@ "globals": "^16.3.0", "husky": "^9.1.3", "jest": "^30.0.0", - "less": "^4.2.0", + "less": "^4.6.2", "less-plugin-glob": "^3.0.0", "lint-staged": "^15.2.7", "memfs": "^4.9.3", @@ -140,6 +140,7 @@ "integrity": "sha512-UlLAnTPrFdNGoFtbSXwcGFQBtQZJCNjaN6hQNP3UPvuNXT1i82N26KL3dZeIpNalWywr9IuQuncaAfUaS1g6sQ==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@ampproject/remapping": "^2.2.0", "@babel/code-frame": "^7.27.1", @@ -2339,7 +2340,8 @@ "resolved": "https://registry.npmjs.org/@cspell/dict-css/-/dict-css-4.0.18.tgz", "integrity": "sha512-EF77RqROHL+4LhMGW5NTeKqfUd/e4OOv6EDFQ/UQQiFyWuqkEKyEz0NDILxOFxWUEVdjT2GQ2cC7t12B6pESwg==", "dev": true, - "license": "MIT" + "license": "MIT", + "peer": true }, "node_modules/@cspell/dict-dart": { "version": "2.3.1", @@ -2479,14 +2481,16 @@ "resolved": "https://registry.npmjs.org/@cspell/dict-html/-/dict-html-4.0.12.tgz", "integrity": "sha512-JFffQ1dDVEyJq6tCDWv0r/RqkdSnV43P2F/3jJ9rwLgdsOIXwQbXrz6QDlvQLVvNSnORH9KjDtenFTGDyzfCaA==", "dev": true, - "license": "MIT" + "license": "MIT", + "peer": true }, "node_modules/@cspell/dict-html-symbol-entities": { "version": "4.0.4", "resolved": "https://registry.npmjs.org/@cspell/dict-html-symbol-entities/-/dict-html-symbol-entities-4.0.4.tgz", "integrity": "sha512-afea+0rGPDeOV9gdO06UW183Qg6wRhWVkgCFwiO3bDupAoyXRuvupbb5nUyqSTsLXIKL8u8uXQlJ9pkz07oVXw==", "dev": true, - "license": "MIT" + "license": "MIT", + "peer": true }, "node_modules/@cspell/dict-java": { "version": "5.0.12", @@ -2684,7 +2688,8 @@ "resolved": "https://registry.npmjs.org/@cspell/dict-typescript/-/dict-typescript-3.2.3.tgz", "integrity": "sha512-zXh1wYsNljQZfWWdSPYwQhpwiuW0KPW1dSd8idjMRvSD0aSvWWHoWlrMsmZeRl4qM4QCEAjua8+cjflm41cQBg==", "dev": true, - "license": "MIT" + "license": "MIT", + "peer": true }, "node_modules/@cspell/dict-vue": { "version": "3.0.5", @@ -2938,6 +2943,7 @@ "integrity": "sha512-BBpRFZK3eX6uMLKz8WxFOBIFFcGFJ/g8XuwjTHCqHROSIsopI+ddn/d5Cfh36+7+e5edVS8dbSHnBNhrLEX0zg==", "dev": true, "license": "MIT", + "peer": true, "engines": { "node": "^18.18.0 || ^20.9.0 || >=21.1.0" }, @@ -2951,6 +2957,7 @@ "integrity": "sha512-Y+X1B1j+/zupKDVJfkKc8uYMjQkGzfnd8lt7vK3y8x9Br6H5dBuhAfFrQ6ff7HAMm/1BwgecyEiRFkYCWPRxmA==", "dev": true, "license": "MIT", + "peer": true, "workspaces": [ "examples/*" ], @@ -3915,6 +3922,7 @@ "integrity": "sha512-bE2DUjruqXlHYP3Q2Gpqiuj2bHq7/88FnuaS0FjeGGLCy+X6a07bGVuwtiOYnPSLHR6jmx5Bwdv+j7l8H+G97A==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@eslint-community/eslint-utils": "^4.7.0", "@typescript-eslint/types": "^8.37.0", @@ -4002,6 +4010,7 @@ "integrity": "sha512-FXx2pKgId/WyYo2jXw63kk7/+TY7u7AziEJxJAnSFzHlqTAS3Ync6SvgYAN/k4/PQpnnVuzoMuVnByKK2qp0ag==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@types/estree": "*", "@types/json-schema": "*" @@ -4096,6 +4105,7 @@ "integrity": "sha512-ut5FthK5moxFKH2T1CUOC6ctR67rQRvvHdFLCD2Ql6KXmMuCrjsSsRI9UsLCm9M18BMwClv4pn327UvB7eeO1w==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "undici-types": "~7.8.0" } @@ -4144,6 +4154,7 @@ "integrity": "sha512-CPoznzpuAnIOl4nhj4tRr4gIPj5AfKgkiJmGQDaq+fQnRJTYlcBjbX3wbciGmpoPf8DREufuPRe1tNMZnGdanA==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@eslint-community/regexpp": "^4.10.0", "@typescript-eslint/scope-manager": "8.38.0", @@ -4184,6 +4195,7 @@ "integrity": "sha512-Zhy8HCvBUEfBECzIl1PKqF4p11+d0aUJS1GeUiuqK9WmOug8YCmC4h4bjyBvMyAMI9sbRczmrYL5lKg/YMbrcQ==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@typescript-eslint/scope-manager": "8.38.0", "@typescript-eslint/types": "8.38.0", @@ -4866,6 +4878,7 @@ "integrity": "sha512-NZyJarBfL7nWwIq+FDL6Zp/yHEhePMNnnJ0y3qfieCrmNvYct8uvtiV41UvlSe6apAfk0fY1FbWx+NwfmpvtTg==", "dev": true, "license": "MIT", + "peer": true, "bin": { "acorn": "bin/acorn" }, @@ -4926,6 +4939,7 @@ "integrity": "sha512-B/gBuNg5SiMTrPkC+A2+cW0RszwxYmn6VYxB/inlBStS5nx6xHIt/ehKRhIMhqusl7a8LjQoZnjCs5vhwxOQ1g==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "fast-deep-equal": "^3.1.3", "fast-uri": "^3.0.1", @@ -5565,6 +5579,7 @@ } ], "license": "MIT", + "peer": true, "dependencies": { "baseline-browser-mapping": "^2.9.0", "caniuse-lite": "^1.0.30001759", @@ -7415,13 +7430,16 @@ "license": "MIT" }, "node_modules/copy-anything": { - "version": "2.0.6", - "resolved": "https://registry.npmjs.org/copy-anything/-/copy-anything-2.0.6.tgz", - "integrity": "sha512-1j20GZTsvKNkc4BY3NpMOM8tt///wY3FpIzozTOFO2ffuZcV61nojHXVKIy3WM+7ADCy5FVhdZYHYDdgTU0yJw==", + "version": "3.0.5", + "resolved": "https://registry.npmjs.org/copy-anything/-/copy-anything-3.0.5.tgz", + "integrity": "sha512-yCEafptTtb4bk7GLEQoM8KVJpxAfdBJYaXyzQEgQQQgYrZiDp8SJmGKlYza6CYjEDNstAdNdKA3UuoULlEbS6w==", "dev": true, "license": "MIT", "dependencies": { - "is-what": "^3.14.1" + "is-what": "^4.1.8" + }, + "engines": { + "node": ">=12.13" }, "funding": { "url": "https://github.com/sponsors/mesqueeb" @@ -8764,6 +8782,7 @@ "integrity": "sha512-LSehfdpgMeWcTZkWZVIJl+tkZ2nuSkyyB9C27MZqFWXuph7DvaowgcTvKqxvpLW1JZIk8PN7hFY3Rj9LQ7m7lg==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@eslint-community/eslint-utils": "^4.2.0", "@eslint-community/regexpp": "^4.12.1", @@ -8854,6 +8873,7 @@ "integrity": "sha512-82GZUjRS0p/jganf6q1rEO25VSoHH0hKPCTrgillPjdI/3bgBhAE1QzHrHTizjpRvy6pGAvKjDJtk2pF9NDq8w==", "dev": true, "license": "MIT", + "peer": true, "bin": { "eslint-config-prettier": "bin/cli.js" }, @@ -9012,6 +9032,7 @@ "integrity": "sha512-whOE1HFo/qJDyX4SnXzP4N6zOWn79WhnCUY/iDR0mPfQZO8wcYE4JClzI2oZrhBnnMUCBCHZhO6VQyoBU95mZA==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@rtsao/scc": "^1.1.0", "array-includes": "^3.1.9", @@ -9056,6 +9077,7 @@ "integrity": "sha512-EE44T0OSMCeXhDrrdsbKAhprobKkPtJTbQz5yEktysNpHeDZTAL1SfDTNKmcFfJkY6yrQLtTKZALrD3j/Gpmiw==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@typescript-eslint/utils": "^8.0.0" }, @@ -9082,6 +9104,7 @@ "integrity": "sha512-MtxYjDZhMQgsWRm/4xYLL0i2EhusWT7itDxlJ80l1NND2AL2Vi5Mvneqv/ikG9+zpran0VsVRXTEHrpLmUZRNw==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@eslint-community/eslint-utils": "^4.5.0", "enhanced-resolve": "^5.17.1", @@ -9135,6 +9158,7 @@ "integrity": "sha512-NAdMYww51ehKfDyDhv59/eIItUVzU0Io9H2E8nHNGKEeeqlnci+1gCvrHib6EmZdf6GxF+LCV5K7UC65Ezvw7w==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "prettier-linter-helpers": "^1.0.0", "synckit": "^0.11.7" @@ -9166,6 +9190,7 @@ "integrity": "sha512-QUzTefvP8stfSXsqKQ+vBQSEsXIlAiCduS/V1Em+FKgL9c21U/IIm20/e3MFy1jyCf14tHAhqC1sX8OTy6VUCg==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@babel/helper-validator-identifier": "^7.27.1", "@eslint-community/eslint-utils": "^4.7.0", @@ -11032,6 +11057,7 @@ "integrity": "sha512-bqWEnJ1Nt3neqx2q5SFfGS8r/ahumIakg3HcwtNlrVlwXIeNumWn/c7Pn/wKzGhf6SaW6H6uWXLqC30STCMchQ==", "dev": true, "license": "MIT", + "peer": true, "engines": { "node": ">=18" }, @@ -12153,11 +12179,17 @@ } }, "node_modules/is-what": { - "version": "3.14.1", - "resolved": "https://registry.npmjs.org/is-what/-/is-what-3.14.1.tgz", - "integrity": "sha512-sNxgpk9793nzSs7bA6JQJGeIuRBQhAaNGG77kzYQgMkrID+lS6SlK07K5LaptscDlSaIgH+GPFzf+d75FVxozA==", + "version": "4.1.16", + "resolved": "https://registry.npmjs.org/is-what/-/is-what-4.1.16.tgz", + "integrity": "sha512-ZhMwEosbFJkA0YhFnNDgTM4ZxDRsS6HqTo7qsZM08fehyRYIYa0yHu5R6mgo1n/8MgaPBXiPimPD77baVFYg+A==", "dev": true, - "license": "MIT" + "license": "MIT", + "engines": { + "node": ">=12.13" + }, + "funding": { + "url": "https://github.com/sponsors/mesqueeb" + } }, "node_modules/is-windows": { "version": "1.0.2", @@ -12328,6 +12360,7 @@ "integrity": "sha512-y2mfcJywuTUkvLm2Lp1/pFX8kTgMO5yyQGq/Sk/n2mN7XWYp4JsCZ/QXW34M8YScgk8bPZlREH04f6blPnoHnQ==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@jest/core": "30.0.5", "@jest/types": "30.0.5", @@ -13269,21 +13302,20 @@ } }, "node_modules/less": { - "version": "4.4.0", - "resolved": "https://registry.npmjs.org/less/-/less-4.4.0.tgz", - "integrity": "sha512-kdTwsyRuncDfjEs0DlRILWNvxhDG/Zij4YLO4TMJgDLW+8OzpfkdPnRgrsRuY1o+oaxJGWsps5f/RVBgGmmN0w==", + "version": "4.6.2", + "resolved": "https://registry.npmjs.org/less/-/less-4.6.2.tgz", + "integrity": "sha512-/f26k/Z8VOtdkmubElnfZGtNoOlnZN6E1g9WkPkRmsz/FNVaaArbwCIMflPdH//RJeowfuTPmdZSp9zyEYQ1gQ==", "dev": true, "license": "Apache-2.0", "dependencies": { - "copy-anything": "^2.0.1", - "parse-node-version": "^1.0.1", - "tslib": "^2.3.0" + "copy-anything": "^3.0.5", + "parse-node-version": "^1.0.1" }, "bin": { "lessc": "bin/lessc" }, "engines": { - "node": ">=14" + "node": ">=18" }, "optionalDependencies": { "errno": "^0.1.1", @@ -16220,6 +16252,7 @@ "integrity": "sha512-I7AIg5boAr5R0FFtJ6rCfD+LFsWHp81dolrFD8S79U9tb8Az2nGrJncnMSnys+bpQJfRUzqs9hnA81OAA3hCuQ==", "dev": true, "license": "MIT", + "peer": true, "bin": { "prettier": "bin/prettier.cjs" }, @@ -18744,7 +18777,8 @@ "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz", "integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==", "dev": true, - "license": "0BSD" + "license": "0BSD", + "peer": true }, "node_modules/type-check": { "version": "0.4.0", @@ -18888,6 +18922,7 @@ "integrity": "sha512-FsZlrYK6bPDGoLeZRuvx2v6qrM03I0U0SnfCLPs/XCCPCFD80xU9Pg09H/K+XFa68uJuZo7l/Xhs+eDRg2l3hg==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@typescript-eslint/eslint-plugin": "8.38.0", "@typescript-eslint/parser": "8.38.0", diff --git a/package.json b/package.json index 9ef9cd2..b9d59d8 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "fix:js": "npm run lint:js -- --fix", "fix:prettier": "npm run lint:prettier -- --write", "fix": "npm-run-all -l fix:js fix:prettier", - "test:only": "cross-env NODE_ENV=test jest", + "test:only": "node --experimental-vm-modules node_modules/jest-cli/bin/jest.js", "test:watch": "npm run test:only -- --watch", "test:coverage": "npm run test:only -- --collectCoverageFrom=\"src/**/*.js\" --coverage", "pretest": "npm run lint", @@ -71,7 +71,7 @@ "globals": "^16.3.0", "husky": "^9.1.3", "jest": "^30.0.0", - "less": "^4.2.0", + "less": "^4.6.2", "less-plugin-glob": "^3.0.0", "lint-staged": "^15.2.7", "memfs": "^4.9.3", diff --git a/src/index.js b/src/index.js index 20e0c2d..18d234b 100644 --- a/src/index.js +++ b/src/index.js @@ -15,7 +15,7 @@ async function lessLoader(source) { let implementation; try { - implementation = getLessImplementation(this, options.implementation); + implementation = await getLessImplementation(this, options.implementation); } catch (error) { callback(error); @@ -38,7 +38,9 @@ async function lessLoader(source) { if (useSourceMap) { lessOptions.sourceMap = { + sourceMapBasepath: "", outputSourceFiles: true, + disableSourcemapAnnotation: true, }; } @@ -123,7 +125,7 @@ async function lessLoader(source) { typeof result.map === "string" ? JSON.parse(result.map) : result.map; if (map && useSourceMap) { - map = normalizeSourceMap(map, this.rootContext); + map = normalizeSourceMap(map); } callback(null, css, map); diff --git a/src/utils.js b/src/utils.js index a20ae4a..59e1d76 100644 --- a/src/utils.js +++ b/src/utils.js @@ -223,20 +223,19 @@ function normalizeSourceMap(map) { newMap.sourceRoot = ""; - // `less` returns POSIX paths, that's why we need to transform them back to native paths. - + // `less` (old versions) returns POSIX paths, that's why we need to transform them back to native paths. newMap.sources = newMap.sources.map((source) => path.normalize(source)); return newMap; } -function getLessImplementation(loaderContext, implementation) { +async function getLessImplementation(loaderContext, implementation) { let resolvedImplementation = implementation; if (!implementation || typeof implementation === "string") { const lessImplPkg = implementation || "less"; - resolvedImplementation = require(lessImplPkg); + resolvedImplementation = (await import(lessImplPkg)).default; } return resolvedImplementation; diff --git a/test/__snapshots__/sourceMap-options.test.js.snap b/test/__snapshots__/sourceMap-options.test.js.snap index 230720c..6fe574c 100644 --- a/test/__snapshots__/sourceMap-options.test.js.snap +++ b/test/__snapshots__/sourceMap-options.test.js.snap @@ -1,4 +1,4 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing exports[`"sourceMap" options should generate source maps when the "devtool" value is "source-map": css 1`] = ` ".modules-dir-some-module, @@ -55,6 +55,7 @@ exports[`"sourceMap" options should generate source maps when value has "false" exports[`"sourceMap" options should generate source maps when value has "false" value, but the "lessOptions.sourceMap.outputSourceFiles" is "true": source map 1`] = ` { + "file": "source-map.css", "mappings": "AAAA;ACEA;EDDC,cAAA;;ACCD;EACE,YAAA", "names": [], "sourceRoot": "", @@ -213,13 +214,14 @@ exports[`"sourceMap" options should work and generate custom source maps: css 1` #it-works { margin: 10px; } -/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIm5vZGVfbW9kdWxlcy9zb21lL21vZHVsZS5sZXNzIiwic291cmNlLW1hcC5sZXNzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBO0FDRUE7RUREQyxjQUFBOztBQ0NEO0VBQ0UsWUFBQSIsInNvdXJjZXNDb250ZW50IjpbIi5tb2R1bGVzLWRpci1zb21lLW1vZHVsZSB7XG5cdGNvbG9yOiBob3RwaW5rO1xufVxuIiwiQGltcG9ydCBcInNvbWUvbW9kdWxlXCI7XG5cbiNpdC13b3JrczpleHRlbmQoLm1vZHVsZXMtZGlyLXNvbWUtbW9kdWxlKSB7XG4gIG1hcmdpbjogMTBweDtcbn1cbiJdfQ== */" +/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIm5vZGVfbW9kdWxlcy9zb21lL21vZHVsZS5sZXNzIiwic291cmNlLW1hcC5sZXNzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBO0FDRUE7RUREQyxjQUFBOztBQ0NEO0VBQ0UsWUFBQSIsImZpbGUiOiJzb3VyY2UtbWFwLmNzcyIsInNvdXJjZXNDb250ZW50IjpbIi5tb2R1bGVzLWRpci1zb21lLW1vZHVsZSB7XG5cdGNvbG9yOiBob3RwaW5rO1xufVxuIiwiQGltcG9ydCBcInNvbWUvbW9kdWxlXCI7XG5cbiNpdC13b3JrczpleHRlbmQoLm1vZHVsZXMtZGlyLXNvbWUtbW9kdWxlKSB7XG4gIG1hcmdpbjogMTBweDtcbn1cbiJdfQ== */" `; exports[`"sourceMap" options should work and generate custom source maps: errors 1`] = `[]`; exports[`"sourceMap" options should work and generate custom source maps: source map 1`] = ` { + "file": "source-map.css", "mappings": "AAAA;ACEA;EDDC,cAAA;;ACCD;EACE,YAAA", "names": [], "sources": [ diff --git a/test/fixtures/folder/customFileLoaderPlugin.js b/test/fixtures/folder/customFileLoaderPlugin.js index a5889cc..e7c6041 100644 --- a/test/fixtures/folder/customFileLoaderPlugin.js +++ b/test/fixtures/folder/customFileLoaderPlugin.js @@ -1,36 +1,39 @@ import path from "path"; -import less from "less"; -class Plugin extends less.FileManager { - supports(filename) { - if (filename === 'forFileLoaderPluginResolve.less') { - return true; - } - - if (filename === "https://fonts.googleapis.com/css?family=Roboto:500") { - return true; - } +export default async () => { + const less = (await import("less")).default; - return false; - } + class Plugin extends less.FileManager { + supports(filename) { + if (filename === 'forFileLoaderPluginResolve.less') { + return true; + } + + if (filename === "https://fonts.googleapis.com/css?family=Roboto:500") { + return true; + } - loadFile(filename, ...args) { - let result; - - if (filename === 'forFileLoaderPluginResolve.less') { - result = path.resolve(__dirname, '../', 'file-load-replacement.less'); - } else if (filename === "https://fonts.googleapis.com/css?family=Roboto:500") { - result = path.resolve(__dirname, '../', 'mock-fonts.less'); + return false; } - return super.loadFile(result, ...args); - }; -} + loadFile(filename, ...args) { + let result; -class CustomFileLoaderPlugin { - install(less, pluginManager) { - pluginManager.addFileManager(new Plugin()); + if (filename === 'forFileLoaderPluginResolve.less') { + result = path.resolve(__dirname, '../', 'file-load-replacement.less'); + } else if (filename === "https://fonts.googleapis.com/css?family=Roboto:500") { + result = path.resolve(__dirname, '../', 'mock-fonts.less'); + } + + return super.loadFile(result, ...args); + }; + } + + class CustomFileLoaderPlugin { + install(less, pluginManager) { + pluginManager.addFileManager(new Plugin()); + } } -} -module.exports = CustomFileLoaderPlugin; + return CustomFileLoaderPlugin; +}; diff --git a/test/helpers/getCodeFromLess.js b/test/helpers/getCodeFromLess.js index c5e7c78..761b8be 100644 --- a/test/helpers/getCodeFromLess.js +++ b/test/helpers/getCodeFromLess.js @@ -1,8 +1,6 @@ import fs from "node:fs"; import path from "node:path"; -import less from "less"; - const pathMap = { "some/css.css": path.resolve( __dirname, @@ -143,37 +141,6 @@ const pathMap = { "3rd/b.less": path.resolve(__dirname, "..", "fixtures", "3rd", "b.less"), }; -class ResolvePlugin extends less.FileManager { - supports(filename) { - if (filename[0] === "/" || path.win32.isAbsolute(filename)) { - return true; - } - - if (this.isPathAbsolute(filename)) { - return false; - } - - return true; - } - - supportsSync() { - return false; - } - - async loadFile(filename, ...args) { - const result = - pathMap[filename] || path.resolve(__dirname, "..", "fixtures", filename); - - return super.loadFile(result, ...args); - } -} - -class CustomImportPlugin { - install(lessInstance, pluginManager) { - pluginManager.addFileManager(new ResolvePlugin()); - } -} - async function getCodeFromLess(testId, options = {}, context = {}) { let pathToFile; @@ -219,6 +186,40 @@ async function getCodeFromLess(testId, options = {}, context = {}) { ...lessOptions, }; + const less = (await import("less")).default; + + class ResolvePlugin extends less.FileManager { + supports(filename) { + if (filename[0] === "/" || path.win32.isAbsolute(filename)) { + return true; + } + + if (this.isPathAbsolute(filename)) { + return false; + } + + return true; + } + + supportsSync() { + return false; + } + + async loadFile(filename, ...args) { + const result = + pathMap[filename] || + path.resolve(__dirname, "..", "fixtures", filename); + + return super.loadFile(result, ...args); + } + } + + class CustomImportPlugin { + install(lessInstance, pluginManager) { + pluginManager.addFileManager(new ResolvePlugin()); + } + } + mergedOptions.plugins.unshift(new CustomImportPlugin()); return less.render(data.toString(), mergedOptions); diff --git a/test/implementation.test.js b/test/implementation.test.js index 02a43b0..b48fafd 100644 --- a/test/implementation.test.js +++ b/test/implementation.test.js @@ -9,9 +9,10 @@ import { describe('"implementation" option', () => { it("should work", async () => { + const less = (await import("less")).default; const testId = "./basic.less"; const compiler = getCompiler(testId, { - implementation: require("less"), + implementation: less, }); const stats = await compile(compiler); const codeFromBundle = getCodeFromBundle(stats, compiler); @@ -25,8 +26,9 @@ describe('"implementation" option', () => { it("should work when implementation option is string", async () => { const testId = "./basic.less"; + const compiler = getCompiler(testId, { - implementation: require.resolve("less"), + implementation: "less", }); const stats = await compile(compiler); const codeFromBundle = getCodeFromBundle(stats, compiler); diff --git a/test/loader.test.js b/test/loader.test.js index 2039761..39175ce 100644 --- a/test/loader.test.js +++ b/test/loader.test.js @@ -14,8 +14,6 @@ import { validateDependencies, } from "./helpers"; -const CustomFileLoaderPlugin = require("./fixtures/folder/customFileLoaderPlugin"); - const nodeModulesPath = path.resolve(__dirname, "fixtures", "node_modules"); jest.setTimeout(30000); @@ -107,6 +105,11 @@ describe("loader", () => { }); it("should work third-party plugins as fileLoader", async () => { + const getCustomFileLoaderPlugin = ( + await import("./fixtures/folder/customFileLoaderPlugin") + ).default.default; + const CustomFileLoaderPlugin = await getCustomFileLoaderPlugin(); + const testId = "./file-load.less"; const compiler = getCompiler(testId, { lessOptions: { @@ -802,7 +805,9 @@ describe("loader", () => { let contextInClass = false; let contextInObject = false; - class Plugin extends require("less").FileManager { + const less = (await import("less")).default; + + class Plugin extends less.FileManager { constructor(less, pluginManager) { super(); diff --git a/test/sourceMap-options.test.js b/test/sourceMap-options.test.js index 5e9034b..dd923d4 100644 --- a/test/sourceMap-options.test.js +++ b/test/sourceMap-options.test.js @@ -110,7 +110,11 @@ describe('"sourceMap" options', () => { const compiler = getCompiler(testId, { sourceMap: false, lessOptions: { - sourceMap: { outputSourceFiles: true }, + sourceMap: { + sourceMapBasepath: "", + outputSourceFiles: true, + disableSourcemapAnnotation: true, + }, }, }); const stats = await compile(compiler); diff --git a/test/validate-options.test.js b/test/validate-options.test.js index 80311bd..c382fa3 100644 --- a/test/validate-options.test.js +++ b/test/validate-options.test.js @@ -24,7 +24,7 @@ describe("validate options", () => { failure: ["string"], }, implementation: { - success: [require("less"), "less"], + success: ["less"], failure: [true, false, () => {}, []], }, lessLogAsWarnOrErr: {