Skip to content

Commit db3ccf3

Browse files
committed
feat(loader): add functions support for locals
1 parent 866abbe commit db3ccf3

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

Diff for: src/loader.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {
88
BASE_URI,
99
SINGLE_DOT_PATH_SEGMENT,
1010
stringifyRequest,
11+
stringifyLocal,
1112
} = require("./utils");
1213
const schema = require("./loader-options.json");
1314

@@ -22,6 +23,7 @@ const MiniCssExtractPlugin = require("./index");
2223
/** @typedef {import("webpack").AssetInfo} AssetInfo */
2324
/** @typedef {import("webpack").NormalModule} NormalModule */
2425
/** @typedef {import("./index.js").LoaderOptions} LoaderOptions */
26+
/** @typedef {{ [key: string]: string | function } | function} Locals */
2527

2628
/** @typedef {any} TODO */
2729

@@ -38,7 +40,7 @@ const MiniCssExtractPlugin = require("./index");
3840

3941
/**
4042
* @param {string} content
41-
* @param {{ loaderContext: import("webpack").LoaderContext<LoaderOptions>, options: LoaderOptions, locals: {[key: string]: string } | undefined }} context
43+
* @param {{ loaderContext: import("webpack").LoaderContext<LoaderOptions>, options: LoaderOptions, locals: Locals | undefined }} context
4244
* @returns {string}
4345
*/
4446
function hotLoader(content, context) {
@@ -95,7 +97,7 @@ function pitch(request) {
9597
* @returns {void}
9698
*/
9799
const handleExports = (originalExports, compilation, assets, assetsInfo) => {
98-
/** @type {{[key: string]: string } | undefined} */
100+
/** @type {Locals | undefined} */
99101
let locals;
100102
let namedExport;
101103

@@ -170,7 +172,8 @@ function pitch(request) {
170172
locals = {};
171173
}
172174

173-
locals[key] = originalExports[key];
175+
/** @type {{ [key: string]: string }} */ (locals)[key] =
176+
originalExports[key];
174177
}
175178
});
176179
} else {
@@ -228,15 +231,14 @@ function pitch(request) {
228231
? Object.keys(locals)
229232
.map(
230233
(key) =>
231-
`\nexport var ${key} = ${JSON.stringify(
232-
/** @type {{[key: string]: string }} */
233-
(locals)[key]
234+
`\nexport var ${key} = ${stringifyLocal(
235+
/** @type {{ [key: string]: string }} */ (locals)[key]
234236
)};`
235237
)
236238
.join("")
237239
: `\n${
238240
esModule ? "export default" : "module.exports ="
239-
} ${JSON.stringify(locals)};`
241+
} ${stringifyLocal(/** @type {function} */ (locals))};`
240242
: esModule
241243
? `\nexport {};`
242244
: "";

Diff for: src/utils.js

+10
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,15 @@ function getUndoPath(filename, outputPath, enforceRelative) {
205205
: append;
206206
}
207207

208+
/**
209+
*
210+
* @param {string | function} value
211+
* @returns {string}
212+
*/
213+
function stringifyLocal(value) {
214+
return typeof value === "function" ? value.toString() : JSON.stringify(value);
215+
}
216+
208217
module.exports = {
209218
trueFn,
210219
findModuleById,
@@ -216,5 +225,6 @@ module.exports = {
216225
BASE_URI,
217226
SINGLE_DOT_PATH_SEGMENT,
218227
stringifyRequest,
228+
stringifyLocal,
219229
getUndoPath,
220230
};

0 commit comments

Comments
 (0)