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

Updating build system for use in CommonJS #141

Merged
merged 3 commits into from
May 17, 2021
Merged

Updating build system for use in CommonJS #141

merged 3 commits into from
May 17, 2021

Conversation

6utt3rfly
Copy link
Collaborator

Trying to use jsep in commonjs wasn't working because it was being treated as a module.

  • Separate dist folders for each build. Add overriding package.json file in cjs folder to set type: 'commonjS'. (Note: there are difficulties with trying to do this using cat in an npm script with {} and some Operating Systems. Simple solution is to copy a file.
  • Updated ReadMe
  • Added tests to lint script and fixed lint errors

- Separate dist folders for each build. Add overriding package.json file in cjs folder to set `type: 'commonjS'`. (Note: there are difficulties with trying to do this using `cat` in an npm script with {} and some Operating Systems. Simple solution is to copy a file.
- Updated ReadMe
- Added tests to lint script and fixed lint errors
README.md Show resolved Hide resolved
@6utt3rfly 6utt3rfly mentioned this pull request May 2, 2021
Copy link
Collaborator

@LeaVerou LeaVerou left a comment

Choose a reason for hiding this comment

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

Mostly LGTM, see comments!

README.md Show resolved Hide resolved
README.md Show resolved Hide resolved
README.md Outdated
let parse_tree = jsep("1 + 1");
// ESM:
import { Jsep } from 'jsep';
const parse_tree = Jsep.parse('1 + 1');
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is the advantage of this for our users over just using jsep()?

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'll go back to both. I've been reading tonight a lot about the "evils" of default exports ... so I think our preferred usage should be to use the named export, but I've got it working for both CJS and ESM with both the default and named export now (without needing '.default')

Copy link
Collaborator

Choose a reason for hiding this comment

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

I always try to keep an open mind, so feel free to send these "evils" my way, but so far I'm entirely unconvinced by the arguments against default exports. The ones I've seen seem to mainly center around inadequacies of tooling. IMHO it's the tooling that needs to be fixed, not default exports that should be avoided. There is a very clear usability benefit to default exports. Not only can the module be imported without looking up its documentation to remember what name to use, but also it offers a clear entry point, that this object is the main thing to use. And the syntax is simpler too. These may seem trivial for experienced developers, but they can make a world of difference to beginners who get easily discouraged and frustrated.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I was waiting for a response to this, not sure if it still applies?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ah sorry! I thought it was resolved by providing both named and default exports?
From what I understand:

  • using default imports prevents tree-shaking if there are parts of a file that are unused
  • mixing named and default exports seems to be discouraged (which is why the build is more difficult for both cjs and esm). But I like that it allows both use cases.
  • default imports can lead to inconsistent naming in a code base and may make large-scale refactoring more difficult.
  • intellisense may be more problematic with default imports (tooling problem, I know)
  • named exports are easier in export aggregation (i.e. barrel files)

In my work, I use a lot of both CJS default and named require statements, but mostly named imports in typescript. (Trying to get away from import * as _ from 'lodash'!). I've found that unit testing named exports can sometimes be easier. But overall, I don't have a strong opinion either way.

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
rollup.config.js Outdated Show resolved Hide resolved
test/tests.js Outdated Show resolved Hide resolved
README.md Show resolved Hide resolved
README.md Outdated
let parse_tree = jsep("1 + 1");
// ESM:
import { Jsep } from 'jsep';
const parse_tree = Jsep.parse('1 + 1');
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'll go back to both. I've been reading tonight a lot about the "evils" of default exports ... so I think our preferred usage should be to use the named export, but I've got it working for both CJS and ESM with both the default and named export now (without needing '.default')

README.md Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
rollup.config.js Outdated Show resolved Hide resolved
package.json Show resolved Hide resolved
- default export now includes static fields. Will allow for removing modifier methods if we like
- default export now adds Jsep as well to support `const { Jsep } = require('jsep')`
- removed eslint disable (switched to arrow functions)
- restored dist file folder structure, and some readme changes
- rollup now does 2 builds so it can use named + default export for ESM, and just the default export for IIFE and CJS (without having to use a .default property)
<script src="/PATH/TO/jsep.min.js"></script>
...
```html
<script src="/PATH/TO/jsep.iife.min.js"></script>
Copy link
Collaborator

Choose a reason for hiding this comment

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

I do think we should mention ESM too. They're not only useful for Node. Not sure if they should be before or after IIFE, I'll leave it up to you. I could argue this both ways: ESM should be first because it's the "canonical" version, or IIFE should be first because ESM might scare novices away.
Also, if we're including <script> elements, we should probably include one around the sample code.

rollup.config.js Outdated Show resolved Hide resolved
update readme example for <script> and ESM
@6utt3rfly
Copy link
Collaborator Author

Not sure if there's still a change needed @LeaVerou ? Or just waiting for a second review by @EricSmekens ?

@6utt3rfly 6utt3rfly requested a review from LeaVerou May 17, 2021 05:51
@LeaVerou
Copy link
Collaborator

Sorry I dropped the ball, thanks for the ping! I approved it now, so you could merge it yourself whenever you want (I'm told this is the new pattern so I figured I'd try it)

@6utt3rfly 6utt3rfly merged commit 60a9b44 into master May 17, 2021
@6utt3rfly 6utt3rfly deleted the commonjs branch May 17, 2021 15:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants