diff --git a/package.json b/package.json index 813706c8bee..f6ef0405126 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/assets/HTMLAsset.js b/src/assets/HTMLAsset.js index 0645afe53a7..2dd0f56de65 100644 --- a/src/assets/HTMLAsset.js +++ b/src/assets/HTMLAsset.js @@ -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 ( diff --git a/src/transforms/posthtml.js b/src/transforms/posthtml.js index f12219bc7eb..e65aee23939 100644 --- a/src/transforms/posthtml.js +++ b/src/transforms/posthtml.js @@ -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 => diff --git a/test/html.js b/test/html.js index cf4313eab93..a88a68d3853 100644 --- a/test/html.js +++ b/test/html.js @@ -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'); diff --git a/test/integration/posthtml-plugin-deps/base.html b/test/integration/posthtml-plugin-deps/base.html new file mode 100644 index 00000000000..8e2a53b1d43 --- /dev/null +++ b/test/integration/posthtml-plugin-deps/base.html @@ -0,0 +1,8 @@ + + + Parcel + + +
+ + diff --git a/test/integration/posthtml-plugin-deps/index.html b/test/integration/posthtml-plugin-deps/index.html new file mode 100644 index 00000000000..5bcc693582e --- /dev/null +++ b/test/integration/posthtml-plugin-deps/index.html @@ -0,0 +1,3 @@ + +

Hello

+
diff --git a/test/integration/posthtml-plugin-deps/posthtml.config.js b/test/integration/posthtml-plugin-deps/posthtml.config.js new file mode 100644 index 00000000000..716f4658e69 --- /dev/null +++ b/test/integration/posthtml-plugin-deps/posthtml.config.js @@ -0,0 +1,7 @@ +module.exports = { + plugins: [ + require('posthtml-extend')({ + root: __dirname + }) + ] +} diff --git a/yarn.lock b/yarn.lock index b3b2de15acf..fa3d416c6ce 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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: