Skip to content

Commit

Permalink
experimental fixWebpackChunksIssue
Browse files Browse the repository at this point in the history
  • Loading branch information
stereobooster committed Oct 23, 2017
1 parent 57df43a commit 6bad686
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
21 changes: 21 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const defaultOptions = {
crawl: true,
waitFor: false,
externalServer: false,
// workaround for https://github.com/geelen/react-snapshot/issues/66#issuecomment-331718560
fixWebpackChunksIssue: false, // experimental
minifyOptions: {
minifyCSS: true,
collapseBooleanAttributes: true,
Expand Down Expand Up @@ -148,6 +150,7 @@ const inlineCss = async ({ page, url }) => {
document.head.appendChild(link);
});

// TODO: separate config for this
Array.from(document.querySelectorAll("script[src]")).forEach(x => {
x.parentNode.removeChild(x);
x.setAttribute("async", "true");
Expand All @@ -164,6 +167,22 @@ const inlineCss = async ({ page, url }) => {
);
};

const fixWebpackChunksIssue = ({ page, basePath }) => {
return page.evaluate(basePath => {
const localScripts = Array.from(
document.querySelectorAll("script[src]")
).filter(x => x.src.startsWith(basePath));
const mainRegexp = /main\.[\w]{8}.js/;
const mainScript = localScripts.filter(x => mainRegexp.test(x.src))[0];
const chunkRegexp = /([\d]+)\.[\w]{8}\.chunk\.js/;
const chunkSripts = localScripts.filter(x => chunkRegexp.test(x.src));
chunkSripts.forEach(x => {
x.parentElement.removeChild(x);
mainScript.parentNode.insertBefore(x, mainScript.nextSibling);
});
}, basePath);
};

const saveAsHtml = async ({ page, filePath, options }) => {
const content = await page.evaluate(() => document.documentElement.outerHTML);
const minifiedContent = options.minifyOptions
Expand Down Expand Up @@ -220,6 +239,8 @@ const run = async userOptions => {
aferFeth: async ({ page, route }) => {
if (options.removeStyleTags) await removeStyleTags({ page });
if (options.inlineCss) await inlineCss({ page, url });
if (options.fixWebpackChunksIssue)
await fixWebpackChunksIssue({ page, basePath });
const filePath = path.join(destinationDir, route);
if (options.saveAs === "html") {
await saveAsHtml({ page, filePath, options });
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-snap",
"version": "0.5.1",
"version": "0.5.2",
"description": "Pre-renders web app into static HTML",
"main": "index.js",
"author": "stereobooster",
Expand Down
10 changes: 7 additions & 3 deletions src/puppeteer_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,12 @@ const getLinks = async ({ page }) => {
const crawl = async ({
options,
basePath,
beforeFetch = ({ page, route }) => { ({ page, route }) },
aferFeth = ({ page, route }) => { ({ page, route }) },
beforeFetch = ({ page, route }) => {
({ page, route });
},
aferFeth = ({ page, route }) => {
({ page, route });
},
onEnd = () => {}
}) => {
let shuttingDown = false;
Expand All @@ -90,7 +94,7 @@ const crawl = async ({
* @param {string} url
* @returns {void}
*/
const addToQueue = (url) => {
const addToQueue = url => {
if (Url.parse(url).hostname === "localhost" && !uniqueUrls[url]) {
uniqueUrls[url] = true;
enqued++;
Expand Down
6 changes: 2 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,5 @@
"noUnusedLocals": true,
"noUnusedParameters": true
},
"include": [
"./src/"
]
}
"include": ["./src/"]
}

0 comments on commit 6bad686

Please sign in to comment.