Skip to content

Commit

Permalink
feat: allow to extend conditionNames (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Oct 6, 2022
1 parent cbce835 commit 5d76d64
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/utils.js
Expand Up @@ -380,7 +380,7 @@ function mergeBlocks(blocks) {
async function createEvaluator(loaderContext, code, options) {
const fileResolve = loaderContext.getResolve({
dependencyType: "stylus",
conditionNames: ["styl", "stylus", "style"],
conditionNames: ["styl", "stylus", "style", "..."],
mainFields: ["styl", "style", "stylus", "main", "..."],
mainFiles: ["index", "..."],
extensions: [".styl", ".css"],
Expand All @@ -391,7 +391,7 @@ async function createEvaluator(loaderContext, code, options) {
// Get cwd for `fastGlob()`
// No need extra options, because they do not used when `resolveToContext` is `true`
const globResolve = loaderContext.getResolve({
conditionNames: ["styl", "stylus", "style"],
conditionNames: ["styl", "stylus", "style", "..."],
resolveToContext: true,
preferRelative: true,
});
Expand Down
28 changes: 25 additions & 3 deletions test/__snapshots__/loader.test.js.snap
Expand Up @@ -965,7 +965,7 @@ exports[`loader should work and respect the "compress" option with the "true" va
exports[`loader should work and respect the "compress" option with the "true" value: warnings 1`] = `[]`;
exports[`loader should work and respect the 'resolve.byDependecy.less' option: css 1`] = `
exports[`loader should work and respect the 'resolve.byDependency.less' option: css 1`] = `
".b {
color: #f00;
}
Expand All @@ -975,9 +975,9 @@ exports[`loader should work and respect the 'resolve.byDependecy.less' option: c
"
`;
exports[`loader should work and respect the 'resolve.byDependecy.less' option: errors 1`] = `[]`;
exports[`loader should work and respect the 'resolve.byDependency.less' option: errors 1`] = `[]`;
exports[`loader should work and respect the 'resolve.byDependecy.less' option: warnings 1`] = `[]`;
exports[`loader should work and respect the 'resolve.byDependency.less' option: warnings 1`] = `[]`;
exports[`loader should work binop import: css 1`] = `
".not-real-nib {
Expand Down Expand Up @@ -1029,6 +1029,28 @@ exports[`loader should work when stylusOptions is function: errors 1`] = `[]`;
exports[`loader should work when stylusOptions is function: warnings 1`] = `[]`;
exports[`loader should work with a package with "styl" and "exports" fields and a custom condition (theme1): css 1`] = `
".load-me {
color: #f00;
}
"
`;
exports[`loader should work with a package with "styl" and "exports" fields and a custom condition (theme1): errors 1`] = `[]`;
exports[`loader should work with a package with "styl" and "exports" fields and a custom condition (theme1): warnings 1`] = `[]`;
exports[`loader should work with a package with "styl" and "exports" fields and a custom condition (theme2): css 1`] = `
".load-me {
color: #00f;
}
"
`;
exports[`loader should work with a package with "styl" and "exports" fields and a custom condition (theme2): errors 1`] = `[]`;
exports[`loader should work with a package with "styl" and "exports" fields and a custom condition (theme2): warnings 1`] = `[]`;
exports[`loader should work with bootstrap: css 1`] = `
"/*!
* Bootstrap v3.4.1 (http://getbootstrap.com)
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.

24 changes: 22 additions & 2 deletions test/helpers/getCodeFromStylus.js
Expand Up @@ -109,12 +109,32 @@ function evaluator() {
};
}

async function getCodeFromStylus(testId, options = {}) {
async function getCodeFromStylus(testId, options = {}, context = {}) {
const defaultOptions = {
shouldUseWebpackImporter: true,
};
const stylusOptions = options.stylusOptions || {};
let pathToFile = path.resolve(__dirname, "..", "fixtures", testId);

let pathToFile;

if (context.packageExportsCustomConditionTestVariant === 1) {
pathToFile = path.resolve(
__dirname,
"..",
"fixtures",
"node_modules/package-with-exports-and-custom-condition/style-1.styl"
);
} else if (context.packageExportsCustomConditionTestVariant === 2) {
pathToFile = path.resolve(
__dirname,
"..",
"fixtures",
"node_modules/package-with-exports-and-custom-condition/style-2.styl"
);
} else {
pathToFile = path.resolve(__dirname, "..", "fixtures", testId);
}

let data;

try {
Expand Down
56 changes: 55 additions & 1 deletion test/loader.test.js
Expand Up @@ -1705,7 +1705,7 @@ describe("loader", () => {
expect(getErrors(stats)).toMatchSnapshot("errors");
});

it("should work and respect the 'resolve.byDependecy.less' option", async () => {
it("should work and respect the 'resolve.byDependency.less' option", async () => {
const testId = "./by-dependency.styl";
const compiler = getCompiler(
testId,
Expand All @@ -1729,4 +1729,58 @@ describe("loader", () => {
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});

it(`should work with a package with "styl" and "exports" fields and a custom condition (theme1)`, async () => {
const testId = "./import-package-with-exports-and-custom-condition.styl";
const compiler = getCompiler(
testId,
{},
{
resolve: {
conditionNames: ["theme1", "..."],
},
}
);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
const codeFromStylus = await getCodeFromStylus(
testId,
{},
{
packageExportsCustomConditionTestVariant: 1,
}
);

expect(codeFromBundle.css).toBe(codeFromStylus.css);
expect(codeFromBundle.css).toMatchSnapshot("css");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});

it(`should work with a package with "styl" and "exports" fields and a custom condition (theme2)`, async () => {
const testId = "./import-package-with-exports-and-custom-condition.styl";
const compiler = getCompiler(
testId,
{},
{
resolve: {
conditionNames: ["theme2", "..."],
},
}
);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
const codeFromStylus = await getCodeFromStylus(
testId,
{},
{
packageExportsCustomConditionTestVariant: 2,
}
);

expect(codeFromBundle.css).toBe(codeFromStylus.css);
expect(codeFromBundle.css).toMatchSnapshot("css");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});
});

0 comments on commit 5d76d64

Please sign in to comment.