ui5-tooling icon indicating copy to clipboard operation
ui5-tooling copied to clipboard

Created files of custom task not recognized in following tasks

Open cschuff opened this issue 4 years ago • 4 comments

I've written a custom task that transpiles TS into JS and thereby renames files from *.ts extension to *.js extension. The created js files are not picked up by the following ui5-tooling tasks createDbgFiles and uglify.

Expected Behavior

I'd expect newly created files to be piped through the following tasks just as any file originally existing on the file system src.

Current Behavior

Newly created files do not appear in follow-up tasks

Steps to Reproduce the Issue

Here is my ui5.yaml:

specVersion: "2.2"
type: application
metadata:
  name: my-app

builder:
  customTasks:
    - name: ts-transpile
      beforeTask: replaceCopyright

---
specVersion: "2.2"
kind: extension
type: task
metadata:
  name: ts-transpile
task:
  path: ./scripts/typescript-transpile.js

And here is an excerpt of the typescript-transpile:

/**
 * Task to transpile TypeScript source based on a given tsconfig.
 *
 * @param {object} parameters Parameters
 * @param {module:@ui5/fs.DuplexCollection} parameters.workspace DuplexCollection to read and write files
 * @param {module:@ui5/fs.AbstractReader} parameters.dependencies Reader or Collection to read dependency files
 * @param {object} parameters.taskUtil Specification Version dependent interface to a
 *                [TaskUtil]{@link module:@ui5/builder.tasks.TaskUtil} instance
 * @param {object} parameters.options Options
 * @param {string} parameters.options.projectName Project name
 * @param {string} [parameters.options.projectNamespace] Project namespace if available
 * @param {string} [parameters.options.configuration] Task configuration if given in ui5.yaml
 * @returns {Promise<undefined>} Promise resolving with <code>undefined</code> once data has been written
 */
module.exports = async function ({
  workspace, dependencies, taskUtil, options,
}) {
  const tsconfig = path.join(__dirname, './../tsconfig.json');

  // Extract configuration from config file
  const config = readConfigFile(tsconfig);

  const allOriginalResources = await workspace.byGlob('**/*.ts');
  // allOriginalResources.map((res) => taskUtil.setTag(res, 'ui5:OmitFromBuildResult', true));

  const allResources = await resourceCopier({
    resources: allOriginalResources,
    options: { pattern: '.ts', replacement: '.js' },
  });

  await Promise.all(
    allResources
      .map(async (resource) => {
        const resourcePath = resource.getPath();
        const value = await resource.getString();
        const transpiled = tsCompile(value, config.options);
        resource.setString(transpiled);

        await workspace.write(resource);
      }),
  ).then(() => console.log('DONE'));
};

I've also tried without resourceCopier before with plain string replace on the resources path. Didn't work either. Also tried using withGlobSource instead of withGlob without any success...

Any ideas?

cschuff avatar Apr 12 '21 07:04 cschuff

Hi Christian, I saw your Slack post but haven't taken the time to look into it yet. Thanks for raising this issue though.

I can confirm your findings. By design, the uglify task only minifies "source" files and no resources created by other tasks. This is because some tasks already create minified resources and an additional minification would take unnecessary time.

You can find this behavior in other tasks as well. Basically wherever byGlobSource is used instead of byGlob.

For the time being, you could call the uglifier processor in your custom task, the same way the uglify task does. Please let me know if this is a suitable solution for you. If not, we could make this a feature request.

RandomByte avatar Apr 12 '21 10:04 RandomByte

That's something I could do...

I face the same problem for createDebugFiles which uses byGlobSource and still does not work.

This behaviour violates my understanding of your virtual files system being used as pipes and being the basis to all tooling execution...

cschuff avatar Apr 12 '21 11:04 cschuff

I face the same problem for createDebugFiles which uses byGlobSource and still does not work.

You're right, I forgot about that connection. The workaround would be the same here, you need to implement that operation in your task as well :confused:

This behaviour violates my understanding of your virtual files system being used as pipes and being the basis to all tooling execution...

Yes, I understand. As one might guess, this was initially meant to be a workaround, but no better solution came up since then. We basically need a way to exclude certain files from minification, debug file creation or other tasks.

With the introduction of tagging we might be able to improve this. I wonder whether tasks can now use them to decide whether a file is relevant for them. In the past they only had the filename. Which was not sufficient to differentiate bundles for example.

Someone needs to look into this and do some research on which additional tags might be required, where to implement them and how to deal with (legacy) custom task. Also, the broader change might not be compatible and hence only doable as part of a major release.

We'll discuss this in the team but please don't expect this to change anytime soon. Your use case sounds valid to me and I think we should improve this behavior though.

I'll relabel this issue as enhancement, since the current behavior - even though not ideal - is by design.

RandomByte avatar Apr 12 '21 15:04 RandomByte

Calling both processors manually in my own task worked. Excited about future improvements here 🤗

cschuff avatar Apr 20 '21 06:04 cschuff

I think this is another issue that can be declared as "resolved" with UI5 Tooling v3. The uglify task has been replaced by minify which will minify all JS resources it can find, including those created by custom tasks 👍

RandomByte avatar Oct 09 '23 13:10 RandomByte