diff --git a/src/transformers/pug.ts b/src/transformers/pug.ts index 1d851bf7..3bf92dfb 100644 --- a/src/transformers/pug.ts +++ b/src/transformers/pug.ts @@ -67,8 +67,21 @@ const transformer: Transformer = async ({ ...options, }; + // separate content that needs to come before the svelte mixins + let preContent = ''; + + if (content.startsWith('extends')) { + 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, diff --git a/test/fixtures/pug/basic.html b/test/fixtures/pug/basic.html new file mode 100644 index 00000000..17f1519a --- /dev/null +++ b/test/fixtures/pug/basic.html @@ -0,0 +1 @@ +
hey
\ No newline at end of file diff --git a/test/fixtures/pug/basic.pug b/test/fixtures/pug/basic.pug new file mode 100644 index 00000000..37c05749 --- /dev/null +++ b/test/fixtures/pug/basic.pug @@ -0,0 +1 @@ +div hey \ No newline at end of file diff --git a/test/fixtures/pug/lib/mixin.pug b/test/fixtures/pug/lib/mixin.pug new file mode 100644 index 00000000..aa1d1963 --- /dev/null +++ b/test/fixtures/pug/lib/mixin.pug @@ -0,0 +1,3 @@ +mixin green + span(style='color: green') + block diff --git a/test/fixtures/pug/lib/template.pug b/test/fixtures/pug/lib/template.pug new file mode 100644 index 00000000..e99a44fb --- /dev/null +++ b/test/fixtures/pug/lib/template.pug @@ -0,0 +1,4 @@ +span(style='color: orange') + block orange +span(style='color: blue') + block blue diff --git a/test/fixtures/pug/mixin.html b/test/fixtures/pug/mixin.html new file mode 100644 index 00000000..34174d8c --- /dev/null +++ b/test/fixtures/pug/mixin.html @@ -0,0 +1 @@ +

hello mixin

\ No newline at end of file diff --git a/test/fixtures/pug/mixin.pug b/test/fixtures/pug/mixin.pug new file mode 100644 index 00000000..bfaa66ef --- /dev/null +++ b/test/fixtures/pug/mixin.pug @@ -0,0 +1,3 @@ +include /pug/lib/mixin.pug ++green + p hello mixin diff --git a/test/fixtures/pug/template.html b/test/fixtures/pug/template.html new file mode 100644 index 00000000..b9f284cd --- /dev/null +++ b/test/fixtures/pug/template.html @@ -0,0 +1 @@ +

hello template

hello template

\ No newline at end of file diff --git a/test/fixtures/pug/template.pug b/test/fixtures/pug/template.pug new file mode 100644 index 00000000..103073a6 --- /dev/null +++ b/test/fixtures/pug/template.pug @@ -0,0 +1,5 @@ +extends /pug/lib/template.pug +block orange + p hello template +block blue + p hello template diff --git a/test/processors/pug.test.ts b/test/processors/pug.test.ts index 762c3ab4..0102a1cf 100644 --- a/test/processors/pug.test.ts +++ b/test/processors/pug.test.ts @@ -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 () => { @@ -29,4 +33,20 @@ h1 HEY expect(preprocessed.toString?.()).toContain(`

HEY

`); }); + + 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); + }); });