Commit 348400a6 authored by Eric MORAND's avatar Eric MORAND
Browse files

Resolve issue #42

parent ff2e5b19
Loading
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -2,12 +2,17 @@
  "name": "twing-loader",
  "version": "0.0.0",
  "main": "dist/index.js",
  "types": "dist/index.d.ts",
  "scripts": {
    "build": "npm run clean && tsc --project src",
    "build": "npm run clean && tsc --project src --declaration && cp src/twig.d.ts dist && echo 'import \"./twig\"' >> dist/index.d.ts",
    "build:test": "npm run clean && tsc --project src --inlineSourceMap --inlineSources",
    "clean": "rimraf dist",
    "test": "ts-node node_modules/tape/bin/tape 'test/**/test.ts'"
  },
  "files": [
    "dist",
    "README.md"
  ],
  "keywords": [
    "twig",
    "twing",
+4 −20
Original line number Diff line number Diff line
@@ -13,11 +13,9 @@ Webpack loader for Twig templates, based on [Twing](https://www.npmjs.com/packag

## Usage

twing-loader comes with two available behaviors. Depending on your need, you can use one or the other by setting the `renderContext` option accordingly.
twing-loader is a very straightforward Webpack loader, requiring only one option - the `environment` that is used to compile the templates.

### Render at runtime

<sub>webpack.config.js</sub>
<sub>webpack.config.mjs</sub>

```javascript
import {createEnvironment, createFilesystemLoader} from "twing";
@@ -50,9 +48,9 @@ module.exports = {
{{ foo }}
```

<sub>index.js</sub>
<sub>index.mjs</sub>

```javascript
```typescript
import {createEnvironment, createArrayLoader} from "twing";
import template from "./index.twig";

@@ -65,20 +63,6 @@ template.render(environment, {
});
```

Note that to import Twig files in TypeScript sources, you need to make the compiler aware of the existence of the `*.twig` type by either referencing the type in the code...

```ts
/// <reference types="twing-loader" />
import {createEnvironment, createArrayLoader} from "twing";
import {render} from "../templates/index.twig";

const environment = createEnvironment(createArrayLoader({}));

render(environment, {}).then(console.log);
```

...or by adding `<path to node_modules>/twing-loader` to the `typeRoots` entry of your TypeScript configuration, as explained [there](https://www.typescriptlang.org/tsconfig#typeRoots).

## Options

| Name        | Required | Type                               | Description                                                                          |
+8 −2
Original line number Diff line number Diff line
declare module "*.twig" {
    export const execute: import("twing").TwingTemplate["execute"];
    export const render: import("twing").TwingTemplate["render"];
    import type {TwingTemplate} from "twing";

    const Template: {
        execute: TwingTemplate["execute"],
        render: TwingTemplate["render"]
    };

    export default Template;
}