diff --git a/.gitignore b/.gitignore index d502512..f85f137 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /node_modules /package-lock.json +index.cjs.js \ No newline at end of file diff --git a/constructor/build.js b/constructor/build.js new file mode 100644 index 0000000..6e190cc --- /dev/null +++ b/constructor/build.js @@ -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)); \ No newline at end of file diff --git a/index.js b/index.js index 004e080..694e355 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ 'use strict'; -const XHTMLEntities = require('./xhtml'); +import * as acorn from "acorn"; +import XHTMLEntities from './xhtml'; const hexNumber = /^[\da-fA-F]+$/; const decimalNumber = /^\d+$/; @@ -70,7 +71,7 @@ function getQualifiedJSXName(object) { getQualifiedJSXName(object.property); } -module.exports = function(options) { +function acornJsx(options) { options = options || {}; return function(Parser) { return plugin({ @@ -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; diff --git a/package.json b/package.json index f42a7ea..07a0a50 100644 --- a/package.json +++ b/package.json @@ -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", + "module": "index.js", "maintainers": [ { "name": "Ingvar Stepanyan", @@ -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" } } diff --git a/test/driver.js b/test/driver.js index 3e152c1..efa335c 100644 --- a/test/driver.js +++ b/test/driver.js @@ -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) { diff --git a/test/run.js b/test/run.js index b9d98bf..e462920 100644 --- a/test/run.js +++ b/test/run.js @@ -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) { @@ -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 = { diff --git a/test/tests-jsx.js b/test/tests-jsx.js index b53549d..072b44d 100644 --- a/test/tests-jsx.js +++ b/test/tests-jsx.js @@ -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': { @@ -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 =