-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
asset-manifest.json generated by CRA2 is not useful #5513
Comments
I think we should just change it to only include "initial" scripts in the order they should be in HTML. Since the only purpose of this file is to reconstruct the HTML. Does that make sense? |
Definitely, it will be really nice to create a more descriptive manifest file. I would also group them by type since normally you would group them by type and then try to inject them in order, a better data structure thinking on deployment will be nice to have.
Btw, we live inside Rails app right now, so a more descriptive file would be nice for us to deploy |
See also #5225 |
Having the asset-manifest only include the "initial" scripts would meet our needs. We have two main ways we use our react app: whole-page and partial-page. The "whole-page" use case can just use the index.html from the build (with some dynamic replacements), but in the "partial-page" case (typically a button or icon that opens a modal dialog rendered via the react app) we don't use the index.html at all and instead leverage the asset-manifest to pull in the main js and css files. Now with CRA 2, the index.html is also including a "15..chunk.js" and of course I have no guarantee that this main vendor chunk will still begin with "15" on my next build. Another possible (and much quicker to implement) way to address this would be to support a boolean environment variable such as |
I see this proposal is removed from 2.x milestone. What's the plan for now? @Timer |
See #5225 (comment) |
As a hack, I created the following script to give me all the css and js paths in their correct order. Note that I needed multiple entry points so I have ejected. If you have not, then the note
code: (written in typescript) import fs from 'fs';
import path from 'path';
import assetManifest from '<path/to/asset-manifest.json>';
const build_directory = '<path/to/create-react-app/build/directory>';
export const getAssetPathFromCRAHtml = async (
entry_key: string // something like `admin`, `pages-manage`, `index`. If you have not ejected, then this will just be `index`
): Promise<{
css_assets: string[];
js_assets: string[];
}> => {
const css_assets: string[] = [];
const js_assets: string[] = [];
const html_string = await new Promise<string>((resolve, reject) => {
fs.readFile(
path.join(build_directory, `${entry_key}.html`),
(error, data) => {
if (error) {
reject(error);
} else {
resolve(data.toString('utf8'));
}
}
);
});
let css_matches;
const cssRe = /<link\shref="(\/static\/css\/.+?)"\srel="stylesheet"/g;
do {
css_matches = cssRe.exec(html_string);
if (css_matches) {
css_assets.push(css_matches[1]);
}
} while (css_matches);
let js_matches;
const jsRe = /<script\ssrc="(\/static\/js\/.+?)"><\/script>/g;
do {
js_matches = jsRe.exec(html_string);
if (js_matches) {
js_assets.push(js_matches[1]);
}
} while (js_matches);
const js_runtime = (assetManifest as {
[key: string]: string;
})[`runtime~${entry_key}.js`];
if (js_runtime) {
js_assets.unshift(js_runtime);
}
return {
css_assets,
js_assets
};
}; |
Sad to see #5955 and similar pull requests and issues being closed by stale bot. Is parsing the generated index.html file the only option forward when including CRA components on a site with a backend? |
I'm not sure what are the plans for this feature but what if we swapped out the webpack plugin for this one. Starting with version 3 it appends the |
We are hoping to add an |
I evaluated Both allow customization of generated manifest, either via However, the data exposed in customisation hooks lacks either the information about the order of the chunks, or whether the chunks needs to be included on the page or are asynchronous. CRA generates In order to allow users build their custom html based on manifest, we will have to follow similar approach. @iansu You mentioned adding entrypoints key to the manifest — does it imply that CRA is planning to support multiple entrypoints? Or we can assume a single index.html for now? Does anyone work on this story already? |
@zastavnitskiy I think #5955 show how you to reconstruct the HTML? |
@jbach, thank you, it does. Do you know why that MR was ignored and eventually closed? On the other side, at #5513 (comment), it seems that the v3.x direction is to extend existing asset-manifest instead of adding a new one. |
As far as I understand, enabling to extend the asset-manifest eventually allows adding the content of html-manifest to that singular manifest. |
I've created a small script (gist) that you can run in your pipeline or with It generates something like this, containing only the paths that you need in your html in the correct order.
Which your backend can then use to generate the I'm looking forward to a proper v3.x implementation by CRA though :) |
We also just got this problem when trying to add React components to an existing ASP.NET MVC page. We solved it similar to @mdecorte. I created a node script that uses I definitely think there should be an option of building a CRA React app optimized for embedding in existing pages, must be a pretty common scenario when slowly migrating to React. |
As mentioned in the discussion in #5306,
asset-manifest.json
generated by CRA2 is less than useful. As an example, this isasset-manifest.json
as generated byyarn build
for my project:It was quite an unpleasant surprise when I upgraded to CRA2. Here are some obvious problems with this file:
It can of course still be parsed by pattern matching file names but there are obvious disadvantages:
listdir()
the build directory and obtain basically the same informationHere is a suggested
asset-manifest.json
format that could make it usable:The text was updated successfully, but these errors were encountered: