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

ESM support #1

Merged
merged 2 commits into from
Feb 11, 2024
Merged
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ This is plugin for [Acorn](http://marijnhaverbeke.nl/acorn/) - a tiny, fast Java

It was created as an experimental alternative, faster [React.js JSX](http://facebook.github.io/react/docs/jsx-in-depth.html) parser. Later, it replaced the [official parser](https://github.com/facebookarchive/esprima) and these days is used by many prominent development tools.

> This is a fork intended to satisfy a specific use case while awaiting the migration of the original package to ESM - https://github.com/acornjs/acorn-jsx/issues/112

## Transpiler

Please note that this tool only parses source code to JSX AST, which is useful for various language tools and services. If you want to transpile your code to regular ES5-compliant JavaScript with source map, check out [Babel](https://babeljs.io/) and [Buble](https://buble.surge.sh/) transpilers which use `acorn-jsx` under the hood.
Expand All @@ -19,7 +21,7 @@ Requiring this module provides you with an Acorn plugin that you can use like th

```javascript
var acorn = require("acorn");
var jsx = require("acorn-jsx");
var jsx = require("@projectevergreen/acorn-jsx-esm");
acorn.Parser.extend(jsx()).parse("my(<jsx/>, 'code');");
```

Expand Down
13 changes: 7 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const XHTMLEntities = require('./xhtml');
import * as acorn from "acorn";
import XHTMLEntities from './xhtml.js';

const hexNumber = /^[\da-fA-F]+$/;
const decimalNumber = /^\d+$/;
Expand Down Expand Up @@ -70,7 +69,7 @@ function getQualifiedJSXName(object) {
getQualifiedJSXName(object.property);
}

module.exports = function(options) {
function acornJsx(options) {
options = options || {};
return function(Parser) {
return plugin({
Expand All @@ -82,14 +81,16 @@ module.exports = function(options) {

// This is `tokTypes` of the peer dep.
// This can be different instances from the actual `tokTypes` this plugin uses.
Object.defineProperty(module.exports, "tokTypes", {
Object.defineProperty(acornJsx, "tokTypes", {
get: function get_tokTypes() {
return getJsxTokens(require("acorn")).tokTypes;
return getJsxTokens(acorn).tokTypes
},
configurable: true,
enumerable: true
});

export default acornJsx;

function plugin(options, Parser) {
const acorn = Parser.acorn || require("acorn");
const acornJsx = getJsxTokens(acorn);
Expand Down
15 changes: 11 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
{
"name": "acorn-jsx",
"name": "@projectevergreen/acorn-jsx-esm",
"description": "Modern, fast React.js JSX parser",
"homepage": "https://github.com/acornjs/acorn-jsx",
"version": "5.3.2",
"homepage": "https://github.com/ProjectEvergreen/acorn-jsx-esm",
"version": "0.1.0",
"type": "module",
"publishConfig": {
"access": "public"
},
"exports": {
".": "./index.js"
},
"maintainers": [
{
"name": "Ingvar Stepanyan",
Expand All @@ -12,7 +19,7 @@
],
"repository": {
"type": "git",
"url": "https://github.com/acornjs/acorn-jsx"
"url": "https://github.com/ProjectEvergreen/acorn-jsx-esm"
},
"license": "MIT",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion xhtml.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
quot: '\u0022',
amp: '&',
apos: '\u0027',
Expand Down