babel-plugin-transform-modules-ui5 7.0.5 → 7.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,25 +4,17 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.ClassTransformVisitor = void 0;
7
-
8
7
  var _core = require("@babel/core");
9
-
10
8
  var th = _interopRequireWildcard(require("../utils/templates"));
11
-
12
9
  var ast = _interopRequireWildcard(require("../utils/ast"));
13
-
14
10
  var classes = _interopRequireWildcard(require("./helpers/classes"));
15
-
16
- function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
17
-
18
- function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
19
-
11
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
12
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
20
13
  const CONSTRUCTOR = "constructor";
21
14
  const ClassTransformVisitor = {
22
15
  ImportDefaultSpecifier(path) {
23
16
  this.importNames.push(path.node.local.name);
24
17
  },
25
-
26
18
  /**
27
19
  * ClassDeclaration visitor.
28
20
  * Use both enter() and exit() to track when the visitor is inside a UI5 class,
@@ -39,35 +31,32 @@ const ClassTransformVisitor = {
39
31
  parent,
40
32
  parentPath
41
33
  } = path;
42
-
43
34
  if (opts.neverConvertClass) {
44
35
  return;
45
36
  }
46
-
47
37
  if (!doesClassExtendFromImport(node, [...this.importNames])) {
48
38
  // If it doesn't extend from an import, treat it as plain ES2015 class.
49
39
  return;
50
- } // If the super class is one of the imports, we'll assume it's a UI5 managed class,
51
- // and therefore may need to be transformed to .extend() syntax.
52
-
40
+ }
53
41
 
42
+ // If the super class is one of the imports, we'll assume it's a UI5 managed class,
43
+ // and therefore may need to be transformed to .extend() syntax.
54
44
  const classInfo = classes.getClassInfo(path, node, path.parent, opts);
55
-
56
45
  if (!shouldConvertClass(file, node, opts, classInfo)) {
57
46
  return;
58
- } // Save super class name for converting super calls
59
-
47
+ }
60
48
 
61
- this.superClassName = classInfo.superClassName; // Find the Block scoped parent (Program or Function body) and search for assigned properties within that (eg. MyClass.X = "X").
49
+ // Save super class name for converting super calls
50
+ this.superClassName = classInfo.superClassName;
62
51
 
52
+ // Find the Block scoped parent (Program or Function body) and search for assigned properties within that (eg. MyClass.X = "X").
63
53
  const {
64
54
  name: className
65
55
  } = node.id;
66
56
  const blockParent = path.findParent(path => path.isBlock()).node;
67
- const staticProps = ast.groupPropertiesByName(ast.getOtherPropertiesOfIdentifier(blockParent, className)); // TODO: flag metadata and renderer for removal if applicable
68
-
57
+ const staticProps = ast.groupPropertiesByName(ast.getOtherPropertiesOfIdentifier(blockParent, className));
58
+ // TODO: flag metadata and renderer for removal if applicable
69
59
  const ui5ExtendClass = classes.convertClassToUI5Extend(path, node, classInfo, staticProps, opts);
70
-
71
60
  if (path.isClassDeclaration()) {
72
61
  if (_core.types.isExportDefaultDeclaration(parent)) {
73
62
  path.parentPath.replaceWithMultiple([...ui5ExtendClass, th.buildExportDefault({
@@ -85,17 +74,13 @@ const ClassTransformVisitor = {
85
74
  ID: _core.types.identifier(classInfo.localName)
86
75
  }));
87
76
  }
88
-
89
77
  parentPath.replaceWithMultiple(ui5ExtendClass);
90
78
  }
91
79
  },
92
-
93
80
  exit() {
94
81
  this.superClassName = null;
95
82
  }
96
-
97
83
  },
98
-
99
84
  /*!
100
85
  * Visits function calls.
101
86
  */
@@ -105,8 +90,8 @@ const ClassTransformVisitor = {
105
90
  } = path;
106
91
  const {
107
92
  callee
108
- } = node; // If the file already has sap.ui.define, get the names of variables it creates to use for the class logic.
109
-
93
+ } = node;
94
+ // If the file already has sap.ui.define, get the names of variables it creates to use for the class logic.
110
95
  if (ast.isCallExpressionCalling(node, "sap.ui.define")) {
111
96
  this.importNames.push(...getRequiredParamsOfSAPUIDefine(path, node).map(req => req.name));
112
97
  return;
@@ -120,7 +105,6 @@ const ClassTransformVisitor = {
120
105
  }
121
106
  }
