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

feat: dynamic rollup configuration for esm flattening #395

Merged
merged 2 commits into from
Dec 19, 2017
Merged

feat: dynamic rollup configuration for esm flattening #395

merged 2 commits into from
Dec 19, 2017

Conversation

alan-agius4
Copy link
Member

@alan-agius4 alan-agius4 commented Dec 11, 2017

I'm submitting a...

[ ] Bug Fix
[X] Feature
[ ] Other (Refactoring, Added tests, Documentation, ...)

Checklist

  • Commit Messages follow the Conventional Commits pattern
    • A feature commit message is prefixed "feat:"
    • A bugfix commit message is prefixed "fix:"
  • Tests for the changes have been added

Description

externals has been removed in favor of umdModuleIds which is now used to just provide the UMD module IDs of external libraries (in case they cannot be auto-guessed by rollup; see further below).

By default all dependencies are now treated as externals and thus are not embedded in the final bundle.

However, if you want to embed one or more of them into the final bundle you can do so by adding the external dependency in the embedded section like so:

{
  "$schema": "../../../src/ng-package.schema.json",
  "lib": {
    "embedded": [
      "lodash",
      "date-fns"
    ]
  }
}

While rollup will do it's best to guess the UMD module ID of an external dependency (and ng-packagr provides common defaults for angular and rxjs) some modules are hard to guess and it's recommended to provide the name of the external dependency. This can be done by using umdModuleIds in the config section like so:

{
  "$schema": "../../../src/ng-package.schema.json",
  "lib": {
    "umdModuleIds": {
      "lodash" : "_",
      "date-fns" : "DateFns",
    }
  }
}

A migration example can be found in ab52732

Does this PR introduce a breaking change?

[X] Yes
[ ] No

Closes: #312

...opts.externals,
const embedded: string[] = opts.embedded || [];
const namedExternals = {
'tslib': 'tslib',
Copy link
Member Author

Choose a reason for hiding this comment

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

might also make sense to add some other defaults that are widely used example, ionic-angular, lodash, moment, date-fns, react etc..

@dherges dherges changed the title Feature/dynamic rollup feat: dynamic rollup configuration for esm flattening Dec 11, 2017
@alan-agius4
Copy link
Member Author

Need to update lockfile, though I am on my mobile at the moment!

embedded.some(x => x === moduleId)
) {
// if it's either 'absolute', marked to embed, starts with a '.' or '/' it's not external
return false;
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think of extracting to a function / lambda?

const externalModuleIdStrategy = (moduleId: string): boolean => {
  /* ... */
}

Then write a unit test for it?

Copy link
Member Author

Choose a reason for hiding this comment

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

I’d love that, if you tell me where to place the unit test etc... :) I’d find it much more easy to test.

Copy link
Member Author

Choose a reason for hiding this comment

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

Small correction from your example, to test it, you need to export it though!

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, yes! 😄

}

return namedExternals[moduleId] || ''; // leave it up to rollup to guess the global name
},
Copy link
Contributor

Choose a reason for hiding this comment

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

See above. Extract to a function / named lambda and then write a unit test for that piece of functionality?

const umdModuleIdStrategy = (moduleId: string): string => { /* ... */ }

/* ... */

describe(`umdModuleIdStrategy`, () => {
  it(`should map rxjs/add/operator/<foo> to Rx.Observable.<foo>`, () => {
    /* ... */
  });
});

???

Copy link
Contributor

Choose a reason for hiding this comment

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

Wdyt?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I agree.

@dherges
Copy link
Contributor

dherges commented Dec 11, 2017

Can you try to reset to origin/master? Ideally it should be enough to keep the changes to these files:

screen shot 2017-12-11 at 21 40 48

Unit tests have been added on the master, command: yarn test:specs

The test file is named <being-tested>.spec.ts. Keep that pattern with rollup.ts and rollup.spec.ts.

@alan-agius4
Copy link
Member Author

Thanks, will do that.

@alan-agius4
Copy link
Member Author

alan-agius4 commented Dec 12, 2017

@dherges Unit tests added, though I found a small issue #402

@dherges
Copy link
Contributor

dherges commented Dec 17, 2017

From rollup's API docs:

globals: Object of id: name pairs, used for umd/iife bundles.

Let's rename namedExternals toumdModuleIds since it's only intention is for helping rollup to find umd module ids.

expect(umdModuleIdStrategy('foo')).to.empty;
});
});
});
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@@ -0,0 +1,2 @@
Sample library: external and embeded dependencies
=====================================
Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, I think we can move this integration test to integration/samples/external

@dherges
Copy link
Contributor

dherges commented Dec 17, 2017

@alan-agius4 When you make these two little adjustments, I think it's good to go!

BREAKING CHANGE:
`externals` has been removed in favor of `umdModuleIds` which is now only used to name the external libraries.

By default all dependencies are now treated as externals and thus are not embedded in the final bundle.

However, If you want to embed one or more of them into the final bundle you can do so by adding the library in the `embeeded` section like so;

```json
{
  "$schema": "../../../src/ng-package.schema.json",
  "lib": {
    "embedded": [
      "lodash",
      "date-fns"
    ]
  }
}
```

While `rollup` will do it's best to guess the name of an external library some modules are hard to guess and it's recommended to provide the name of the external dependency. This can be done by using `umdModuleIds` in the config section like so;

```json
{
  "$schema": "../../../src/ng-package.schema.json",
  "lib": {
    "umdModuleIds": {
      "lodash" : "_",
      "date-fns" : "DateFns",
    }
  }
}
```

Closes: #312
@alan-agius4
Copy link
Member Author

@dherges updated as requested.

@dherges
Copy link
Contributor

dherges commented Dec 19, 2017

@alan-agius4 perfect!

@dherges dherges merged commit 5712429 into ng-packagr:master Dec 19, 2017
@alan-agius4 alan-agius4 deleted the feature/dynamic-rollup branch December 19, 2017 18:23
@dherges
Copy link
Contributor

dherges commented Dec 19, 2017

Just added a few words to the readme, hoping to clarify and be precise on the terms "externals", "dependency", "bundle", and so on

@dherges
Copy link
Contributor

dherges commented Dec 19, 2017

Thank you Alan, this is a good step to the right direction!

@alan-agius4
Copy link
Member Author

Glad to help :)

@github-actions
Copy link

This PR has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

This action has been performed automatically by a bot.

@github-actions github-actions bot locked and limited conversation to collaborators Jun 19, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

[PR WELCOME] Dynamic rollup configuration // print descriptive error messages
2 participants