Skip to content

Commit

Permalink
feat: support excluding directories (#4)
Browse files Browse the repository at this point in the history
Co-authored-by: Pascal Canadas <[email protected]>
Co-authored-by: Enzo Innocenzi <[email protected]>
  • Loading branch information
3 people authored Jan 24, 2024
1 parent c1cfeca commit c2a7f73
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export default function(options: Partial<Options> = {}): Plugin {
input: 'resources/mail',
output: 'resources/views/emails',
extension: '.blade.php',
exclude: [],
logger: config.logger,
building: config.command === 'build',
log: true,
Expand All @@ -73,9 +74,16 @@ export default function(options: Partial<Options> = {}): Plugin {
input = path.join(compileOptions.input, '**/*.mjml').replace(/\\/g, '/')
}

const excludes = Array.isArray(compileOptions.exclude)
? compileOptions.exclude
: [compileOptions.exclude]
const files = await fg(input)
debug.mjml('Compiling MJML files:', { input, files })
files.forEach((file) => compileInput(file, compileOptions))
files.forEach((file) => {
if (!excludes.some((exclude) => file.startsWith(exclude))) {
compileInput(file, compileOptions)
}
})
},
configureServer(server) {
if (compileOptions.watch === false) {
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface Options {
input: string
output: string
extension: string
exclude: string | string[]
watch: boolean
log: boolean
mjml?: MJMLParsingOptions
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/has-excludes/mail/mail.mjml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<mjml>
<mj-body>
<mj-section>
<mj-column>
<mj-image width="100px" src="https://mjml.io/assets/img/logo-small.png"></mj-image>
<mj-divider border-color="#F45E43"></mj-divider>
<mj-text font-size="20px" color="#F45E43" font-family="helvetica">Hello World</mj-text>
</mj-column>
</mj-section>
</mj-body>
</mjml>
11 changes: 11 additions & 0 deletions test/fixtures/has-excludes/partials/_foo.mjml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<mjml>
<mj-body>
<mj-section>
<mj-column>
<mj-image width="100px" src="https://mjml.io/assets/img/logo-small.png"></mj-image>
<mj-divider border-color="#F45E43"></mj-divider>
<mj-text font-size="20px" color="#F45E43" font-family="helvetica">Hello World</mj-text>
</mj-column>
</mj-section>
</mj-body>
</mjml>
21 changes: 21 additions & 0 deletions test/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,27 @@ test('it compiles', async() => {
expect(fs.existsSync(path.resolve(output, 'mail', 'mail.html'))).toBe(true)
})

test('it can exclude directories', async() => {
expect(fs.existsSync(output)).toBe(false)

await build({
root: fixtures,
logLevel: 'silent',
plugins: [
mjml({
log: false,
extension: '.html',
input: path.resolve(fixtures, 'has-excludes'),
output,
exclude: [path.resolve(fixtures, 'has-excludes', 'partials')],
}),
],
})

expect(fs.existsSync(path.resolve(output, 'mail', 'mail.html'))).toBe(true)
expect(!fs.existsSync(path.resolve(output, 'partials', '_foo.html'))).toBe(true)
})

test('it throws on compilation errors', async() => {
expect(fs.existsSync(output)).toBe(false)

Expand Down

0 comments on commit c2a7f73

Please sign in to comment.