diff --git a/__tests__/package-hoister.js b/__tests__/package-hoister.js new file mode 100644 index 0000000000..3dfa965be0 --- /dev/null +++ b/__tests__/package-hoister.js @@ -0,0 +1,44 @@ +/* @flow */ + +import PackageHoister, {HoistManifest} from '../src/package-hoister.js'; +import type PackageResolver from '../src/package-resolver.js'; +import type PackageReference from '../src/package-reference.js'; +import type Config from '../src/config.js'; +import type {Manifest} from '../src/types.js'; + +const path = require('path'); + +test('Produces valid destination paths for scoped modules', () => { + const expected = path.join(__dirname, './node_modules/@scoped/dep'); + const scopedPackageName = '@scoped/dep'; + + const config = (({ + cwd: __dirname, + getFolder(): string { + return 'node_modules'; + }, + }: any): Config); + + const resolver = (({}: any): PackageResolver); + + const key = scopedPackageName; + const parts = [scopedPackageName]; + + const pkg = (({ + _reference: (({}: any): PackageReference), + }: any): Manifest); + + const info = new HoistManifest(key, parts, pkg, ''); + + const tree = new Map([ + ['@scoped/dep', info], + ]); + + const packageHoister = new PackageHoister(config, resolver, false); + packageHoister.tree = tree; + + const result = packageHoister.init(); + const [actual] = result[0]; + + expect(actual).toEqual(expected); +}); diff --git a/src/package-hoister.js b/src/package-hoister.js index ab1c3630f9..19f524e009 100644 --- a/src/package-hoister.js +++ b/src/package-hoister.js @@ -349,7 +349,7 @@ export default class PackageHoister { for (const [key, info] of this.tree.entries()) { // decompress the location and push it to the flat tree. this path could be made // up of modules from different registries so we need to handle this specially - const parts = []; + const parts: Array = []; const keyParts = key.split('#'); for (let i = 0; i < keyParts.length; i++) { const key = keyParts.slice(0, i + 1).join('#'); @@ -363,13 +363,15 @@ export default class PackageHoister { // remove the first part which will be the folder name and replace it with a // hardcoded modules folder parts.shift(); - parts.unshift(this.config.modulesFolder); + const modulesFolder = (this.config.modulesFolder == null) ? '' : this.config.modulesFolder; + parts.unshift(modulesFolder); } else { // first part will be the registry-specific module folder - parts.unshift(this.config.cwd); + const cwd = (this.config.cwd == null) ? '' : this.config.cwd; + parts.unshift(cwd); } - const loc = parts.join(path.sep); + const loc = path.join(...parts); flatTree.push([loc, info]); }