npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

requirejs-esm-converter

v1.1.0

Published

Converts source RequireJS modules from AMD to ESM format.

Downloads

45

Readme

RequireJS ESM Converter

Latest version Dependency status Code coverage

Converts source RequireJS modules from AMD to ESM format.

If you enabled usage of ESM modules in your RequireJS project (using requirejs-babel7, requirejs-esm or requirejs-esm-preprocessor, for example), you might want to convert all your code base to the ESM format to follow the same consistent coding standard and use the same set of tools to build, test and analyse your sources.

Synopsis

Convert all JavaScript sources in the project's subdirectories:

❯ requirejs-to-esm '*/**/*.js' '!node_modules'

lib/impl/analyse.js: converted
lib/impl/convert.js: converted
lib/index.js: multiple statements
test/index.js: converted

The file lib/index.js wasn't an AMD module. (It didn't contain a single define or require statement.) It will need an inspection and manual conversion.

Installation

This module can be installed globally using NPM, PNPM or Yarn. Make sure, that you use Node.js version 14 or newer.

npm i -g requirejs-esm-converter
pnpm i -g requirejs-esm-converter
yarn add --global requirejs-esm-converter

Command Line

requirejs-to-esm [option...] [<pattern> ...]

You can use BASH patterns for including and excluding files (only files). Patterns are case-sensitive and have to use slashes as directory separators. A pattern to exclude from processing starts with "!".

Options

-d|--[no-]dry-run  only log results, no writing to files
-p|--[no-]print    write to stdout instead of overwriting files
-V|--version       print version number
-h|--help          print usage instructions

Files will overwritten with the converted output, if they are recognised as AMD modules and if the dry-run mode is not enabled.

Examples

requirejs-to-esm '**/*.js' '!node_modules'
requirejs-to-esm -d "lib/*.js"
echo "define({})" | requirejs-to-esm"

API

convert(source: string): { code: string, warnings: string[] }

The function convert converts the source in AMD to code in ESM, optionally returning warnings. If the conversion is impossible because the input is not an AMD module, it will throw ConvertError. If the source file cannot be parsed, it will throw SyntaxError.

import { convert } from 'requirejs-to-esm'

const input =`define(["test"], function (test) {
  "use strict";
  console.log('imported:', test);
  return 42; // ultimate answer
});`
const { code } = convert(input)
console.log(code)

// Result:
//
// import test from "test";
//
// console.log('imported:', test);
// export default 42; // ultimate answer

Contributing

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

License

Copyright (c) 2022-2024 Ferdinand Prantl

Licensed under the MIT license.