项目作者: todm

项目描述 :
Task (and Middleware) for transpiling next-gen javascript into backwards compatible code using BabelJS
高级语言: JavaScript
项目地址: git://github.com/todm/ui5-task-babel.git
创建时间: 2021-09-15T16:56:30Z
项目社区:https://github.com/todm/ui5-task-babel

开源协议:

下载


ui5-task-babel

Task (and Middleware) for transpiling next-gen javascript into backwards compatible code using BabelJS. Also provides project shims for polyfills.

Without any configuration the task will use @babel/preset-env to transpile the code. With babel config files you can further control the transpile process.

Also supports typescript via the @babel/preset-typescript package.

Check out @todms/babel-plugin-ui5-esm for ES-Module support (import / export)

Changes in v3

  • Support for specVersion 3
  • Configuration changes
    • removed passFile
    • added searchExclude to middleware

Installation

Add the package to your project as a dependency.

  1. npm i -D @todms/ui5-task-babel

Include the task and middleware in your ui5.yaml file

  1. specVersion: '3.0'
  2. #...
  3. builder:
  4. customTasks:
  5. - name: ui5-task-babel
  6. beforeTask: generateComponentPreload
  7. server:
  8. customMiddleware:
  9. - name: ui5-middleware-babel
  10. beforeMiddleware: serveResources

Configuration

Without any configuration the task transpile all *.js files with the @babel/preset-env preset. You can change the babel configuration by adding a babel config file (babel.config.js) or by changing the babelConfig object in the task configuration. Config files will override the babel configuration in ui5.yaml.

Task Configuration

Name Type Default Description
include `string \ string[]` ['**/*.js'] Files that should be transformed
exclude `string \ string[]` [] Files that should not me transformed
babelConfig Object preset-env + Inline SourceMaps Babel configuration
forceExtension `string \ false` false Force a specific file extension for transformed files
iife boolen true Wraps code as iife to ensure top level scope

Middleware Configuration

Name Type Default Description
include `string \ string[]` ['**/*.js'] Files that should be included
exclude `string \ string[]` [] Files that should be excluded
babelConfig Object preset-env + Inline SourceMaps Babel configuration
searchInclude `string \ string[]` ['**/*.js'] Files that should be included when Searching the project for fitting files
iife boolen true Wraps code as iife to ensure top level scope
onError `’next’\ ‘error’\ ‘exit’` 'error' Defines behaviour when an error occures.
next: next middleware will be called,
error: server will return 503,
exit: Server will end

Regenerator-Runtime and Polyfills

Depending on your babel configuration and the usage of async/await or other non-transpilable js features you may need to add polyfills to your app.

If your app runs standalone via an index.html file this can easily be done by including the corresponding js files with a script tag.

  1. <script src="https://polyfill.io/v3/polyfill.min.js"></script>
  2. <script src="https://cdn.jsdelivr.net/npm/regenerator-runtime@0.13.9/runtime.min.js"></script>

If you load your app in launchpad and can’t change the html file or you don’t want to use a cdn for your polyfills you need to add them as resources to your project.

Project shims for regenerator-runtime and core-js are already provided in this package

Add regenerator-runtime and core-js-bundle as dependencies

  1. npm i -D core-js-bundle regenerator-runtime

Regenerator-runtime and core-js will now be added to your projects resource folder.
You can register them in your manifest.json, import them directly in a controller or use them in html as a script tag.

  1. // manifest.json
  2. {
  3. // ...
  4. "sap.ui5": {
  5. "resources": {
  6. "js": [
  7. { "uri": "/resources/polyfills/core-js-bundle/minified.js" },
  8. { "uri": "/resources/polyfills/regenerator-runtime/runtime.js" }
  9. ]
  10. }
  11. }
  12. }

Examples

Basic usage

  1. const myVar = window?.location;

Output:

  1. (function(){"use strict";
  2. var _window;
  3. var myVar = (_window = window) === null || _window === void 0 ? void 0 : _window.location;
  4. })()

Configuring typescript

Install typescript preset and ui5 types

  1. npm i -D @babel/preset-typescript @sapui5/ts-types
  2. # if you want to setup ESModules install @sapui5/ts-types-esm instead

Add a tsconfig.json file to configure the typescript compiler and add the installed types

  1. // tsconfig.json
  2. {
  3. "compilerOptions": {
  4. "target": "ES3",
  5. "module": "ES2020",
  6. "moduleResolution": "Node",
  7. "strict": false,
  8. "types": ["@ui5/ts-types"]
  9. }
  10. }

Change task/middleware configuarion in ui5.yaml

  1. # ui5.yaml
  2. builder:
  3. customTasks:
  4. - name: ui5-task-babel
  5. beforeTask: generateComponentPreload
  6. configuration:
  7. include: ['**/*.ts'] # include all .ts files
  8. forceExtension: 'js' # rename all included files to *.js
  9. server:
  10. customMiddleware:
  11. - name: ui5-middleware-babel
  12. beforeMiddleware: serveResources
  13. configuration:
  14. searchInclude: ['**/*.ts', '**/*.js'] # if file.ts is requested the middleware will search for file.ts and file.js

With this configuration all .ts files will be transformed to javascript both in builds and dev server