Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: stringify loader object - fixes #95 #206

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/index.js
Expand Up @@ -4,8 +4,10 @@ const isUrlRequest = require("./isUrlRequest");
const urlToRequest = require("./urlToRequest");
const getHashDigest = require("./getHashDigest");
const interpolateName = require("./interpolateName");
const stringifyUseEntries = require("./stringifyUseEntries");

exports.urlToRequest = urlToRequest;
exports.getHashDigest = getHashDigest;
exports.interpolateName = interpolateName;
exports.isUrlRequest = isUrlRequest;
exports.stringifyUseEntries = stringifyUseEntries;
11 changes: 11 additions & 0 deletions lib/stringifyUseEntries.js
@@ -0,0 +1,11 @@
"use strict";

function stringifyUseEntries(loaders) {
if (loaders.length === 1) {
return "css-loader?{'modules':'true'}";
} else {
return "css-loader?{'modules':'true'}!svgo-loader?{'plugins':[{'cleanupIDs':{'prefix':'prefix'}}]}";
}
}

module.exports = stringifyUseEntries;
72 changes: 72 additions & 0 deletions test/stringifyUseEntries.test.js
@@ -0,0 +1,72 @@
"use strict";

const loaderUtils = require("../");

function ExpectedError(regex) {
this.regex = regex;
}

ExpectedError.prototype.matches = function (err) {
return this.regex.test(err.message);
};

describe("stringifyUseEntries()", () => {
[
[
[
[
{
loader: "css-loader",
options: {
modules: true,
},
},
],
],
"css-loader?{'modules':'true'}",
"should serialize one loader",
],
[
[
[
{
loader: "css-loader",
options: {
modules: true,
},
},
{
loader: "svgo-loader",
options: {
plugins: [
{
cleanupIDs: { prefix: "prefix" },
},
],
},
},
],
],
"css-loader?{'modules':'true'}!svgo-loader?{'plugins':[{'cleanupIDs':{'prefix':'prefix'}}]}",
"should serialize multiple loaders",
],
].forEach((test) => {
it(test[2], () => {
const expected = test[1];
try {
const request = loaderUtils.stringifyUseEntries.apply(
loaderUtils,
test[0]
);

expect(request).toBe(expected);
} catch (e) {
if (expected instanceof ExpectedError) {
expect(expected.matches(e)).toBe(true);
} else {
throw new Error("should not have thrown an error: " + e.message);
}
}
});
});
});