Build files name change - files that are generated from create react app - reactjs

The build files generated through create react app have different names(hash code) every time.
I would like to give a custom names for the generated files.
Is there any possibility to do the same?

You can change the output filename by customizing the filename property in webpack config -- refer to https://webpack.js.org/guides/caching/
The default implementation is kept like this because, because every time you build an asset, it generates a new name and browsers won't be able to serve a cached response.
If you change the name to a constant you might need to clear the browser cache manually/ disable cache to see your changes immediately. (I think...Applicable only in prod mode as dev mode makes use of Hot module replacement)
Steps to change file name in CRA.
npm run eject This will unwind the hidden configs from CRA and show some additional config folders
Move to the config folder.
Edit file webpack.config.js (Somewhere around line 172 - 180 you can see filename: section where this is defined)

Following up to my comment, if you absolutely must change Webpack configuration you can also consider libraries such as:
Craco
Rescripts

Related

Gatsby build command - contentful plugin not getting spaceId & accessToken from .env file

I've been working on this for several hours and have been trying out a number of suggestions in similar questions here.
I can run my gatsby site develop build just fine, but when I run "gatsby build" I'm running into this error.
Invalid plugin options for "gatsby-source-contentful":
- "accessToken" is required
- "spaceId" is required
not finished open and validate gatsby-configs, load plugins - 1.427s
I have one .env file named ".env" with both of the necessary env variables, and in my gatsby-config file, I have it configured like this...
resolve: 'gatsby-source-contentful',
options: {
spaceId: process.env.CONTENTFUL_SPACE_ID,
accessToken: process.env.CONTENTFUL_ACCESS_TOKEN
}
I've tested out the accessToken and spaceId by manually adding them into my gatsby-config plugins options and ran "gatsby build" and they are working fine. When I change the config back to the env variables though, it's no good and I get the error I mentioned previously.
One thing I tried just recently was to make another .env file and name it ".env.production" and it threw a different error like this...
“error Cannot access Contentful space “*********ab” on environment “master” with access token “***************************************1234”. Make sure to double check them! (value)”
This led me to look into the master environment on contentful but I'm pretty new to it and it's got me confused.
I can find a lot of questions here with the same type of problem but I'm yet to find a solution that works. Any gatsby experts out there who've had this problem?
According to Environment variables docs:
In development, Gatsby will load environment variables from a file
named .env.development. For builds, it will load from .env.production.
So, in your scenario, just duplicate and rename your .env file to .env.development and .env.production. Gatsby will take the needed data depending on the fired command.

React Navigation: Why doesn't minified file in `lib` directory update when I change corresponding file in `src`?

I'm using Material Top Tab navigator in my React Native app. Specifically, I use the function createMaterialTopTabNavigator() which is found in react-navigation-tabs. react-navigation-tabs contains the file lib -> module -> views -> MaterialTopTabBar.js, which is a minified file corresponding to src -> views -> MaterialTopTabBar.tsx. I wanted to change something in MaterialTopTabBar.tsx because I might fork the module, but when I edited MaterialTopTabBar.tsx, even when I re-ran react-native run-ios, MaterialTopTabBar.js (in lib) didn't get updated. When I edited MaterialTopTabBar.js (the minified file) directly, the changes did show.
Does anyone know why MaterialTopTabBar.js doesn't get updated when I change MaterialTopTabBar.tsx and re-build the project?
UPDATE: I think it's that MaterialTopTabBar.tsx was transpiled to MaterialTopTabBar.js before the module was shipped, rather than when I build the project. One way I was able to solve the problem was to manually convert the Typescript code in MaterialTopTabBar.tsx to JS, and paste it into MaterialTopTabBar.js.
not lil data. I do not know if the bundler proccess is run by webpack or some else. but let's take CRA for example. The webpack-dev-server
has the following
// for some reason broken when imported through webpack. If you just want to
// use an image, put it in `src` and `import` it from JavaScript instead.
contentBase: paths.appPublic,
contentBasePublicPath: paths.publicUrlOrPath,
// By default files from `contentBase` will not trigger a page reload.
watchContentBase: true,
// Enable hot reloading server. It will provide WDS_SOCKET_PATH endpoint
so you need to find the place were contentBase and stuff are declare

Gatsby build path issues

