Skip to content

Commit

Permalink
feat: configuration file support
Browse files Browse the repository at this point in the history
  • Loading branch information
fiadone committed Jan 3, 2022
1 parent f4e029f commit aa412e2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 }`
Expand All @@ -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 }`

Expand Down Expand Up @@ -73,4 +74,6 @@ More in details, a *html* file should look like this:
</script>
```

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).
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.
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -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 })

Expand Down
7 changes: 1 addition & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
15 changes: 15 additions & 0 deletions tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit aa412e2

Please sign in to comment.