Skip to content

Commit 5d76d64

Browse files
feat: allow to extend conditionNames (#365)
1 parent cbce835 commit 5d76d64

File tree

10 files changed

+134
-8
lines changed

10 files changed

+134
-8
lines changed

src/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ function mergeBlocks(blocks) {
380380
async function createEvaluator(loaderContext, code, options) {
381381
const fileResolve = loaderContext.getResolve({
382382
dependencyType: "stylus",
383-
conditionNames: ["styl", "stylus", "style"],
383+
conditionNames: ["styl", "stylus", "style", "..."],
384384
mainFields: ["styl", "style", "stylus", "main", "..."],
385385
mainFiles: ["index", "..."],
386386
extensions: [".styl", ".css"],
@@ -391,7 +391,7 @@ async function createEvaluator(loaderContext, code, options) {
391391
// Get cwd for `fastGlob()`
392392
// No need extra options, because they do not used when `resolveToContext` is `true`
393393
const globResolve = loaderContext.getResolve({
394-
conditionNames: ["styl", "stylus", "style"],
394+
conditionNames: ["styl", "stylus", "style", "..."],
395395
resolveToContext: true,
396396
preferRelative: true,
397397
});

test/__snapshots__/loader.test.js.snap

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,7 @@ exports[`loader should work and respect the "compress" option with the "true" va
965965
966966
exports[`loader should work and respect the "compress" option with the "true" value: warnings 1`] = `[]`;
967967
968-
exports[`loader should work and respect the 'resolve.byDependecy.less' option: css 1`] = `
968+
exports[`loader should work and respect the 'resolve.byDependency.less' option: css 1`] = `
969969
".b {
970970
color: #f00;
971971
}
@@ -975,9 +975,9 @@ exports[`loader should work and respect the 'resolve.byDependecy.less' option: c
975975
"
976976
`;
977977
978-
exports[`loader should work and respect the 'resolve.byDependecy.less' option: errors 1`] = `[]`;
978+
exports[`loader should work and respect the 'resolve.byDependency.less' option: errors 1`] = `[]`;
979979
980-
exports[`loader should work and respect the 'resolve.byDependecy.less' option: warnings 1`] = `[]`;
980+
exports[`loader should work and respect the 'resolve.byDependency.less' option: warnings 1`] = `[]`;
981981
982982
exports[`loader should work binop import: css 1`] = `
983983
".not-real-nib {
@@ -1029,6 +1029,28 @@ exports[`loader should work when stylusOptions is function: errors 1`] = `[]`;
10291029
10301030
exports[`loader should work when stylusOptions is function: warnings 1`] = `[]`;
10311031
1032+
exports[`loader should work with a package with "styl" and "exports" fields and a custom condition (theme1): css 1`] = `
1033+
".load-me {
1034+
color: #f00;
1035+
}
1036+
"
1037+
`;
1038+
1039+
exports[`loader should work with a package with "styl" and "exports" fields and a custom condition (theme1): errors 1`] = `[]`;
1040+
1041+
exports[`loader should work with a package with "styl" and "exports" fields and a custom condition (theme1): warnings 1`] = `[]`;
1042+
1043+
exports[`loader should work with a package with "styl" and "exports" fields and a custom condition (theme2): css 1`] = `
1044+
".load-me {
1045+
color: #00f;
1046+
}
1047+
"
1048+
`;
1049+
1050+
exports[`loader should work with a package with "styl" and "exports" fields and a custom condition (theme2): errors 1`] = `[]`;
1051+
1052+
exports[`loader should work with a package with "styl" and "exports" fields and a custom condition (theme2): warnings 1`] = `[]`;
1053+
10321054
exports[`loader should work with bootstrap: css 1`] = `
10331055
"/*!
10341056
* Bootstrap v3.4.1 (http://getbootstrap.com)
Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 21 additions & 0 deletions
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.styl

Lines changed: 3 additions & 0 deletions
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.styl

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/helpers/getCodeFromStylus.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,32 @@ function evaluator() {
109109
};
110110
}
111111

112-
async function getCodeFromStylus(testId, options = {}) {
112+
async function getCodeFromStylus(testId, options = {}, context = {}) {
113113
const defaultOptions = {
114114
shouldUseWebpackImporter: true,
115115
};
116116
const stylusOptions = options.stylusOptions || {};
117-
let pathToFile = path.resolve(__dirname, "..", "fixtures", testId);
117+
118+
let pathToFile;
119+
120+
if (context.packageExportsCustomConditionTestVariant === 1) {
121+
pathToFile = path.resolve(
122+
__dirname,
123+
"..",
124+
"fixtures",
125+
"node_modules/package-with-exports-and-custom-condition/style-1.styl"
126+
);
127+
} else if (context.packageExportsCustomConditionTestVariant === 2) {
128+
pathToFile = path.resolve(
129+
__dirname,
130+
"..",
131+
"fixtures",
132+
"node_modules/package-with-exports-and-custom-condition/style-2.styl"
133+
);
134+
} else {
135+
pathToFile = path.resolve(__dirname, "..", "fixtures", testId);
136+
}
137+
118138
let data;
119139

120140
try {

test/loader.test.js

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1705,7 +1705,7 @@ describe("loader", () => {
17051705
expect(getErrors(stats)).toMatchSnapshot("errors");
17061706
});
17071707

1708-
it("should work and respect the 'resolve.byDependecy.less' option", async () => {
1708+
it("should work and respect the 'resolve.byDependency.less' option", async () => {
17091709
const testId = "./by-dependency.styl";
17101710
const compiler = getCompiler(
17111711
testId,
@@ -1729,4 +1729,58 @@ describe("loader", () => {
17291729
expect(getWarnings(stats)).toMatchSnapshot("warnings");
17301730
expect(getErrors(stats)).toMatchSnapshot("errors");
17311731
});
1732+
1733+
it(`should work with a package with "styl" and "exports" fields and a custom condition (theme1)`, async () => {
1734+
const testId = "./import-package-with-exports-and-custom-condition.styl";
1735+
const compiler = getCompiler(
1736+
testId,
1737+
{},
1738+
{
1739+
resolve: {
1740+
conditionNames: ["theme1", "..."],
1741+
},
1742+
}
1743+
);
1744+
const stats = await compile(compiler);
1745+
const codeFromBundle = getCodeFromBundle(stats, compiler);
1746+
const codeFromStylus = await getCodeFromStylus(
1747+
testId,
1748+
{},
1749+
{
1750+
packageExportsCustomConditionTestVariant: 1,
1751+
}
1752+
);
1753+
1754+
expect(codeFromBundle.css).toBe(codeFromStylus.css);
1755+
expect(codeFromBundle.css).toMatchSnapshot("css");
1756+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
1757+
expect(getErrors(stats)).toMatchSnapshot("errors");
1758+
});
1759+
1760+
it(`should work with a package with "styl" and "exports" fields and a custom condition (theme2)`, async () => {
1761+
const testId = "./import-package-with-exports-and-custom-condition.styl";
1762+
const compiler = getCompiler(
1763+
testId,
1764+
{},
1765+
{
1766+
resolve: {
1767+
conditionNames: ["theme2", "..."],
1768+
},
1769+
}
1770+
);
1771+
const stats = await compile(compiler);
1772+
const codeFromBundle = getCodeFromBundle(stats, compiler);
1773+
const codeFromStylus = await getCodeFromStylus(
1774+
testId,
1775+
{},
1776+
{
1777+
packageExportsCustomConditionTestVariant: 2,
1778+
}
1779+
);
1780+
1781+
expect(codeFromBundle.css).toBe(codeFromStylus.css);
1782+
expect(codeFromBundle.css).toMatchSnapshot("css");
1783+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
1784+
expect(getErrors(stats)).toMatchSnapshot("errors");
1785+
});
17321786
});

0 commit comments

Comments
 (0)