From 7423541be5bb5974e13862190ba82899ebaa0367 Mon Sep 17 00:00:00 2001 From: Jasper De Moor Date: Sat, 21 Jul 2018 10:57:21 -0700 Subject: [PATCH] Process inline styles and scripts (#1456) --- src/Pipeline.js | 6 + src/assets/CSSAsset.js | 27 ++-- src/assets/HTMLAsset.js | 95 +++++++++++++- src/transforms/htmlnano.js | 13 +- src/visitors/dependencies.js | 12 ++ test/html.js | 116 +++++++++++++++++- .../html-inline-coffeescript/index.html | 11 ++ .../html-inline-css-import/index.html | 8 ++ .../html-inline-css-import/test.css | 3 + .../html-inline-js-require/index.html | 8 ++ .../html-inline-js-require/test.js | 1 + test/integration/html-inline-js/index.html | 23 ++++ test/integration/html-inline-sass/index.html | 12 ++ test/integration/html-inline-styles/bg.jpg | 1 + test/integration/html-inline-styles/img.jpg | 1 + .../integration/html-inline-styles/index.html | 20 +++ 16 files changed, 337 insertions(+), 20 deletions(-) create mode 100644 test/integration/html-inline-coffeescript/index.html create mode 100644 test/integration/html-inline-css-import/index.html create mode 100644 test/integration/html-inline-css-import/test.css create mode 100644 test/integration/html-inline-js-require/index.html create mode 100644 test/integration/html-inline-js-require/test.js create mode 100644 test/integration/html-inline-js/index.html create mode 100644 test/integration/html-inline-sass/index.html create mode 100644 test/integration/html-inline-styles/bg.jpg create mode 100644 test/integration/html-inline-styles/img.jpg create mode 100644 test/integration/html-inline-styles/index.html diff --git a/src/Pipeline.js b/src/Pipeline.js index 16c9a9c5b02..4d20622ae3c 100644 --- a/src/Pipeline.js +++ b/src/Pipeline.js @@ -65,6 +65,12 @@ class Pipeline { subAsset.cacheData = Object.assign(asset.cacheData, subAsset.cacheData); let processed = await this.processAsset(subAsset); + if (rendition.meta) { + for (let res of processed) { + res.meta = rendition.meta; + } + } + generated = generated.concat(processed); asset.hash = md5(asset.hash + subAsset.hash); } else { diff --git a/src/assets/CSSAsset.js b/src/assets/CSSAsset.js index 4cf066ac1f0..db4631ef53a 100644 --- a/src/assets/CSSAsset.js +++ b/src/assets/CSSAsset.js @@ -29,19 +29,19 @@ class CSSAsset extends Asset { collectDependencies() { this.ast.root.walkAtRules('import', rule => { - let params = valueParser(rule.params).nodes; - let [name, ...media] = params; + let params = valueParser(rule.params); + let [name, ...media] = params.nodes; let dep; - if (name.type === 'string') { - dep = name.value; - } else if ( + if ( name.type === 'function' && name.value === 'url' && name.nodes.length ) { - dep = name.nodes[0].value; + name = name.nodes[0]; } + dep = name.value; + if (!dep) { throw new Error('Could not find import name for ' + rule); } @@ -50,10 +50,19 @@ class CSSAsset extends Asset { return; } - media = valueParser.stringify(media).trim(); - this.addDependency(dep, {media, loc: rule.source.start}); + // If this came from an inline ' - ) + html.includes('') ); - // minifyJson + // mergeStyles assert( - html.includes('') + html.includes( + '' + ) ); // minifySvg is false @@ -589,4 +589,108 @@ describe('html', function() { ] }); }); + + it('should process inline JS', async function() { + let b = await bundle(__dirname + '/integration/html-inline-js/index.html', { + production: true + }); + + const bundleContent = (await fs.readFile(b.name)).toString(); + assert(!bundleContent.includes('someArgument')); + }); + + it('should process inline styles', async function() { + let b = await bundle( + __dirname + '/integration/html-inline-styles/index.html', + {production: true} + ); + + await assertBundleTree(b, { + name: 'index.html', + assets: ['index.html'], + childBundles: [ + { + type: 'jpg', + assets: ['bg.jpg'], + childBundles: [] + }, + { + type: 'jpg', + assets: ['img.jpg'], + childBundles: [] + } + ] + }); + }); + + it('should process inline styles using lang', async function() { + let b = await bundle( + __dirname + '/integration/html-inline-sass/index.html', + {production: true} + ); + + await assertBundleTree(b, { + name: 'index.html', + assets: ['index.html'] + }); + + let html = await fs.readFile(__dirname + '/dist/index.html', 'utf8'); + + assert(html.includes('')); + }); + + it('should process inline non-js scripts', async function() { + let b = await bundle( + __dirname + '/integration/html-inline-coffeescript/index.html', + {production: true} + ); + + await assertBundleTree(b, { + name: 'index.html', + assets: ['index.html'] + }); + + let html = await fs.readFile(__dirname + '/dist/index.html', 'utf8'); + + assert(html.includes('alert("Hello, World!")')); + }); + + it('should handle inline css with @imports', async function() { + let b = await bundle( + __dirname + '/integration/html-inline-css-import/index.html', + {production: true} + ); + + await assertBundleTree(b, { + name: 'index.html', + assets: ['index.html'], + childBundles: [ + { + type: 'css', + assets: ['test.css'] + } + ] + }); + + let html = await fs.readFile(__dirname + '/dist/index.html', 'utf8'); + assert(html.includes('@import')); + }); + + it('should error on imports and requires in inline + + \ No newline at end of file diff --git a/test/integration/html-inline-css-import/index.html b/test/integration/html-inline-css-import/index.html new file mode 100644 index 00000000000..69ef7a89736 --- /dev/null +++ b/test/integration/html-inline-css-import/index.html @@ -0,0 +1,8 @@ + + + + + + \ No newline at end of file diff --git a/test/integration/html-inline-css-import/test.css b/test/integration/html-inline-css-import/test.css new file mode 100644 index 00000000000..454e0aae0d6 --- /dev/null +++ b/test/integration/html-inline-css-import/test.css @@ -0,0 +1,3 @@ +h1 { + color: red +} \ No newline at end of file diff --git a/test/integration/html-inline-js-require/index.html b/test/integration/html-inline-js-require/index.html new file mode 100644 index 00000000000..db239f85561 --- /dev/null +++ b/test/integration/html-inline-js-require/index.html @@ -0,0 +1,8 @@ + + + + + + \ No newline at end of file diff --git a/test/integration/html-inline-js-require/test.js b/test/integration/html-inline-js-require/test.js new file mode 100644 index 00000000000..4b2e66372c8 --- /dev/null +++ b/test/integration/html-inline-js-require/test.js @@ -0,0 +1 @@ +console.log('test') diff --git a/test/integration/html-inline-js/index.html b/test/integration/html-inline-js/index.html new file mode 100644 index 00000000000..edcdbc27493 --- /dev/null +++ b/test/integration/html-inline-js/index.html @@ -0,0 +1,23 @@ + + + + Inline JavaScript Parcel + + + + + + + + \ No newline at end of file diff --git a/test/integration/html-inline-sass/index.html b/test/integration/html-inline-sass/index.html new file mode 100644 index 00000000000..51d5848b1dd --- /dev/null +++ b/test/integration/html-inline-sass/index.html @@ -0,0 +1,12 @@ + + + + Inline JavaScript Parcel + + + + + \ No newline at end of file diff --git a/test/integration/html-inline-styles/bg.jpg b/test/integration/html-inline-styles/bg.jpg new file mode 100644 index 00000000000..084f7b47390 --- /dev/null +++ b/test/integration/html-inline-styles/bg.jpg @@ -0,0 +1 @@ +This is a background image \ No newline at end of file diff --git a/test/integration/html-inline-styles/img.jpg b/test/integration/html-inline-styles/img.jpg new file mode 100644 index 00000000000..ff5ab932824 --- /dev/null +++ b/test/integration/html-inline-styles/img.jpg @@ -0,0 +1 @@ +This should be an image \ No newline at end of file diff --git a/test/integration/html-inline-styles/index.html b/test/integration/html-inline-styles/index.html new file mode 100644 index 00000000000..8379ebc2c5c --- /dev/null +++ b/test/integration/html-inline-styles/index.html @@ -0,0 +1,20 @@ + + + + Inline JavaScript Parcel + + + + +
+

+

+

+
+ + \ No newline at end of file