From aa412e2d817c85777e9ffefbfa57e2944ea3a53a Mon Sep 17 00:00:00 2001 From: fiadone Date: Mon, 3 Jan 2022 19:00:29 +0100 Subject: [PATCH] feat: configuration file support --- README.md | 9 ++++++--- index.js | 11 +++++++++-- package.json | 7 +------ tasks.js | 15 +++++++++++++++ 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index d1a1899..e5bd215 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,9 @@ export default { ``` ### Options -An options object can also be passed as argument with the properties listed here below. +The plugin can be configured both via the *twig.config.js* file from the project root or by passing a configuration object directly as argument to the function above (in this last case, the configuration file will be ignored). + +Here below the list of the supported options. #### `filters` __type__ `{ [key: string]: (...args: any[]) => any }` @@ -33,7 +35,6 @@ __default__ `{}` A collection of custom filters to extend *Twig*. Look at [*twig.js* documentation](https://github.com/twigjs/twig.js/wiki/Extending-twig.js) to learn more. - #### `functions` __type__ `{ [key: string]: (...args: any[]) => any }` @@ -73,4 +74,6 @@ More in details, a *html* file should look like this: ``` -where `template` is the path of the *twig* template to be rendered (relative to the *cwd*), and `data` is the local context for that page (eventually merged with the *globals* provided via plugin options). \ No newline at end of file +where `template` is the path of the *twig* template to be rendered (relative to the *cwd*), and `data` is the local context for that page (eventually merged with the *globals* provided via plugin options). + +> ℹ️ The *script* tag is not mandatory, since a plain text representation of the *json* will be correctly parsed too. However, it is recommended for readability and syntax highlighting purposes. \ No newline at end of file diff --git a/index.js b/index.js index ec43324..ab1cdc2 100644 --- a/index.js +++ b/index.js @@ -1,12 +1,19 @@ // @ts-check const { extname } = require('path') -const { configureTwig, parseHTML, renderTemplate } = require('./tasks') +const { retrieveOptions, configureTwig, parseHTML, renderTemplate } = require('./tasks') /** * @param {import('.').Options} options * @returns {import('vite').Plugin} */ -function viteTwigPlugin({ filters = {}, functions = {}, globals = {}, settings = {} } = {}) { +function viteTwigPlugin(options) { + + const { + filters = {}, + functions = {}, + globals = {}, + settings = {} + } = options || retrieveOptions() configureTwig({ filters, functions }) diff --git a/package.json b/package.json index f28ee3d..8d7e9c0 100644 --- a/package.json +++ b/package.json @@ -1,17 +1,12 @@ { "name": "vite-plugin-twig", - "version": "1.0.0", + "version": "1.1.0", "license": "MIT", "keywords": [ "vite-plugin", "twig" ], "author": "Andrea \"Fiad\" Fiadone", - "files": [ - "index.js", - "index.d.ts", - "tasks.js" - ], "main": "index.js", "types": "index.d.ts", "repository": { diff --git a/tasks.js b/tasks.js index 3fd1ac5..6df7057 100644 --- a/tasks.js +++ b/tasks.js @@ -2,6 +2,20 @@ const process = require('process') const path = require('path') const Twig = require('twig') +/** + * Retrieves options from the configuration file + * @returns {object} + */ + function retrieveOptions() { + try { + const config = path.resolve(process.cwd(), 'twig.config.js') + return require(config) + } catch (err) { + return {} + } +} + + /** * It handles Twig configuration and extension * @param {object} extensions @@ -47,6 +61,7 @@ function renderTemplate(template, context, settings) { }) } +module.exports.retrieveOptions = retrieveOptions module.exports.configureTwig = configureTwig module.exports.parseHTML = parseHTML module.exports.renderTemplate = renderTemplate \ No newline at end of file