1
1
import { extname , resolve } from 'path' ;
2
2
import { sync as nodeResolveSync } from 'resolve' ;
3
3
import { createFilter } from 'rollup-pluginutils' ;
4
- import { EXTERNAL_PREFIX , HELPERS , HELPERS_ID , PROXY_PREFIX } from './helpers.js' ;
4
+ import { peerDependencies } from '../package.json' ;
5
+ import { EXTERNAL_SUFFIX , getIdFromExternalProxyId , getIdFromProxyId , HELPERS , HELPERS_ID , PROXY_SUFFIX } from './helpers' ;
5
6
import { getIsCjsPromise , setIsCjsPromise } from './is-cjs' ;
6
7
import { getResolveId } from './resolve-id' ;
7
8
import { checkEsModule , hasCjsKeywords , transformCommonjs } from './transform.js' ;
@@ -27,8 +28,8 @@ export default function commonjs(options = {}) {
27
28
} ) ;
28
29
}
29
30
30
- const esModulesWithoutDefaultExport = Object . create ( null ) ;
31
- const esModulesWithDefaultExport = Object . create ( null ) ;
31
+ const esModulesWithoutDefaultExport = new Set ( ) ;
32
+ const esModulesWithDefaultExport = new Set ( ) ;
32
33
const allowDynamicRequire = ! ! options . ignore ; // TODO maybe this should be configurable?
33
34
34
35
const ignoreRequire =
@@ -38,24 +39,59 @@ export default function commonjs(options = {}) {
38
39
? id => options . ignore . includes ( id )
39
40
: ( ) => false ;
40
41
41
- let entryModuleIdsPromise = null ;
42
-
43
42
const resolveId = getResolveId ( extensions ) ;
44
43
45
44
const sourceMap = options . sourceMap !== false ;
46
45
46
+ function transformAndCheckExports ( code , id ) {
47
+ {
48
+ const { isEsModule, hasDefaultExport, ast } = checkEsModule ( this . parse , code , id ) ;
49
+ if ( isEsModule ) {
50
+ ( hasDefaultExport ? esModulesWithDefaultExport : esModulesWithoutDefaultExport ) . add ( id ) ;
51
+ return null ;
52
+ }
53
+
54
+ // it is not an ES module but it does not have CJS-specific elements.
55
+ if ( ! hasCjsKeywords ( code , ignoreGlobal ) ) {
56
+ esModulesWithoutDefaultExport . add ( id ) ;
57
+ return null ;
58
+ }
59
+
60
+ const transformed = transformCommonjs (
61
+ this . parse ,
62
+ code ,
63
+ id ,
64
+ this . getModuleInfo ( id ) . isEntry ,
65
+ ignoreGlobal ,
66
+ ignoreRequire ,
67
+ customNamedExports [ id ] ,
68
+ sourceMap ,
69
+ allowDynamicRequire ,
70
+ ast
71
+ ) ;
72
+ if ( ! transformed ) {
73
+ esModulesWithoutDefaultExport . add ( id ) ;
74
+ return null ;
75
+ }
76
+
77
+ return transformed ;
78
+ }
79
+ }
80
+
47
81
return {
48
82
name : 'commonjs' ,
49
83
50
- options ( options ) {
51
- resolveId . setRollupOptions ( options ) ;
52
- const input = options . input || options . entry ;
53
- const entryModules = Array . isArray ( input )
54
- ? input
55
- : typeof input === 'object' && input !== null
56
- ? Object . values ( input )
57
- : [ input ] ;
58
- entryModuleIdsPromise = Promise . all ( entryModules . map ( entry => resolveId ( entry ) ) ) ;
84
+ buildStart ( ) {
85
+ const [ major , minor ] = this . meta . rollupVersion . split ( '.' ) . map ( Number ) ;
86
+ const minVersion = peerDependencies . rollup . slice ( 2 ) ;
87
+ const [ minMajor , minMinor ] = minVersion . split ( '.' ) . map ( Number ) ;
88
+ if ( major < minMajor || ( major === minMajor && minor < minMinor ) ) {
89
+ this . error (
90
+ `Insufficient Rollup version: "rollup-plugin-commonjs" requires at least rollup@${ minVersion } but found rollup@${
91
+ this . meta . rollupVersion
92
+ } .`
93
+ ) ;
94
+ }
59
95
} ,
60
96
61
97
resolveId,
@@ -64,25 +100,25 @@ export default function commonjs(options = {}) {
64
100
if ( id === HELPERS_ID ) return HELPERS ;
65
101
66
102
// generate proxy modules
67
- if ( id . startsWith ( EXTERNAL_PREFIX ) ) {
68
- const actualId = id . slice ( EXTERNAL_PREFIX . length ) ;
103
+ if ( id . endsWith ( EXTERNAL_SUFFIX ) ) {
104
+ const actualId = getIdFromExternalProxyId ( id ) ;
69
105
const name = getName ( actualId ) ;
70
106
71
107
return `import ${ name } from ${ JSON . stringify ( actualId ) } ; export default ${ name } ;` ;
72
108
}
73
109
74
- if ( id . startsWith ( PROXY_PREFIX ) ) {
75
- const actualId = id . slice ( PROXY_PREFIX . length ) ;
110
+ if ( id . endsWith ( PROXY_SUFFIX ) ) {
111
+ const actualId = getIdFromProxyId ( id ) ;
76
112
const name = getName ( actualId ) ;
77
113
78
114
return getIsCjsPromise ( actualId ) . then ( isCjs => {
79
115
if ( isCjs )
80
116
return `import { __moduleExports } from ${ JSON . stringify (
81
117
actualId
82
118
) } ; export default __moduleExports;`;
83
- else if ( esModulesWithoutDefaultExport [ actualId ] )
119
+ else if ( esModulesWithoutDefaultExport . has ( actualId ) )
84
120
return `import * as ${ name } from ${ JSON . stringify ( actualId ) } ; export default ${ name } ;` ;
85
- else if ( esModulesWithDefaultExport [ actualId ] ) {
121
+ else if ( esModulesWithDefaultExport . has ( actualId ) ) {
86
122
return `export {default} from ${ JSON . stringify ( actualId ) } ;` ;
87
123
} else
88
124
return `import * as ${ name } from ${ JSON . stringify (
@@ -94,51 +130,20 @@ export default function commonjs(options = {}) {
94
130
95
131
transform ( code , id ) {
96
132
if ( ! filter ( id ) || extensions . indexOf ( extname ( id ) ) === - 1 ) {
97
- setIsCjsPromise ( id , Promise . resolve ( null ) ) ;
133
+ setIsCjsPromise ( id , null ) ;
98
134
return null ;
99
135
}
100
136
101
- const transformPromise = entryModuleIdsPromise
102
- . then ( entryModuleIds => {
103
- const { isEsModule, hasDefaultExport, ast } = checkEsModule ( this . parse , code , id ) ;
104
- if ( isEsModule ) {
105
- ( hasDefaultExport ? esModulesWithDefaultExport : esModulesWithoutDefaultExport ) [
106
- id
107
- ] = true ;
108
- return null ;
109
- }
110
-
111
- // it is not an ES module but it does not have CJS-specific elements.
112
- if ( ! hasCjsKeywords ( code , ignoreGlobal ) ) {
113
- esModulesWithoutDefaultExport [ id ] = true ;
114
- return null ;
115
- }
116
-
117
- const transformed = transformCommonjs (
118
- this . parse ,
119
- code ,
120
- id ,
121
- entryModuleIds . indexOf ( id ) !== - 1 ,
122
- ignoreGlobal ,
123
- ignoreRequire ,
124
- customNamedExports [ id ] ,
125
- sourceMap ,
126
- allowDynamicRequire ,
127
- ast
128
- ) ;
129
- if ( ! transformed ) {
130
- esModulesWithoutDefaultExport [ id ] = true ;
131
- return null ;
132
- }
133
-
134
- return transformed ;
135
- } )
136
- . catch ( err => {
137
- this . error ( err , err . loc ) ;
138
- } ) ;
137
+ let transformed ;
138
+ try {
139
+ transformed = transformAndCheckExports . call ( this , code , id ) ;
140
+ } catch ( err ) {
141
+ transformed = null ;
142
+ this . error ( err , err . loc ) ;
143
+ }
139
144
140
- setIsCjsPromise ( id , transformPromise . then ( Boolean , ( ) => false ) ) ;
141
- return transformPromise ;
145
+ setIsCjsPromise ( id , Boolean ( transformed ) ) ;
146
+ return transformed ;
142
147
}
143
148
} ;
144
149
}
0 commit comments