Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow pug template inheritance #572

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/transformers/pug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,21 @@ const transformer: Transformer<Options.Pug> = async ({
...options,
};

// separate content that needs to come before the svelte mixins
let preContent = '';

if (content.startsWith('extends')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the example of inheritance from pug's documentation, what do you think of also adding support for files that start with a comment?

//- page-a.pug
extends layout.pug

block scripts
  script(src='/jquery.js')
  script(src='/pets.js')

const split = content.indexOf('\n') + 1;

preContent = content.substring(0, split);
content = content.substring(split);
}

const { type: identationType } = detectIndent(content);
const input = `${GET_MIXINS(identationType ?? 'space')}\n${content}`;
const input = `${preContent}${GET_MIXINS(
identationType ?? 'space',
)}\n${content}`;

const compiled = pug.compile(
input,
pugOptions,
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/pug/basic.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div>hey</div>
1 change: 1 addition & 0 deletions test/fixtures/pug/basic.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
div hey
3 changes: 3 additions & 0 deletions test/fixtures/pug/lib/mixin.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mixin green
span(style='color: green')
block
4 changes: 4 additions & 0 deletions test/fixtures/pug/lib/template.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
span(style='color: orange')
block orange
span(style='color: blue')
block blue
1 change: 1 addition & 0 deletions test/fixtures/pug/mixin.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<span style="color: green"><p>hello mixin</p></span>
3 changes: 3 additions & 0 deletions test/fixtures/pug/mixin.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include /pug/lib/mixin.pug
+green
p hello mixin
1 change: 1 addition & 0 deletions test/fixtures/pug/template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<span style="color: orange"><p>hello template</p></span><span style="color: blue"><p>hello template</p></span>
5 changes: 5 additions & 0 deletions test/fixtures/pug/template.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
extends /pug/lib/template.pug
block orange
p hello template
block blue
p hello template
28 changes: 24 additions & 4 deletions test/processors/pug.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { pug } from '../../src';
import { getFixtureContent, preprocess } from '../utils';
import { getFixtureContent, getFixturePath, preprocess } from '../utils';

const EXPECTED_TEMPLATE = getFixtureContent('template.html');
const EXPECTED = {
BASIC: getFixtureContent('pug/basic.html'),
MIXIN: getFixtureContent('pug/mixin.html'),
TEMPLATE: getFixtureContent('pug/template.html'),
};

describe(`processor - pug`, () => {
it('should preprocess the whole file', async () => {
const template = getFixtureContent('template.pug');
const template = getFixtureContent('pug/basic.pug');
const preprocessed = await preprocess(template, [pug()]);

expect(preprocessed.toString?.()).toContain(EXPECTED_TEMPLATE);
expect(preprocessed.toString?.()).toContain(EXPECTED.BASIC);
});

it('should support prepended data', async () => {
Expand All @@ -29,4 +33,20 @@ h1 HEY

expect(preprocessed.toString?.()).toContain(`<h1>HEY</h1>`);
});

it('should support mixins', async () => {
const template = getFixtureContent('pug/mixin.pug');
const options = { basedir: getFixturePath('.') };
const preprocessed = await preprocess(template, [pug(options)]);

expect(preprocessed.toString?.()).toContain(EXPECTED.MIXIN);
});

it('should support template inheritance', async () => {
const template = getFixtureContent('pug/template.pug');
const options = { basedir: getFixturePath('.') };
const preprocessed = await preprocess(template, [pug(options)]);

expect(preprocessed.toString?.()).toContain(EXPECTED.TEMPLATE);
});
});