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-transpile): support JS-based babel config files #970

Merged
merged 1 commit into from
Mar 4, 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
18 changes: 14 additions & 4 deletions packages/ui5-tooling-transpile/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ async function loadBabelConfigOptions(babelConfigOptions, skipBabelPresetPluginR
const partialConfig = await babel.loadPartialConfigAsync(
Object.assign(
{
cwd,
configFile: false,
babelrc: false,
filename: "src/dummy.js" // necessary for ignore/include/exclude
filename: "src/dummy.js", // necessary for ignore/include/exclude
envName: process.env.BABEL_ENV || process.env.NODE_ENV || "development"
},
babelConfigOptions
)
Expand Down Expand Up @@ -112,9 +114,17 @@ async function findBabelConfigOptions(cwd) {
} else if (configFile) {
// for a babel config file we load it on our own to normalize the plugin/preset paths
// => no recursive merging of babel config is possible with this approach
babelConfigOptions = JSONC.parse(fs.readFileSync(configFile, { encoding: "utf8" }));
// let Babel lookup the configuration file with the Babel API
//partialConfig = await loadBabelConfigOptions({ configFile, filename: dir, babelrc: true });
try {
babelConfigOptions = JSONC.parse(fs.readFileSync(configFile, { encoding: "utf8" }));
} catch (err) {
// no JSON so we let Babel lookup the configuration file with the Babel API
const partialConfig = await babel.loadPartialConfigAsync({ cwd });
// but we only extract the presets and plugins and ignore the other properties
babelConfigOptions = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@petermuessig Is it really intended to ignore other babel config options, such as the targets property?
The docu of the task seems to suggest otherwise:

The following configuration options will only be taken into account if no inline babel configuration is maintained in the ui5.yaml as babelConfig or no external babel configuration exists in any configuration file as described in Babels configuration section

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, if I enable more, I need to rework the util.js a bit - will require a bit more time then...

plugins: partialConfig?.options?.plugins,
presets: partialConfig?.options?.presets
};
}
}

return babelConfigOptions ? { configFile, babelConfigOptions } : undefined;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
// comments are allowed
"presets": ["transform-ui5", "@babel/preset-typescript", "@babel/preset-env"],
"sourceMaps": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
>0.5% and not dead
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = function (api) {
api.cache(true);

const presets = ["transform-ui5", "@babel/preset-typescript", "@babel/preset-env"];
const plugins = [
[
"transform-async-to-promises",
{
inlineHelpers: true
}
]
];

return {
presets,
plugins
};
};
21 changes: 21 additions & 0 deletions packages/ui5-tooling-transpile/test/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,27 @@ test("usage of external babel config", async (t) => {
cwd
);
t.true(config !== undefined);
t.deepEqual(config.presets[0].file.request, "transform-ui5");
t.deepEqual(config.presets[1].file.request, "@babel/preset-typescript");
t.deepEqual(config.presets[2].file.request, "@babel/preset-env");
});

test("usage of external js babel config", async (t) => {
const cwd = path.join(__dirname, "__assets__/external-js");
const config = await t.context.util.createBabelConfig(
{
configuration: {
debug: true,
skipBabelPresetPluginResolve: true
}
},
cwd
);
t.true(config !== undefined);
t.deepEqual(config.presets[0].file.request, "transform-ui5");
t.deepEqual(config.presets[1].file.request, "@babel/preset-typescript");
t.deepEqual(config.presets[2].file.request, "@babel/preset-env");
t.deepEqual(config.plugins[0].file.request, "transform-async-to-promises");
});

test("inject configuration options", async (t) => {
Expand Down
Loading