diff --git a/test/__snapshots__/loader.test.js.snap b/test/__snapshots__/loader.test.js.snap index fa591850..70dd8b67 100644 --- a/test/__snapshots__/loader.test.js.snap +++ b/test/__snapshots__/loader.test.js.snap @@ -334390,6 +334390,126 @@ exports[`loader should work with a package with "sass" and "exports" fields ('sa exports[`loader should work with a package with "sass" and "exports" fields ('sass-embedded', 'legacy' API, 'scss' syntax): warnings 1`] = `[]`; +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme1) ('dart-sass', 'legacy' API, 'sass' syntax): css 1`] = ` +".load-me { + color: red; +}" +`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme1) ('dart-sass', 'legacy' API, 'sass' syntax): errors 1`] = `[]`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme1) ('dart-sass', 'legacy' API, 'sass' syntax): warnings 1`] = `[]`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme1) ('dart-sass', 'legacy' API, 'scss' syntax): css 1`] = ` +".load-me { + color: red; +}" +`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme1) ('dart-sass', 'legacy' API, 'scss' syntax): errors 1`] = `[]`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme1) ('dart-sass', 'legacy' API, 'scss' syntax): warnings 1`] = `[]`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme1) ('node-sass', 'legacy' API, 'sass' syntax): css 1`] = ` +".load-me { + color: red; } +" +`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme1) ('node-sass', 'legacy' API, 'sass' syntax): errors 1`] = `[]`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme1) ('node-sass', 'legacy' API, 'sass' syntax): warnings 1`] = `[]`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme1) ('node-sass', 'legacy' API, 'scss' syntax): css 1`] = ` +".load-me { + color: red; } +" +`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme1) ('node-sass', 'legacy' API, 'scss' syntax): errors 1`] = `[]`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme1) ('node-sass', 'legacy' API, 'scss' syntax): warnings 1`] = `[]`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme1) ('sass-embedded', 'legacy' API, 'sass' syntax): css 1`] = ` +".load-me { + color: red; +}" +`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme1) ('sass-embedded', 'legacy' API, 'sass' syntax): errors 1`] = `[]`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme1) ('sass-embedded', 'legacy' API, 'sass' syntax): warnings 1`] = `[]`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme1) ('sass-embedded', 'legacy' API, 'scss' syntax): css 1`] = ` +".load-me { + color: red; +}" +`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme1) ('sass-embedded', 'legacy' API, 'scss' syntax): errors 1`] = `[]`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme1) ('sass-embedded', 'legacy' API, 'scss' syntax): warnings 1`] = `[]`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme2) ('dart-sass', 'legacy' API, 'sass' syntax): css 1`] = ` +".load-me { + color: blue; +}" +`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme2) ('dart-sass', 'legacy' API, 'sass' syntax): errors 1`] = `[]`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme2) ('dart-sass', 'legacy' API, 'sass' syntax): warnings 1`] = `[]`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme2) ('dart-sass', 'legacy' API, 'scss' syntax): css 1`] = ` +".load-me { + color: blue; +}" +`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme2) ('dart-sass', 'legacy' API, 'scss' syntax): errors 1`] = `[]`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme2) ('dart-sass', 'legacy' API, 'scss' syntax): warnings 1`] = `[]`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme2) ('node-sass', 'legacy' API, 'sass' syntax): css 1`] = ` +".load-me { + color: blue; } +" +`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme2) ('node-sass', 'legacy' API, 'sass' syntax): errors 1`] = `[]`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme2) ('node-sass', 'legacy' API, 'sass' syntax): warnings 1`] = `[]`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme2) ('node-sass', 'legacy' API, 'scss' syntax): css 1`] = ` +".load-me { + color: blue; } +" +`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme2) ('node-sass', 'legacy' API, 'scss' syntax): errors 1`] = `[]`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme2) ('node-sass', 'legacy' API, 'scss' syntax): warnings 1`] = `[]`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme2) ('sass-embedded', 'legacy' API, 'sass' syntax): css 1`] = ` +".load-me { + color: blue; +}" +`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme2) ('sass-embedded', 'legacy' API, 'sass' syntax): errors 1`] = `[]`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme2) ('sass-embedded', 'legacy' API, 'sass' syntax): warnings 1`] = `[]`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme2) ('sass-embedded', 'legacy' API, 'scss' syntax): css 1`] = ` +".load-me { + color: blue; +}" +`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme2) ('sass-embedded', 'legacy' API, 'scss' syntax): errors 1`] = `[]`; + +exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme2) ('sass-embedded', 'legacy' API, 'scss' syntax): warnings 1`] = `[]`; + exports[`loader should work with an alias ('dart-sass', 'legacy' API, 'sass' syntax): css 1`] = ` "a { color: red; diff --git a/test/helpers/getCodeFromSass.js b/test/helpers/getCodeFromSass.js index be2085a3..95f9c9c1 100644 --- a/test/helpers/getCodeFromSass.js +++ b/test/helpers/getCodeFromSass.js @@ -4,7 +4,7 @@ import fs from "fs"; import { klona } from "klona/full"; -async function getCodeFromSass(testId, options) { +async function getCodeFromSass(testId, options, context = {}) { const loaderOptions = klona(options); let sassOptions = options.sassOptions || {}; @@ -56,6 +56,27 @@ async function getCodeFromSass(testId, options) { testFolder, "node_modules/package-with-exports/style.scss" ); + const pathToSassPackageWithExportsFieldsAndCustomConditionReplacer = () => { + if (context.packageExportsCustomConditionTestVariant === 1) { + return path.resolve( + testFolder, + "node_modules/package-with-exports-and-custom-condition/style-1.scss" + ); + } + + if (context.packageExportsCustomConditionTestVariant === 2) { + return path.resolve( + testFolder, + "node_modules/package-with-exports-and-custom-condition/style-2.scss" + ); + } + + console.warn( + "Expedted to receive .packageExportsCustomConditionTestVariant to properly resolve stylesheet in sass only compilation. " + ); + return ""; + }; + const pathToSCSSPackageWithIndexFile = path.resolve( testFolder, "node_modules/scss-package-with-index/index.scss" @@ -765,7 +786,11 @@ async function getCodeFromSass(testId, options) { ) .replace(/^~package-with-js-main-field/, pathToPackageWithJsMainField) .replace(/^~package-with-index/, pathToPackageWithIndex) - .replace(/^package-with-exports/, pathToSassPackageWithExportsFields) + .replace( + /^package-with-exports-and-custom-condition$/, + pathToSassPackageWithExportsFieldsAndCustomConditionReplacer + ) + .replace(/^package-with-exports$/, pathToSassPackageWithExportsFields) .replace(/^file:\/\/\/language/, pathToLanguage) .replace(/^\/sass\/language.sass/, pathToLanguage) .replace(/^\/scss\/language.scss/, pathToLanguage) diff --git a/test/loader.test.js b/test/loader.test.js index dd580e08..50f6cebf 100644 --- a/test/loader.test.js +++ b/test/loader.test.js @@ -1037,6 +1037,64 @@ describe("loader", () => { }); } + if (!isModernAPI) { + it(`should work with a package with "sass" and "exports" fields and a custom condition (theme1) ('${implementationName}', '${api}' API, '${syntax}' syntax)`, async () => { + const testId = getTestId( + "import-package-with-exports-and-custom-condition", + syntax + ); + const options = { + implementation, + api, + }; + const compiler = getCompiler(testId, { + loader: { options }, + resolve: { + conditionNames: ["theme1", "..."], + }, + }); + const stats = await compile(compiler); + const codeFromBundle = getCodeFromBundle(stats, compiler); + const codeFromSass = await getCodeFromSass(testId, options, { + packageExportsCustomConditionTestVariant: 1, + }); + + expect(codeFromBundle.css).toBe(codeFromSass.css); + expect(codeFromBundle.css).toMatchSnapshot("css"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); + }); + } + + if (!isModernAPI) { + it(`should work with a package with "sass" and "exports" fields and a custom condition (theme2) ('${implementationName}', '${api}' API, '${syntax}' syntax)`, async () => { + const testId = getTestId( + "import-package-with-exports-and-custom-condition", + syntax + ); + const options = { + implementation, + api, + }; + const compiler = getCompiler(testId, { + loader: { options }, + resolve: { + conditionNames: ["theme2", "..."], + }, + }); + const stats = await compile(compiler); + const codeFromBundle = getCodeFromBundle(stats, compiler); + const codeFromSass = await getCodeFromSass(testId, options, { + packageExportsCustomConditionTestVariant: 2, + }); + + expect(codeFromBundle.css).toBe(codeFromSass.css); + expect(codeFromBundle.css).toMatchSnapshot("css"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); + }); + } + if (!isModernAPI) { it(`should support resolving using the "file" schema ('${implementationName}', '${api}' API, '${syntax}' syntax)`, async () => { const testId = getTestId("import-file-scheme", syntax); diff --git a/test/node_modules/package-with-exports-and-custom-condition/index.cjs b/test/node_modules/package-with-exports-and-custom-condition/index.cjs new file mode 100644 index 00000000..032a82bd --- /dev/null +++ b/test/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/node_modules/package-with-exports-and-custom-condition/index.js b/test/node_modules/package-with-exports-and-custom-condition/index.js new file mode 100644 index 00000000..032a82bd --- /dev/null +++ b/test/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/node_modules/package-with-exports-and-custom-condition/package.json b/test/node_modules/package-with-exports-and-custom-condition/package.json new file mode 100644 index 00000000..8da16219 --- /dev/null +++ b/test/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", + "sass": { + "theme1": "./style-1.scss", + "theme2": "./style-2.scss" + } + } +} diff --git a/test/node_modules/package-with-exports-and-custom-condition/style-1.scss b/test/node_modules/package-with-exports-and-custom-condition/style-1.scss new file mode 100644 index 00000000..b11fa36b --- /dev/null +++ b/test/node_modules/package-with-exports-and-custom-condition/style-1.scss @@ -0,0 +1,3 @@ +.load-me { + color: red; +} diff --git a/test/node_modules/package-with-exports-and-custom-condition/style-2.scss b/test/node_modules/package-with-exports-and-custom-condition/style-2.scss new file mode 100644 index 00000000..cf895141 --- /dev/null +++ b/test/node_modules/package-with-exports-and-custom-condition/style-2.scss @@ -0,0 +1,3 @@ +.load-me { + color: blue; +} diff --git a/test/node_modules/package-with-exports/package.json b/test/node_modules/package-with-exports/package.json index ccbd86d5..c56504d0 100644 --- a/test/node_modules/package-with-exports/package.json +++ b/test/node_modules/package-with-exports/package.json @@ -12,8 +12,8 @@ "main": "index.cjs", "sass": "style.scss", "exports": { - "sass": "./style.scss", "require": "./index.cjs", - "import": "./index.js" + "import": "./index.js", + "sass": "./style.scss" } } diff --git a/test/sass/import-package-with-exports-and-custom-condition.sass b/test/sass/import-package-with-exports-and-custom-condition.sass new file mode 100644 index 00000000..64aab54a --- /dev/null +++ b/test/sass/import-package-with-exports-and-custom-condition.sass @@ -0,0 +1 @@ +@import 'package-with-exports-and-custom-condition' \ No newline at end of file diff --git a/test/scss/import-package-with-exports-and-custom-condition.scss b/test/scss/import-package-with-exports-and-custom-condition.scss new file mode 100644 index 00000000..879bf779 --- /dev/null +++ b/test/scss/import-package-with-exports-and-custom-condition.scss @@ -0,0 +1 @@ +@import 'package-with-exports-and-custom-condition';