Skip to content

Commit 038ade6

Browse files
fathybdevongovett
authored andcommitted
Watch PostHTML plugins dependencies (#1675)
1 parent c966120 commit 038ade6

8 files changed

Lines changed: 56 additions & 2 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
"node-sass": "^4.7.2",
9595
"nyc": "^11.1.0",
9696
"postcss-modules": "^1.1.0",
97+
"posthtml-extend": "^0.2.1",
9798
"posthtml-include": "^1.1.0",
9899
"prettier": "^1.13.0",
99100
"pug": "^2.0.3",

src/assets/HTMLAsset.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,20 @@ class HTMLAsset extends Asset {
112112
}
113113

114114
collectDependencies() {
115-
this.ast.walk(node => {
115+
let {ast} = this;
116+
117+
// Add bundled dependencies from plugins like posthtml-extend or posthtml-include, if any
118+
if (ast.messages) {
119+
ast.messages.forEach(message => {
120+
if (message.type === 'dependency') {
121+
this.addDependency(message.file, {
122+
includedInParent: true
123+
});
124+
}
125+
});
126+
}
127+
128+
ast.walk(node => {
116129
if (node.attrs) {
117130
if (node.tag === 'meta') {
118131
if (

src/transforms/posthtml.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ async function getConfig(asset) {
3838
config = config || {};
3939
const plugins = config.plugins;
4040
if (typeof plugins === 'object') {
41+
// This is deprecated in favor of result messages but kept for compatibility
42+
// See https://github.com/posthtml/posthtml-include/blob/e4f2a57c2e52ff721eed747b65eddf7d7a1451e3/index.js#L18-L26
4143
const depConfig = {
4244
addDependencyTo: {
4345
addDependency: name =>

test/html.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,19 @@ describe('html', function() {
116116
assert(asset.dependencies.get(other).includedInParent);
117117
});
118118

119+
it('should add dependencies referenced by plugins', async () => {
120+
const b = await bundle(
121+
__dirname + '/integration/posthtml-plugin-deps/index.html'
122+
);
123+
const asset = b.assets.values().next().value;
124+
const other = path.join(
125+
__dirname,
126+
'/integration/posthtml-plugin-deps/base.html'
127+
);
128+
assert(asset.dependencies.has(other));
129+
assert(asset.dependencies.get(other).includedInParent);
130+
});
131+
119132
it('should insert sibling CSS bundles for JS files in the HEAD', async function() {
120133
let b = await bundle(__dirname + '/integration/html-css/index.html');
121134

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<html>
2+
<head>
3+
<title>Parcel</title>
4+
</head>
5+
<body>
6+
<div class="content"></div>
7+
</body>
8+
</html>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<extends src='base.html'>
2+
<p>Hello</p>
3+
</extends>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
plugins: [
3+
require('posthtml-extend')({
4+
root: __dirname
5+
})
6+
]
7+
}

yarn.lock

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5777,6 +5777,13 @@ postcss@^6.0.1, postcss@^6.0.19, postcss@^6.0.20:
57775777
source-map "^0.6.1"
57785778
supports-color "^5.4.0"
57795779

5780+
posthtml-extend@^0.2.1:
5781+
version "0.2.1"
5782+
resolved "https://registry.yarnpkg.com/posthtml-extend/-/posthtml-extend-0.2.1.tgz#d023ce7ce4dd6071071b50e315dfefa87da8a979"
5783+
dependencies:
5784+
posthtml "^0.11.3"
5785+
posthtml-parser "^0.3.0"
5786+
57805787
posthtml-include@^1.1.0:
57815788
version "1.1.0"
57825789
resolved "https://registry.yarnpkg.com/posthtml-include/-/posthtml-include-1.1.0.tgz#6a11efff05dfba4e9f29788dea1d17248f04f1e1"
@@ -5789,7 +5796,7 @@ posthtml-parser@^0.1.1:
57895796
dependencies:
57905797
htmlparser2 "^3.8.3"
57915798

5792-
posthtml-parser@^0.3.3:
5799+
posthtml-parser@^0.3.0, posthtml-parser@^0.3.3:
57935800
version "0.3.3"
57945801
resolved "https://registry.yarnpkg.com/posthtml-parser/-/posthtml-parser-0.3.3.tgz#3fe986fca9f00c0f109d731ba590b192f26e776d"
57955802
dependencies:

0 commit comments

Comments
 (0)