Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

chore!: use pure ES modules #577

Merged
merged 1 commit into from
Jan 19, 2022
Merged

chore!: use pure ES modules #577

merged 1 commit into from
Jan 19, 2022

Conversation

ehmicky
Copy link
Contributor

@ehmicky ehmicky commented Dec 3, 2021

Part of #451

This switches from CommonJS to pure ES modules.

This is a breaking change, since consumers that still use CommonJS will need to use a dynamic import() to load this module instead of require(). Consumers that use pure ES modules will not need to change anything.

This still works when used with browser consumers, including our front-end app (draft PR at https://github.com/netlify/netlify-react-ui/pull/9862).

@ehmicky ehmicky added the type: chore work needed to keep the product and development running smoothly label Dec 3, 2021
@ehmicky ehmicky force-pushed the chore/pure-esm branch 13 times, most recently from 7a81bfd to 1587661 Compare December 6, 2021 18:37
// it('Next.js site framework detected on load - vanilla JS site', () => {
// cy.contains('Vanilla JS Site').click()
// cy.contains('"name":"Next.js"')
// })
Copy link
Contributor Author

@ehmicky ehmicky Dec 6, 2021

Choose a reason for hiding this comment

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

I am hitting some issues to make those tests work. Those are testing whether the framework-info browser bundler can be loaded as a global variable. However, netlify-react-ui is loading framework-info through Webpack (which is tested by the "React site" test). Therefore, I believe we can remove those tests since they are now irrelevant. However, I am considering doing this as a separate, follow-up PR.

@ehmicky ehmicky mentioned this pull request Dec 6, 2021
@ehmicky ehmicky force-pushed the chore/pure-esm branch 9 times, most recently from f6d7445 to 30a14b7 Compare December 7, 2021 15:27
path: path.resolve(`${__dirname}/../dist`),
filename: 'index.js',
path: DIST_DIR,
filename: 'index.cjs',
library: 'frameworkInfo',
libraryTarget: 'umd',
Copy link
Contributor Author

@ehmicky ehmicky Dec 7, 2021

Choose a reason for hiding this comment

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

This is keeping UMD as a format for browser consumers, as opposed to using ES modules.
I initially tried with ES modules, but this created a wide array of problems when integrating it with netlify-react-ui. Those are listed in https://github.com/netlify/netlify-react-ui/issues/9869.
Keeping it as UMD is a smaller change for browser consumers, which will be simpler to integrate.

That being said, non-browser consumers will still use pure ES modules, and this repository can still use pure ES modules, which is the main goal.

path: path.resolve(`${__dirname}/../dist`),
filename: 'index.js',
path: DIST_DIR,
filename: 'index.cjs',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need to use .cjs since the top-level package.json is using type: 'module' but the bundle is not using ES modules.

@ehmicky ehmicky force-pushed the chore/pure-esm branch 3 times, most recently from 957c9a8 to 3ae1857 Compare January 18, 2022 16:11
@ehmicky ehmicky force-pushed the chore/pure-esm branch 4 times, most recently from c915f5a to 73ba8da Compare January 18, 2022 18:46
"main": "./src/main.js",
"browser": "./dist/index.js",
"type": "module",
"main": "./dist/index.cjs",
Copy link
Contributor Author

@ehmicky ehmicky Jan 18, 2022

Choose a reason for hiding this comment

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

The main field is needed for Webpack and Jest to work correctly when used by browser consumers like our front-end app. It must point to the Webpack bundle.

Choose a reason for hiding this comment

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

shouldn't we use the browsers field instead of the main field if it is used by browsers?

https://docs.npmjs.com/cli/v8/configuring-npm/package-json#browser

Copy link
Contributor Author

@ehmicky ehmicky Jan 19, 2022

Choose a reason for hiding this comment

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

The main problem is that the frontend application is using several tools which are not quite completely ready for pure ES modules and have their own (and conflicting) interpretation of how those fields should be used: Webpack, Jest, tsc. In the future, the exports field is actually meant to replace both the main and browser fields, but those tools are not ready for it yet.

What complicates things even more is that the frontend application is run and bundled in browser mode, but when it is run by Jest, it is in Node.js mode, and will use the main field instead of the browser field.

I have spent a few hours (due to prereleases process and CI speed) trying several combinations of either main, browser or main+browser, using both ./src/main.js and/or ./dist/index.cjs and this ended up being the only combination which worked with our frontend application.

// TODO: rename this file to `index.js` once Cypress supports it for pure
// ES modules
const noop = function () {}
export default noop

Choose a reason for hiding this comment

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

export default {}?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed 👍

@@ -117,5 +121,8 @@
],
"engines": {
"node": "^12.20.0 || ^14.14.0 || >=16.0.0"
},
"ava": {
"verbose": true

Choose a reason for hiding this comment

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

That was previously specified inside the ava config file shouldn't we add the files here as well?

export default {
  files: ['test/*.js'],
  verbose: true,
}

Copy link
Contributor Author

@ehmicky ehmicky Jan 19, 2022

Choose a reason for hiding this comment

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

files finds test/*.js by default in Ava, so we should be able to remove it there. The test files seem to be correctly found by Ava.

Copy link
Contributor

Choose a reason for hiding this comment

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

This will be moot in ava v4 since only the verbose reporter is left IIRC :)

lukasholzer
lukasholzer previously approved these changes Jan 19, 2022
@ehmicky
Copy link
Contributor Author

ehmicky commented Jan 19, 2022

Fixed 👍

@kodiakhq kodiakhq bot merged commit 2d3cf6a into main Jan 19, 2022
@kodiakhq kodiakhq bot deleted the chore/pure-esm branch January 19, 2022 16:57
@ehmicky ehmicky mentioned this pull request Jan 19, 2022
5 tasks
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
automerge type: chore work needed to keep the product and development running smoothly
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants