From 43cd20c7c321c07d98df73bd16405f46f86cfc4f Mon Sep 17 00:00:00 2001 From: Alexander Akait <4567934+alexander-akait@users.noreply.github.com> Date: Fri, 7 Oct 2022 00:32:59 +0300 Subject: [PATCH] feat: allow to extend `conditionNames` (#488) --- src/utils.js | 2 +- test/__snapshots__/loader.test.js.snap | 22 ++++++++ ...age-with-exports-and-custom-condition.less | 1 + .../index.cjs | 1 + .../index.js | 1 + .../package.json | 22 ++++++++ .../style-1.less | 3 ++ .../style-2.less | 3 ++ .../package-with-exports/package.json | 4 +- test/helpers/getCodeFromLess.js | 23 +++++++- test/loader.test.js | 54 +++++++++++++++++++ 11 files changed, 131 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/import-package-with-exports-and-custom-condition.less create mode 100644 test/fixtures/node_modules/package-with-exports-and-custom-condition/index.cjs create mode 100644 test/fixtures/node_modules/package-with-exports-and-custom-condition/index.js create mode 100644 test/fixtures/node_modules/package-with-exports-and-custom-condition/package.json create mode 100644 test/fixtures/node_modules/package-with-exports-and-custom-condition/style-1.less create mode 100644 test/fixtures/node_modules/package-with-exports-and-custom-condition/style-2.less diff --git a/src/utils.js b/src/utils.js index 57f852d5..184bd444 100644 --- a/src/utils.js +++ b/src/utils.js @@ -34,7 +34,7 @@ const MODULE_REQUEST_REGEX = /^[^?]*~/; function createWebpackLessPlugin(loaderContext, implementation) { const resolve = loaderContext.getResolve({ dependencyType: "less", - conditionNames: ["less", "style"], + conditionNames: ["less", "style", "..."], mainFields: ["less", "style", "main", "..."], mainFiles: ["index", "..."], extensions: [".less", ".css"], diff --git a/test/__snapshots__/loader.test.js.snap b/test/__snapshots__/loader.test.js.snap index cdbfd74b..c0700984 100644 --- a/test/__snapshots__/loader.test.js.snap +++ b/test/__snapshots__/loader.test.js.snap @@ -583,6 +583,28 @@ exports[`loader should work third-party plugins as fileLoader: errors 1`] = `[]` exports[`loader should work third-party plugins as fileLoader: warnings 1`] = `[]`; +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme1): css 1`] = ` +".load-me { + color: red; +} +" +`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme1): errors 1`] = `[]`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme1): warnings 1`] = `[]`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme2): css 1`] = ` +".load-me { + color: blue; +} +" +`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme2): errors 1`] = `[]`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme2): warnings 1`] = `[]`; + exports[`loader should work: css 1`] = ` ".box { color: #fe33ac; diff --git a/test/fixtures/import-package-with-exports-and-custom-condition.less b/test/fixtures/import-package-with-exports-and-custom-condition.less new file mode 100644 index 00000000..879bf779 --- /dev/null +++ b/test/fixtures/import-package-with-exports-and-custom-condition.less @@ -0,0 +1 @@ +@import 'package-with-exports-and-custom-condition'; diff --git a/test/fixtures/node_modules/package-with-exports-and-custom-condition/index.cjs b/test/fixtures/node_modules/package-with-exports-and-custom-condition/index.cjs new file mode 100644 index 00000000..032a82bd --- /dev/null +++ b/test/fixtures/node_modules/package-with-exports-and-custom-condition/index.cjs @@ -0,0 +1 @@ +console.log('Some js, clearly not sass'); \ No newline at end of file diff --git a/test/fixtures/node_modules/package-with-exports-and-custom-condition/index.js b/test/fixtures/node_modules/package-with-exports-and-custom-condition/index.js new file mode 100644 index 00000000..032a82bd --- /dev/null +++ b/test/fixtures/node_modules/package-with-exports-and-custom-condition/index.js @@ -0,0 +1 @@ +console.log('Some js, clearly not sass'); \ No newline at end of file diff --git a/test/fixtures/node_modules/package-with-exports-and-custom-condition/package.json b/test/fixtures/node_modules/package-with-exports-and-custom-condition/package.json new file mode 100644 index 00000000..faf0aae8 --- /dev/null +++ b/test/fixtures/node_modules/package-with-exports-and-custom-condition/package.json @@ -0,0 +1,22 @@ +{ + "name": "package-with-exports-and-custom-condition", + "version": "1.0.0", + "description": "test", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "MIT", + "type": "module", + "module": "index.js", + "main": "index.cjs", + "sass": "style-1.scss", + "exports": { + "require": "./index.cjs", + "import": "./index.js", + "less": { + "theme1": "./style-1.less", + "theme2": "./style-2.less" + } + } +} diff --git a/test/fixtures/node_modules/package-with-exports-and-custom-condition/style-1.less b/test/fixtures/node_modules/package-with-exports-and-custom-condition/style-1.less new file mode 100644 index 00000000..b11fa36b --- /dev/null +++ b/test/fixtures/node_modules/package-with-exports-and-custom-condition/style-1.less @@ -0,0 +1,3 @@ +.load-me { + color: red; +} diff --git a/test/fixtures/node_modules/package-with-exports-and-custom-condition/style-2.less b/test/fixtures/node_modules/package-with-exports-and-custom-condition/style-2.less new file mode 100644 index 00000000..cf895141 --- /dev/null +++ b/test/fixtures/node_modules/package-with-exports-and-custom-condition/style-2.less @@ -0,0 +1,3 @@ +.load-me { + color: blue; +} diff --git a/test/fixtures/node_modules/package-with-exports/package.json b/test/fixtures/node_modules/package-with-exports/package.json index 8d1d7ebb..21e60704 100644 --- a/test/fixtures/node_modules/package-with-exports/package.json +++ b/test/fixtures/node_modules/package-with-exports/package.json @@ -12,8 +12,8 @@ "main": "index.cjs", "less": "style.less", "exports": { - "less": "./style.less", "require": "./index.cjs", - "import": "./index.js" + "import": "./index.js", + "less": "./style.less" } } diff --git a/test/helpers/getCodeFromLess.js b/test/helpers/getCodeFromLess.js index 3f047a3f..5cc5511d 100644 --- a/test/helpers/getCodeFromLess.js +++ b/test/helpers/getCodeFromLess.js @@ -150,8 +150,27 @@ class CustomImportPlugin { } } -async function getCodeFromLess(testId, options = {}) { - const pathToFile = path.resolve(__dirname, "..", "fixtures", testId); +async function getCodeFromLess(testId, options = {}, context = {}) { + let pathToFile; + + if (context.packageExportsCustomConditionTestVariant === 1) { + pathToFile = path.resolve( + __dirname, + "..", + "fixtures", + "node_modules/package-with-exports-and-custom-condition/style-1.less" + ); + } else if (context.packageExportsCustomConditionTestVariant === 2) { + pathToFile = path.resolve( + __dirname, + "..", + "fixtures", + "node_modules/package-with-exports-and-custom-condition/style-2.less" + ); + } else { + pathToFile = path.resolve(__dirname, "..", "fixtures", testId); + } + const defaultOptions = { plugins: [], relativeUrls: true, diff --git a/test/loader.test.js b/test/loader.test.js index 57a45be0..267d139f 100644 --- a/test/loader.test.js +++ b/test/loader.test.js @@ -937,4 +937,58 @@ describe("loader", () => { expect(getWarnings(stats)).toMatchSnapshot("warnings"); expect(getErrors(stats)).toMatchSnapshot("errors"); }); + + it(`should work with a package with "sass" and "exports" fields and a custom condition (theme1)`, async () => { + const testId = "./import-package-with-exports-and-custom-condition.less"; + const compiler = getCompiler( + testId, + {}, + { + resolve: { + conditionNames: ["theme1", "..."], + }, + } + ); + const stats = await compile(compiler); + const codeFromBundle = getCodeFromBundle(stats, compiler); + const codeFromLess = await getCodeFromLess( + testId, + {}, + { + packageExportsCustomConditionTestVariant: 1, + } + ); + + expect(codeFromBundle.css).toBe(codeFromLess.css); + expect(codeFromBundle.css).toMatchSnapshot("css"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); + }); + + it(`should work with a package with "sass" and "exports" fields and a custom condition (theme2)`, async () => { + const testId = "./import-package-with-exports-and-custom-condition.less"; + const compiler = getCompiler( + testId, + {}, + { + resolve: { + conditionNames: ["theme2", "..."], + }, + } + ); + const stats = await compile(compiler); + const codeFromBundle = getCodeFromBundle(stats, compiler); + const codeFromLess = await getCodeFromLess( + testId, + {}, + { + packageExportsCustomConditionTestVariant: 2, + } + ); + + expect(codeFromBundle.css).toBe(codeFromLess.css); + expect(codeFromBundle.css).toMatchSnapshot("css"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); + }); });