ast-module-types

Collection of useful helper functions when trying to determine module type (CommonJS or AMD) properties of an AST node.

MIT 25 个版本
安装
npm install ast-module-types
yarn add ast-module-types
pnpm add ast-module-types
bun add ast-module-types
README

ast-module-types

CI npm version npm downloads

Collection of useful helper functions when trying to determine module type (CommonJS or AMD) properties of an AST node.

AST checks are based on the Esprima (Spidermonkey) format

npm install ast-module-types

API

Each of these takes in a single AST node argument and returns a boolean.

  • isDefineAMD: if node matches any form of an AMD define function call
  • isRequire: if node matches a require function call (declaring a dependency)
  • isTopLevelRequire: if node matches a require at the very top of the file.
  • isAMDDriverScriptRequire: if node matches an AMD driver script's require call require([deps], function)
  • isExports: if the node matches CommonJS module.exports or exports (defining a module)

Detecting the various forms of defining an AMD module

  • isNamedForm: if the node is a define call of the form: define('name', [deps], func)
  • isDependencyForm: if the node is a define call of the form: define([deps], func)
  • isFactoryForm: if the node is a define call of the form: define(func(require))
  • isNoDependencyForm: if the node is a define call of the form: define({})
  • isREMForm: if the node matches the form: define(function(require, exports, module){});

ES6 Types

All types abide by the EStree spec

  • isES6Import: if the node is any of the es6 import forms
  • isES6Export: if the node is of any es6 export forms
  • isDynamicImport: if the node is a dynamic import() call

Usage

ESM

import { isDefineAMD } from 'ast-module-types';

// Assume node is some node of an AST that you parsed using esprima or esprima-fb
// ...

console.log(isDefineAMD(node));

CommonJS

const { isDefineAMD } = require('ast-module-types');

// Assume node is some node of an AST that you parsed using esprima or esprima-fb
// ...

console.log(isDefineAMD(node));

License

MIT

版本列表
7.0.0 2026-05-19
6.0.2 2026-05-09
6.0.1 2025-02-01
6.0.0 2024-04-09
5.0.0 2023-05-04
4.0.0 2023-03-19
3.0.0 2022-02-22
2.7.1 2020-11-01
2.7.0 2020-10-26
2.6.0 2019-11-23
2.5.0 2019-02-04
2.4.0 2018-09-25
2.3.2 2016-06-16
2.3.1 2016-06-16
2.3.0 2015-07-04
2.2.2 2015-01-20
2.2.1 2014-11-12
2.2.0 2014-11-12
2.1.0 2014-10-28
2.0.1 2014-10-25
2.0.0 2014-10-25
1.2.0 2014-06-09
1.1.1 2014-06-06
1.1.0 2014-06-06
1.0.0 2014-03-28