包详细信息

js-module-formats

yahoo18.6kBSD0.1.2

Detect different types of JavaScript modules formats

JavaScript, module, es6, amd

自述文件

JavaScript Module Formats

Build Status Dependency Status npm Version

Micro library to detect different types of JavaScript modules formats given some JavaScript source code.

Goals, Overview & Features

With the new ES Module syntax arrival, projects will commence the transition to write modules in ES format, and in some cases, rewrite/adjust modules to be ES module. As a result, complex applications might ended up having multiple module formats in their application, while the proper transpile process will be necessary. This micro library will help you to detect what type of module does a JavaScript file defines, and take the appropriate steps based on that information.

Installation

Install using npm:

$ npm install js-module-formats

Usage

By calling detect() with JavaScript source code, it returns one of the following values:

  • yui Modules
  • amd Modules
  • cjs CommonJS modules (including nodejs modules)
  • es Modules
  • undefined if the detection fails

To detect the module format of a file called file.js:

var fs            = require('fs'),
    moduleFormats = require('js-module-formats').detect;

var source = fs.readFileSync(__dirname + '/file.js', 'utf8');

console.log(moduleFormats.detect(source));

Note: ES modules without import or export statements will not be detected.

License

This software is free to use under the Yahoo! Inc. BSD license. See the LICENSE file for license text and copyright information.

更新日志

Javascript Module Formats Change History

0.1.2 (2014-04-21)

  • [PR #11] adding support for steal format.

0.1.1 (2014-03-31)

  • [PR #10] improving detection for cjs to support exporting string, number, etc.

0.1.0 (2014-03-17)

  • Adding linting for pretest and npm run lint.
  • [PR #6] improving ES modules detection by using RegExp.
  • [PR #5] Switch detect to detect(src).
  • [PR #4] improving cjs detection, including export instance detection.

0.0.2 (2014-03-05)

  • support for cjs detection.

0.0.1 (2014-03-05)

  • first commit/release with support for amd, yui and es.