babel-plugin-amd-checker

0.0.2 • Public • Published

babel-plugin-amd-checker

Latest version Dependency status

A Babel plugin to check the format of your modules when compiling your code using Babel. This plugin allows you to abort the module transformation, if the module source complies with AMD and let the transformation apply only on ESM modules. transform the path of each source module using a custom JavaScript function.

This plugin is supposed to be used with requirejs-babel7, where the target environment is a web browser using @babel/standalone. If if detects an AMD module, it will throw an error of the class AmdDetected and abort the module transformation. The error can be caught and ignored in the build pipeline.

Table of Contents

Installation

This module can be installed in your project using NPM, PNPM or Yarn. Make sure, that you use Node.js version 6 or newer.

npm i -D babel-plugin-amd-checker
pnpm i -D babel-plugin-amd-checker
yarn add babel-plugin-amd-checker

Babel Configuration Examples

Prevent the transpiler to wrap source files that are already wrapped by define or require as AMD modules:

{
  plugins: ['amd-checker']
}

A typical configuration combined with babel-plugin-module-resolver-standalone and set within requirejs-babel7 by default:

{
  plugins: [
    'amd-checker',
    'transform-modules-amd',
    [
      'module-resolver',
      {
        resolvePath: function (sourcePath, currentFile, opts) {
          // Avoid prefixing modules handled by other plugins.
          if (sourcePath.indexOf('!') < 0) {
            return 'es6!' + sourcePath;
          }
        }
      }
    ]
  ]
}

Error handling during the transpilation in a RequireJS plugin:

var amdChecker = require('babel-plugin-amd-checker')
babel.registerPlugin('amd-checker', amdChecker);

var code;
try {
  code = babel.transform(text, options).code;
} catch (error) {
  if (!(error instanceof amdChecker.AmdDetected)) {
    return onload.error(error);
  }
  code = text;
}

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Lint and test your code.

License

Copyright (c) 2022 Ferdinand Prantl

Licensed under the MIT license.

Package Sidebar

Install

npm i babel-plugin-amd-checker

Weekly Downloads

0

Version

0.0.2

License

MIT

Unpacked Size

8.49 kB

Total Files

4

Last publish

Collaborators

  • prantlf