Files
alfresco-content-app/docs/extending/redistributable-libraries.md
Denys Vuika 2854c17cd9 [ADF-5183] Upgrade to Angular 10 (#1506)
* upgrade preparation fixes

* remove fdescribe

* update browserlist config

* ng8

* ngrx 8

* ng9

* ngrx 9

* remove entryComponents

* unit tests

* ng 10

* latest ADF

* fix unit tests

* fix lint

* update deps and travis

* code fixes

* upgrade webdriver

* cleanup libs

* fix test

* update test

* Use browserTarget as target for lite-serve

* Use the update webdriver with CI condition

* Use version console.log('load', path

* Fix path sh

* Try to use remote env

* Add the . to export variabled

* Use hardcoded chrome version

* Remove the run remote

* Avoid to use the escape

* Skip flaky e2e and raise issue ACA-3615

* SKip failing e2e

* Skip flaky e2e and raise issue ACA-3615

* Fix close app toolbar menu and preconditions + tests of  mark-favorite.test.ts  Personal Files section

* Fix mark-favorite tests

* Fix ext-header test

* Fix new-menu tests

* Fix lint

* no message

* Fix viewer tests

Co-authored-by: maurizio vitale <maurizio.vitale@alfresco.com>
Co-authored-by: Cristina Jalba <cristina.jalba@ness.com>
2020-07-09 09:37:06 +01:00

5.3 KiB

Title
Title
Redistributable libraries

Redistributable libraries

Extension libraries are based on the standard Angular libraries and definition files in the JSON format.

Please read more details in the following article: Library support in Angular CLI 6

See also

Creating extension library

First, generate a new project within the workspace:

ng generate library my-extension

You will get a new project in the projects/my-extensions folder. By default, the project contains at least the following content:

  • Example component my-extension.component.ts
  • Example service my-extension.service.ts
  • Angular Module example my-extension.module.ts

Next, build the project with the following command:

ng build my-extension

Angular CLI automatically configures Typescript path mappings for the project, so that you do not need any additional steps to link the library.

Register dynamic components

Now we need to register MyExtensionComponent as an extension component. Update the code as in the next example:

import { ExtensionService } from '@alfresco/adf-extensions';

@NgModule({...})
export class MyExtensionModule {
    constructor(extensions: ExtensionService) {
        extensions.setComponents({
            'my-extension.main.component': MyExtensionComponent,
        });
    }
}

Now you can use the my-extension.main.component identifier in the JSON definitions if you want to reference the MyExtensionComponent.

Plugin definition file

Create a new assets/my-extension.json file in the library project root folder with the following content:

{
  "$schema": "../../../extension.schema.json",
  "$version": "1.0.0",
  "$name": "plugin1",
  "$description": "demo plugin",

  "routes": [
    {
      "id": "my.extension.route",
      "path": "ext/my/route",
      "component": "my-extension.main.component"
    }
  ],

  "features": {
    "navbar": [
      {
        "id": "my.extension.nav",
        "items": [
          {
            "id": "my.extension.main",
            "icon": "extension",
            "title": "My Extension",
            "route": "my.extension.route"
          }
        ]
      }
    ]
  }
}

Update the root package.json file and append the following entry to the scripts section:

{
    "scripts": {
        ...,

        "build:my-extension":
            "ng build my-extension && cpr projects/my-extension/assets dist/my-extension/assets --deleteFirst"
    }
}

You can now use that script to build the library and copy assets to the output folder.

Tip: It is good practice to provide installation instructions for your library in the README.md file. Be sure to mention that developers should have a build rule to copy your plugin definition file to the assets/plugins folder of the main application.

Publishing library to NPM

Before you publish you should always rebuild the library:

npm run build:my-extension

Go to the output folder and run the publish command.

cd dist/my-extension
npm publish

Note, you are required to have a valid NPM account.

See more details in the Publishing your library article.

Consuming extension library

Assuming you have published your extension library to NPM, you can install it using the standard command:

npm install my-extension

This installs the library and all its dependencies.

Note: You do not need to install the library in the original workspace as the application is already configured to use the local version from the dist folder.

Copy assets

Edit the angular.json configuration file and add the following rule if you develop and test extension libraries in the same workspace.

{
  "glob": "**/*.json",
  "input": "dist/my-extension/assets",
  "output": "/assets/plugins"
}

Use the following rule if you are installing an extension from NPM:

{
  "glob": "**/*.json",
  "input": "node_modules/my-extension/assets",
  "output": "/assets/plugins"
}

Register module

In the main application, edit the src/app/extensions.module.ts file and append the module declaration as in the next example:

...
import { MyExtensionModule } from 'my-extension';

@NgModule({
    ...
    imports: [
        ...,
        MyExtensionModule
    ]
})
export class AppExtensionsModule {}

Register plugin

Finally, update the assets/app.extensions.json file and add a reference to the new plugin:

{
    "$references": [
        ...,
        "my-extension.json"
    ]
}

Testing library

Run the application and ensure you have an extra navigation sidebar entry:

npm start

Click the My Extension link and in the main content area you will see the extension component coming from your library.

Note: Depending on the application setup, you may need enabling external plugins via the Settings dialog available for admin users (clicking the application profile button).