8000 feat: Consolidate Plugin - Pass baseDir & includeDir as options (#1225) · fuse-box/fuse-box@26e7ebe · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
8000
This repository was archived by the owner on Jun 20, 2023. It is now read-only.

Commit 26e7ebe

Browse files
Kompwunchanged
authored andcommitted
feat: Consolidate Plugin - Pass baseDir & includeDir as options (#1225)
1 parent 0b138f2 commit 26e7ebe

File tree

3 files changed

+87
-2
lines changed

3 files changed

+87
-2
lines changed

docs/plugins/html/ConsolidatePlugin.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,49 @@ ConsolidatePlugin({ engine: 'pug', extension: '.custom-extension' }) // will pro
6666

6767
You can mix and match as you please.
6868

69+
### baseDir
70+
The `baseDir` option allows you to set the root path
71+
```js
72+
ConsolidatePlugin({ baseDir: 'my/root/path' })
73+
```
74+
75+
### includeDir
76+
The `includeDir` option allows you to set the root path to include something
77+
```js
78+
ConsolidatePlugin({ engine: 'my/include/root/' })
79+
```
80+
81+
Also, it allows to avoid `../../../../../` mess for example let's take a look at this `pug` file:
82+
83+
```pug
84+
extends ../layouts/base
85+
block content
86+
include ../partials/header
87+
include ../partials/side-nav
88+
89+
+header
90+
+side-nav
91+
section
92+
h2 I love ConsolidatePlugin !
93+
```
94+
*the `pug` file is located in `views/`*
95+
96+
As you can see, the `pug` file extends and includes other `pug` files.
97+
By setting the `includeDir` option it allows you to avoid doing `../` to go outside `views/`.
98+
So it'll look like this:
99+
100+
```pug
101+
extends /layouts/base
102+
block content
103+
include /partials/header
104+
include /partials/side-nav
105+
106+
+header
107+
+side-nav
108+
section
109+
h2 I love ConsolidatePlugin !
110+
```
111+
69112
### useDefault
70113
`useDefault` is enable by default. So the transpiled output would look like:
71114

src/plugins/ConsolidatePlugin.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ export interface IConsolidatePluginOptions {
55
engine: string;
66
extension?: string;
77
useDefault?: boolean;
8+
baseDir?: string;
9+
includeDir?: string;
810
}
911

1012
export class ConsolidatePluginClass implements Plugin {
1113
public test: RegExp;
1214
private extension: string;
1315
private engine: string;
1416
private useDefault: boolean;
17+
private baseDir: string;
18+
private includeDir: string;
1519

1620
constructor(options: IConsolidatePluginOptions) {
1721
if (!options.engine) {
@@ -22,6 +26,8 @@ export class ConsolidatePluginClass implements Plugin {
2226
this.engine = options.engine;
2327
this.extension = options.extension || `.${options.engine}`;
2428
this.useDefault = (options.useDefault !== undefined) ? options.useDefault : true;
29+
this.baseDir = options.baseDir;
30+
this.includeDir = options.includeDir;
2531
this.test = new RegExp(this.extension);
2632
}
2733

@@ -54,8 +60,8 @@ export class ConsolidatePluginClass implements Plugin {
5460
file.contents = await consolidate[this.engine].render(file.contents, {
5561
cache: false,
5662
filename: 'base',
57-
basedir: file.context.homeDir,
58-
includeDir: file.context.homeDir
63+
baseDir: this.baseDir || file.context.homeDir,
64+
includeDir: this.includeDir || file.context.homeDir
5965
});
6066

6167
if (this.useDefault) {

src/tests/plugins/ConsolidatePlugin.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,40 @@ export class ConsolidatePluginTest {
7373
should(window.FuseBox.import("./template.pug")).deepEqual({ default: '<p>I should be included</p>' });
7474
}));
7575
}
76+
77+
"Should allow user to specify baseDir"() {
78+
return createEnv({
79+
project: {
80+
files: {
81+
"template.pug": "p Compiled with Pug"
82+
},
83+
plugins: [ConsolidatePlugin({
84+
engine: 'pug',
85+
baseDir: ''
86+
})],
87+
instructions: "template.pug"
88+
},
89+
}).then((result) => {
90+
const template = result.project.FuseBox.import('./template.pug');
91+
should(template.default).equal("<p>Compiled with Pug</p>");
92+
});
93+
}
94+
95+
"Should allow user to specify includeDir"() {
96+
return createEnv({
97+
project: {
98+
files: {
99+
"template.pug": "p Compiled with Pug"
100+
},
101+
plugins: [ConsolidatePlugin({
102+
engine: 'pug',
103+
includeDir: ''
104+
})],
105+
instructions: "template.pug"
106+
},
107+
}).then((result) => {
108+
const template = result.project.FuseBox.import('./template.pug');
109+
should(template.default).equal("<p>Compiled with Pug</p>");
110+
});
111+
}
76112
}

0 commit comments

Comments
 (0)
0