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

Add build setup (ES5 & ES6, minified & unminified) #33

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
# Folders
dist
node_modules

# Files
*.log

# OS
._*
.cache
.DS_Store
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ node_js:
- 12
- 14
- 15
- 16

arch:
- amd64
Expand Down
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Match balanced string pairs, like `{` and `}` or `<b>` and `</b>`. Supports regu
Get the first matching pair of braces:

```js
var balanced = require('balanced-match')
import balanced from 'balanced-match'

console.log(balanced('{', '}', 'pre{in{nested}}post'))
console.log(balanced('{', '}', 'pre{first}between{second}post'))
Expand All @@ -32,6 +32,14 @@ $ node example.js
{ start: 3, end: 17, pre: 'pre', body: 'in{nest}', post: 'post' }
```

## Installation

With [npm](https://npmjs.org):

```bash
npm install balanced-match
```

## API

### var m = balanced(a, b, str)
Expand All @@ -58,14 +66,6 @@ If there's no match, `undefined` will be returned.

If the `str` contains more `a` than `b` / there are unmatched pairs, the first match that was closed will be used. For example, `{{a}` will match `[ 1, 3 ]` and `{a}}` will match `[0, 2]`.

## Installation

With [npm](https://npmjs.org) do:

```bash
npm install balanced-match
```

## Security contact information

To report a security vulnerability, please use the
Expand Down
2 changes: 1 addition & 1 deletion example.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const balanced = require('./')
const balanced = require('./dist/balanced-match.js')
Copy link
Owner

Choose a reason for hiding this comment

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

node comes with native support in recent versions, I wonder if we need to add a bundler, or can just bump the major and make it an ESM only module, as @sindresorhus has recently done with many modules (eg sindresorhus/p-queue@8c7325a)

Copy link
Author

@jhildenbiddle jhildenbiddle May 1, 2021

Choose a reason for hiding this comment

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

That's certainly an option and sindresorhus has made a case for doing so on his blog, but I don't think moving to ESM only is the right decision for every project. For server-side libraries, sure. For projects that may run in a browser, it makes sense to continue offering an ES5 version a bit longer.

A few issues I think are worth considering:

  1. The package you linked to (p-queue) is better positioned to go "ESM only" because the library requires Promise support. Practically speaking, this means that if the library is run in a browser it will almost certainly do so in environments that support ESM modules.

  2. Is this library able to run client-side? If so, then I believe the library should offer legacy-compatible code at least until MS officially ends support for IE11 (August 17, 2021).

    The alternative is to force library consumers to wrestle with webpack, rollup, parcel, etc. Even if these tools are already being used in a project, they have to be (re)configured to transpile some-but-not-all dependencies in /node_modules to avoid accidentally accidentally shipping ES6 code in ES5 bundles when they update to ESM-only versions of a library. This is what happened to me when updating balance-match to v2 in css-vars-ponyfill: I was already using babel, but it was configured to ignore files in /node_modules. As a result, rollup+babel created an ES5 bundle that contained properly transpired code (my source) and untranspiled ES6 code (balance-match v2). See v2.4.4 no longer transpiles to pure ECMAScript 5 jhildenbiddle/css-vars-ponyfill#158 for details.

  3. Is this a library consumers may want to load from a CDN? If so, then I believe the library should offer minified as well as unminifed versions:

    <!-- Minified UMD -->
    <script src="https://unpkg.com/balanced-match@3"></script>
    <!-- Minified ESM -->
    <script type="module" src="https://unpkg.com/balanced-match@3/dist/balances-match.esm.min.js"></script>

Just my $0.02. :)

If you prefer going "ESM only", I'm happy to made those changes and update the docs accordingly. Given that previous versions of balanced-match were written in ES5, I think it's important to let consumers know that the current version requires transpilation if used in legacy environments.

Copy link
Author

@jhildenbiddle jhildenbiddle May 7, 2021

Choose a reason for hiding this comment

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

Hi @juliangruber.

To add some context for the desire to provide an ES5-friendly version: css-vars-ponyfill is a library I maintain that is used primarily to make life easier for projects that are forced to support IE11. It is downloaded over 100k per week from npm and over 110m per month from just one CDN (jsdelivr). As devs we're all anxious to move past legacy browsers, but not everyone has the luxury to do so.

Happy to make changes if requested.

Thanks!

Copy link
Owner

Choose a reason for hiding this comment

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

For server-side libraries, sure. For projects that may run in a browser, it makes sense to continue offering an ES5 version a bit longer.

Interesting, in my experience it's the opposite. All of my client side projects are transpiled, none of my server side projects are, so I've been using ESM in the browser for a while. Is it the opposite for you?

If you were to consume and not have a bundler set up, how would you do it? Is that a realistic use case for this library?

Copy link
Owner

Choose a reason for hiding this comment

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

I think that for "entry point" libraries, like jQuery this makes sense, but for this library I anticipate it will be consumed in conjunction with others, where then using a bundler makes a lot more sense.

Thank you for all this work on this so far, however I'm not convinced this is the right way, and am still in favor of going ESM only (if anything).

Copy link
Owner

Choose a reason for hiding this comment

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

Copy link
Author

Choose a reason for hiding this comment

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

I asked for community input in https://twitter.com/juliangruber/status/1391336252096516098

The poll is interesting, but it misses the point of the PR. A better poll would be "should library authors pre-transpile distributable files to ES5 or are library consumers responsible for transpiling their dependencies to ES5?" I would expect the answer to be different for new libraries that have never offered ES5 versions versus well-established libraries that have always been ES5.

I think in 2021 there is a strong argument for moving to ES modules only, but the code in that module should still be ES5 if there is a reasonable chance it will be run in a legacy browser. Give the age and popularity of this library, I think that is a reasonable assumption.

Again, just my $0.02. 😄

Copy link
Contributor

Choose a reason for hiding this comment

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

i think we should just move to ESM


console.log(balanced('{', '}', 'pre{in{nested}}post'))
console.log(balanced('{', '}', 'pre{first}between{second}post'))
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict'
module.exports = balanced
export default balanced

function balanced (a, b, str) {
if (a instanceof RegExp) a = maybeMatch(a, str)
if (b instanceof RegExp) b = maybeMatch(b, str)
Expand All @@ -23,6 +23,7 @@ function maybeMatch (reg, str) {
}

balanced.range = range

function range (a, b, str) {
let begs, beg, left, right, result
let ai = str.indexOf(a)
Expand Down
Loading