Skip to content

Commit 43cd20c

Browse files
feat: allow to extend conditionNames (#488)
1 parent c1aa4f5 commit 43cd20c

File tree

11 files changed

+131
-5
lines changed

11 files changed

+131
-5
lines changed

src/utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const MODULE_REQUEST_REGEX = /^[^?]*~/;
3434
function createWebpackLessPlugin(loaderContext, implementation) {
3535
const resolve = loaderContext.getResolve({
3636
dependencyType: "less",
37-
conditionNames: ["less", "style"],
37+
conditionNames: ["less", "style", "..."],
3838
mainFields: ["less", "style", "main", "..."],
3939
mainFiles: ["index", "..."],
4040
extensions: [".less", ".css"],

test/__snapshots__/loader.test.js.snap

+22
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,28 @@ exports[`loader should work third-party plugins as fileLoader: errors 1`] = `[]`
583583
584584
exports[`loader should work third-party plugins as fileLoader: warnings 1`] = `[]`;
585585
586+
exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme1): css 1`] = `
587+
".load-me {
588+
color: red;
589+
}
590+
"
591+
`;
592+
593+
exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme1): errors 1`] = `[]`;
594+
595+
exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme1): warnings 1`] = `[]`;
596+
597+
exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme2): css 1`] = `
598+
".load-me {
599+
color: blue;
600+
}
601+
"
602+
`;
603+
604+
exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme2): errors 1`] = `[]`;
605+
606+
exports[`loader should work with a package with "sass" and "exports" fields and a custom condition (theme2): warnings 1`] = `[]`;
607+
586608
exports[`loader should work: css 1`] = `
587609
".box {
588610
color: #fe33ac;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import 'package-with-exports-and-custom-condition';

test/fixtures/node_modules/package-with-exports-and-custom-condition/index.cjs

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/node_modules/package-with-exports-and-custom-condition/index.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/node_modules/package-with-exports-and-custom-condition/package.json

+22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/node_modules/package-with-exports-and-custom-condition/style-1.less

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/node_modules/package-with-exports-and-custom-condition/style-2.less

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/node_modules/package-with-exports/package.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/helpers/getCodeFromLess.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,27 @@ class CustomImportPlugin {
150150
}
151151
}
152152

153-
async function getCodeFromLess(testId, options = {}) {
154-
const pathToFile = path.resolve(__dirname, "..", "fixtures", testId);
153+
async function getCodeFromLess(testId, options = {}, context = {}) {
154+
let pathToFile;
155+
156+
if (context.packageExportsCustomConditionTestVariant === 1) {
157+
pathToFile = path.resolve(
158+
__dirname,
159+
"..",
160+
"fixtures",
161+
"node_modules/package-with-exports-and-custom-condition/style-1.less"
162+
);
163+
} else if (context.packageExportsCustomConditionTestVariant === 2) {
164+
pathToFile = path.resolve(
165+
__dirname,
166+
"..",
167+
"fixtures",
168+
"node_modules/package-with-exports-and-custom-condition/style-2.less"
169+
);
170+
} else {
171+
pathToFile = path.resolve(__dirname, "..", "fixtures", testId);
172+
}
173+
155174
const defaultOptions = {
156175
plugins: [],
157176
relativeUrls: true,

test/loader.test.js

+54
Original file line numberDiff line numberDiff line change
@@ -937,4 +937,58 @@ describe("loader", () => {
937937
expect(getWarnings(stats)).toMatchSnapshot("warnings");
938938
expect(getErrors(stats)).toMatchSnapshot("errors");
939939
});
940+
941+
it(`should work with a package with "sass" and "exports" fields and a custom condition (theme1)`, async () => {
942+
const testId = "./import-package-with-exports-and-custom-condition.less";
943+
const compiler = getCompiler(
944+
testId,
945+
{},
946+
{
947+
resolve: {
948+
conditionNames: ["theme1", "..."],
949+
},
950+
}
951+
);
952+
const stats = await compile(compiler);
953+
const codeFromBundle = getCodeFromBundle(stats, compiler);
954+
const codeFromLess = await getCodeFromLess(
955+
testId,
956+
{},
957+
{
958+
packageExportsCustomConditionTestVariant: 1,
959+
}
960+
);
961+
962+
expect(codeFromBundle.css).toBe(codeFromLess.css);
963+
expect(codeFromBundle.css).toMatchSnapshot("css");
964+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
965+
expect(getErrors(stats)).toMatchSnapshot("errors");
966+
});
967+
968+
it(`should work with a package with "sass" and "exports" fields and a custom condition (theme2)`, async () => {
969+
const testId = "./import-package-with-exports-and-custom-condition.less";
970+
const compiler = getCompiler(
971+
testId,
972+
{},
973+
{
974+
resolve: {
975+
conditionNames: ["theme2", "..."],
976+
},
977+
}
978+
);
979+
const stats = await compile(compiler);
980+
const codeFromBundle = getCodeFromBundle(stats, compiler);
981+
const codeFromLess = await getCodeFromLess(
982+
testId,
983+
{},
984+
{
985+
packageExportsCustomConditionTestVariant: 2,
986+
}
987+
);
988+
989+
expect(codeFromBundle.css).toBe(codeFromLess.css);
990+
expect(codeFromBundle.css).toMatchSnapshot("css");
991+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
992+
expect(getErrors(stats)).toMatchSnapshot("errors");
993+
});
940994
});

0 commit comments

Comments
 (0)