Skip to content
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
merged 2 commits into from
Oct 5, 2017
Merged

Conversation

sis0k0
Copy link
Contributor

@sis0k0 sis0k0 commented Oct 3, 2017

NPM 5+ creates symlinks for all locally installed node packages.

  • use INIT_CWD instead of __dirname when the package is symlinked
    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 invoke ns-bundle:
// /home/project/node_modules/nativescript-dev-webpack/bin/ns-bundle

console.log(process.env.INIT_CWD); // /home/project
console.log(__dirname); // /home/nativescript-dev-webpack/bin

Since the script needs the project directory, we need to use INIT_CWD instead of __dirname.

  • ns-bundle script: invoke webpack with node and --preserve-symlinks
node --preserve-symlinks ./node_modules/.bin/webpack

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 of nativescript-dev-webpack from its original location - /home/nativescript-dev-webpack.
This will cause the following import (one of many) to fail:

// nativescript-dev-webpack/plugins/GenerateBundleStarterPlugin.js
const { RawSource } = require("webpack-sources");

The webpack-sources package is not a dependency of the nativescript-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.

@sis0k0 sis0k0 changed the title wip: Vlaeva/npm5 local install refactor: support local install with npm 5 Oct 3, 2017
@sis0k0 sis0k0 requested a review from PanayotCankov October 3, 2017 16:04
@NativeScript NativeScript deleted a comment from sis0k0 Oct 4, 2017
@@ -30,11 +30,36 @@ const writePackageJson = (content, projectDir) => {
fs.writeFileSync(packageJsonPath, JSON.stringify(content, null, 2))
}

const getProjectDir = ({ nestingLvl } = { nestingLvl: 0 }) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be useful: https://www.npmjs.com/package/app-root-path or may be not. :)

@@ -152,7 +152,9 @@ function webpack(platform, env) {
console.log(`Running webpack for ${platform}...`);

const args = [
`webpack`,
`node`,
`--preserve-symlinks`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@sis0k0 sis0k0 force-pushed the vlaeva/npm5-local-install branch from 17ebdb3 to 1eb923c Compare October 5, 2017 11:10
@sis0k0 sis0k0 force-pushed the vlaeva/npm5-local-install branch from 1eb923c to 1191a50 Compare October 5, 2017 11:24
@sis0k0 sis0k0 force-pushed the vlaeva/npm5-local-install branch from 1191a50 to 1b892dc Compare October 5, 2017 11:45
@sis0k0 sis0k0 merged commit d17ea4d into master Oct 5, 2017
@sis0k0 sis0k0 deleted the vlaeva/npm5-local-install branch October 5, 2017 12:26
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants