1.2.0 • Published 4 months ago

bare-module-lexer v1.2.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
4 months ago

bare-module-lexer

Heuristic lexer for detecting imports and exports in JavaScript modules. It trades off correctness for performance, aiming to reliably support the most common import and export patterns with as little overhead as possible.

Usage

const lex = require('bare-module-lexer')

lex(`
  const foo = require('./foo.js')
  exports.bar = 42
`)

// {
//   imports: [
//     { specifier: './foo.js', type: REQUIRE, names: [], position: [ 15, 24, 32 ] }
//   ],
//   exports: [
//     { name: 'bar', position: [ 37, 45, 48 ] }
//   ]
// }

API

const { imports, exports } = lex(source[, encoding][, options])

imports is an array of objects with the following shape:

imports = {
  specifier: 'string',
  type: number,
  names: ['string'],
  position: [
    number, // Import start
    number, // Specifier start
    number // Specifier end
  ]
}

exports is an array of objects with the following shape:

exports = {
  name: 'string',
  position: [
    number, // Export start
    number, // Name start
    number // Name end
  ]
}

Options are reserved.

lex.constants

ConstantDescription
REQUIRECommonJS require().
IMPORTES module import.
DYNAMICES module import() if IMPORT is set.
ADDONCommonJS require.addon() if REQUIRE is set, or ES module import.meta.addon() if IMPORT is set.
ASSETCommonJS require.asset() if REQUIRE is set, or ES module import.meta.asset() if IMPORT is set.
RESOLVECommonJS require.resolve() or require.addon.resolve() if REQUIRE and optionally ADDON are set, or ES module import.meta.resolve() or import.meta.addon.resolve() if IMPORT and optionally ADDON are set.
REEXPORTRe-export of a CommonJS require() if REQUIRE is set, or ES module export from if IMPORT is set.

License

Apache-2.0

1.2.0

4 months ago

1.1.0

7 months ago

1.0.7

7 months ago

1.0.6

7 months ago

1.0.5

8 months ago

1.0.2

8 months ago

1.0.4

8 months ago

1.0.3

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago