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

ascjs

v6.0.3

Published

ES2015 to CommonJS import/export transformer

Downloads

4,267

Readme

ascjs

License: ISC Build Status Coverage Status donate Greenkeeper badge

ES2015 to CommonJS import/export transformer


Looking for a CommonJS minimalistic bundler ?

Fully based on ascjs, asbundle is a no-brainer to create out of the box browser compatible bundles. Don't miss it out!


This module does one thing only: it loosely transpiles ES2015 import/export statements into valid CommonJS in order to fix the only part of Node that's incompatible with modern JS.

How to

You can use ascjs as binary utility or as module.

npm install -g ascjs

# to see what you can do
ascjs --help

As executable, you can use ascjs to output, or save, some code content.

ascjs code
ascjs --ignore=a.js,b.js sourceFile
ascjs --no-default
ascjs sourceFile
ascjs sourceFile destFile

# folders are recursively parsed
# destFolder is mandatory
ascjs sourceFolder destFolder

You can also use it via pipe operator.

echo code | ascjs
cat source.js | ascjs | uglifyjs -o dest.js

As module, you can require it and use it to convert ESM to CJS.

const ascjs = require('ascjs');

ascjs('import "test";');
// require("test");

Features

  • extremely lightweight, based on babylon for performance and reliability, it transforms only imports/exports ignoring everything else
  • produces modern JavaScript, you are in charge of extra transformations if needed
  • indentation, spaces, semi or no-semi are preserved: beautiful source code remains beautiful
  • uses same Babel convention, resolving export default ... intent as exports.default
  • you can finally write .js code and transform it for Node only before publishing on npm
  • you could write .mjs modules and transform them into CommonJS for Browserify or other bundlers as target

Constrains

  • live bindings for exported values are not preserved. You need to delegate in scope eventual changes
  • dynamic import(...) is untouched. If you write that, let Webpack handle it for you later on
  • there is no magic whatsoever in module names resolution, what you write in ESM is what you get as CJS

Flags

  • --ignore=... a comma separated paths to ignore parsing
  • --no-default remove the __esModule flag and export default via module.exports =

Example

This module can transform the following ES2015+ code

import func, {a, b} from './module.js';
import * as tmp from 'other';

const val = 123;

export default function test() {
  console.log('ascjs');
};

export {func, val};

into the following one:

'use strict';
const func = (m => m.__esModule ? m.default : m)(require('./module.js'));
const {a, b} = require('./module.js');
const tmp = require('other');

const val = 123;

function test() {
  console.log('ascjs');
}
Object.defineProperty(exports, '__esModule', {value: true}).default = test;

exports.func = func;
exports.val = val;