Skip to content

Commit

Permalink
feat: switch to ESM source and distributions
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

Adds `exports`

Also:
1. updates benchmark
2. fixes escodegen with missing options
  • Loading branch information
brettz9 committed Mar 6, 2022
1 parent 5f23e07 commit 267899b
Show file tree
Hide file tree
Showing 45 changed files with 2,358 additions and 4,346 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ indent_style = space
indent_size = 4

[*.json]
indent_size = 2
indent_size = 4

[*.yml]
indent_size = 2
3 changes: 1 addition & 2 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ coverage

test/*/*.js

escodegen.browser.min.js
escodegen.browser.js
dist
8 changes: 6 additions & 2 deletions .eslintrc.js → .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ module.exports = {
SharedArrayBuffer: 'readonly'
},
env: {
node: true,
es6: true
},
parserOptions: {
sourceType: 'module',
ecmaVersion: 2018
ecmaVersion: 2020
},
overrides: [{
files: ['*-node.js', '.eslintrc.cjs', 'benchmark/**', 'bin/**', 'tools/**'],
env: {
node: true
}
}, {
files: '.eslintrc.js',
parserOptions: {
sourceType: 'script'
Expand Down
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ node_modules/
# Cover
.coverage_data/
cover_html/
coverage

.idea

npm-debug.log

escodegen.browser.min.js
escodegen.browser.js
dist
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ for a demo.
Escodegen can be used in a web browser:

```html
<script src="escodegen.browser.js"></script>
<script src="dist/escodegen.umd.js"></script>
```

escodegen.browser.js can be found in tagged revisions on GitHub.
`dist/escodegen.umd.js` can be found in tagged revisions on GitHub.

Or in a Node.js application via npm:

Expand Down Expand Up @@ -54,7 +54,7 @@ After that,
npm run-script build
```

will generate `escodegen.browser.js`, which can be used in browser environments.
will generate `dist/escodegen.umd.js`, which can be used in browser environments.

And,

Expand Down
16 changes: 10 additions & 6 deletions benchmark/asts.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
var fs = require('fs'),
path = require('path'),
esprima = require('esprima');
import fs from 'fs';
import path from 'path';
import { dirname } from 'path';
import { fileURLToPath } from 'url';

var FILES_PATH = path.join(__dirname, './asts');
// import esprima from 'esprima';

const __dirname = dirname(fileURLToPath(import.meta.url));
const FILES_PATH = path.join(__dirname, './asts');

var FILES = [
'jQuery 1.7.1',
Expand All @@ -22,8 +26,8 @@ function slug(name) {
return name.toLowerCase().replace(/\s/g, '-');
}

module.exports = FILES.map(function (file) {
var astJson = fs.readFileSync(FILES_PATH + '/' + slug(file) + '-ast.json');
export default FILES.map(function (file) {
const astJson = fs.readFileSync(`${FILES_PATH}/${slug(file)}-ast.json`);

return JSON.parse(astJson);
});
13 changes: 6 additions & 7 deletions benchmark/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
var Benchmark = require('benchmark'),
escodegen = require('../'),
old = require('./old.js'),
esotope = require('esotope'),
asts = require('./asts');

import Benchmark from 'benchmark';
import esotope from 'esotope';
import * as escodegen from '../src/escodegen-node.js';
import old from './old.cjs';
import asts from './asts.js';

function cycle(codegen) {
for (var i = 0; i < asts.length; i++)
Expand Down Expand Up @@ -32,7 +31,7 @@ new Benchmark.Suite()
})

.on('complete', function () {
console.log('Fastest is ' + this.filter('fastest').pluck('name'));
console.log(`Fastest is ${this.filter('fastest').map('name')}`);

console.log('esotope is x' + (this[0].hz / this[1].hz).toFixed(2) + ' times faster vs escodegen.');
})
Expand Down
File renamed without changes.
126 changes: 126 additions & 0 deletions benchmark/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions benchmark/package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
{
"private": true,
"name": "benchmark",
"version": "0.1.0",
"description": "Regression benchmarking. Compare speed with upstream npm version",
"main": "index.js",
"type": "module",
"scripts": {
"test": "node index"
},
"author": "Ivan Nikulin <[email protected]>",
"license": "MIT",
"dependencies": {
"benchmark": "~1.0.0",
"benchmark": "~2.1.4",
"esotope": "*"
},
"optionalDependencies": {
"microtime": "~0.4.0"
"microtime": "~3.0.0"
}
}
5 changes: 2 additions & 3 deletions benchmark/trace_cycle.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
var asts = require('./asts'),
escodegen = require('../');
import asts from './asts.js';
import escodegen from '../src/escodegen-node.js';

for (var j = 0; j < 50; j++) {
for (var i = 0; i < asts.length; i++)
escodegen.generate(asts[0]);
}

17 changes: 8 additions & 9 deletions bin/escodegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/*jslint sloppy:true node:true */
import fs from 'fs';
import path from 'path';
import esprima from 'esprima';
import Optionator from 'optionator';
import * as escodegen from '../src/escodegen-node.js';

var fs = require('fs'),
path = require('path'),
root = path.join(path.dirname(fs.realpathSync(__filename)), '..'),
esprima = require('esprima'),
escodegen = require(root),
optionator = require('optionator')({
const optionator = Optionator({
prepend: 'Usage: escodegen [options] file...',
options: [
{
Expand All @@ -43,7 +42,6 @@ var fs = require('fs'),
}),
args = optionator.parse(process.argv),
files = args._,
options,
esprimaOptions = {
raw: true,
tokens: true,
Expand All @@ -56,6 +54,7 @@ if (files.length === 0) {
process.exit(1);
}

let options;
if (args.config) {
try {
options = JSON.parse(fs.readFileSync(args.config, 'utf-8'));
Expand All @@ -68,7 +67,7 @@ files.forEach(function (filename) {
var content = fs.readFileSync(filename, 'utf-8'),
syntax = esprima.parse(content, esprimaOptions);

if (options.comment) {
if (options && options.comment) {
escodegen.attachComments(syntax, syntax.comments, syntax.tokens);
}

Expand Down
15 changes: 7 additions & 8 deletions bin/esgenerate.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/*jslint sloppy:true node:true */
import fs from 'fs';
import * as escodegen from '../src/escodegen-node.js';
import Optionator from 'optionator';

var fs = require('fs'),
path = require('path'),
root = path.join(path.dirname(fs.realpathSync(__filename)), '..'),
escodegen = require(root),
optionator = require('optionator')({
const optionator = Optionator({
prepend: 'Usage: esgenerate [options] file.json ...',
options: [
{
Expand All @@ -41,14 +39,15 @@ var fs = require('fs'),
]
}),
args = optionator.parse(process.argv),
files = args._,
options;
files = args._;

if (files.length === 0) {
console.log(optionator.generateHelp());
process.exit(1);
}

let options;

if (args.config) {
try {
options = JSON.parse(fs.readFileSync(args.config, 'utf-8'));
Expand Down
1 change: 0 additions & 1 deletion escodegen.js.map

This file was deleted.

Loading

0 comments on commit 267899b

Please sign in to comment.