Skip to content

Commit

Permalink
Watch PostHTML plugins dependencies (#1675)
Browse files Browse the repository at this point in the history
  • Loading branch information
fathyb authored and devongovett committed Jul 7, 2018
1 parent c966120 commit 038ade6
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
"node-sass": "^4.7.2",
"nyc": "^11.1.0",
"postcss-modules": "^1.1.0",
"posthtml-extend": "^0.2.1",
"posthtml-include": "^1.1.0",
"prettier": "^1.13.0",
"pug": "^2.0.3",
Expand Down
15 changes: 14 additions & 1 deletion src/assets/HTMLAsset.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,20 @@ class HTMLAsset extends Asset {
}

collectDependencies() {
this.ast.walk(node => {
let {ast} = this;

// Add bundled dependencies from plugins like posthtml-extend or posthtml-include, if any
if (ast.messages) {
ast.messages.forEach(message => {
if (message.type === 'dependency') {
this.addDependency(message.file, {
includedInParent: true
});
}
});
}

ast.walk(node => {
if (node.attrs) {
if (node.tag === 'meta') {
if (
Expand Down
2 changes: 2 additions & 0 deletions src/transforms/posthtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ async function getConfig(asset) {
config = config || {};
const plugins = config.plugins;
if (typeof plugins === 'object') {
// This is deprecated in favor of result messages but kept for compatibility
// See https://github.com/posthtml/posthtml-include/blob/e4f2a57c2e52ff721eed747b65eddf7d7a1451e3/index.js#L18-L26
const depConfig = {
addDependencyTo: {
addDependency: name =>
Expand Down
13 changes: 13 additions & 0 deletions test/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,19 @@ describe('html', function() {
assert(asset.dependencies.get(other).includedInParent);
});

it('should add dependencies referenced by plugins', async () => {
const b = await bundle(
__dirname + '/integration/posthtml-plugin-deps/index.html'
);
const asset = b.assets.values().next().value;
const other = path.join(
__dirname,
'/integration/posthtml-plugin-deps/base.html'
);
assert(asset.dependencies.has(other));
assert(asset.dependencies.get(other).includedInParent);
});

it('should insert sibling CSS bundles for JS files in the HEAD', async function() {
let b = await bundle(__dirname + '/integration/html-css/index.html');

Expand Down
8 changes: 8 additions & 0 deletions test/integration/posthtml-plugin-deps/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>Parcel</title>
</head>
<body>
<div class="content"></div>
</body>
</html>
3 changes: 3 additions & 0 deletions test/integration/posthtml-plugin-deps/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<extends src='base.html'>
<p>Hello</p>
</extends>
7 changes: 7 additions & 0 deletions test/integration/posthtml-plugin-deps/posthtml.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
plugins: [
require('posthtml-extend')({
root: __dirname
})
]
}
9 changes: 8 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5777,6 +5777,13 @@ postcss@^6.0.1, postcss@^6.0.19, postcss@^6.0.20:
source-map "^0.6.1"
supports-color "^5.4.0"

posthtml-extend@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/posthtml-extend/-/posthtml-extend-0.2.1.tgz#d023ce7ce4dd6071071b50e315dfefa87da8a979"
dependencies:
posthtml "^0.11.3"
posthtml-parser "^0.3.0"

posthtml-include@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/posthtml-include/-/posthtml-include-1.1.0.tgz#6a11efff05dfba4e9f29788dea1d17248f04f1e1"
Expand All @@ -5789,7 +5796,7 @@ posthtml-parser@^0.1.1:
dependencies:
htmlparser2 "^3.8.3"

posthtml-parser@^0.3.3:
posthtml-parser@^0.3.0, posthtml-parser@^0.3.3:
version "0.3.3"
resolved "https://registry.yarnpkg.com/posthtml-parser/-/posthtml-parser-0.3.3.tgz#3fe986fca9f00c0f109d731ba590b192f26e776d"
dependencies:
Expand Down

0 comments on commit 038ade6

Please sign in to comment.