babel-plugin-amd-checker
v0.0.2
Published
Module format checking plugin for Babel usable in both Node.js the web browser environments.
Downloads
4
Maintainers
Readme
babel-plugin-amd-checker
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.