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

Build packages using Rollup #10386

Merged
merged 52 commits into from
Feb 3, 2022
Merged

Build packages using Rollup #10386

merged 52 commits into from
Feb 3, 2022

Conversation

swissspidy
Copy link
Collaborator

@swissspidy swissspidy commented Jan 25, 2022

Context

Before we can publish our packages on npm we need a build step for them, ideally to produce both CommonJS and ESM versions.

Summary

This is an attempt at this by leveraging Rollup and several Rollup plugins that fit our needs.

Building packages creates dist and dist-module folders that are then used for distribution.

The src folder is entirely ignored for publishing on npm.

Basic usage:

# Build all packages using Rollup
$ npm run bundle

# Build only the migration package
$ npm run bundle -- --configPackages=migration

# Build only the module version of all packages
$ npm run bundle -- --configEntries=es

# Build only the CommonJS version of the migration and text-sets packages
$ npm run bundle -- --configEntries=cjs --configPackages=migration,text-sets

Relevant Technical Choices

Decided to try Rollup because we're already using it in the project and it was relatively easy to get off the ground.

Changed some imports to avoid circular dependencies, though this still needs more work.

Changed some imports to avoid mixing default imports and named exports. Quote:

It is bad practice to mix default and named exports in the same module, though it is allowed by the specification.

Used custom resolvers for webpack and Jest so they continue to look for packages in the src folders instead of dist.

To-do

  • Testing
  • Fix inline web worker usage in story-editor
  • Fix import/no-unresolved false positives
  • Add sourcemaps?
  • Fix duplicate React version in react-photo-gallery
  • Fix some circular dependencies (see below)
(!) Circular dependencies
packages/story-editor/src/masks/index.js -> packages/story-editor/src/elements/index.js -> packages/story-editor/src/elements/elementTypes.js -> packages/story-editor/src/elements/text/index.js -> packages/story-editor/src/elements/text/display.js -> packages/story-editor/src/elements/shared/index.js -> packages/story-editor/src/utils/elementBorder.js -> packages/story-editor/src/masks/index.js

packages/story-editor/src/app/story/index.js -> packages/story-editor/src/app/story/useStoryReducer/reducers/index.js -> packages/story-editor/src/app/story/useStoryReducer/reducers/combineElements.js -> packages/story-editor/src/masks/index.js -> packages/story-editor/src/elements/index.js -> packages/story-editor/src/elements/elementTypes.js -> packages/story-editor/src/elements/text/index.js -> packages/story-editor/src/elements/text/edit.js -> packages/story-editor/src/app/story/index.js

packages/story-editor/src/app/story/index.js -> packages/story-editor/src/app/story/useStoryReducer/reducers/index.js -> packages/story-editor/src/app/story/useStoryReducer/reducers/combineElements.js -> packages/story-editor/src/masks/index.js -> packages/story-editor/src/elements/index.js -> packages/story-editor/src/elements/elementTypes.js -> packages/story-editor/src/elements/text/index.js -> packages/story-editor/src/elements/text/frame.js -> packages/story-editor/src/app/story/index.js

...and 48 more

User-facing changes

None

Testing Instructions

  • This is a non-user-facing change and requires no QA

This PR can be tested by following these steps:

  1. Verify that all status checks pass
  2. Run npm run bundle to build the packages yourself

Reviews

Does this PR have a security-related impact?

Not that I could think of

Does this PR change what data or activity we track or use?

No

Does this PR have a legal-related impact?

No

Checklist

  • This PR addresses an existing issue and I have linked this PR to it in ZenHub
  • I have tested this code to the best of my abilities
  • I have verified accessibility to the best of my abilities (docs)
  • I have verified i18n and l10n (translation, right-to-left layout) to the best of my abilities
  • This code is covered by automated tests (unit, integration, and/or e2e) to verify it works as intended (docs)
  • I have added documentation where necessary
  • I have added a matching Type: XYZ label to the PR

