From c11a4b88f2994bd0905d606db6972f22119eef1a Mon Sep 17 00:00:00 2001 From: Eric Lau <44459660+erxclau@users.noreply.github.com> Date: Thu, 11 Aug 2022 21:28:39 -0400 Subject: [PATCH] Add basename var to nunjucks (#12) * Add basename var to nunjucks * Change variable name --- dist/index.js | 7 ++++++- dist/index.js.map | 2 +- src/index.js | 6 +++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/dist/index.js b/dist/index.js index aa82f75..d9b688c 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,5 +1,6 @@ var $4BP8v$parcelplugin = require("@parcel/plugin"); var $4BP8v$nunjucks = require("nunjucks"); +var $4BP8v$path = require("path"); function $parcel$defineInteropFlag(a) { Object.defineProperty(a, '__esModule', {value: true, configurable: true}); @@ -13,6 +14,7 @@ $parcel$defineInteropFlag(module.exports); $parcel$export(module.exports, "default", () => $f7f4a66df2960fae$export$2e2bcd8739ae039); + var $f7f4a66df2960fae$export$2e2bcd8739ae039 = new (0, $4BP8v$parcelplugin.Transformer)({ async loadConfig ({ config: config }) { // load config file @@ -39,7 +41,10 @@ var $f7f4a66df2960fae$export$2e2bcd8739ae039 = new (0, $4BP8v$parcelplugin.Trans autoescape: false }); let code = await asset.getCode(); - let result = (0, $4BP8v$nunjucks.renderString)(code, config); + let result = (0, $4BP8v$nunjucks.renderString)(code, { + ...config, + filename: (0, $4BP8v$path.basename)(asset.filePath) + }); asset.setCode(result); return [ asset diff --git a/dist/index.js.map b/dist/index.js.map index e5fe213..0586253 100644 --- a/dist/index.js.map +++ b/dist/index.js.map @@ -1 +1 @@ -{"mappings":";;;;;;;;;;;;;AAAA;;IAGA,wCAgCG,GAhCY,IAAI,CAAA,GAAA,+BAAW,CAAA,CAAC;IAC7B,MAAM,UAAU,EAAC,UAAE,MAAM,CAAA,EAAE,EAAE;QAC3B,mBAAmB;QACnB,MAAM,YAAE,QAAQ,CAAA,EAAE,GAAG,MAAM,MAAM,CAAC,SAAS,CAAC;YAAC,aAAa;SAAC,CAAC,AAAC;QAC7D,8BAA8B;QAC9B,IAAI,MAAM,GAAG,IAAI,AAAC;QAClB,MAAM,OAAO,GAAG,QAAQ,EAAE,KAAK,EAAE,MAAM,CACrC,CAAC,CAAC,GAAK,CAAC,CAAC,CAAC,cAAc,CAAC,SAAS,CAAC,CACpC,AAAC;QACF,IAAI,OAAO,EAAE,MAAM,KAAK,CAAC,EACvB,MAAM,GAAI,CAAA,MAAM,MAAM,CAAC,SAAS,CAAC;YAAC,OAAO,CAAC,CAAC,CAAC,CAAC,MAAM;SAAC,CAAC,CAAA,EAAG,QAAQ,CAAC;aAC5D,IAAI,OAAO,EAAE,MAAM,GAAG,CAAC,EAC5B,MAAM,GAAG,MAAM,OAAO,CAAC,GAAG,CACxB,OAAO,CAAC,GAAG,CACT,OAAO,GAAG,GAAM,CAAA,MAAM,MAAM,CAAC,SAAS,CAAC;gBAAC,GAAG,CAAC,MAAM;aAAC,CAAC,CAAA,EAAG,QAAQ,CAChE,CACF,CAAC;QAGJ,OAAO;YACL,MAAM,EAAE,QAAQ;YAChB,GAAG,EAAE,OAAO,CAAC,GAAG,CAAC,QAAQ;oBACzB,MAAM;SACP,CAAC;KACH;IACD,MAAM,SAAS,EAAC,SAAE,KAAK,CAAA,UAAE,MAAM,CAAA,EAAE,EAAE;QACjC,CAAA,GAAA,yBAAS,CAAA,CAAC;YAAE,UAAU,EAAE,KAAK;SAAE,CAAC,CAAC;QACjC,IAAI,IAAI,GAAG,MAAM,KAAK,CAAC,OAAO,EAAE,AAAC;QACjC,IAAI,MAAM,GAAG,CAAA,GAAA,4BAAY,CAAA,CAAC,IAAI,EAAE,MAAM,CAAC,AAAC;QACxC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QACtB,OAAO;YAAC,KAAK;SAAC,CAAC;KAChB;CACF,CAAC","sources":["src/index.js"],"sourcesContent":["import { Transformer } from \"@parcel/plugin\";\nimport { renderString, configure } from \"nunjucks\";\n\nexport default new Transformer({\n async loadConfig({ config }) {\n // load config file\n const { contents } = await config.getConfig([\"config.json\"]);\n // find archie files to insert\n let archie = null;\n const confaml = contents?.fetch?.filter(\n (d) => !d.hasOwnProperty(\"sheetId\")\n );\n if (confaml?.length === 1) {\n archie = (await config.getConfig([confaml[0].output]))?.contents;\n } else if (confaml?.length > 1) {\n archie = await Promise.all(\n confaml.map(\n async (aml) => (await config.getConfig([aml.output]))?.contents\n )\n );\n }\n\n return {\n config: contents,\n env: process.env.NODE_ENV,\n archie,\n };\n },\n async transform({ asset, config }) {\n configure({ autoescape: false });\n let code = await asset.getCode();\n let result = renderString(code, config);\n asset.setCode(result);\n return [asset];\n },\n});\n"],"names":[],"version":3,"file":"index.js.map","sourceRoot":"../"} \ No newline at end of file +{"mappings":";;;;;;;;;;;;;;AAAA;;;IAIA,wCAmCG,GAnCY,IAAI,CAAA,GAAA,+BAAW,CAAA,CAAC;IAC7B,MAAM,UAAU,EAAC,UAAE,MAAM,CAAA,EAAE,EAAE;QAC3B,mBAAmB;QACnB,MAAM,YAAE,QAAQ,CAAA,EAAE,GAAG,MAAM,MAAM,CAAC,SAAS,CAAC;YAAC,aAAa;SAAC,CAAC,AAAC;QAC7D,8BAA8B;QAC9B,IAAI,MAAM,GAAG,IAAI,AAAC;QAClB,MAAM,OAAO,GAAG,QAAQ,EAAE,KAAK,EAAE,MAAM,CACrC,CAAC,CAAC,GAAK,CAAC,CAAC,CAAC,cAAc,CAAC,SAAS,CAAC,CACpC,AAAC;QACF,IAAI,OAAO,EAAE,MAAM,KAAK,CAAC,EACvB,MAAM,GAAI,CAAA,MAAM,MAAM,CAAC,SAAS,CAAC;YAAC,OAAO,CAAC,CAAC,CAAC,CAAC,MAAM;SAAC,CAAC,CAAA,EAAG,QAAQ,CAAC;aAC5D,IAAI,OAAO,EAAE,MAAM,GAAG,CAAC,EAC5B,MAAM,GAAG,MAAM,OAAO,CAAC,GAAG,CACxB,OAAO,CAAC,GAAG,CACT,OAAO,GAAG,GAAM,CAAA,MAAM,MAAM,CAAC,SAAS,CAAC;gBAAC,GAAG,CAAC,MAAM;aAAC,CAAC,CAAA,EAAG,QAAQ,CAChE,CACF,CAAC;QAGJ,OAAO;YACL,MAAM,EAAE,QAAQ;YAChB,GAAG,EAAE,OAAO,CAAC,GAAG,CAAC,QAAQ;oBACzB,MAAM;SACP,CAAC;KACH;IACD,MAAM,SAAS,EAAC,SAAE,KAAK,CAAA,UAAE,MAAM,CAAA,EAAE,EAAE;QACjC,CAAA,GAAA,yBAAS,CAAA,CAAC;YAAE,UAAU,EAAE,KAAK;SAAE,CAAC,CAAC;QACjC,IAAI,IAAI,GAAG,MAAM,KAAK,CAAC,OAAO,EAAE,AAAC;QACjC,IAAI,MAAM,GAAG,CAAA,GAAA,4BAAY,CAAA,CAAC,IAAI,EAAE;YAC9B,GAAG,MAAM;YACT,QAAQ,EAAE,CAAA,GAAA,oBAAQ,CAAA,CAAC,KAAK,CAAC,QAAQ,CAAC;SACnC,CAAC,AAAC;QACH,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QACtB,OAAO;YAAC,KAAK;SAAC,CAAC;KAChB;CACF,CAAC","sources":["src/index.js"],"sourcesContent":["import { basename } from \"path\";\nimport { Transformer } from \"@parcel/plugin\";\nimport { renderString, configure } from \"nunjucks\";\n\nexport default new Transformer({\n async loadConfig({ config }) {\n // load config file\n const { contents } = await config.getConfig([\"config.json\"]);\n // find archie files to insert\n let archie = null;\n const confaml = contents?.fetch?.filter(\n (d) => !d.hasOwnProperty(\"sheetId\")\n );\n if (confaml?.length === 1) {\n archie = (await config.getConfig([confaml[0].output]))?.contents;\n } else if (confaml?.length > 1) {\n archie = await Promise.all(\n confaml.map(\n async (aml) => (await config.getConfig([aml.output]))?.contents\n )\n );\n }\n\n return {\n config: contents,\n env: process.env.NODE_ENV,\n archie,\n };\n },\n async transform({ asset, config }) {\n configure({ autoescape: false });\n let code = await asset.getCode();\n let result = renderString(code, {\n ...config,\n filename: basename(asset.filePath),\n });\n asset.setCode(result);\n return [asset];\n },\n});\n"],"names":[],"version":3,"file":"index.js.map","sourceRoot":"../"} \ No newline at end of file diff --git a/src/index.js b/src/index.js index 549fa80..f5c579a 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,4 @@ +import { basename } from "path"; import { Transformer } from "@parcel/plugin"; import { renderString, configure } from "nunjucks"; @@ -29,7 +30,10 @@ export default new Transformer({ async transform({ asset, config }) { configure({ autoescape: false }); let code = await asset.getCode(); - let result = renderString(code, config); + let result = renderString(code, { + ...config, + filename: basename(asset.filePath), + }); asset.setCode(result); return [asset]; },