Skip to content

Commit

Permalink
feat(loader): add functions support for locals
Browse files Browse the repository at this point in the history
  • Loading branch information
yungvldai committed Oct 24, 2022
1 parent 866abbe commit 6f77727
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/loader.js
Expand Up @@ -8,6 +8,7 @@ const {
BASE_URI,
SINGLE_DOT_PATH_SEGMENT,
stringifyRequest,
stringifyLocals,
} = require("./utils");
const schema = require("./loader-options.json");

Expand Down Expand Up @@ -228,15 +229,14 @@ function pitch(request) {
? Object.keys(locals)
.map(
(key) =>
`\nexport var ${key} = ${JSON.stringify(
/** @type {{[key: string]: string }} */
(locals)[key]
`\nexport var ${key} = ${stringifyLocals(
/** @type {{ [key: string]: string }} */ (locals)[key]
)};`
)
.join("")
: `\n${
esModule ? "export default" : "module.exports ="
} ${JSON.stringify(locals)};`
} ${stringifyLocals(locals)};`
: esModule
? `\nexport {};`
: "";
Expand Down
12 changes: 12 additions & 0 deletions src/utils.js
Expand Up @@ -205,6 +205,17 @@ function getUndoPath(filename, outputPath, enforceRelative) {
: append;
}

/**
*
* @param {*} object
* @returns {string}
*/
function stringifyLocals(object) {
return JSON.stringify(object, (key, value) =>
typeof value === "function" ? value.toString() : value
);
}

module.exports = {
trueFn,
findModuleById,
Expand All @@ -216,5 +227,6 @@ module.exports = {
BASE_URI,
SINGLE_DOT_PATH_SEGMENT,
stringifyRequest,
stringifyLocals,
getUndoPath,
};

0 comments on commit 6f77727

Please sign in to comment.