See #10251

@swissspidy swissspidy added Type: Infrastructure Changes impacting testing infrastructure or build tooling Pod: WP & Infra labels Jan 25, 2022
@lgtm-com

This comment was marked as resolved.

dedupe: [],
}),
babel({
babelHelpers: 'inline',
Copy link
Contributor

Choose a reason for hiding this comment

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

Trying to learn why inline is used as an option for babelHelpers here

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I used runtime initially as it was the recommendation, but I got some warnings when building.

@mohdsayed
Copy link
Contributor

mohdsayed commented Jan 26, 2022

(!) Circular dependencies
packages/story-editor/src/masks/index.js -> packages/story-editor/src/elements/index.js -> packages/story-editor/src/elements/elementTypes.js -> packages/story-editor/src/elements/text/index.js -> packages/story-editor/src/elements/text/display.js -> packages/story-editor/src/elements/shared/index.js -> packages/story-editor/src/utils/elementBorder.js -> packages/story-editor/src/masks/index.js
packages/story-editor/src/app/story/index.js -> packages/story-editor/src/app/story/useStoryReducer/reducers/index.js -> packages/story-editor/src/app/story/useStoryReducer/reducers/combineElements.js -> packages/story-editor/src/masks/index.js -> packages/story-editor/src/elements/index.js -> packages/story-editor/src/elements/elementTypes.js -> packages/story-editor/src/elements/text/index.js -> packages/story-editor/src/elements/text/edit.js -> packages/story-editor/src/app/story/index.js
packages/story-editor/src/app/story/index.js -> packages/story-editor/src/app/story/useStoryReducer/reducers/index.js -> packages/story-editor/src/app/story/useStoryReducer/reducers/combineElements.js -> packages/story-editor/src/masks/index.js -> packages/story-editor/src/elements/index.js -> packages/story-editor/src/elements/elementTypes.js -> packages/story-editor/src/elements/text/index.js -> packages/story-editor/src/elements/text/frame.js -> packages/story-editor/src/app/story/index.js
...and 48 more

51 indirect circular dependencies are a lot to resolve. 😵‍💫

@mohdsayed
Copy link
Contributor

I think we may ignore these circular dependency warning for now by using onwarn and deal with them later separately.

onwarn(warning, warn) {
  if (warning.code !== 'CIRCULAR_DEPENDENCY') {
    warn(warning);
  }
}

@swissspidy
Copy link
Collaborator Author

They don't cause the build to error though, so not a big deal to keep them visible and remind os of this technical debt :)

@mohdsayed
Copy link
Contributor

They don't cause the build to error though, so not a big deal to keep them visible and remind os of this technical debt :)

Yes, maybe a lot of them will be resolved automatically when we break down the story-editor in more packages in the upcoming package organization PRs.

@mohdsayed

This comment was marked as resolved.

@mohdsayed
Copy link
Contributor

mohdsayed commented Jan 26, 2022

@swissspidy Can you explain this one a bit from the to-do list?

Fix import/no-unresolved false positives

@swissspidy
Copy link
Collaborator Author

Rollup doesn't seem to have support for Web Workers, see rollup/rollup#3842

Tried rollup-plugin-web-worker-loader npm package however it breaks with the following error

I'm inclined to just ditch our current web worker logic completely. Not a huge fan of the webpack worker-loader anyway.

We could take a very simple approach by manually turning it into an inline/embedded web worker (see examples at https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers#embedded_workers) using a blob — which is basically what worker-loader is doing anyway — and what rollup-plugin-web-worker-loader should be supporting as well.

So something like this:

function generateBlurHash() {
// ...
}

const blob = new Blob(['('+generateBlurHash.toString()+')()'], {type: 'text/javascript'})
const workerUrl = URL.createObjectURL(blob);

new Worker(workerUrl)

At least for the short term this should do the trick.

Suggestions welcome!

@swissspidy Can you explain this one a bit from the to-do list?

Fix import/no-unresolved false positives

Sure.

Basically right now we have tons of ESLint warnings here on this PR.

They are caused by all the changes I did to the package.json files for all the packages (specifically, changing the main field).

They come from eslint-plugin-import, specifically the no-unresolved rule

It uses Node-style module resolution by default, which has some open issues about this

import-js/eslint-plugin-import#1868
import-js/eslint-plugin-import#1810

Perhaps using eslint-import-resolver-webpack fixes this?

Otherwise I suppose we can just disable this rule until the above issues get fixed.

@mohdsayed
Copy link
Contributor

Great idea of turning into an inline/embedded web worker using blob 👍

For Eslint warnings, I tried eslint-import-resolver-webpack, but that didn't work for me.

Gutenberg also had a similar issue here WordPress/gutenberg#31792 for which they used eslint-import-resolver-node, see /.eslintrc.js#L53

@swissspidy

This comment has been minimized.

@mohdsayed

This comment has been minimized.

@swissspidy

This comment has been minimized.

@mohdsayed

This comment has been minimized.

@swissspidy

This comment has been minimized.

@mohdsayed
Copy link
Contributor

Tested it after manually merging #10297 and

In package.json

  1. Updated "module": "./dist-module/index.js" to "module": "dist-module/index.js"
  2. Removed "type": "module"
  3. Removed "exports" ( don't know why but it imports from ./src directory when leaving this in package.json )

I think all import issues were resolved but now we have some node errors.

ERROR in ./node_modules/@googleforcreators/story-editor/dist-module/index-276f5876.js
Module not found: Error: Can't resolve './raw' in '/Users/sayedtaqui/playground/react-app/node_modules/@googleforcreators/story-editor/dist-module'
 @ ./node_modules/@googleforcreators/story-editor/dist-module/index-276f5876.js 404:624305-624330
 @ ./node_modules/@googleforcreators/story-editor/dist-module/index.js
 @ ./src/index.js

ERROR in ./node_modules/@googleforcreators/story-editor/dist-module/index-276f5876.js
Module not found: Error: Can't resolve './raw' in '/Users/sayedtaqui/playground/react-app/node_modules/@googleforcreators/story-editor/dist-module'
 @ ./node_modules/@googleforcreators/story-editor/dist-module/index-276f5876.js 136:46199-46219
 @ ./node_modules/@googleforcreators/story-editor/dist-module/index.js
 @ ./src/index.js

ERROR in ./node_modules/@googleforcreators/story-editor/dist-module/index-c3e659ac.js
Module not found: Error: Can't resolve 'fs' in '/Users/sayedtaqui/playground/react-app/node_modules/@googleforcreators/story-editor/dist-module'
 @ ./node_modules/@googleforcreators/story-editor/dist-module/index-c3e659ac.js 17:99-117 17:24780-24781
 @ ./node_modules/@googleforcreators/story-editor/dist-module/index-276f5876.js
 @ ./node_modules/@googleforcreators/story-editor/dist-module/index.js
 @ ./src/index.js

@swissspidy

This comment has been minimized.

@mohdsayed

This comment has been minimized.

@swissspidy

This comment has been minimized.

package.json Outdated Show resolved Hide resolved
@lgtm-com

This comment has been minimized.

@lgtm-com

This comment has been minimized.

@swissspidy swissspidy changed the title [TRY] [WIP] Build packages using Rollup Build packages using Rollup Feb 3, 2022
@swissspidy swissspidy marked this pull request as ready for review February 3, 2022 08:37
@swissspidy swissspidy requested review from mohdsayed and removed request for spacedmonkey February 3, 2022 08:37
@swissspidy swissspidy merged commit db9773a into main Feb 3, 2022
@swissspidy swissspidy deleted the add/10251-rollup branch February 3, 2022 21:23
swissspidy added a commit that referenced this pull request Feb 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Infrastructure Changes impacting testing infrastructure or build tooling
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants