This repository was archived by the owner on Aug 7, 2021. It is now read-only.
refactor: support local install with npm 5 #281
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
NPM 5+ creates symlinks for all locally installed node packages.
process.env.INIT_CWD
is present from npm 5.4 and contains the original working directory that npm was executed from, opposed to __dirname which contains the script directory (if it's a symlink - the original destination on the file system).If we installed nativescript-dev-webpack from
/home/nativescript-dev-webpack
with npm 5+, a symlink will be created. Then, if we try to invokens-bundle
:Since the script needs the project directory, we need to use
INIT_CWD
instead of__dirname
.By default, node resolves all symlinked packages to their original locations. For example, if your project directory is
/home/my-project
and you installed/home/nativescript-dev-webpack
, node will resolve all imports ofnativescript-dev-webpack
from its original location -/home/nativescript-dev-webpack
.This will cause the following import (one of many) to fail:
The
webpack-sources
package is not a dependency of thenativescript-dev-webpack
plugin and therefore, is not present in/home/nativescript-dev-webpack/node_modules
. However, it is a dependency of the project and is meant to be resolved from there (/home/project/node_modules/webpack-sources
).With this change, when we are spawning new child process for webpack from the
ns-bundle
script, we provide the--preserve-symlinks
flag to instruct node to preserve all symlinks, therefore the plugin will be resolved from/project/node_modules/nativescript-dev-webpack
.