@@ -28,9 +28,15 @@ export default function nodeResolve ( options = {} ) {
28
28
throw new Error ( `At least one of options.module, options.main or options.jsnext must be true` ) ;
29
29
}
30
30
31
+ let preserveSymlinks ;
32
+
31
33
return {
32
34
name : 'node-resolve' ,
33
35
36
+ options ( options ) {
37
+ preserveSymlinks = options . preserveSymlinks ;
38
+ } ,
39
+
34
40
resolveId ( importee , importer ) {
35
41
if ( / \0 / . test ( importee ) ) return null ; // ignore IDs with null character, these belong to other plugins
36
42
@@ -64,43 +70,49 @@ export default function nodeResolve ( options = {} ) {
64
70
let disregardResult = false ;
65
71
let packageBrowserField = false ;
66
72
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 ) ;
86
89
}
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
+ } ;
90
108
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 ) ,
104
116
( err , resolved ) => {
105
117
if ( options . browser && packageBrowserField ) {
106
118
if ( packageBrowserField [ resolved ] ) {
@@ -110,7 +122,7 @@ export default function nodeResolve ( options = {} ) {
110
122
}
111
123
112
124
if ( ! disregardResult && ! err ) {
113
- if ( resolved && fs . existsSync ( resolved ) ) {
125
+ if ( ! preserveSymlinks && resolved && fs . existsSync ( resolved ) ) {
114
126
resolved = fs . realpathSync ( resolved ) ;
115
127
}
116
128
0 commit comments