Skip to content

Commit

Permalink
feat: allow to extend conditionNames (#488)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Oct 6, 2022
1 parent c1aa4f5 commit 43cd20c
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/utils.js
Expand Up @@ -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"],
Expand Down
22 changes: 22 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Expand Up @@ -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;
Expand Down
@@ -0,0 +1 @@
@import 'package-with-exports-and-custom-condition';

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions test/fixtures/node_modules/package-with-exports/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 21 additions & 2 deletions test/helpers/getCodeFromLess.js
Expand Up @@ -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,
Expand Down
54 changes: 54 additions & 0 deletions test/loader.test.js
Expand Up @@ -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");
});
});

0 comments on commit 43cd20c

Please sign in to comment.