/
index.js
254 lines (219 loc) · 7.13 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import { extname } from 'path';
import { createFilter } from '@rollup/pluginutils';
import getCommonDir from 'commondir';
import { peerDependencies } from '../package.json';
import analyzeTopLevelStatements from './analyze-top-level-statements';
import {
getDynamicPackagesEntryIntro,
getDynamicPackagesModule,
isDynamicModuleImport,
isModuleRegisterProxy,
unwrapModuleRegisterProxy
} from './dynamic-packages-manager';
import getDynamicRequirePaths from './dynamic-require-paths';
import {
DYNAMIC_JSON_PREFIX,
DYNAMIC_PACKAGES_ID,
EXTERNAL_SUFFIX,
getHelpersModule,
HELPERS_ID,
isWrappedId,
PROXY_SUFFIX,
unwrapId
} from './helpers';
import { setIsCjsPromise } from './is-cjs';
import { hasCjsKeywords } from './parse';
import {
getDynamicJsonProxy,
getDynamicRequireProxy,
getSpecificHelperProxy,
getStaticRequireProxy,
getUnknownRequireProxy
} from './proxies';
import getResolveId from './resolve-id';
import validateRollupVersion from './rollup-version';
import transformCommonjs from './transform-commonjs';
import { normalizePathSlashes } from './utils';
export default function commonjs(options = {}) {
const extensions = options.extensions || ['.js'];
const filter = createFilter(options.include, options.exclude);
const {
ignoreGlobal,
ignoreDynamicRequires,
requireReturnsDefault: requireReturnsDefaultOption,
esmExternals
} = options;
const getRequireReturnsDefault =
typeof requireReturnsDefaultOption === 'function'
? requireReturnsDefaultOption
: () => requireReturnsDefaultOption;
let esmExternalIds;
const isEsmExternal =
typeof esmExternals === 'function'
? esmExternals
: Array.isArray(esmExternals)
? ((esmExternalIds = new Set(esmExternals)), (id) => esmExternalIds.has(id))
: () => esmExternals;
const defaultIsModuleExports =
typeof options.defaultIsModuleExports === 'boolean' ? options.defaultIsModuleExports : 'auto';
const { dynamicRequireModuleSet, dynamicRequireModuleDirPaths } = getDynamicRequirePaths(
options.dynamicRequireTargets
);
const isDynamicRequireModulesEnabled = dynamicRequireModuleSet.size > 0;
const commonDir = isDynamicRequireModulesEnabled
? getCommonDir(null, Array.from(dynamicRequireModuleSet).concat(process.cwd()))
: null;
const esModulesWithDefaultExport = new Set();
const esModulesWithNamedExports = new Set();
const ignoreRequire =
typeof options.ignore === 'function'
? options.ignore
: Array.isArray(options.ignore)
? (id) => options.ignore.includes(id)
: () => false;
const getIgnoreTryCatchRequireStatementMode = (id) => {
const mode =
typeof options.ignoreTryCatch === 'function'
? options.ignoreTryCatch(id)
: Array.isArray(options.ignoreTryCatch)
? options.ignoreTryCatch.includes(id)
: options.ignoreTryCatch || false;
return {
canConvertRequire: mode !== 'remove' && mode !== true,
shouldRemoveRequireStatement: mode === 'remove'
};
};
const resolveId = getResolveId(extensions);
const sourceMap = options.sourceMap !== false;
function transformAndCheckExports(code, id) {
if (isDynamicRequireModulesEnabled && this.getModuleInfo(id).isEntry) {
// eslint-disable-next-line no-param-reassign
code =
getDynamicPackagesEntryIntro(dynamicRequireModuleDirPaths, dynamicRequireModuleSet) + code;
}
const { isEsModule, hasDefaultExport, hasNamedExports, ast } = analyzeTopLevelStatements(
this.parse,
code,
id
);
if (hasDefaultExport) {
esModulesWithDefaultExport.add(id);
}
if (hasNamedExports) {
esModulesWithNamedExports.add(id);
}
if (
!dynamicRequireModuleSet.has(normalizePathSlashes(id)) &&
(!hasCjsKeywords(code, ignoreGlobal) || (isEsModule && !options.transformMixedEsModules))
) {
return { meta: { commonjs: { isCommonJS: false } } };
}
let disableWrap = false;
// avoid wrapping in createCommonjsModule, as this is a commonjsRegister call
if (isModuleRegisterProxy(id)) {
disableWrap = true;
// eslint-disable-next-line no-param-reassign
id = unwrapModuleRegisterProxy(id);
}
return transformCommonjs(
this.parse,
code,
id,
isEsModule,
ignoreGlobal || isEsModule,
ignoreRequire,
ignoreDynamicRequires && !isDynamicRequireModulesEnabled,
getIgnoreTryCatchRequireStatementMode,
sourceMap,
isDynamicRequireModulesEnabled,
dynamicRequireModuleSet,
disableWrap,
commonDir,
ast,
defaultIsModuleExports
);
}
return {
name: 'commonjs',
buildStart() {
validateRollupVersion(this.meta.rollupVersion, peerDependencies.rollup);
if (options.namedExports != null) {
this.warn(
'The namedExports option from "@rollup/plugin-commonjs" is deprecated. Named exports are now handled automatically.'
);
}
},
resolveId,
load(id) {
if (id === HELPERS_ID) {
return getHelpersModule(isDynamicRequireModulesEnabled, ignoreDynamicRequires);
}
if (id.startsWith(HELPERS_ID)) {
return getSpecificHelperProxy(id);
}
if (isWrappedId(id, EXTERNAL_SUFFIX)) {
const actualId = unwrapId(id, EXTERNAL_SUFFIX);
return getUnknownRequireProxy(
actualId,
isEsmExternal(actualId) ? getRequireReturnsDefault(actualId) : true
);
}
if (id === DYNAMIC_PACKAGES_ID) {
return getDynamicPackagesModule(dynamicRequireModuleDirPaths, commonDir);
}
if (id.startsWith(DYNAMIC_JSON_PREFIX)) {
return getDynamicJsonProxy(id, commonDir);
}
if (isDynamicModuleImport(id, dynamicRequireModuleSet)) {
return `export default require(${JSON.stringify(normalizePathSlashes(id))});`;
}
if (isModuleRegisterProxy(id)) {
return getDynamicRequireProxy(
normalizePathSlashes(unwrapModuleRegisterProxy(id)),
commonDir
);
}
if (isWrappedId(id, PROXY_SUFFIX)) {
const actualId = unwrapId(id, PROXY_SUFFIX);
return getStaticRequireProxy(
actualId,
getRequireReturnsDefault(actualId),
esModulesWithDefaultExport,
esModulesWithNamedExports
);
}
return null;
},
transform(code, rawId) {
let id = rawId;
if (isModuleRegisterProxy(id)) {
id = unwrapModuleRegisterProxy(id);
}
const extName = extname(id);
if (
extName !== '.cjs' &&
id !== DYNAMIC_PACKAGES_ID &&
!id.startsWith(DYNAMIC_JSON_PREFIX) &&
(!filter(id) || !extensions.includes(extName))
) {
return null;
}
try {
return transformAndCheckExports.call(this, code, rawId);
} catch (err) {
return this.error(err, err.loc);
}
},
// eslint-disable-next-line no-shadow
moduleParsed({ id, meta: { commonjs } }) {
if (commonjs) {
const isCjs = commonjs.isCommonJS;
if (isCjs != null) {
setIsCjsPromise(id, isCjs);
return;
}
}
setIsCjsPromise(id, null);
}
};
}