Having issues running gatsby build with gatsby-starter-wordpress-advanced theme:
Error: ENOENT: no such file or directory, open 'C:\Users\Tobias\Desktop\Gatsby\gatsby-starter-wordpress-advanced\.template-cache\tmp-\.js'"
I figured this might be a problem with the path. The path should rather look like:
writing tmp-new-page/ template: open '.template-cache/tmp-new-page.js'
See repo: https://github.com/henrikwirth/gatsby-starter-wordpress-advanced/blob/master/create/utils.js
Line 53 you find the function createPageWithTemplate. I've tried console.log(page.uri) to see what's going on. It outputs the filename correctly. I've also tried with gatsby clean to clear the cache. It seems to be some kind of backslash issue where the path comes with a \ .js at the end instead of sample-page.js:
no such file or directory, open 'C:\Users\Tobias\Desktop\Gatsby\gatsby-starter-wordpress-advanced\.template-cache\sample-page\.js'
The issue have been resolved. The problem was related to update in WPGraphQL WordPress plugin. Had to update the paths, because the page.uri is different in the newer versions of WPGraphQL. Before it was just: some-page now it is /some-page/.
Secondly in the page template creation process the theme was using the uri, therefore, this messed up the paths for the template files. This has been switched to page.slug instead now and some extra checks, to make sure the frontPage is not ending up producing a wrong path.
The master branch of the starter theme have been updated.

How can I make typescript compile only needed files?

This is an XY problem; answering any part would do.
X: I want to generate multiple bundles out of a single source tree. I'm aware of webpack supporting this, but I'm afraid, I can't use it. For example, my Router gets fed by one or more maps mapping a route (string) to a page (component). I'm storing these maps in separate files and comment importing and using them out as needed (I wrote a simple script doing this for me).
Is there a better solution?
Y: So when building the admin bundle, the user pages are not reachable from the index.tsx. Nonetheless, I'm getting typescript errors for them. The same happens even when I create a new unused file containing an error.
How can I avoid compiling unused files?
I'm not posting here all my config files as I hope, there's a simple setting for this somewhere. I'll do it when needed. Alone the list makes me depressed:
.babelrc
.env
.eslintrc
.gitignore
config-overrides.js
package.json
tsconfig.json
tslint-imports.json
This is a partial answer, but hopefully it will take you in the right direction. Typescript looks at tsconfig.json. Assuming your initiation is from an npm script, you can specify which tsconfig to use with the --project flag, like tsc --project tsconfig-1.json.
In your tsconfig.json file, only include the entry file, like
include: [
"src/app/index.ts"
]
If you have something like "src/**/*", then it will process all files. But it you specify only the entry file, then it will process only the files in that import tree. The tree is defined by the import/import() statements.
For reference: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html
If you want to create multiple bundles, then you'll need to run multiple npm scripts and have multiple tsconfig files or else specify the files from the command line.

gruntjs / angularjs - optional development config?

Like most js web apps we have a config.js file that contains global config information about the app, base api urls and such. These values are often different in local development than in production.
I've looked at answers like: Development mode for AngularJS using GruntJS, and also things like grunt-replace for creating an on-the-fly config file.
My issue is that the "development" part varies from developer to developer, we all need a version of the API setup so the base api urls will be different. I'd like to allow each developer to override specific variables in the config in a way that doesn't require them to commit that info to the git repo (I agree that this isn't best practice, everything should be in the repo, but as this is only 1/2 variables for this project I can overlook it)
Any ideas on how to achieve this setup?
You can use grunt-preprocess. I would have production (and dev-server, etc) values in a file, say env.json. You could use grunt to look for an optional file, say overrides.json or developer.json, which would extend/overwrite the values from env.json.
var envFile = require('./env.json');
You can create command line options to grunt with grunt.option, e.g. var env = grunt.option('env') || undefined;, which could be used to turn off overriding.
You can get data from the optional file using fs.existsSync:
var fs = require('fs');
var developerFile;
if (fs.existsSync('./developer.json')) {
developerFile = require('./developer.json');
}
The simplest way to define the grunt-preprocess context would be to use the developer.json file if present, or the env.json file if not:
context: developerFile ? developerFile : envFile;
This requires the developer file to be complete. An alternative is to extend the envFile with options from developerFile if it's present.
In my project, we use different config files (which are basically files with JS object). So every developer has his app/configs/developer/config.js file, which is not comited in the source control, so every developer has his own setup. Project uses link to app/scripts/config.js by default and this file is just a soft link to developers config file. However, there are also app/configs/staging/config.js and app/configs/production/config.js files, which are replaced when using gruntjj to build project. Those configs are just copied to build solution instead of soft linked file.
I hope that makes sense..

Resources