-
-
Notifications
You must be signed in to change notification settings - Fork 561
/
index.js
221 lines (192 loc) · 6.06 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
import { extname } from 'path';
import { createFilter } from '@rollup/pluginutils';
import getCommonDir from 'commondir';
import { peerDependencies } from '../package.json';
import {
getDynamicPackagesEntryIntro,
getDynamicPackagesModule,
isModuleRegistrationProxy
} from './dynamic-packages-manager';
import getDynamicRequirePaths from './dynamic-require-paths';
import {
DYNAMIC_JSON_PREFIX,
DYNAMIC_PACKAGES_ID,
EXTERNAL_SUFFIX,
getHelpersModule,
getIdFromExternalProxyId,
getIdFromProxyId,
HELPERS_ID,
PROXY_SUFFIX
} from './helpers';
import { setIsCjsPromise } from './is-cjs';
import {
getDynamicJsonProxy,
getDynamicRequireProxy,
getSpecificHelperProxy,
getStaticRequireProxy,
getUnknownRequireProxy
} from './proxies';
import getResolveId from './resolve-id';
import {
checkEsModule,
hasCjsKeywords,
normalizePathSlashes,
transformCommonjs
} from './transform';
export default function commonjs(options = {}) {
const extensions = options.extensions || ['.js'];
const filter = createFilter(options.include, options.exclude);
const {
ignoreGlobal,
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 { 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 resolveId = getResolveId(extensions);
const sourceMap = options.sourceMap !== false;
function transformAndCheckExports(code, id) {
if (isDynamicRequireModulesEnabled && this.getModuleInfo(id).isEntry) {
code = getDynamicPackagesEntryIntro(
dynamicRequireModuleDirPaths,
dynamicRequireModuleSet
) + code;
}
const { isEsModule, hasDefaultExport, hasNamedExports, ast } = checkEsModule(
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 } } };
}
// avoid wrapping in createCommonjsModule, as this is a commonjsRegister call
const disableWrap = isModuleRegistrationProxy(id, dynamicRequireModuleSet);
return transformCommonjs(
this.parse,
code,
id,
isEsModule,
ignoreGlobal || isEsModule,
ignoreRequire,
sourceMap,
isDynamicRequireModulesEnabled,
dynamicRequireModuleSet,
disableWrap,
commonDir,
ast
);
}
return {
name: 'commonjs',
buildStart() {
if (options.namedExports != null) {
this.warn(
'The namedExports option from "@rollup/plugin-commonjs" is deprecated. Named exports are now handled automatically.'
);
}
const [major, minor] = this.meta.rollupVersion.split('.').map(Number);
const minVersion = peerDependencies.rollup.slice(2);
const [minMajor, minMinor] = minVersion.split('.').map(Number);
if (major < minMajor || (major === minMajor && minor < minMinor)) {
this.error(
`Insufficient Rollup version: "@rollup/plugin-commonjs" requires at least rollup@${minVersion} but found rollup@${this.meta.rollupVersion}.`
);
}
},
resolveId,
load(id) {
if (id === HELPERS_ID) {
return getHelpersModule(isDynamicRequireModulesEnabled);
}
if (id.startsWith(HELPERS_ID)) {
return getSpecificHelperProxy(id);
}
if (id.endsWith(EXTERNAL_SUFFIX)) {
const actualId = getIdFromExternalProxyId(id);
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 (isModuleRegistrationProxy(id, dynamicRequireModuleSet)) {
return getDynamicRequireProxy(normalizePathSlashes(id), commonDir);
}
if (id.endsWith(PROXY_SUFFIX)) {
const actualId = getIdFromProxyId(id);
return getStaticRequireProxy(
actualId,
getRequireReturnsDefault(actualId),
esModulesWithDefaultExport,
esModulesWithNamedExports
);
}
return null;
},
transform(code, 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, id);
} catch (err) {
return this.error(err, err.loc);
}
},
moduleParsed({ id, meta: { commonjs } }) {
if (commonjs) {
const isCjs = commonjs.isCommonJS;
if (isCjs != null) {
setIsCjsPromise(id, isCjs);
return;
}
}
setIsCjsPromise(id, null);
}
};
}