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 1 commit
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
10 changes: 4 additions & 6 deletions constructor/build.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
const path = require("path");
const rollup = require("rollup/dist/rollup.js").rollup;
const commonjs = require("@rollup/plugin-commonjs");
import path from "path";
import { rollup } from "rollup";

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

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

let outputOptions = {
output: {
file: path.resolve(PATH_ROOT, "index.mjs"),
format: "esm",
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"
}
}
Expand Down
48 changes: 17 additions & 31 deletions index.mjs → index.cjs.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
// DO NOT edit this file, it's auto generate from 'constructor/build.js', any changes will be overwrite.

Choose a reason for hiding this comment

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

Should this file be in .gitignore since it is generated?

Copy link
Author

@SaekiRaku SaekiRaku Nov 13, 2020

Choose a reason for hiding this comment

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

Yes, I have added it to .gitignore.


import acorn from 'acorn';

function createCommonjsModule(fn, basedir, module) {
return module = {
path: basedir,
exports: {},
require: function (path, base) {
return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);
}
}, fn(module, module.exports), module.exports;
}
'use strict';

function commonjsRequire () {
throw new Error('Dynamic requires are not currently supported by @rollup/plugin-commonjs');
}
var acorn = require('acorn');

var xhtml = {
var XHTMLEntities = {
quot: '\u0022',
amp: '&',
apos: '\u0027',
Expand Down Expand Up @@ -272,10 +260,6 @@ var xhtml = {
diams: '\u2666'
};

var acornJsx = createCommonjsModule(function (module) {



const hexNumber = /^[\da-fA-F]+$/;
const decimalNumber = /^\d+$/;

Expand Down Expand Up @@ -344,7 +328,7 @@ function getQualifiedJSXName(object) {
getQualifiedJSXName(object.property);
}

module.exports = function(options) {
var acornJsx = function(options) {
options = options || {};
return function(Parser) {
return plugin({
Expand All @@ -356,26 +340,29 @@ 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(acorn).tokTypes;
},
configurable: true,
enumerable: true
});

// let tokTypes = acornJsx.tokTypes;
// export { tokTypes };

function plugin(options, Parser) {
const acorn$1 = Parser.acorn || acorn;
const acornJsx = getJsxTokens(acorn$1);
const tt = acorn$1.tokTypes;
const acorn = Parser.acorn || acorn;
const acornJsx = getJsxTokens(acorn);
const tt = acorn.tokTypes;
const tok = acornJsx.tokTypes;
const tokContexts = acorn$1.tokContexts;
const tokContexts = acorn.tokContexts;
const tc_oTag = acornJsx.tokContexts.tc_oTag;
const tc_cTag = acornJsx.tokContexts.tc_cTag;
const tc_expr = acornJsx.tokContexts.tc_expr;
const isNewLine = acorn$1.isNewLine;
const isIdentifierStart = acorn$1.isIdentifierStart;
const isIdentifierChar = acorn$1.isIdentifierChar;
const isNewLine = acorn.isNewLine;
const isIdentifierStart = acorn.isIdentifierStart;
const isIdentifierChar = acorn.isIdentifierChar;

return class extends Parser {
// Expose actual `tokTypes` and `tokContexts` to other plugins.
Expand Down Expand Up @@ -491,7 +478,7 @@ function plugin(options, Parser) {
entity = String.fromCharCode(parseInt(str, 10));
}
} else {
entity = xhtml[str];
entity = XHTMLEntities[str];
}
break;
}
Expand Down Expand Up @@ -760,6 +747,5 @@ function plugin(options, Parser) {
}
};
}
});

export default acornJsx;
module.exports = acornJsx;
19 changes: 14 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) {
var acornJsx = function(options) {
Copy link
Member

Choose a reason for hiding this comment

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

This can be a function declaration.

options = options || {};
return function(Parser) {
return plugin({
Expand All @@ -82,16 +83,24 @@ 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;

// If use this, rollup will bundle as `exports.default` and `exports.tokTypes`. That make the CommonJS users have to change their require code like `require("acorn-jsx").default`.

// let tokTypes = acornJsx.tokTypes;
// export { tokTypes };

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
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"description": "Modern, fast React.js JSX parser",
"homepage": "https://github.com/acornjs/acorn-jsx",
"version": "5.2.0",
"module": "index.mjs",
"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 @@ -17,15 +18,16 @@
},
"license": "MIT",
"scripts": {
"test": "node test/run.js",
"prepublishOnly": "node constructor/build.js"
"test": "node -r esm test/run.js",
"prepublishOnly": "node -r esm constructor/build.js"
},
"peerDependencies": {
"acorn": "^6.0.0 || ^7.0.0"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^12.0.0",
"acorn": "^7.0.0",
"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
10 changes: 6 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,7 @@ function log(title, message) {
if (typeof console === "object") console.log(title, message);
}

const acorn = require("acorn"), jsx = require("..")
// const acorn = require("acorn"), jsx = require("..")
Copy link
Member

Choose a reason for hiding this comment

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

This looks like a leftover?

Copy link
Member

Choose a reason for hiding this comment

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

(same for few other comments)

const Parser = acorn.Parser.extend(jsx())

var stats, modes = {
Expand Down
19 changes: 13 additions & 6 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,12 +3732,12 @@ 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;
}
// 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)");

Expand Down
17 changes: 11 additions & 6 deletions test/tests-misc.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
"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";

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

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