babel-plugin-transform-modules-ui5
Version:
An unofficial babel plugin for SAP UI5.
187 lines (181 loc) • 7.12 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.buildInheritingFunction = exports.buildInheritingConstructor = exports.buildExtendAssign = exports.buildExportDefault = exports.buildDynamicImportHelper = exports.buildDefineGlobal = exports.buildDefine = exports.buildDefaultImportInterop = exports.buildDefaultImportDeconstructor = exports.buildDeclareExports = exports.buildConstDeclaration = exports.buildAssign = exports.buildAllExportHelper = exports.buildAllExport = void 0;
exports.buildNamedExport = buildNamedExport;
exports.exportsIdentifier = exports.buildTempExport = exports.buildReturnExports = exports.buildReturn = exports.buildRequire = exports.buildNamedImportDestructor = void 0;
var _core = require("@babel/core");
const exportName = "__exports";
const exportsIdentifier = exports.exportsIdentifier = _core.types.identifier(exportName);
const buildAssign = exports.buildAssign = (0, _core.template)(`
OBJECT.NAME = VALUE;
`);
const buildRequire = exports.buildRequire = (0, _core.template)(`
sap.ui.require(SOURCES, function (PARAMS) {
BODY;
});
`);
const buildDefine = exports.buildDefine = (0, _core.template)(`
sap.ui.define(SOURCES, function (PARAMS) {
BODY;
});
`);
const buildDefineGlobal = exports.buildDefineGlobal = (0, _core.template)(`
sap.ui.define(SOURCES, function (PARAMS) {
BODY;
}, true);
`);
// Uses 'var' since it gets added during wrap
const buildDeclareExports = exports.buildDeclareExports = (0, _core.template)(`
var ${exportName} = {
__esModule: true
};
`);
// Uses 'var' since it gets added during wrap
const buildTempExport = exports.buildTempExport = (0, _core.template)(`
var ${exportName} = VALUE;
`);
const buildExportDefault = exports.buildExportDefault = (0, _core.template)(`
export default VALUE;
`);
const buildReturnExports = exports.buildReturnExports = (0, _core.template)(`
return ${exportName};
`);
function buildNamedExport(obj) {
return buildAssign({
OBJECT: exportsIdentifier,
NAME: obj.key,
VALUE: obj.value
});
}
const buildAllExportHelper = exports.buildAllExportHelper = (0, _core.template)(`
function extendExports(exports, obj) {
obj && Object.keys(obj).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return obj[key];
}
});
});
}
`);
const buildAllExport = exports.buildAllExport = (0, _core.template)(`
extendExports(${exportName}, LOCAL);
`);
const buildReturn = exports.buildReturn = (0, _core.template)(`
return ID;
`);
const buildDefaultImportInterop = exports.buildDefaultImportInterop = (0, _core.template)(`
function _interopRequireDefault(obj) { return (obj && obj.__esModule && typeof obj.default !== "undefined") ? obj.default : obj; }
`);
const buildDefaultImportDeconstructor = exports.buildDefaultImportDeconstructor = (0, _core.template)(`
const LOCAL = _interopRequireDefault(MODULE);
`);
/*
* Scenarios for dynamic exports:
*
* 1.) ES module with no exports
*
* (await import("./moduleWithNoExports.js")) ==> Module
* (await import("./moduleWithNoExports.js")).default ==> undefined
*
* 2.) ES module with default export
*
* (await import("./moduleWithDefaultExport.js")) ==> Module
* (await import("./moduleWithDefaultExport.js")).default ==> DefaultExport
*
* 3.) ES module with named exports
*
* (await import("./moduleWithNamedExport.js")) ==> Module
* (await import("./moduleWithNamedExport.js")).default ==> undefined
* (await import("./moduleWithNamedExport.js")).NamedExport ==> NamedExport
*
* A Module object is always returned by the dynamic import.
*
* ---
*
* The dynamic import script template simulates the default and named exports
* for non-ES modules so that the TypeScript code completion can be properly
* used. The template below is used to translate dynamic imports to sap.ui.require
* calls which are typically be used for the following scenarios:
*
* 1.) UI5 Libraries
*
* A UI5 library exports its complete namespace and all available
* types/enums/... are accessible as named properties on that
* namespace, e.g.:
*
* (await import("sap/m/library")) => { __esModule: true, ... }
* (await import("sap/m/library")).default => undefined
* (await import("sap/m/library")).ButtonType => sap/m/ButtonType
*
* 2.) UI5 Classes
*
* A UI5 class exports itself as default. When importing a UI5 class
* it is expected to be accessible via the default property.
*
* (await import("sap/m/Button")) => { __esModule: true, ... }
* (await import("sap/m/Button")).default => sap/m/Button
*
* 3.) Static Helpers (sap/m/MessageBox)
*
* (await import("sap/m/MessageBox")) => { __esModule: true, ... }
* (await import("sap/m/MessageBox")).default => sap/m/MessageBox
* (await import("sap/m/MessageBox")).Action => undefined
* (await import("sap/m/MessageBox")).default.Action => sap/m/MessageBox.Action
*
* 4.) undefined or null (no exports)
*
* (await import("./non/exporting/module")) => { __esModule: true, ... }
* (await import("./non/exporting/module")) => undefined
*
* 5.) Other dependencies (jsPDF)
*
* (await import("jsPDF")) => { __esModule: true, jsPDF: ?, ... }
* (await import("sap/m/MessageBox")).jsPDF => jsPDF
*
* When requiring other dependencies they are already flagged as __esModule
* and must not be processed in our dynamic import to require handler.
*/
// TODO: inject __extends instead of Object.assign unless useBuiltIns in set
const buildDynamicImportHelper = exports.buildDynamicImportHelper = (0, _core.template)(`
function __ui5_require_async(path) {
return new Promise(function(resolve, reject) {
sap.ui.require([path], function(module) {
if (!(module && module.__esModule)) {
module = module === null || !(typeof module === "object" && path.endsWith("/library")) ? { default: module } : module;
Object.defineProperty(module, "__esModule", { value: true });
}
resolve(module);
}, function(err) {
reject(err);
});
});
}
`);
const buildConstDeclaration = exports.buildConstDeclaration = (0, _core.template)(`
const NAME = VALUE;
`);
const buildNamedImportDestructor = exports.buildNamedImportDestructor = (0, _core.template)(`
const LOCAL = MODULE[IMPORTED];
`);
const buildExtendAssign = exports.buildExtendAssign = (0, _core.template)(`
const NAME = SUPER.extend(FQN, OBJECT);
`);
// This is use when there is not already the function, so always propagate arguments.
const buildInheritingFunction = exports.buildInheritingFunction = (0, _core.template)(`
function NAME() {
if (typeof SUPER.prototype.NAME === 'function') {
SUPER.prototype.NAME.apply(this, arguments);
}
}
`);
// This is use when there is not already the function, so always propagate arguments.
const buildInheritingConstructor = exports.buildInheritingConstructor = (0, _core.template)(`
function constructor() {
SUPER.prototype.constructor.apply(this, arguments);
}
`);