Skip to content

feat: return the [folder] template #1372

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,30 @@ function escapeLocalIdent(localident) {
);
}

function resolveFolderTemplate(loaderContext, localIdentName, options) {
const { context } = options;
let { resourcePath } = loaderContext;
const parsed = path.parse(loaderContext.resourcePath);

if (parsed.dir) {
resourcePath = parsed.dir + path.sep;
}

let directory = path
.relative(context, `${resourcePath}_`)
.replace(/\\/g, "/")
.replace(/\.\.(\/)?/g, "_$1");
directory = directory.substr(0, directory.length - 1);

let folder = "";

if (directory.length > 1) {
folder = path.basename(directory);
}

return localIdentName.replace(/\[folder\]/gi, () => folder);
}

function defaultGetLocalIdent(
loaderContext,
localIdentName,
Expand Down Expand Up @@ -387,19 +411,33 @@ function defaultGetLocalIdent(
};

// eslint-disable-next-line no-underscore-dangle
let result = loaderContext._compilation.getPath(localIdentName, data);
let interpolatedFilename = loaderContext._compilation.getPath(
localIdentName,
data
);

if (options.regExp) {
const match = loaderContext.resourcePath.match(options.regExp);

if (match) {
match.forEach((matched, i) => {
result = result.replace(new RegExp(`\\[${i}\\]`, "ig"), matched);
interpolatedFilename = interpolatedFilename.replace(
new RegExp(`\\[${i}\\]`, "ig"),
matched
);
});
}
}

return result;
if (localIdentName.includes("[folder]")) {
interpolatedFilename = resolveFolderTemplate(
loaderContext,
interpolatedFilename,
options
);
}

return interpolatedFilename;
}

function fixedEncodeURIComponent(str) {
Expand Down
40 changes: 40 additions & 0 deletions test/modules-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1909,6 +1909,46 @@ describe('"modules" option', () => {
expect(getErrors(stats)).toMatchSnapshot("errors");
});

it("should work with [folder]", async () => {
const compiler = getCompiler("./modules/localIdentName/localIdentName.js", {
modules: { localIdentName: "[local]-[folder]-[name]" },
});
const stats = await compile(compiler);

expect(
getModuleSource("./modules/localIdentName/localIdentName.css", stats)
).toMatchSnapshot("module");
expect(getExecutedCode("main.bundle.js", compiler, stats)).toMatchSnapshot(
"result"
);
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});

it("should work with [folder] 2", async () => {
const compiler = getCompiler("./modules/localIdentName/localIdentName.js", {
modules: {
localIdentName: "[local]-[folder][name]",
localIdentContext: path.resolve(
__dirname,
"fixtures",
"modules",
"localIdentName"
),
},
});
const stats = await compile(compiler);

expect(
getModuleSource("./modules/localIdentName/localIdentName.css", stats)
).toMatchSnapshot("module");
expect(getExecutedCode("main.bundle.js", compiler, stats)).toMatchSnapshot(
"result"
);
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});

it('should work and prefer relative for "composes"', async () => {
const compiler = getCompiler("./modules/prefer-relative/source.js", {
modules: { mode: "local" },
Expand Down