evanw / evanw/esbuild

Compatability between inject and CJS

Open
#1,697 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
40.1k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

So, the [docs](https://esbuild.github.io/) are kind of a weird mishmash of `cjs` and `esm`, although they mention **CommonJS** as a "major feature".

We've hit a snag with trying to `inject` some global libraries (like **jQuery**, **React** and the like) into our bundle. Let's say we have a file called `vendor`, which pass to `inject`, containing the following:

```javascript
module.exports = {
$: require('jquery'),
React: require('react'),
ReactDOM: require('react-dom')
};
```
^ The above won't work, and the bundle will complain about a missing **React** dependency. However, transforming the file to `esm` -format will work:

```javascript
import * as $ from 'jquery';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

export { $, React, ReactDOM }
```
Am I to understand that `cjs` simply won't work with `inject`? If so, are there other features that behave similarly? The docs don't really make this clear.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the inject documentation at https://esbuild.github.io/ and reproduce the CommonJS and ESM examples using jQuery, React, and ReactDOM. Clarify whether CommonJS works with inject and identify any other features with similar module-format behavior; done when the documentation states these limitations clearly.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
build-system
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.