122
107
  },
123
-
124
108
  /**
125
109
  * Convert object method constructor() to constructor: function constructor(),
126
110
  * since a UI5 class is not a real class.
@@ -129,7 +113,6 @@ const ClassTransformVisitor = {
129
113
  const {
130
114
  node
131
115
  } = path;
132
-
133
116
  if (node.key.name === CONSTRUCTOR) {
134
117
  // The keyword 'constructor' should not be used as a shorthand
135
118
  // method name in an object. It might(?) work on some objects,
@@ -137,79 +120,65 @@ const ClassTransformVisitor = {
137
120
  path.replaceWith(_core.types.objectProperty(_core.types.identifier(CONSTRUCTOR), _core.types.functionExpression(_core.types.identifier(CONSTRUCTOR), node.params, node.body)));
138
121
  }
139
122
  }
140
-
141
123
  };
142
124
  exports.ClassTransformVisitor = ClassTransformVisitor;
143
-
144
125
  function isSuperApply(callee) {
145
126
  return _core.types.isIdentifier(callee.property, {
146
- "name": "apply"
127
+ name: "apply"
147
128
  }) && _core.types.isSuper(callee.object.object);
148
129
  }
149
-
150
130
  function getRequiredParamsOfSAPUIDefine(path, node) {
151
131
  const defineArgs = node.arguments;
152
132
  const callbackNode = defineArgs.find(argNode => _core.types.isFunction(argNode));
153
133
  return callbackNode.params; // Identifier
154
134
  }
135
+
155
136
  /**
156
137
  * Replace super() call
157
138
  */
158
-
159
-
160
139
  function replaceConstructorSuperCall(path, node, superClassName) {
161
140
  replaceSuperNamedCall(path, node, superClassName, CONSTRUCTOR);
162
141
  }
142
+
163
143
  /**
164
144
  * Replace super.method() call
165
145
  */
166
-
167
-
168
146
  function replaceObjectSuperCall(path, node, superClassName) {
169
147
  replaceSuperNamedCall(path, node, superClassName, node.callee.property.name);
170
148
  }
149
+
171
150
  /**
172
151
  * Replace super.method.apply() call
173
152
  */
174
-
175
-
176
153
  function replaceSuperApplyCall(path, node, superClassName) {
177
154
  const methodName = node.callee.object.property.name;
178
155
  path.replaceWith(_core.types.callExpression(_core.types.identifier(`${superClassName}.prototype.${methodName}.apply`), node.arguments));
179
156
  }
180
-
181
157
  function replaceSuperNamedCall(path, node, superClassName, methodName) {
182
158
  // .call() is better for simple args (or not args) but doesn't work right for spread args
183
159
  // if it gets further transpiled by babel spread args transform (will be .call.apply(...).
184
160
  const thisEx = _core.types.thisExpression();
185
-
186
161
  const hasSpread = node.arguments.some(_core.types.isSpreadElement);
187
162
  const caller = hasSpread ? "apply" : "call";
188
163
  const callArgs = hasSpread ? [thisEx, _core.types.arrayExpression(node.arguments)] : [thisEx, ...node.arguments];
189
164
  path.replaceWith(_core.types.callExpression(_core.types.identifier(`${superClassName}.prototype.${methodName}.${caller}`), callArgs));
190
165
  }
