Skip to content
This repository was archived by the owner on Aug 4, 2021. It is now read-only.

Commit c609f8f

Browse files
authored
Merge pull request #140 from kellyselden/preserveSymlinks
respect `preserveSymlinks` option from upstream
2 parents 634dfae + 93dedb0 commit c609f8f

File tree

4 files changed

+87
-60
lines changed

4 files changed

+87
-60
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"es5-ext": "^0.10.39",
88
"eslint": "^4.18.0",
99
"mocha": "^5.0.1",
10-
"rollup": "^0.54.1",
10+
"rollup": "^0.56.3",
1111
"rollup-plugin-buble": "^0.19.2",
1212
"rollup-plugin-commonjs": "^8.3.0",
1313
"string-capitalize": "^1.0.1",

src/index.js

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,15 @@ export default function nodeResolve ( options = {} ) {
2828
throw new Error( `At least one of options.module, options.main or options.jsnext must be true` );
2929
}
3030

31+
let preserveSymlinks;
32+
3133
return {
3234
name: 'node-resolve',
3335

36+
options ( options ) {
37+
preserveSymlinks = options.preserveSymlinks;
38+
},
39+
3440
resolveId ( importee, importer ) {
3541
if ( /\0/.test( importee ) ) return null; // ignore IDs with null character, these belong to other plugins
3642

@@ -64,43 +70,49 @@ export default function nodeResolve ( options = {} ) {
6470
let disregardResult = false;
6571
let packageBrowserField = false;
6672

67-
resolveId(
68-
importee,
69-
Object.assign({
70-
basedir: dirname( importer ),
71-
packageFilter ( pkg, pkgPath ) {
72-
const pkgRoot = dirname( pkgPath );
73-
if (options.browser && typeof pkg[ 'browser' ] === 'object') {
74-
packageBrowserField = Object.keys(pkg[ 'browser' ]).reduce((browser, key) => {
75-
const resolved = pkg[ 'browser' ][ key ] === false ? false : resolve( pkgRoot, pkg[ 'browser' ][ key ] );
76-
browser[ key ] = resolved;
77-
if ( key[0] === '.' ) {
78-
const absoluteKey = resolve( pkgRoot, key );
79-
browser[ absoluteKey ] = resolved;
80-
if ( !extname(key) ) {
81-
exts.reduce( ( browser, ext ) => {
82-
browser[ absoluteKey + ext ] = browser[ key ];
83-
return browser;
84-
}, browser );
85-
}
73+
const resolveOptions = {
74+
basedir: dirname( importer ),
75+
packageFilter ( pkg, pkgPath ) {
76+
const pkgRoot = dirname( pkgPath );
77+
if (options.browser && typeof pkg[ 'browser' ] === 'object') {
78+
packageBrowserField = Object.keys(pkg[ 'browser' ]).reduce((browser, key) => {
79+
const resolved = pkg[ 'browser' ][ key ] === false ? false : resolve( pkgRoot, pkg[ 'browser' ][ key ] );
80+
browser[ key ] = resolved;
81+
if ( key[0] === '.' ) {
82+
const absoluteKey = resolve( pkgRoot, key );
83+
browser[ absoluteKey ] = resolved;
84+
if ( !extname(key) ) {
85+
exts.reduce( ( browser, ext ) => {
86+
browser[ absoluteKey + ext ] = browser[ key ];
87+
return browser;
88+
}, browser );
8689
}
87-
return browser;
88-
}, {});
89-
}
90+
}
91+
return browser;
92+
}, {});
93+
}
94+
95+
if (options.browser && typeof pkg[ 'browser' ] === 'string') {
96+
pkg[ 'main' ] = pkg[ 'browser' ];
97+
} else if ( useModule && pkg[ 'module' ] ) {
98+
pkg[ 'main' ] = pkg[ 'module' ];
99+
} else if ( useJsnext && pkg[ 'jsnext:main' ] ) {
100+
pkg[ 'main' ] = pkg[ 'jsnext:main' ];
101+
} else if ( ( useJsnext || useModule ) && !useMain ) {
102+
disregardResult = true;
103+
}
104+
return pkg;
105+
},
106+
extensions: options.extensions
107+
};
90108

91-
if (options.browser && typeof pkg[ 'browser' ] === 'string') {
92-
pkg[ 'main' ] = pkg[ 'browser' ];
93-
} else if ( useModule && pkg[ 'module' ] ) {
94-
pkg[ 'main' ] = pkg[ 'module' ];
95-
} else if ( useJsnext && pkg[ 'jsnext:main' ] ) {
96-
pkg[ 'main' ] = pkg[ 'jsnext:main' ];
97-
} else if ( ( useJsnext || useModule ) && !useMain ) {
98-
disregardResult = true;
99-
}
100-
return pkg;
101-
},
102-
extensions: options.extensions
103-
}, customResolveOptions ),
109+
if (preserveSymlinks !== undefined) {
110+
resolveOptions.preserveSymlinks = preserveSymlinks;
111+
}
112+
113+
resolveId(
114+
importee,
115+
Object.assign( resolveOptions, customResolveOptions ),
104116
( err, resolved ) => {
105117
if (options.browser && packageBrowserField) {
106118
if (packageBrowserField[ resolved ]) {
@@ -110,7 +122,7 @@ export default function nodeResolve ( options = {} ) {
110122
}
111123

112124
if ( !disregardResult && !err ) {
113-
if ( resolved && fs.existsSync( resolved ) ) {
125+
if ( !preserveSymlinks && resolved && fs.existsSync( resolved ) ) {
114126
resolved = fs.realpathSync( resolved );
115127
}
116128

test/test.js

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ function executeBundle ( bundle ) {
1414
}).then( generated => {
1515
const fn = new Function ( 'module', 'exports', 'assert', generated.code );
1616
const module = { exports: {} };
17-
17+
1818
fn( module, module.exports, assert );
19-
19+
2020
return module;
2121
});
2222
}
@@ -436,24 +436,7 @@ describe( 'rollup-plugin-node-resolve', function () {
436436
});
437437
});
438438

439-
it( 'resolves symlinked packages', () => {
440-
createMissingDirectories();
441-
linkDirectories();
442-
443-
return rollup.rollup({
444-
input: 'samples/symlinked/first/index.js',
445-
plugins: [
446-
nodeResolve()
447-
]
448-
}).then( executeBundle ).then( module => {
449-
assert.equal( module.exports.number1, module.exports.number2 );
450-
}).then(() => {
451-
unlinkDirectories();
452-
}).catch(err => {
453-
unlinkDirectories();
454-
throw err;
455-
});
456-
439+
describe( 'symlinks', () => {
457440
function createMissingDirectories () {
458441
createDirectory( './samples/symlinked/first/node_modules' );
459442
createDirectory( './samples/symlinked/second/node_modules' );
@@ -477,6 +460,38 @@ describe( 'rollup-plugin-node-resolve', function () {
477460
fs.unlinkSync('./samples/symlinked/first/node_modules/third');
478461
fs.unlinkSync('./samples/symlinked/second/node_modules/third');
479462
}
463+
464+
beforeEach( () => {
465+
createMissingDirectories();
466+
linkDirectories();
467+
});
468+
469+
afterEach( () => {
470+
unlinkDirectories();
471+
});
472+
473+
it( 'resolves symlinked packages', () => {
474+
return rollup.rollup({
475+
input: 'samples/symlinked/first/index.js',
476+
plugins: [
477+
nodeResolve()
478+
]
479+
}).then( executeBundle ).then( module => {
480+
assert.equal( module.exports.number1, module.exports.number2 );
481+
});
482+
});
483+
484+
it( 'preserves symlinks if `preserveSymlinks` is true', () => {
485+
return rollup.rollup({
486+
input: 'samples/symlinked/first/index.js',
487+
plugins: [
488+
nodeResolve()
489+
],
490+
preserveSymlinks: true
491+
}).then( executeBundle ).then( module => {
492+
assert.notEqual( module.exports.number1, module.exports.number2 );
493+
});
494+
});
480495
});
481496

482497
it( 'prefers jsnext:main field over main', () => {

0 commit comments

Comments
 (0)