This repository was archived by the owner on Aug 4, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +42
-2
lines changed Expand file tree Collapse file tree 2 files changed +42
-2
lines changed Original file line number Diff line number Diff line change 1
1
import { realpathSync , existsSync } from 'fs' ;
2
- import { extname , resolve } from 'path' ;
2
+ import { extname , resolve , normalize } from 'path' ;
3
3
import { sync as nodeResolveSync , isCore } from 'resolve' ;
4
4
import { createFilter } from 'rollup-pluginutils' ;
5
5
import { peerDependencies } from '../package.json' ;
@@ -40,6 +40,10 @@ export default function commonjs(options = {}) {
40
40
} catch ( err ) {
41
41
resolvedId = resolve ( id ) ;
42
42
}
43
+
44
+ // Note: customNamedExport's keys must be normalized file paths.
45
+ // resolve and nodeResolveSync both return normalized file paths
46
+ // so no additional normalization is necessary.
43
47
customNamedExports [ resolvedId ] = options . namedExports [ id ] ;
44
48
45
49
if ( existsSync ( resolvedId ) ) {
@@ -81,14 +85,16 @@ export default function commonjs(options = {}) {
81
85
return null ;
82
86
}
83
87
88
+ const normalizedId = normalize ( id ) ;
89
+
84
90
const transformed = transformCommonjs (
85
91
this . parse ,
86
92
code ,
87
93
id ,
88
94
this . getModuleInfo ( id ) . isEntry ,
89
95
ignoreGlobal ,
90
96
ignoreRequire ,
91
- customNamedExports [ id ] ,
97
+ customNamedExports [ normalizedId ] ,
92
98
sourceMap ,
93
99
allowDynamicRequire ,
94
100
ast
Original file line number Diff line number Diff line change @@ -822,5 +822,39 @@ exports.shuffleArray = shuffleArray_1;
822
822
`
823
823
) ;
824
824
} ) ;
825
+
826
+ it ( 'normalizes paths used in the named export map' , async ( ) => {
827
+ // Deliberately denormalizes file paths and ensures named exports
828
+ // continue to work.
829
+ function hookedResolve ( ) {
830
+ const resolvePlugin = resolve ( ) ;
831
+ const oldResolve = resolvePlugin . resolveId ;
832
+ resolvePlugin . resolveId = async function ( ) {
833
+ const result = await oldResolve . apply ( resolvePlugin , arguments ) ;
834
+ if ( result ) {
835
+ result . id = result . id . replace ( / \/ | \\ / , path . sep ) ;
836
+ }
837
+
838
+ return result ;
839
+ } ;
840
+
841
+ return resolvePlugin ;
842
+ }
843
+
844
+ const bundle = await rollup ( {
845
+ input : 'samples/custom-named-exports/main.js' ,
846
+ plugins : [
847
+ hookedResolve ( ) ,
848
+ commonjs ( {
849
+ namedExports : {
850
+ 'samples/custom-named-exports/secret-named-exporter.js' : [ 'named' ] ,
851
+ external : [ 'message' ]
852
+ }
853
+ } )
854
+ ]
855
+ } ) ;
856
+
857
+ await executeBundle ( bundle ) ;
858
+ } ) ;
825
859
} ) ;
826
860
} ) ;
You can’t perform that action at this time.
0 commit comments