Skip to content

Commit 6f77727

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

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/loader.js

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

@@ -228,15 +229,14 @@ function pitch(request) {
228229
? Object.keys(locals)
229230
.map(
230231
(key) =>
231-
`\nexport var ${key} = ${JSON.stringify(
232-
/** @type {{[key: string]: string }} */
233-
(locals)[key]
232+
`\nexport var ${key} = ${stringifyLocals(
233+
/** @type {{ [key: string]: string }} */ (locals)[key]
234234
)};`
235235
)
236236
.join("")
237237
: `\n${
238238
esModule ? "export default" : "module.exports ="
239-
} ${JSON.stringify(locals)};`
239+
} ${stringifyLocals(locals)};`
240240
: esModule
241241
? `\nexport {};`
242242
: "";

src/utils.js

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

208+
/**
209+
*
210+
* @param {*} object
211+
* @returns {string}
212+
*/
213+
function stringifyLocals(object) {
214+
return JSON.stringify(object, (key, value) =>
215+
typeof value === "function" ? value.toString() : value
216+
);
217+
}
218+
208219
module.exports = {
209220
trueFn,
210221
findModuleById,
@@ -216,5 +227,6 @@ module.exports = {
216227
BASE_URI,
217228
SINGLE_DOT_PATH_SEGMENT,
218229
stringifyRequest,
230+
stringifyLocals,
219231
getUndoPath,
220232
};

0 commit comments

Comments
 (0)