191
-
192
166
  function doesClassExtendFromImport(node, imports) {
193
167
  const superClass = node.superClass;
194
168
  return superClass && imports.some(imported => imported === superClass.name);
195
169
  }
196
-
197
170
  function shouldConvertClass(file, node, opts, classInfo) {
198
171
  if (classInfo.nonUI5) {
199
172
  return false;
200
173
  }
201
-
202
174
  if (opts.autoConvertAllExtendClasses == true) {
203
175
  return true;
204
176
  }
205
-
206
177
  if (classInfo.name || classInfo.alias || classInfo.controller || classInfo.namespace) {
207
178
  return true;
208
179
  }
209
-
210
180
  if (/.*[.]controller[.]js$/.test(file.opts.filename) && opts.autoConvertControllerClass !== false) {
211
181
  return true;
212
182
  }
213
-
214
183
  return false;
215
184
  }
package/dist/index.js CHANGED
@@ -1,17 +1,13 @@
1
1
  "use strict";
2
2
 
3
3
  var _visitor = require("./classes/visitor");
4
-
5
4
  var _visitor2 = require("./modules/visitor");
6
-
7
5
  var _wrapper = require("./modules/helpers/wrapper");
8
-
9
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
10
-
11
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
12
-
13
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
14
-
6
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
7
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
8
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
9
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
10
+ function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
15
11
  /*
16
12
  References:
17
13
  Babel Plugin Handbook: https://github.com/thejameskyle/babel-handbook/blob/master/translations/en/plugin-handbook.md
@@ -19,6 +15,7 @@ AST Spec: https://github.com/babel/babel/blob/master/packages/babel-parser/ast/s
19
15
  Babel Types (t.*) https://github.com/babel/babel/tree/master/packages/babel-types
20
16
  AST Explorer: https://astexplorer.net/
21
17
  */
18
+
22
19
  module.exports = () => {
23
20
  const UI5Visitor = _objectSpread({
24
21
  Program: {
@@ -27,20 +24,20 @@ module.exports = () => {
27
24
  }) {
28
25
  if (this.ran) return;
29
26
  this.ran = true;
30
-
31
27
  if (opts.onlyConvertNamedClass == false) {
32
28
  throw new Error("ERROR: onlyConvertNamedClass=false is no longer supported. Use autoConvertAllExtendClassesByDefault=true");
33
- } // TODO: enable this in a later version
29
+ }
30
+ // TODO: enable this in a later version
34
31
  // else if (opts.onlyConvertNamedClass == true) {
35
32
  // console.warn('WARN: onlyConvertNamedClass=true is now the default behaviour') // eslint-disable-line no-console
36
33
  // }
37
- // Opts
38
-
39
34
 
35
+ // Opts
40
36
  this.namespacePrefix = opts.namespacePrefix;
41
37
  this.noImportInteropPrefixes = opts.noImportInteropPrefixes || ["sap/"];
42
- this.noImportInteropPrefixesRegexp = new RegExp(this.noImportInteropPrefixes.map(p => `(^${p}.*)`).join("|")); // Properties for Module Transform
38
+ this.noImportInteropPrefixesRegexp = new RegExp(this.noImportInteropPrefixes.map(p => `(^${p}.*)`).join("|"));
43
39
 
40
+ // Properties for Module Transform
44
41
  this.programNode = path.node;
45
42
  this.defaultExport = null;
46
43
  this.defaultExportNode = null;
@@ -48,22 +45,21 @@ module.exports = () => {
48
45
  this.ignoredImports = [];
49
46
  this.imports = [];
50
47
  this.namedExports = [];
51
- this.injectDynamicImportHelper = false; // Properties for Class Transform
48
+ this.injectDynamicImportHelper = false;
52
49
 
53
- this.importNames = []; // The classes must be converted right away before any other class transforms get a chance to run.
50
+ // Properties for Class Transform
51
+ this.importNames = [];
54
52
 
53
+ // The classes must be converted right away before any other class transforms get a chance to run.
55
54
  path.traverse(_visitor.ClassTransformVisitor, this);
56
55
  },
57
-
58
56
  exit(path, {
59
57
  opts
60
58
  }) {
61
59
  (0, _wrapper.wrap)(this, path.node, opts);
62
60
  }
63
-
64
61
  }
65
62
  }, _visitor2.ModuleTransformVisitor);
