diff --git a/packages/plugin/__test__/__snapshots__/test.js.snap b/packages/plugin/__test__/__snapshots__/test.js.snap index 797af20..c8e99be 100644 --- a/packages/plugin/__test__/__snapshots__/test.js.snap +++ b/packages/plugin/__test__/__snapshots__/test.js.snap @@ -1480,6 +1480,28 @@ sap.ui.define(["./foo"], function (__Foo) { });" `; +exports[`min-wrap min-wrap-test.js 1`] = ` +"sap.ui.define(["../../model/someFile", "../schema/otherFile"], function (____model_someFile, ___schema_otherFile) { + "use strict"; + + const getElementPath = ____model_someFile["getElementPath"]; + const createSchema = ___schema_otherFile["createSchema"]; + getElementPath(createSchema(), "elementSchema"); +});" +`; + +exports[`min-wrap min-wrap-test-wrap.js 1`] = ` +""use strict"; + +const x = 1; +sap.ui.define(["../../model/someFile", "../schema/otherFile"], function (____model_someFile, ___schema_otherFile) { + const getElementPath = ____model_someFile["getElementPath"]; + const createSchema = ___schema_otherFile["createSchema"]; + getElementPath(createSchema(), "elementSchema"); + console.log(x); +});" +`; + exports[`min-wrap min-wrap-ui5.js 1`] = ` ""use strict"; @@ -2109,6 +2131,46 @@ exports[`typescript ts-export-type.ts 1`] = ` exports[`typescript ts-export-type-only.ts 1`] = `""`; +exports[`typescript ts-export-type-reexport.ts 1`] = ` +"sap.ui.define(["./ts-export-type-reexport-enum"], function (___ts_export_type_reexport_enum) { + "use strict"; + + var __exports = { + __esModule: true + }; + 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]; + } + }); + }); + } + extendExports(__exports, ___ts_export_type_reexport_enum); + return __exports; +});" +`; + +exports[`typescript ts-export-type-reexport-enum.ts 1`] = ` +"sap.ui.define([], function () { + "use strict"; + + var FemRendererModel = function (FemRendererModel) { + FemRendererModel["AppModel"] = "AppModel"; + FemRendererModel["RouteModel"] = "RouteModel"; + return FemRendererModel; + }(FemRendererModel || {}); + var __exports = { + __esModule: true + }; + __exports.FemRendererModel = FemRendererModel; + return __exports; +});" +`; + exports[`typescript ts-index.ts 1`] = ` "sap.ui.define(["./_private_/index"], function (____private__index) { "use strict"; diff --git a/packages/plugin/__test__/fixtures/min-wrap/min-wrap-test-wrap.js b/packages/plugin/__test__/fixtures/min-wrap/min-wrap-test-wrap.js new file mode 100644 index 0000000..3d3d6c4 --- /dev/null +++ b/packages/plugin/__test__/fixtures/min-wrap/min-wrap-test-wrap.js @@ -0,0 +1,7 @@ +const x = 1; // This should not be part of sap-ui-define + +import { getElementPath } from "../../model/someFile"; +import { createSchema } from "../schema/otherFile"; + +getElementPath(createSchema(), "elementSchema"); +console.log(x); diff --git a/packages/plugin/__test__/fixtures/min-wrap/min-wrap-test.js b/packages/plugin/__test__/fixtures/min-wrap/min-wrap-test.js new file mode 100644 index 0000000..b0c7a7e --- /dev/null +++ b/packages/plugin/__test__/fixtures/min-wrap/min-wrap-test.js @@ -0,0 +1,4 @@ +import { getElementPath } from "../../model/someFile"; +import { createSchema } from "../schema/otherFile"; + +getElementPath(createSchema(), "elementSchema"); diff --git a/packages/plugin/__test__/fixtures/typescript/ts-export-type-reexport-enum.ts b/packages/plugin/__test__/fixtures/typescript/ts-export-type-reexport-enum.ts new file mode 100644 index 0000000..3848864 --- /dev/null +++ b/packages/plugin/__test__/fixtures/typescript/ts-export-type-reexport-enum.ts @@ -0,0 +1,4 @@ +export enum FemRendererModel { + AppModel = "AppModel", + RouteModel = "RouteModel", +} diff --git a/packages/plugin/__test__/fixtures/typescript/ts-export-type-reexport.ts b/packages/plugin/__test__/fixtures/typescript/ts-export-type-reexport.ts new file mode 100644 index 0000000..3885f31 --- /dev/null +++ b/packages/plugin/__test__/fixtures/typescript/ts-export-type-reexport.ts @@ -0,0 +1,7 @@ +export type RouterContext = { + mock: boolean; + service: string; + path: string; +}; + +export * from "./ts-export-type-reexport-enum"; diff --git a/packages/plugin/src/modules/helpers/wrapper.js b/packages/plugin/src/modules/helpers/wrapper.js index c1b0481..09bb743 100644 --- a/packages/plugin/src/modules/helpers/wrapper.js +++ b/packages/plugin/src/modules/helpers/wrapper.js @@ -92,6 +92,11 @@ export function wrap(visitor, programNode, opts) { const fullBody = body; const newBody = []; + // If there is no lastBeforeWrapping, the first import is not marked + if (!fullBody.find((item) => item.lastBeforeWrapping)) { + reachedFirstImport = true; + } + for (const item of fullBody) { if (reachedFirstImport) { newBody.push(item); diff --git a/packages/plugin/src/modules/visitor.js b/packages/plugin/src/modules/visitor.js index 1211dbd..6c2b739 100644 --- a/packages/plugin/src/modules/visitor.js +++ b/packages/plugin/src/modules/visitor.js @@ -203,14 +203,13 @@ export const ModuleTransformVisitor = { } // this is the very first import in noWrapBeforeImport mode and there are sibling nodes before this import - if ( - opts.noWrapBeforeImport && - !this.firstImportMarked && - path.inList && - path.key > 0 - ) { - // mark the direct predecessor as the last one to exclude from wrapping - path.getSibling(path.key - 1).node.lastBeforeWrapping = true; + if (opts.noWrapBeforeImport && !this.firstImportMarked && path.inList) { + // for the first element there is a special case as we can't mark the previous one + // therefore we do not mark anything to make clear that there's nothing to exclude + if (path.key > 0) { + // mark the direct predecessor as the last one to exclude from wrapping + path.getSibling(path.key - 1).node.lastBeforeWrapping = true; + } this.firstImportMarked = true; }