1 | 'use strict';
|
2 |
|
3 | function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
4 |
|
5 | var fs = require('fs');
|
6 | var path = require('path');
|
7 | var rollupPluginutils = require('rollup-pluginutils');
|
8 | var estreeWalker = require('estree-walker');
|
9 | var MagicString = _interopDefault(require('magic-string'));
|
10 | var resolve = require('resolve');
|
11 |
|
12 | var HELPERS_ID = '\0commonjsHelpers';
|
13 |
|
14 | var HELPERS = "\nexport var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};\n\nexport function commonjsRequire () {\n\tthrow new Error('Dynamic requires are not currently supported by rollup-plugin-commonjs');\n}\n\nexport function unwrapExports (x) {\n\treturn x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;\n}\n\nexport function createCommonjsModule(fn, module) {\n\treturn module = { exports: {} }, fn(module, module.exports), module.exports;\n}";
|
15 |
|
16 | var PREFIX = '\0commonjs-proxy:';
|
17 | var EXTERNAL = '\0commonjs-external:';
|
18 |
|
19 | function isFile ( file ) {
|
20 | try {
|
21 | var stats = fs.statSync( file );
|
22 | return stats.isFile();
|
23 | } catch ( err ) {
|
24 | return false;
|
25 | }
|
26 | }
|
27 |
|
28 | function addJsExtensionIfNecessary ( file ) {
|
29 | if ( isFile( file ) ) { return file; }
|
30 |
|
31 | file += '.js';
|
32 | if ( isFile( file ) ) { return file; }
|
33 |
|
34 | return null;
|
35 | }
|
36 |
|
37 | var absolutePath = /^(?:\/|(?:[A-Za-z]:)?[\\|/])/;
|
38 |
|
39 | function isAbsolute ( path$$1 ) {
|
40 | return absolutePath.test( path$$1 );
|
41 | }
|
42 |
|
43 | function defaultResolver ( importee, importer ) {
|
44 |
|
45 | if ( isAbsolute( importee ) ) { return addJsExtensionIfNecessary( path.resolve( importee ) ); }
|
46 |
|
47 |
|
48 | if ( importer === undefined ) { return addJsExtensionIfNecessary( path.resolve( process.cwd(), importee ) ); }
|
49 |
|
50 |
|
51 | if ( importee[0] !== '.' ) { return null; }
|
52 |
|
53 | return addJsExtensionIfNecessary( path.resolve( path.dirname( importer ), importee ) );
|
54 | }
|
55 |
|
56 | function isReference ( node, parent ) {
|
57 | if ( parent.type === 'MemberExpression' ) { return parent.computed || node === parent.object; }
|
58 |
|
59 |
|
60 | if ( parent.type === 'Property' && node !== parent.value ) { return false; }
|
61 |
|
62 |
|
63 | if ( parent.type === 'MethodDefinition' ) { return false; }
|
64 |
|
65 |
|
66 | if ( parent.type === 'ExportSpecifier' && node !== parent.local ) { return false; }
|
67 |
|
68 | return true;
|
69 | }
|
70 |
|
71 | function flatten ( node ) {
|
72 | var parts = [];
|
73 |
|
74 | while ( node.type === 'MemberExpression' ) {
|
75 | if ( node.computed ) { return null; }
|
76 |
|
77 | parts.unshift( node.property.name );
|
78 | node = node.object;
|
79 | }
|
80 |
|
81 | if ( node.type !== 'Identifier' ) { return null; }
|
82 |
|
83 | var name = node.name;
|
84 | parts.unshift( name );
|
85 |
|
86 | return { name: name, keypath: parts.join( '.' ) };
|
87 | }
|
88 |
|
89 | function extractNames ( node ) {
|
90 | var names = [];
|
91 | extractors[ node.type ]( names, node );
|
92 | return names;
|
93 | }
|
94 |
|
95 | var extractors = {
|
96 | Identifier: function Identifier ( names, node ) {
|
97 | names.push( node.name );
|
98 | },
|
99 |
|
100 | ObjectPattern: function ObjectPattern ( names, node ) {
|
101 | node.properties.forEach( function (prop) {
|
102 | extractors[ prop.value.type ]( names, prop.value );
|
103 | });
|
104 | },
|
105 |
|
106 | ArrayPattern: function ArrayPattern ( names, node ) {
|
107 | node.elements.forEach( function (element) {
|
108 | if ( element ) { extractors[ element.type ]( names, element ); }
|
109 | });
|
110 | },
|
111 |
|
112 | RestElement: function RestElement ( names, node ) {
|
113 | extractors[ node.argument.type ]( names, node.argument );
|
114 | },
|
115 |
|
116 | AssignmentPattern: function AssignmentPattern ( names, node ) {
|
117 | extractors[ node.left.type ]( names, node.left );
|
118 | }
|
119 | };
|
120 |
|
121 |
|
122 | function isTruthy ( node ) {
|
123 | if ( node.type === 'Literal' ) { return !!node.value; }
|
124 | if ( node.type === 'ParenthesizedExpression' ) { return isTruthy( node.expression ); }
|
125 | if ( node.operator in operators ) { return operators[ node.operator ]( node ); }
|
126 | }
|
127 |
|
128 | function isFalsy ( node ) {
|
129 | return not( isTruthy( node ) );
|
130 | }
|
131 |
|
132 | function not ( value ) {
|
133 | return value === undefined ? value : !value;
|
134 | }
|
135 |
|
136 | function equals ( a, b, strict ) {
|
137 | if ( a.type !== b.type ) { return undefined; }
|
138 | if ( a.type === 'Literal' ) { return strict ? a.value === b.value : a.value == b.value; }
|
139 | }
|
140 |
|
141 | var operators = {
|
142 | '==': function (x) {
|
143 | return equals( x.left, x.right, false );
|
144 | },
|
145 |
|
146 | '!=': function (x) { return not( operators['==']( x ) ); },
|
147 |
|
148 | '===': function (x) {
|
149 | return equals( x.left, x.right, true );
|
150 | },
|
151 |
|
152 | '!==': function (x) { return not( operators['===']( x ) ); },
|
153 |
|
154 | '!': function (x) { return isFalsy( x.argument ); },
|
155 |
|
156 | '&&': function (x) { return isTruthy( x.left ) && isTruthy( x.right ); },
|
157 |
|
158 | '||': function (x) { return isTruthy( x.left ) || isTruthy( x.right ); }
|
159 | };
|
160 |
|
161 | function getName ( id ) {
|
162 | var name = rollupPluginutils.makeLegalIdentifier( path.basename( id, path.extname( id ) ) );
|
163 | if (name !== 'index') {
|
164 | return name;
|
165 | } else {
|
166 | var segments = path.dirname( id ).split( path.sep );
|
167 | return rollupPluginutils.makeLegalIdentifier( segments[segments.length - 1] );
|
168 | }
|
169 | }
|
170 |
|
171 | var reserved = 'abstract arguments boolean break byte case catch char class const continue debugger default delete do double else enum eval export extends false final finally float for function goto if implements import in instanceof int interface let long native new null package private protected public return short static super switch synchronized this throw throws transient true try typeof var void volatile while with yield'.split( ' ' );
|
172 | var blacklist = { __esModule: true };
|
173 | reserved.forEach( function (word) { return blacklist[ word ] = true; } );
|
174 |
|
175 | var exportsPattern = /^(?:module\.)?exports(?:\.([a-zA-Z_$][a-zA-Z_$0-9]*))?$/;
|
176 |
|
177 | var firstpassGlobal = /\b(?:require|module|exports|global)\b/;
|
178 | var firstpassNoGlobal = /\b(?:require|module|exports)\b/;
|
179 | var importExportDeclaration = /^(?:Import|Export(?:Named|Default))Declaration/;
|
180 | var functionType = /^(?:FunctionDeclaration|FunctionExpression|ArrowFunctionExpression)$/;
|
181 |
|
182 | function deconflict ( scope, globals, identifier ) {
|
183 | var i = 1;
|
184 | var deconflicted = identifier;
|
185 |
|
186 | while ( scope.contains( deconflicted ) || globals.has( deconflicted ) || deconflicted in blacklist ) { deconflicted = identifier + "_" + (i++); }
|
187 | scope.declarations[ deconflicted ] = true;
|
188 |
|
189 | return deconflicted;
|
190 | }
|
191 |
|
192 | function tryParse ( parse, code, id ) {
|
193 | try {
|
194 | return parse( code, { allowReturnOutsideFunction: true });
|
195 | } catch ( err ) {
|
196 | err.message += " in " + id;
|
197 | throw err;
|
198 | }
|
199 | }
|
200 |
|
201 | function checkFirstpass (code, ignoreGlobal) {
|
202 | var firstpass = ignoreGlobal ? firstpassNoGlobal : firstpassGlobal;
|
203 | return firstpass.test(code);
|
204 | }
|
205 |
|
206 | function checkEsModule ( parse, code, id ) {
|
207 | var ast = tryParse( parse, code, id );
|
208 |
|
209 |
|
210 | var hasDefaultExport = false;
|
211 | var isEsModule = false;
|
212 | for ( var i = 0, list = ast.body; i < list.length; i += 1 ) {
|
213 | var node = list[i];
|
214 |
|
215 | if ( node.type === 'ExportDefaultDeclaration' )
|
216 | { hasDefaultExport = true; }
|
217 | if ( importExportDeclaration.test( node.type ) )
|
218 | { isEsModule = true; }
|
219 | }
|
220 |
|
221 | return { isEsModule: isEsModule, hasDefaultExport: hasDefaultExport, ast: ast };
|
222 | }
|
223 |
|
224 | function transformCommonjs ( parse, code, id, isEntry, ignoreGlobal, ignoreRequire, customNamedExports, sourceMap, allowDynamicRequire, astCache ) {
|
225 | var ast = astCache || tryParse( parse, code, id );
|
226 |
|
227 | var magicString = new MagicString( code );
|
228 |
|
229 | var required = {};
|
230 |
|
231 |
|
232 | var sources = [];
|
233 |
|
234 | var uid = 0;
|
235 |
|
236 | var scope = rollupPluginutils.attachScopes( ast, 'scope' );
|
237 | var uses = { module: false, exports: false, global: false, require: false };
|
238 |
|
239 | var lexicalDepth = 0;
|
240 | var programDepth = 0;
|
241 |
|
242 | var globals = new Set();
|
243 |
|
244 | var HELPERS_NAME = deconflict( scope, globals, 'commonjsHelpers' );
|
245 |
|
246 | var namedExports = {};
|
247 |
|
248 |
|
249 | var shouldWrap = /__esModule/.test( code );
|
250 |
|
251 | function isRequireStatement ( node ) {
|
252 | if ( !node ) { return; }
|
253 | if ( node.type !== 'CallExpression' ) { return; }
|
254 | if ( node.callee.name !== 'require' || scope.contains( 'require' ) ) { return; }
|
255 | if ( node.arguments.length !== 1 || (node.arguments[0].type !== 'Literal' && (node.arguments[0].type !== 'TemplateLiteral' || node.arguments[0].expressions.length > 0) ) ) { return; }
|
256 | if ( ignoreRequire( node.arguments[0].value ) ) { return; }
|
257 |
|
258 | return true;
|
259 | }
|
260 |
|
261 | function getRequired ( node, name ) {
|
262 | var source = node.arguments[0].type === 'Literal' ? node.arguments[0].value : node.arguments[0].quasis[0].value.cooked;
|
263 |
|
264 | var existing = required[ source ];
|
265 | if ( existing === undefined ) {
|
266 | sources.push( source );
|
267 |
|
268 | if ( !name ) {
|
269 | do { name = "require$$" + (uid++); }
|
270 | while ( scope.contains( name ) );
|
271 | }
|
272 |
|
273 | required[ source ] = { source: source, name: name, importsDefault: false };
|
274 | }
|
275 |
|
276 | return required[ source ];
|
277 | }
|
278 |
|
279 |
|
280 |
|
281 |
|
282 | var assignedTo = new Set();
|
283 | estreeWalker.walk( ast, {
|
284 | enter: function enter ( node ) {
|
285 | if ( node.type !== 'AssignmentExpression' ) { return; }
|
286 | if ( node.left.type === 'MemberExpression' ) { return; }
|
287 |
|
288 | extractNames( node.left ).forEach( function (name) {
|
289 | assignedTo.add( name );
|
290 | });
|
291 | }
|
292 | });
|
293 |
|
294 | estreeWalker.walk( ast, {
|
295 | enter: function enter ( node, parent ) {
|
296 | if ( sourceMap ) {
|
297 | magicString.addSourcemapLocation( node.start );
|
298 | magicString.addSourcemapLocation( node.end );
|
299 | }
|
300 |
|
301 |
|
302 | if ( parent && ( parent.type === 'IfStatement' || parent.type === 'ConditionalExpression' ) ) {
|
303 | if ( node === parent.consequent && isFalsy( parent.test ) ) { return this.skip(); }
|
304 | if ( node === parent.alternate && isTruthy( parent.test ) ) { return this.skip(); }
|
305 | }
|
306 |
|
307 | if ( node._skip ) { return this.skip(); }
|
308 |
|
309 | programDepth += 1;
|
310 |
|
311 | if ( node.scope ) { scope = node.scope; }
|
312 | if ( functionType.test( node.type ) ) { lexicalDepth += 1; }
|
313 |
|
314 |
|
315 | if ( node.type === 'ReturnStatement' && lexicalDepth === 0 ) {
|
316 | shouldWrap = true;
|
317 | }
|
318 |
|
319 |
|
320 | if ( node.type === 'ThisExpression' && lexicalDepth === 0 ) {
|
321 | uses.global = true;
|
322 | if ( !ignoreGlobal ) { magicString.overwrite( node.start, node.end, (HELPERS_NAME + ".commonjsGlobal"), { storeName: true } ); }
|
323 | return;
|
324 | }
|
325 |
|
326 |
|
327 | if ( node.type === 'UnaryExpression' && node.operator === 'typeof' ) {
|
328 | var flattened = flatten( node.argument );
|
329 | if ( !flattened ) { return; }
|
330 |
|
331 | if ( scope.contains( flattened.name ) ) { return; }
|
332 |
|
333 | if ( flattened.keypath === 'module.exports' || flattened.keypath === 'module' || flattened.keypath === 'exports' ) {
|
334 | magicString.overwrite( node.start, node.end, "'object'", { storeName: false } );
|
335 | }
|
336 | }
|
337 |
|
338 |
|
339 |
|
340 | if ( node.type === 'Identifier' ) {
|
341 | if ( isReference( node, parent ) && !scope.contains( node.name ) ) {
|
342 | if ( node.name in uses ) {
|
343 | if ( node.name === 'require' ) {
|
344 | if ( allowDynamicRequire ) { return; }
|
345 | magicString.overwrite( node.start, node.end, (HELPERS_NAME + ".commonjsRequire"), { storeName: true } );
|
346 | }
|
347 |
|
348 | uses[ node.name ] = true;
|
349 | if ( node.name === 'global' && !ignoreGlobal ) {
|
350 | magicString.overwrite( node.start, node.end, (HELPERS_NAME + ".commonjsGlobal"), { storeName: true } );
|
351 | }
|
352 |
|
353 |
|
354 |
|
355 | if ( node.name === 'module' || node.name === 'exports' ) {
|
356 | shouldWrap = true;
|
357 | }
|
358 | }
|
359 |
|
360 | if ( node.name === 'define' ) {
|
361 | magicString.overwrite( node.start, node.end, 'undefined', { storeName: true } );
|
362 | }
|
363 |
|
364 | globals.add( node.name );
|
365 | }
|
366 |
|
367 | return;
|
368 | }
|
369 |
|
370 |
|
371 | if ( node.type === 'AssignmentExpression' ) {
|
372 | if ( node.left.type !== 'MemberExpression' ) { return; }
|
373 |
|
374 | var flattened$1 = flatten( node.left );
|
375 | if ( !flattened$1 ) { return; }
|
376 |
|
377 | if ( scope.contains( flattened$1.name ) ) { return; }
|
378 |
|
379 | var match = exportsPattern.exec( flattened$1.keypath );
|
380 | if ( !match || flattened$1.keypath === 'exports' ) { return; }
|
381 |
|
382 | uses[ flattened$1.name ] = true;
|
383 |
|
384 |
|
385 |
|
386 | if ( programDepth > 3 ) { shouldWrap = true; }
|
387 |
|
388 | node.left._skip = true;
|
389 |
|
390 | if ( flattened$1.keypath === 'module.exports' && node.right.type === 'ObjectExpression' ) {
|
391 | return node.right.properties.forEach( function (prop) {
|
392 | if ( prop.computed || prop.key.type !== 'Identifier' ) { return; }
|
393 | var name = prop.key.name;
|
394 | if ( name === rollupPluginutils.makeLegalIdentifier( name ) ) { namedExports[ name ] = true; }
|
395 | });
|
396 | }
|
397 |
|
398 | if ( match[1] ) { namedExports[ match[1] ] = true; }
|
399 | return;
|
400 | }
|
401 |
|
402 |
|
403 | if ( node.type === 'VariableDeclarator' && node.id.type === 'Identifier' && isRequireStatement( node.init ) ) {
|
404 |
|
405 | if ( scope.parent ) { return; }
|
406 |
|
407 |
|
408 | if ( assignedTo.has( node.id.name ) ) { return; }
|
409 |
|
410 | var r$1 = getRequired( node.init, node.id.name );
|
411 | r$1.importsDefault = true;
|
412 |
|
413 | if ( r$1.name === node.id.name ) {
|
414 | node._shouldRemove = true;
|
415 | }
|
416 | }
|
417 |
|
418 | if ( !isRequireStatement( node ) ) { return; }
|
419 |
|
420 | var r = getRequired( node );
|
421 |
|
422 | if ( parent.type === 'ExpressionStatement' ) {
|
423 |
|
424 | magicString.remove( parent.start, parent.end );
|
425 | } else {
|
426 | r.importsDefault = true;
|
427 | magicString.overwrite( node.start, node.end, r.name );
|
428 | }
|
429 |
|
430 | node.callee._skip = true;
|
431 | },
|
432 |
|
433 | leave: function leave ( node ) {
|
434 | programDepth -= 1;
|
435 | if ( node.scope ) { scope = scope.parent; }
|
436 | if ( functionType.test( node.type ) ) { lexicalDepth -= 1; }
|
437 |
|
438 | if ( node.type === 'VariableDeclaration' ) {
|
439 | var keepDeclaration = false;
|
440 | var c = node.declarations[0].start;
|
441 |
|
442 | for ( var i = 0; i < node.declarations.length; i += 1 ) {
|
443 | var declarator = node.declarations[i];
|
444 |
|
445 | if ( declarator._shouldRemove ) {
|
446 | magicString.remove( c, declarator.end );
|
447 | } else {
|
448 | if ( !keepDeclaration ) {
|
449 | magicString.remove( c, declarator.start );
|
450 | keepDeclaration = true;
|
451 | }
|
452 |
|
453 | c = declarator.end;
|
454 | }
|
455 | }
|
456 |
|
457 | if ( !keepDeclaration ) {
|
458 | magicString.remove( node.start, node.end );
|
459 | }
|
460 | }
|
461 | }
|
462 | });
|
463 |
|
464 | if ( !sources.length && !uses.module && !uses.exports && !uses.require && ( ignoreGlobal || !uses.global ) ) {
|
465 | if ( Object.keys( namedExports ).length ) {
|
466 | throw new Error( ("Custom named exports were specified for " + id + " but it does not appear to be a CommonJS module") );
|
467 | }
|
468 | return null;
|
469 | }
|
470 |
|
471 | var includeHelpers = shouldWrap || uses.global || uses.require;
|
472 | var importBlock = ( includeHelpers ? [ ("import * as " + HELPERS_NAME + " from '" + HELPERS_ID + "';") ] : [] ).concat(
|
473 | sources.map( function (source) {
|
474 |
|
475 |
|
476 | return ("import '" + source + "';");
|
477 | }),
|
478 | sources.map( function (source) {
|
479 | var ref = required[ source ];
|
480 | var name = ref.name;
|
481 | var importsDefault = ref.importsDefault;
|
482 | return ("import " + (importsDefault ? (name + " from ") : "") + "'" + PREFIX + source + "';");
|
483 | })
|
484 | ).join( '\n' ) + '\n\n';
|
485 |
|
486 | var namedExportDeclarations = [];
|
487 | var wrapperStart = '';
|
488 | var wrapperEnd = '';
|
489 |
|
490 | var moduleName = deconflict( scope, globals, getName( id ) );
|
491 | if ( !isEntry ) {
|
492 | var exportModuleExports = {
|
493 | str: ("export { " + moduleName + " as __moduleExports };"),
|
494 | name: '__moduleExports'
|
495 | };
|
496 |
|
497 | namedExportDeclarations.push( exportModuleExports );
|
498 | }
|
499 |
|
500 | var name = getName( id );
|
501 |
|
502 | function addExport ( x ) {
|
503 | var deconflicted = deconflict( scope, globals, name );
|
504 |
|
505 | var declaration = deconflicted === name ?
|
506 | ("export var " + x + " = " + moduleName + "." + x + ";") :
|
507 | ("var " + deconflicted + " = " + moduleName + "." + x + ";\nexport { " + deconflicted + " as " + x + " };");
|
508 |
|
509 | namedExportDeclarations.push({
|
510 | str: declaration,
|
511 | name: x
|
512 | });
|
513 | }
|
514 |
|
515 | if ( customNamedExports ) { customNamedExports.forEach( addExport ); }
|
516 |
|
517 | var defaultExportPropertyAssignments = [];
|
518 | var hasDefaultExport = false;
|
519 |
|
520 | if ( shouldWrap ) {
|
521 | var args = "module" + (uses.exports ? ', exports' : '');
|
522 |
|
523 | wrapperStart = "var " + moduleName + " = " + HELPERS_NAME + ".createCommonjsModule(function (" + args + ") {\n";
|
524 | wrapperEnd = "\n});";
|
525 | } else {
|
526 | var names = [];
|
527 |
|
528 | ast.body.forEach( function (node) {
|
529 | if ( node.type === 'ExpressionStatement' && node.expression.type === 'AssignmentExpression' ) {
|
530 | var left = node.expression.left;
|
531 | var flattened = flatten( left );
|
532 |
|
533 | if ( !flattened ) { return; }
|
534 |
|
535 | var match = exportsPattern.exec( flattened.keypath );
|
536 | if ( !match ) { return; }
|
537 |
|
538 | if ( flattened.keypath === 'module.exports' ) {
|
539 | hasDefaultExport = true;
|
540 | magicString.overwrite( left.start, left.end, ("var " + moduleName) );
|
541 | } else {
|
542 | var name = match[1];
|
543 | var deconflicted = deconflict( scope, globals, name );
|
544 |
|
545 | names.push({ name: name, deconflicted: deconflicted });
|
546 |
|
547 | magicString.overwrite( node.start, left.end, ("var " + deconflicted) );
|
548 |
|
549 | var declaration = name === deconflicted ?
|
550 | ("export { " + name + " };") :
|
551 | ("export { " + deconflicted + " as " + name + " };");
|
552 |
|
553 | if ( name !== 'default' ) {
|
554 | namedExportDeclarations.push({
|
555 | str: declaration,
|
556 | name: name
|
557 | });
|
558 | delete namedExports[name];
|
559 | }
|
560 |
|
561 | defaultExportPropertyAssignments.push( (moduleName + "." + name + " = " + deconflicted + ";") );
|
562 | }
|
563 | }
|
564 | });
|
565 |
|
566 | if ( !hasDefaultExport ) {
|
567 | wrapperEnd = "\n\nvar " + moduleName + " = {\n" + (names.map( function (ref) {
|
568 | var name = ref.name;
|
569 | var deconflicted = ref.deconflicted;
|
570 |
|
571 | return ("\t" + name + ": " + deconflicted);
|
572 | } ).join( ',\n' )) + "\n};";
|
573 | }
|
574 | }
|
575 | Object.keys( namedExports )
|
576 | .filter( function (key) { return !blacklist[ key ]; } )
|
577 | .forEach( addExport );
|
578 |
|
579 | var defaultExport = /__esModule/.test( code ) ?
|
580 | ("export default " + HELPERS_NAME + ".unwrapExports(" + moduleName + ");") :
|
581 | ("export default " + moduleName + ";");
|
582 |
|
583 | var named = namedExportDeclarations
|
584 | .filter( function (x) { return x.name !== 'default' || !hasDefaultExport; } )
|
585 | .map( function (x) { return x.str; } );
|
586 |
|
587 | var exportBlock = '\n\n' + [ defaultExport ]
|
588 | .concat( named )
|
589 | .concat( hasDefaultExport ? defaultExportPropertyAssignments : [] )
|
590 | .join( '\n' );
|
591 |
|
592 | magicString.trim()
|
593 | .prepend( importBlock + wrapperStart )
|
594 | .trim()
|
595 | .append( wrapperEnd + exportBlock );
|
596 |
|
597 | code = magicString.toString();
|
598 | var map = sourceMap ? magicString.generateMap() : null;
|
599 |
|
600 | return { code: code, map: map };
|
601 | }
|
602 |
|
603 | function getCandidatesForExtension ( resolved, extension ) {
|
604 | return [
|
605 | resolved + extension,
|
606 | resolved + path.sep + "index" + extension
|
607 | ];
|
608 | }
|
609 |
|
610 | function getCandidates ( resolved, extensions ) {
|
611 | return extensions.reduce(
|
612 | function ( paths, extension ) { return paths.concat( getCandidatesForExtension ( resolved, extension ) ); },
|
613 | [resolved]
|
614 | );
|
615 | }
|
616 |
|
617 |
|
618 |
|
619 | function first ( candidates ) {
|
620 | return function () {
|
621 | var args = [], len = arguments.length;
|
622 | while ( len-- ) args[ len ] = arguments[ len ];
|
623 |
|
624 | return candidates.reduce( function ( promise, candidate ) {
|
625 | return promise.then( function (result) { return result != null ?
|
626 | result :
|
627 | Promise.resolve( candidate.apply( void 0, args ) ); } );
|
628 | }, Promise.resolve() );
|
629 | };
|
630 | }
|
631 |
|
632 | function startsWith ( str, prefix ) {
|
633 | return str.slice( 0, prefix.length ) === prefix;
|
634 | }
|
635 |
|
636 |
|
637 | function commonjs ( options ) {
|
638 | if ( options === void 0 ) options = {};
|
639 |
|
640 | var extensions = options.extensions || ['.js'];
|
641 | var filter = rollupPluginutils.createFilter( options.include, options.exclude );
|
642 | var ignoreGlobal = options.ignoreGlobal;
|
643 |
|
644 | var customNamedExports = {};
|
645 | if ( options.namedExports ) {
|
646 | Object.keys( options.namedExports ).forEach( function (id) {
|
647 | var resolvedId;
|
648 |
|
649 | try {
|
650 | resolvedId = resolve.sync( id, { basedir: process.cwd() });
|
651 | } catch ( err ) {
|
652 | resolvedId = path.resolve( id );
|
653 | }
|
654 |
|
655 | customNamedExports[ resolvedId ] = options.namedExports[ id ];
|
656 | });
|
657 | }
|
658 |
|
659 | var esModulesWithoutDefaultExport = [];
|
660 |
|
661 | var allowDynamicRequire = !!options.ignore;
|
662 |
|
663 | var ignoreRequire = typeof options.ignore === 'function' ?
|
664 | options.ignore :
|
665 | Array.isArray( options.ignore ) ? function (id) { return ~options.ignore.indexOf( id ); } :
|
666 | function () { return false; };
|
667 |
|
668 | var entryModuleIdsPromise = null;
|
669 |
|
670 | function resolveId ( importee, importer ) {
|
671 | if ( importee === HELPERS_ID ) { return importee; }
|
672 |
|
673 | if ( importer && startsWith( importer, PREFIX ) ) { importer = importer.slice( PREFIX.length ); }
|
674 |
|
675 | var isProxyModule = startsWith( importee, PREFIX );
|
676 | if ( isProxyModule ) { importee = importee.slice( PREFIX.length ); }
|
677 |
|
678 | return resolveUsingOtherResolvers( importee, importer ).then( function (resolved) {
|
679 | if ( resolved ) { return isProxyModule ? PREFIX + resolved : resolved; }
|
680 |
|
681 | resolved = defaultResolver( importee, importer );
|
682 |
|
683 | if ( isProxyModule ) {
|
684 | if ( resolved ) { return PREFIX + resolved; }
|
685 | return EXTERNAL + importee;
|
686 | }
|
687 |
|
688 | return resolved;
|
689 | });
|
690 | }
|
691 |
|
692 | var sourceMap = options.sourceMap !== false;
|
693 |
|
694 | var resolveUsingOtherResolvers;
|
695 |
|
696 | var isCjsPromises = Object.create(null);
|
697 | function getIsCjsPromise ( id ) {
|
698 | var isCjsPromise = isCjsPromises[id];
|
699 | if (isCjsPromise)
|
700 | { return isCjsPromise.promise; }
|
701 |
|
702 | var promise = new Promise( function (resolve$$1) {
|
703 | isCjsPromises[id] = isCjsPromise = {
|
704 | resolve: resolve$$1,
|
705 | promise: undefined
|
706 | };
|
707 | });
|
708 | isCjsPromise.promise = promise;
|
709 |
|
710 | return promise;
|
711 | }
|
712 | function setIsCjsPromise ( id, promise ) {
|
713 | var isCjsPromise = isCjsPromises[id];
|
714 | if (isCjsPromise) {
|
715 | if (isCjsPromise.resolve) {
|
716 | isCjsPromise.resolve(promise);
|
717 | isCjsPromise.resolve = undefined;
|
718 | }
|
719 | }
|
720 | else {
|
721 | isCjsPromises[id] = { promise: promise, resolve: undefined };
|
722 | }
|
723 | }
|
724 |
|
725 | return {
|
726 | name: 'commonjs',
|
727 |
|
728 | options: function options ( options$1 ) {
|
729 | var resolvers = ( options$1.plugins || [] )
|
730 | .map( function (plugin) {
|
731 | if ( plugin.resolveId === resolveId ) {
|
732 |
|
733 | return function ( importee, importer ) {
|
734 | if ( importee[0] !== '.' || !importer ) { return; }
|
735 |
|
736 | var resolved = path.resolve( path.dirname( importer ), importee );
|
737 | var candidates = getCandidates( resolved, extensions );
|
738 |
|
739 | for ( var i = 0; i < candidates.length; i += 1 ) {
|
740 | try {
|
741 | var stats = fs.statSync( candidates[i] );
|
742 | if ( stats.isFile() ) { return candidates[i]; }
|
743 | } catch ( err ) { }
|
744 | }
|
745 | };
|
746 | }
|
747 |
|
748 | return plugin.resolveId;
|
749 | })
|
750 | .filter( Boolean );
|
751 |
|
752 | var isExternal = function (id) { return options$1.external ?
|
753 | Array.isArray( options$1.external ) ? ~options$1.external.indexOf( id ) :
|
754 | options$1.external(id) :
|
755 | false; };
|
756 |
|
757 | resolvers.unshift( function (id) { return isExternal( id ) ? false : null; } );
|
758 |
|
759 | resolveUsingOtherResolvers = first( resolvers );
|
760 |
|
761 | var entryModules = [].concat( options$1.input || options$1.entry );
|
762 | entryModuleIdsPromise = Promise.all(
|
763 | entryModules.map( function (entry) { return resolveId( entry ); })
|
764 | );
|
765 | },
|
766 |
|
767 | resolveId: resolveId,
|
768 |
|
769 | load: function load ( id ) {
|
770 | if ( id === HELPERS_ID ) { return HELPERS; }
|
771 |
|
772 |
|
773 | if ( startsWith( id, EXTERNAL ) ) {
|
774 | var actualId = id.slice( EXTERNAL.length );
|
775 | var name = getName( actualId );
|
776 |
|
777 | return ("import " + name + " from " + (JSON.stringify( actualId )) + "; export default " + name + ";");
|
778 | }
|
779 |
|
780 | if ( startsWith( id, PREFIX ) ) {
|
781 | var actualId$1 = id.slice( PREFIX.length );
|
782 | var name$1 = getName( actualId$1 );
|
783 |
|
784 | return ( ( extensions.indexOf( path.extname( id ) ) === -1 ) ? Promise.resolve(false) : getIsCjsPromise( actualId$1 ) )
|
785 | .then( function (isCjs) {
|
786 | if ( isCjs )
|
787 | { return ("import { __moduleExports } from " + (JSON.stringify( actualId$1 )) + "; export default __moduleExports;"); }
|
788 | else if (esModulesWithoutDefaultExport.indexOf(actualId$1) !== -1)
|
789 | { return ("import * as " + name$1 + " from " + (JSON.stringify( actualId$1 )) + "; export default " + name$1 + ";"); }
|
790 | else
|
791 | { return ("import * as " + name$1 + " from " + (JSON.stringify( actualId$1 )) + "; export default ( " + name$1 + " && " + name$1 + "['default'] ) || " + name$1 + ";"); }
|
792 | });
|
793 | }
|
794 | },
|
795 |
|
796 | transform: function transform ( code, id ) {
|
797 | var this$1 = this;
|
798 |
|
799 | if ( !filter( id ) ) { return null; }
|
800 | if ( extensions.indexOf( path.extname( id ) ) === -1 ) { return null; }
|
801 |
|
802 | var transformPromise = entryModuleIdsPromise.then( function (entryModuleIds) {
|
803 | var ref = checkEsModule( this$1.parse, code, id );
|
804 | var isEsModule = ref.isEsModule;
|
805 | var hasDefaultExport = ref.hasDefaultExport;
|
806 | var ast = ref.ast;
|
807 | if ( isEsModule ) {
|
808 | if ( !hasDefaultExport )
|
809 | { esModulesWithoutDefaultExport.push( id ); }
|
810 | return;
|
811 | }
|
812 |
|
813 |
|
814 | if ( !checkFirstpass( code, ignoreGlobal ) ) {
|
815 | esModulesWithoutDefaultExport.push( id );
|
816 | return;
|
817 | }
|
818 |
|
819 | var transformed = transformCommonjs( this$1.parse, code, id, entryModuleIds.indexOf(id) !== -1, ignoreGlobal, ignoreRequire, customNamedExports[ id ], sourceMap, allowDynamicRequire, ast );
|
820 | if ( !transformed ) {
|
821 | esModulesWithoutDefaultExport.push( id );
|
822 | return;
|
823 | }
|
824 |
|
825 | return transformed;
|
826 | }).catch(function (err) {
|
827 | this$1.error(err, err.loc);
|
828 | });
|
829 |
|
830 | setIsCjsPromise(id, transformPromise.then( function (transformed) { return transformed ? true : false; }, function () { return true; } ));
|
831 |
|
832 | return transformPromise;
|
833 | }
|
834 | };
|
835 | }
|
836 |
|
837 | module.exports = commonjs;
|
838 |
|