-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Pug support from parcel-plugin-pug (#1051)
- Loading branch information
1 parent
4bd32f2
commit 09a959d
Showing
21 changed files
with
416 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const path = require('path'); | ||
const Asset = require('../Asset'); | ||
const localRequire = require('../utils/localRequire'); | ||
|
||
class PugAsset extends Asset { | ||
constructor(name, pkg, options) { | ||
super(name, pkg, options); | ||
this.type = 'html'; | ||
} | ||
|
||
async generate() { | ||
const pug = await localRequire('pug', this.name); | ||
const config = | ||
(await this.getConfig(['.pugrc', '.pugrc.js', 'pug.config.js'])) || {}; | ||
|
||
const compiled = pug.compile(this.contents, { | ||
compileDebug: false, | ||
filename: this.name, | ||
basedir: path.dirname(this.name), | ||
pretty: !this.options.minify, | ||
templateName: path.basename(this.basename, path.extname(this.basename)), | ||
filters: config.filters, | ||
filterOptions: config.filterOptions, | ||
filterAliases: config.filterAliases | ||
}); | ||
|
||
if (compiled.dependencies) { | ||
for (let item of compiled.dependencies) { | ||
this.addDependency(item, { | ||
includedInParent: true | ||
}); | ||
} | ||
} | ||
|
||
return compiled(); | ||
} | ||
} | ||
|
||
module.exports = PugAsset; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module.exports = { | ||
filters: { | ||
'custom-filter': function (text, options) { | ||
return 'FILTERED: ' + text; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
:custom-filter() | ||
Hello! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
doctype html | ||
html | ||
body | ||
block content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<body> | ||
<h1>Yep, it's working!</h1> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
extends ./base.pug | ||
|
||
block content | ||
include other.pug |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
h1 Yep, it's working! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
doctype html | ||
html | ||
head | ||
title Minify | ||
body | ||
h1 Minified |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
include ./mixins.pug | ||
|
||
doctype html | ||
html | ||
head | ||
title Pug variables | ||
body | ||
+greetings('Parcel') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
mixin greetings(name) | ||
h1 Greetings, #{name} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
doctype html | ||
html | ||
head | ||
title Pug variables | ||
body | ||
- var image = './100x100.png' | ||
img(src=image, alt='100x100') |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
body { | ||
background: red; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
alert('Hi'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
doctype html | ||
html | ||
head | ||
link(rel='stylesheet', href='index.css') | ||
meta(property='og:image', content='100x100.png') | ||
body | ||
h1 Hello world | ||
a(href='#hash_link') Hash link | ||
a(href='mailto:[email protected]') Mailto link | ||
a(href='tel:+33636757575') Tel link | ||
script(src='index.js') | ||
script(src='https://unpkg.com/parcel-bundler') | ||
i hello | ||
i world | ||
svg | ||
use(href='icons.svg#icon-repo-pull') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
const assert = require('assert'); | ||
const fs = require('fs'); | ||
const {bundle, assertBundleTree} = require('./utils'); | ||
|
||
describe('pug', function() { | ||
it('should support bundling HTML', async function() { | ||
const b = await bundle(__dirname + '/integration/pug/index.pug'); | ||
|
||
assertBundleTree(b, { | ||
name: 'index.html', | ||
assets: ['index.pug'], | ||
childBundles: [ | ||
{ | ||
type: 'png', | ||
assets: ['100x100.png'], | ||
childBundles: [] | ||
}, | ||
{ | ||
type: 'svg', | ||
assets: ['icons.svg'], | ||
childBundles: [] | ||
}, | ||
{ | ||
type: 'css', | ||
assets: ['index.css'], | ||
childBundles: [] | ||
}, | ||
{ | ||
type: 'js', | ||
assets: ['index.js'], | ||
childBundles: [ | ||
{ | ||
type: 'map' | ||
} | ||
] | ||
} | ||
] | ||
}); | ||
|
||
const files = fs.readdirSync(__dirname + '/dist'); | ||
const html = fs.readFileSync(__dirname + '/dist/index.html'); | ||
for (const file of files) { | ||
const ext = file.match(/\.([0-9a-z]+)(?:[?#]|$)/i)[0]; | ||
if (file !== 'index.html' && ext !== '.map') { | ||
assert(html.includes(file)); | ||
} | ||
} | ||
}); | ||
|
||
it('should support include and extends files', async function() { | ||
const b = await bundle( | ||
__dirname + '/integration/pug-include-extends/index.pug' | ||
); | ||
|
||
assertBundleTree(b, { | ||
name: 'index.html', | ||
assets: ['index.pug'] | ||
}); | ||
|
||
const html = fs.readFileSync(__dirname + '/dist/index.html', 'utf-8'); | ||
const expect = fs.readFileSync( | ||
__dirname + '/integration/pug-include-extends/expect.html', | ||
'utf-8' | ||
); | ||
|
||
assert.equal(html, expect, 'Content mismatch'); | ||
}); | ||
|
||
it('should support variables', async function() { | ||
const b = await bundle(__dirname + '/integration/pug-var/index.pug'); | ||
|
||
assertBundleTree(b, { | ||
name: 'index.html', | ||
assets: ['index.pug'] | ||
}); | ||
|
||
const html = fs.readFileSync(__dirname + '/dist/index.html', 'utf-8'); | ||
|
||
assert(/src="\/?100x100.*.png"/.test(html)); | ||
}); | ||
|
||
it('should support mixins', async function() { | ||
const b = await bundle(__dirname + '/integration/pug-mixins/index.pug'); | ||
|
||
assertBundleTree(b, { | ||
name: 'index.html', | ||
assets: ['index.pug'] | ||
}); | ||
|
||
const html = fs.readFileSync(__dirname + '/dist/index.html', 'utf-8'); | ||
assert(html.includes('Greetings, Parcel')); | ||
}); | ||
|
||
it('should support filters', async function() { | ||
const b = await bundle(__dirname + '/integration/pug-filters/index.pug'); | ||
|
||
assertBundleTree(b, { | ||
name: 'index.html', | ||
assets: ['index.pug'] | ||
}); | ||
|
||
const html = fs.readFileSync(__dirname + '/dist/index.html', 'utf-8'); | ||
assert(html.includes('FILTERED: Hello!')); | ||
}); | ||
|
||
it('should minify HTML in production mode', async function() { | ||
const b = await bundle(__dirname + '/integration/pug-minify/index.pug', { | ||
production: true | ||
}); | ||
|
||
assertBundleTree(b, { | ||
name: 'index.html', | ||
assets: ['index.pug'] | ||
}); | ||
|
||
const html = fs.readFileSync(__dirname + '/dist/index.html', 'utf-8'); | ||
|
||
assert(html.includes('Minified')); | ||
}); | ||
}); |
Oops, something went wrong.