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 support for ES Module #113

Open
wants to merge 5 commits into
base: main
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/node_modules
/package-lock.json
index.cjs.js
19 changes: 19 additions & 0 deletions constructor/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import path from "path";
import { rollup } from "rollup";

const PATH_ROOT = path.resolve(__dirname, "../");

let inputOptions = {
input: path.resolve(PATH_ROOT, "index.js"),
external: ["acorn"]
}

let outputOptions = {
output: {
file: path.resolve(PATH_ROOT, "index.cjs.js"),
format: "cjs",
banner: "// DO NOT edit this file, it's auto generate from 'constructor/build.js', any changes will be overwrite. \n"
}
}

rollup(inputOptions).then(bundle => bundle.write(outputOptions));
14 changes: 9 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

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

@thescientist13 thescientist13 Jan 31, 2024

Choose a reason for hiding this comment

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

I think this import would need to have an extension?

import XHTMLEntities from './xhtml.js';


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

module.exports = function(options) {
function acornJsx(options) {
options = options || {};
return function(Parser) {
return plugin({
Expand All @@ -82,16 +83,19 @@ 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 acorn = Parser.acorn || acorn;
const acornJsx = getJsxTokens(acorn);
const tt = acorn.tokTypes;
const tok = acornJsx.tokTypes;
Expand Down
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"description": "Modern, fast React.js JSX parser",
"homepage": "https://github.com/acornjs/acorn-jsx",
"version": "5.3.1",
"main": "index.cjs.js",

Choose a reason for hiding this comment

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

also, I think we would want to add "type": "module" to package.json as well

"module": "index.js",
"maintainers": [
{
"name": "Ingvar Stepanyan",
Expand All @@ -16,12 +18,16 @@
},
"license": "MIT",
"scripts": {
"test": "node test/run.js"
"test": "node -r esm test/run.js",
"prepublishOnly": "node -r esm constructor/build.js"
},
"peerDependencies": {
"acorn": "^6.0.0 || ^7.0.0 || ^8.0.0"
},
"devDependencies": {
"acorn": "^8.0.1"
"@rollup/plugin-commonjs": "^12.0.0",
"acorn": "^8.0.1",
"esm": "^3.2.25",
"rollup": "^2.12.0"
}
}
8 changes: 4 additions & 4 deletions test/driver.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
var tests = [];

exports.test = function(code, ast, options, pluginOptions) {
export function test(code, ast, options, pluginOptions) {
tests.push({code, ast, options, pluginOptions});
};
exports.testFail = function(code, message, options, pluginOptions) {
export function testFail(code, message, options, pluginOptions) {
tests.push({code, error: message, options, pluginOptions});
};
exports.testAssert = function(code, assert, options) {
export function testAssert(code, assert, options) {
tests.push({code, assert, options});
};

exports.runTests = function(config, callback) {
export function runTests(config, callback) {
var parse = config.parse;

for (var i = 0; i < tests.length; ++i) {
Expand Down
9 changes: 5 additions & 4 deletions test/run.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var driver = require("./driver.js");
require("./tests-jsx.js");
require("./tests-misc.js");
import * as driver from "./driver.js";
import "./tests-jsx.js";
import "./tests-misc.js";
import * as acorn from "acorn";
import jsx from "../index.js";

function group(name) {
if (typeof console === "object" && console.group) {
Expand All @@ -18,7 +20,6 @@ function log(title, message) {
if (typeof console === "object") console.log(title, message);
}

const acorn = require("acorn"), jsx = require("..")
const Parser = acorn.Parser.extend(jsx())

var stats, modes = {
Expand Down
14 changes: 7 additions & 7 deletions test/tests-jsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import { test } from "./driver.js";
import { testFail } from "./driver.js";
import acornJsx from "../index.js";
import { tokTypes as acornTokens } from "acorn";

let jsxTokens = acornJsx.tokTypes;

var fbTestFixture = {
// Taken and adapted from esprima-fb/fbtest.js.
'JSX': {
Expand Down Expand Up @@ -3725,13 +3732,6 @@ var fbTestFixture = {
}
};

if (typeof exports !== "undefined") {
var test = require("./driver.js").test;
var testFail = require("./driver.js").testFail;
var jsxTokens = require("..").tokTypes;
var acornTokens = require("acorn").tokTypes;
}

testFail("var x = <div>one</div><div>two</div>;", "Adjacent JSX elements must be wrapped in an enclosing tag (1:22)");

testFail("<a:b.c />", "Unexpected token (1:4)");
Expand Down
10 changes: 4 additions & 6 deletions test/tests-misc.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
"use strict";

if (typeof exports !== "undefined") {
var assert = require("assert");
var acorn = require("acorn");
var jsx = require("..");
var testAssert = require("./driver.js").testAssert;
}
import assert from "assert";
import * as acorn from "acorn";
import jsx from "../index.js";
import { testAssert } from "./driver";

testAssert("// the enhanced Parser instance should have a static property 'acornJsx'.", function() {
const JsxParser = acorn.Parser.extend(jsx());
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