diff --git a/packages/cli/src/lifecycles/prerender.js b/packages/cli/src/lifecycles/prerender.js index 194d521a0..6ba752330 100644 --- a/packages/cli/src/lifecycles/prerender.js +++ b/packages/cli/src/lifecycles/prerender.js @@ -6,7 +6,7 @@ import path from 'path'; import { WorkerPool } from '../lib/threadpool.js'; function isLocalLink(url = '') { - return url.indexOf('http') !== 0 && url.indexOf('//') !== 0; + return url !== '' && (url.indexOf('http') !== 0 && url.indexOf('//') !== 0); } function createOutputDirectory(route, outputPathDir) { @@ -29,7 +29,8 @@ function trackResourcesForRoute(html, compilation, route) { style: true }); - const scripts = root.querySelectorAll('head script') + // intentionally support `, ` - - `); - } else if (optimizationAttr === 'static' || optimization === 'static') { - headContents = headContents.replace(``, ''); - } - } else if (type === 'link') { - if (!optimizationAttr && (optimization !== 'none' && optimization !== 'inline')) { - const optimizedFilePath = `/${optimizedFileName}`; - - headContents = headContents.replace(src, optimizedFilePath); - headContents = headContents.replace('
', ` - - - `); - } else if (optimizationAttr === 'inline' || optimization === 'inline') { - // https://github.com/ProjectEvergreen/greenwood/issues/810 - // when pre-rendering, puppeteer normalizes everything to - // but if not using pre-rendering, then it could come out as - // not great, but best we can do for now until #742 - headContents = headContents.replace(``, ` - - `).replace(``, ` - - `); - } + for (const pageResource of pageResources) { + const keyedResource = this.compilation.resources.get(pageResource.sourcePathURL.pathname); + const { contents, src, type, optimizationAttr, optimizedFileContents, optimizedFileName, rawAttributes } = keyedResource; + + if (src) { + if (type === 'script') { + if (!optimizationAttr && optimization === 'default') { + const optimizedFilePath = `/${optimizedFileName}`; + + body = body.replace(src, optimizedFilePath); + body = body.replace('', ` + + + `); + } else if (optimizationAttr === 'inline' || optimization === 'inline') { + const isModule = rawAttributes.indexOf('type="module') >= 0 ? ' type="module"' : ''; + + body = body.replace(``, ` + + `); + } else if (optimizationAttr === 'static' || optimization === 'static') { + body = body.replace(``, ''); } - } else { - if (type === 'script') { - if (optimizationAttr === 'static' || optimization === 'static') { - headContents = headContents.replace(``, ''); - } else if (optimizationAttr === 'none') { - headContents = headContents.replace(contents, contents.replace(/\.\//g, '/').replace(/\$/g, '$$$')); - } else { - headContents = headContents.replace(contents, optimizedFileContents.replace(/\.\//g, '/').replace(/\$/g, '$$$')); - } - } else if (type === 'style') { - headContents = headContents.replace(contents, optimizedFileContents); + } else if (type === 'link') { + if (!optimizationAttr && (optimization !== 'none' && optimization !== 'inline')) { + const optimizedFilePath = `/${optimizedFileName}`; + + body = body.replace(src, optimizedFilePath); + body = body.replace('', ` + + + `); + } else if (optimizationAttr === 'inline' || optimization === 'inline') { + // https://github.com/ProjectEvergreen/greenwood/issues/810 + // when pre-rendering, puppeteer normalizes everything to + // but if not using pre-rendering, then it could come out as + // not great, but best we can do for now until #742 + body = body.replace(``, ` + + `).replace(``, ` + + `); + } + } + } else { + if (type === 'script') { + if (optimizationAttr === 'static' || optimization === 'static') { + body = body.replace(``, ''); + } else if (optimizationAttr === 'none') { + body = body.replace(contents, contents.replace(/\.\//g, '/').replace(/\$/g, '$$$')); + } else { + body = body.replace(contents, optimizedFileContents.replace(/\.\//g, '/').replace(/\$/g, '$$$')); } + } else if (type === 'style') { + body = body.replace(contents, optimizedFileContents); } } - - // TODO clean up lit-polyfill as part of https://github.com/ProjectEvergreen/greenwood/issues/728 - headContents = headContents.replace(/ tag in the ', function() { + it('should have one + \ No newline at end of file diff --git a/packages/cli/test/cases/build.default.workspace-javascript-css/src/scripts/popup.js b/packages/cli/test/cases/build.default.workspace-javascript-css/src/scripts/popup.js new file mode 100644 index 000000000..f0acde54b --- /dev/null +++ b/packages/cli/test/cases/build.default.workspace-javascript-css/src/scripts/popup.js @@ -0,0 +1 @@ +alert('surprise!'); \ No newline at end of file diff --git a/packages/plugin-polyfills/test/cases/dsd/dsd.spec.js b/packages/plugin-polyfills/test/cases/dsd/dsd.spec.js index dba03f1b5..ac5be9443 100644 --- a/packages/plugin-polyfills/test/cases/dsd/dsd.spec.js +++ b/packages/plugin-polyfills/test/cases/dsd/dsd.spec.js @@ -74,7 +74,7 @@ describe('Build Greenwood With: ', function() { it('should have the expected DSD polyfill content in the polyfill