Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ui5-tooling-modules): properly support keepDynamicImports #1101

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions packages/ui5-tooling-modules/lib/rollup-plugin-dynamic-imports.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,47 @@
module.exports = function ({ moduleName, keepDynamicImports } = {}) {
// by default the dynamic imports are kept (can be disabled with false or define a list of modules to keep)
keepDynamicImports = keepDynamicImports === undefined ? true : keepDynamicImports;
// if the value is an array, the module needs to be listed to keep the dynamic imports
if (typeof keepDynamicImports !== "boolean" && Array.isArray(keepDynamicImports)) {
keepDynamicImports = keepDynamicImports.indexOf(moduleName) !== -1;
}
const { existsSync, readFileSync } = require("fs");

module.exports = function ({ findPackageJson, keepDynamicImports } = {}) {
// ensure to keep the dynamic import
return {
name: "dynamic-imports",
renderDynamicImport({ targetModuleId }) {
if (!targetModuleId && keepDynamicImports) {
/*
resolveDynamicImport(specifier, importer, { attributes } = {}) {
const code = this.getModuleInfo(importer)?.code;
if (code) {
// get the import statement around the specifier
const codeBeforeImport = code.substring(0, specifier.start);
const importStatementStart = codeBeforeImport.lastIndexOf("(") + 1;
const codeAfterImport = code.substring(specifier.end);
const importStatementEnd = codeAfterImport.indexOf(")");
const importStatement = code.substring(importStatementStart, specifier.end + importStatementEnd);
// check for a / * webpackIgnore: true * / in the import statement ignoring whitespaces
const hasIgnoreMarker = importStatement.match(/\/\*\s*webpackIgnore:\s*true\s*\*\//);
if (hasIgnoreMarker) {
return false;
}
}
return null;
},
*/
renderDynamicImport({ moduleId, targetModuleId /*, customResolution, format */ }) {
// detect whether the dynamic import should be kept or not
let keepDynamicImport = true;
if (Array.isArray(keepDynamicImports)) {
const pkgJsonFile = findPackageJson(moduleId);
const pkgJson = existsSync(pkgJsonFile) ? JSON.parse(readFileSync(pkgJsonFile, { encoding: "utf8" })) : {};
keepDynamicImport = keepDynamicImports.indexOf(pkgJson.name) !== -1;
} else if (typeof keepDynamicImports === "boolean") {
keepDynamicImport = keepDynamicImports;
}
// empty targetModuleId means it is a dynamic import using a variable and in this
// case we keep it if the module is in the list or based on the boolean value
if (!targetModuleId && keepDynamicImport) {
return {
left: "import(",
right: ")",
};
}
return null;
},
};
};
7 changes: 4 additions & 3 deletions packages/ui5-tooling-modules/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -1168,9 +1168,10 @@ module.exports = function (log, projectInfo) {
isMiddleware,
pluginOptions,
};
if (modules.length === 1) {
options.afterPlugins.push(dynamicImports({ moduleName: modules[0].name, keepDynamicImports }));
}
// by default we add the dynamic imports plugin to keep dynamic imports for the given modules
// if the keepDynamicImports is a boolean, we keep the dynamic imports for all modules
options.afterPlugins.push(dynamicImports({ findPackageJson, keepDynamicImports }));
// when minifying the code, we add the terser plugin
if (minify) {
options.afterPlugins.push(require("@rollup/plugin-terser")());
}
Expand Down
Loading