66
-
67
63
  return {
68
64
  visitor: UI5Visitor
69
65
  };
@@ -5,17 +5,11 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.collapseNamedExports = collapseNamedExports;
7
7
  exports.getDefaultExportName = getDefaultExportName;
8
-
9
8
  var _core = require("@babel/core");
10
-
11
9
  var th = _interopRequireWildcard(require("../../utils/templates"));
12
-
13
10
  var ast = _interopRequireWildcard(require("../../utils/ast"));
14
-
15
- function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
16
-
17
- function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
18
-
11
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
12
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
19
13
  /**
20
14
  * Collapse named exports onto the default export so that the default export can be returned directly,
21
15
  * so that exports can be easily used by code that does not include an import interop.
@@ -33,14 +27,12 @@ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj;
33
27
  */
34
28
  function collapseNamedExports(programNode, defaultExport, namedExports, opts) {
35
29
  namedExports = filterOutExportsWhichAlreadyMatchPropsOnDefault(programNode, defaultExport, namedExports);
36
-
37
30
  if (!namedExports.length || opts.noExportExtend) {
38
31
  return {
39
32
  filteredExports: namedExports,
40
33
  newDefaultExportIdentifier: null
41
34
  };
42
35
  }
43
-
44
36
  if (namedExports.some(exp => exp.conflict)) {
45
37
  return {
46
38
  filteredExports: namedExports,
@@ -48,10 +40,8 @@ function collapseNamedExports(programNode, defaultExport, namedExports, opts) {
48
40
  newDefaultExportIdentifier: null
49
41
  };
50
42
  }
51
-
52
43
  let exportVariableName = getDefaultExportName(defaultExport);
53
44
  let newDefaultExportIdentifier = null;
54
-
55
45
  if (!exportVariableName) {
56
46
  // Something anonymous (literal, anon function, etc...)
57
47
  programNode.body.push(th.buildTempExport({
@@ -60,9 +50,7 @@ function collapseNamedExports(programNode, defaultExport, namedExports, opts) {
60
50
  newDefaultExportIdentifier = th.exportsIdentifier;
61
51
  exportVariableName = newDefaultExportIdentifier.name;
62
52
  }
63
-
64
53
  const id = _core.types.identifier(exportVariableName);
65
-
66
54
  for (const namedExport of namedExports) {
67
55
  programNode.body.push(th.buildAssign({
68
56
  OBJECT: id,
@@ -70,35 +58,29 @@ function collapseNamedExports(programNode, defaultExport, namedExports, opts) {
70
58
  VALUE: namedExport.value
71
59
  }));
72
60
  }
73
-
74
61
  return {
75
62
  filteredExports: [],
76
63
  conflictingExports: [],
77
64
  newDefaultExportIdentifier: newDefaultExportIdentifier
78
65
  };
79
66
  }
80
-
81
67
  function getDefaultExportName(defaultExport) {
82
68
  return defaultExport.name || ast.getIdName(defaultExport);
83
69
  }
84
-
85
70
  function filterOutExportsWhichAlreadyMatchPropsOnDefault(programNode, defaultExport, namedExports) {
86
71
  namedExports = [...namedExports]; // Shallow clone for safe splicing
72
+ const defaultObjectProperties = ast.findPropertiesOfNode(programNode, defaultExport);
87
73
 
88
- const defaultObjectProperties = ast.findPropertiesOfNode(programNode, defaultExport); // Loop through the default object's props and see if it already has a matching definition of the named exports.
74
+ // Loop through the default object's props and see if it already has a matching definition of the named exports.
89
75
  // If the definition matches, we can ignore the named export. If the definition does not match, mark it as a conflict.
90
-
91
76
  if (defaultObjectProperties) {
92
77
  for (const defaultExportProperty of defaultObjectProperties) {
93
78
  if (!defaultExportProperty.key) {
94
79
  // i.e. SpreadProperty
95
80
  continue;
96
81
  }
97
-
98
82
  const matchingNamedExportIndex = namedExports.findIndex(namedExport => namedExport.key.name === defaultExportProperty.key.name); // get the index for splicing
99
-
100
83
  const matchingNamedExport = namedExports[matchingNamedExportIndex];
101
-
102
84
  if (matchingNamedExport && !matchingNamedExport.conflict) {
103
85
  if (areSame(defaultExportProperty, matchingNamedExport)) {
104
86
  namedExports.splice(matchingNamedExportIndex, 1);
@@ -108,20 +90,16 @@ function filterOutExportsWhichAlreadyMatchPropsOnDefault(programNode, defaultExp
108
90
  }
109
91
  }
110
92
  }
111
-
112
93
  return namedExports;
113
94
  }
114
-
115
95
  function areSame(defaultExportProperty, matchingNamedExport) {
116
96
  const defaultExportValue = defaultExportProperty.value;
117
97
  const declaration = matchingNamedExport.declaration; // i.e. 'export const a = 1' or 'export let a = b'
118
-
119
98
  if (!defaultExportValue) {
120
99
  // i.e. object method
121
100
  return false; // always a conflict
122
101
  } else if (_core.types.isIdentifier(defaultExportValue)) {
123
102
  const valueName = defaultExportValue.name;
124
-
125
103
  if (valueName === matchingNamedExport.value.name) {
126
104
  // Same identifier reference. Safe to ignore
127
105
  return true;
@@ -137,8 +115,7 @@ function areSame(defaultExportProperty, matchingNamedExport) {
137
115
  // Same literal. Safe to ignore
138
116
  return true;
139
117
  }
140
- } // Either a conflicting value or no value at all (i.e. method)
141
-
142
-
118
+ }
119
+ // Either a conflicting value or no value at all (i.e. method)
143
120
  return false;
144
121
  }
@@ -4,24 +4,17 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.wrap = wrap;
7
-
8
7
  var _core = require("@babel/core");
9
-
10
8
  var eh = _interopRequireWildcard(require("./exports"));
11
-
12
9
  var th = _interopRequireWildcard(require("../../utils/templates"));
13
-
14
10
  var ast = _interopRequireWildcard(require("../../utils/ast"));
15
-
16
- function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
17
-
18
- function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
19
-
11
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
12
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
20
13
  function wrap(visitor, programNode, opts) {
21
14
  let {
22
15
  defaultExport,
23
16
  exportGlobal,
24
- firstImport,
17
+ firstImportMarked,
25
18
  imports,
26
19
  namedExports,
27
20
  ignoredImports,
@@ -34,62 +27,54 @@ function wrap(visitor, programNode, opts) {
34
27
  } = programNode;
35
28
  let allExportHelperAdded = false;
36
29
  let extendAdded = false;
37
- opts.collapse = !opts.noExportCollapse; // opts.extend = !opts.noExportExtend
38
- // Before adding anything, see if the named exports can be collapsed into the default export.
30
+ opts.collapse = !opts.noExportCollapse;
31
+ // opts.extend = !opts.noExportExtend
39
32
 
33
+ // Before adding anything, see if the named exports can be collapsed into the default export.
40
34
  if (defaultExport && namedExports.length && opts.collapse) {
41
35
  let {
42
36
  filteredExports,
43
37
  conflictingExports,
44
38
  newDefaultExportIdentifier
45
39
  } = eh.collapseNamedExports(programNode, defaultExport, namedExports, opts);
46
-
47
40
  if (filteredExports.length && !opts.allowUnsafeMixedExports) {
48
41
  throw new Error(`Unsafe mixing of conflicting default and named exports. The following named exports are conflicting: (${ast.getPropNames(conflictingExports).join(", ")}).`);
49
42
  } else {
50
43
  namedExports = filteredExports;
51
- } // The default export may have changed if the collapse logic needed to assign a prop when the default export was previously anonymous.
52
-
44
+ }
53
45
 
46
+ // The default export may have changed if the collapse logic needed to assign a prop when the default export was previously anonymous.
54
47
  if (newDefaultExportIdentifier) {
55
48
  extendAdded = true; // If an anonymous default export needed to be assigned to a a variable, it uses the exports name for convenience.
56
-
57
49
  defaultExport = newDefaultExportIdentifier;
58
50
  }
59
51
  }
60
-
61
- const preDefine = [...ignoredImports]; // If the noWrapBeforeImport opt is set, split any code before the first import and afterwards into separate arrays.
52
+ const preDefine = [...ignoredImports];
53
+ // If the noWrapBeforeImport opt is set, split any code before the first import and afterwards into separate arrays.
62
54
  // This should be done before any interops or other vars are injected.
63
-
64
- if (opts.noWrapBeforeImport && firstImport) {
55
+ if (opts.noWrapBeforeImport && firstImportMarked) {
65
56
  let reachedFirstImport = false;
66
57
  const fullBody = body;
67
58
  const newBody = [];
68
-
69
59
  for (const item of fullBody) {
70
- if (item === firstImport) {
71
- reachedFirstImport = true;
72
- }
73
-
74
60
  if (reachedFirstImport) {
75
61
  newBody.push(item);
76
62
  } else {
77
63
  preDefine.push(item);
78
64
  }
65
+ if (item.lastBeforeWrapping) {
66
+ reachedFirstImport = true;
67
+ }
79
68
  }
80
-
81
69
  if (preDefine.length && !hasUseStrict(programNode)) {
82
70
  programNode.directives = [_core.types.directive(_core.types.directiveLiteral("use strict")), ...(programNode.directives || [])];
83
71
  }
84
-
85
72
  body = newBody;
86
73
  }
87
-
88
74
  if (injectDynamicImportHelper) {
89
75
  // import() to sap.ui.require() w/ promise and interop
90
76
  body.unshift(th.buildDynamicImportHelper());
91
77
  }
92
-
93
78
  if (!namedExports.length && defaultExport) {
94
79
  // If there's no named exports, return the default export
95
80
  body.push(_core.types.returnStatement(defaultExport));
@@ -104,7 +89,6 @@ function wrap(visitor, programNode, opts) {
104
89
  body.push(th.buildAllExportHelper());
105
90
  allExportHelperAdded = true;
106
91
  }
107
-
108
92
  body.push(th.buildAllExport({
109
93
  LOCAL: namedExport.value
110
94
  }));
@@ -112,28 +96,22 @@ function wrap(visitor, programNode, opts) {
112
96
  body.push(th.buildNamedExport(namedExport));
113
97
  }
114
98
  }
115
-
116
99
  if (defaultExport) {
117
100
  body.push(th.buildNamedExport({
118
101
  key: _core.types.identifier("default"),
119
102
  value: defaultExport
120
103
  }));
121
104
  }
122
-
123
105
  body.push(th.buildReturnExports());
124
106
  }
125
-
126
107
  if (imports.some(imp => imp.interop)) {
127
108
  body.unshift(th.buildDefaultImportInterop());
128
109
  }
129
-
130
110
  programNode.body = [...preDefine, generateDefine(body, imports, exportGlobal || opts.exportAllGlobal)];
131
111
  }
132
-
133
112
  function hasUseStrict(node) {
134
113
  return (node.directives || []).some(directive => directive.value.value === "use strict");
135
114
  }
136
-
137
115
  function generateDefine(body, imports, exportGlobal) {
138
116
  const defineOpts = {
139
117
  SOURCES: _core.types.arrayExpression(imports.map(i => _core.types.stringLiteral(i.src))),