-
Notifications
You must be signed in to change notification settings - Fork 490
Absolute paths using REACT_APP_NODE_PATH
instead of NODE_PATH
#178
Conversation
Changing `NODE_PATH` to `REACT_APP_NODE_PATH`
Changing Dev Config to use `REACT_APP_NODE_PATH` instead of `NODE_PATH`
Setting prod config to use `REACT_APP_NODE_PATH` instead of `NODE_PATH`
Updating createJestConfig to use `REACT_APP_NODE_PATH` and `node_modules` as the base for module resolution.
Adjusting code to avoid linting issue where `env` variable was defined but not used. Updated logic of the moduleDirectories to be closer to how the dev and prod configs are setup allowing for splitting based upon a path delimiter as well as making the path relative. EG: The line: `{full-path}/some/path;{full-path}/some/other/path`: Should result in: ['/some/path', '/some/other/path'] by removing {full-path} and split based upon the `;` delimiter.
@eonwhite @DorianGrey could you have a look over this? |
I'm not sure about the advantage of renaming |
Hey @DorianGrey @wmonk, Thank you for responding! I apologize for the background. About the switching from
I tried variants, but
I interpreted this as the same issue when they were not. |
Again, apologies, going to remove all traces of responses in #.122 to reduce confusion. I ran through tests again, and Actions taken:
It is thus unnecessary to rename Again, Apologies, thanks for your time! |
@DorianGrey here is a concept that may work in
if (!!process.env.NODE_PATH) {
const tsconfig = JSON.parse(fs.readFileSync(paths.appTsConfig, 'utf8'));
const hasMultiplePaths = process.env.NODE_PATH.indexOf(path.delimiter) !== -1;
const hasBaseUrl = typeof tsconfig.compilerOptions.baseUrl !== 'undefined';
const hasPaths = typeof tsconfig.compilerOptions.paths !== 'undefined';
if (hasMultiplePaths && !(hasBaseUrl && hasPaths)) {
throw new Error(
`Setting multiple paths using the NODE_PATH env variable requires adding baseUrl and paths to compilerOptions in tsconfig.json. For more information on how to setup paths in tsconfig.json visit: https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping
`
);
}
if (!hasBaseUrl) {
throw new Error(
`Setting a path using the NODE_PATH env variable requires adding baseUrl to compilerOptions in tsconfig.json. For more information on how to setup baseUrl visit: https://www.typescriptlang.org/docs/handbook/module-resolution.html#base-url
`
);
}
} |
Just a short hint for
That does not work really well in most terminals on windows when Regarding the checks: The seconds check looks good. However, there is a caveat on the first check:
This one isn't precise enough - it would only check for an amount, not for matching paths.
it should be ensured that all paths included by |
Hey @DorianGrey, I am going to put examples below to see if I am understanding these correctly. If you have time please let me know your thoughts! Assumptions
ExamplesAssuming the following import on the examples: import 'src/common/components/Test' Example 1 - Match anything with two search paths (glob & generated/glob)tsconfig.json
Result: Example 2 - Match
|
Examples 2, 3 and 4 should be correct, from what I can see. |
Hey @DorianGrey, Sorry for taking so long to respond, Example 1 was from the configs on the typescript site above wasn't sure about that. Here is round two, this would conceptually be placed around line 47 on /**
* Determine if tsconfig.json and NODE_ENV contain the same configuration with paths.
*
* @param {*} tsconfig compilerOptions
* @param {*} The process.env.NODE_ENV variable
*/
function validatePathsMatchBetweenConfigs(compilerOptions, nodePaths) {
const tsPaths = compilerOptions.paths;
const mismatchedPaths = nodePaths
.split(path.delimiter)
.reduce(
(unmatchedPaths, nodePath) => unmatchedPaths.filter(tsPath => tsPath !== nodePath),
Object.keys(tsPaths).map(key => `${compilerOptions.baseUrl}/${tsPaths[key]}`)
);
return mismatchedPaths.length === 0;
}
if (!!process.env.NODE_PATH) {
const tsconfig = JSON.parse(fs.readFileSync(paths.appTsConfig, 'utf8'));
const options = tsconfig.compilerOptions;
const nodePath = process.env.NODE_PATH;
const hasMultiplePaths = nodePath.indexOf(path.delimiter) !== -1;
const hasBaseUrl = typeof options.baseUrl !== 'undefined';
const hasPaths = typeof options.paths !== 'undefined';
if (hasMultiplePaths) {
if (!(hasBaseUrl && hasPaths)) {
throw new Error(
`Setting multiple paths using the NODE_PATH env variable requires adding baseUrl and paths to compilerOptions in tsconfig.json. For more information on how to setup paths in tsconfig.json visit: https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping
`
);
}
const pathsAreInSync = validatePathsMatchBetweenConfigs(options, nodePath);
if (!pathsAreInSync) {
throw new Error(
`A mismatch exists between NODE_PATH env variable and paths set in the compilerOptions in tsconfig.json. For more information on how to setup paths and baseUrl visit: https://www.typescriptlang.org/docs/handbook/module-resolution.html#base-url
`
);
}
}
if (!hasBaseUrl) {
throw new Error(
`Setting a path using the NODE_PATH env variable requires adding baseUrl to compilerOptions in tsconfig.json. For more information on how to setup baseUrl visit: https://www.typescriptlang.org/docs/handbook/module-resolution.html#base-url
`
);
}
} |
There are several caveats with this approach.
Never mentioned this would by easy ;) |
Thank you again for your time @DorianGrey! Agreed on the full matching it seems that any match checking would end up being brittle in nature. (2, 3, and 4).
Below in the gist I reworked the concept (moving towards external file) using the direct port of what you used for tslint. I added a concept in, if the system does throw an error we could put an example (clearly marked as an example partial) so that users could get a quick idea of a possible way to use Multiple Paths
Single Path
Again appreciate your thoughts and time! https://gist.github.com/smacpherson64/425d8003d9033d83f85e445b8c5fbb61 |
That looks good, aside from some (minor) code issues:
A more detailed comparison of the paths would be favorable, but I'm afraid there is quite a huge potential for false positives during this evaluation. Suppose it's better to leave it cut off as it is in your current implementation. Might add some improvements here in case we find a robust way to implement such checks. Thinking about this a little more, it comes to my mind that Something like: const paths = require('./paths');
// ...
const currentPath = paths.resolveApp();
const nodePaths = nodePathEnv
.split(path.delimiter)
.filter(p => p.indexOf(currentPath) >= 0)
;
if (nodePaths.length > 0) {
// The rest of the checks
} |
Closing PR as it is does not resolve issue
Aimed to fix #.122
Creating concept to utilize
REACT_APP_
variables in.env
files to solve the issue of Absolute paths.