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

es6-module-transpiler-npm-resolver

v0.3.0

Published

ES6 Module Transpiler Extension to Resolver from NPM modules with jsnext:main flag.

Downloads

40

Readme

es6-module-transpiler-npm-resolver

ES6 Module Transpiler Extension to Resolver from NPM modules with jsnext:main flag.

Overview

ES6 Module Transpiler es6-module-transpiler is an experimental compiler that allows you to write your JavaScript using a subset of the current ES6 module syntax, and compile it into various formats. Part of the transpilation process is the validation and resolution process, which is one of the benefits of ES6 modules, reporting static errors if the modules you're trying to import are not available, or the named export that you're referencing to does not exists.

By default, the transpiler implements a relative path resolution process, which means that only modules in a common folder structure are available, while this is good enough for experimentation, it falls short for bigger projects where sharing pieces and sharing components in paramount. This is the reason for es6-module-transpiler-npm-resolver exists, it allows to depend on external modules, which are, essentially, npm modules that holds ES6 modules that you can depend on.

Disclaimer

This resolver DOES NOT provide any interoperability between ES6 modules and CommonJS modules, it is just allow to package ES6 code alongside with the code in an NPM package.

The way of keeping ES6 source code in NPM package is a moving target, and we foresee this changing in the future, for now, we are just sticking to the jsnext:main flag.

Usage

jsnext:main flag

As we mentioned before, transpiler will rely on relative paths to resolve imported modules, e.g.:

import foo from "./path/to/foo";

Without this resolver, if you want to import a module that was installed by a regular NPM package, you will have to do this:

import foo from "./node_modules/foo/path/to/main-es6-module";

That's pretty bad, and risky, because there is no guarantee that foo is a peer module.

What you want to do is to import a module by relying on the NPM mechanism under the hood, so you can simply do:

import foo from "foo";

The es6-module-transpiler-npm-resolver will use the mechanism used for node to resolve the closest module called foo, and will inspect the package.json that describes the package to look for a custom configuration called jsnext:main, which is very similar to the regular main configuration, but points to a file that represents the main ES6 module implementation. If that flag is not set, the transpiler will throw an lookup error since the module cannot be found. Here is an example for foo:

{
  "name": "foo",
  "version": "0.0.1",
  "main": "index.js",
  "jsnext:main": "path/to/main-es6-module.js"
}

Executable

If you plan to use the compile-modules CLI, the resolver can be used directly from the command line:

$ npm install -g es6-module-transpiler
$ npm install es6-module-transpiler-npm-resolver
$ compile-modules convert -r ./node_modules/es6-module-transpiler-npm-resolver path/to/module.js -o build/bundle.js

The -r option allow you to specify the path or name of the specific resolver.

Library

You can also use the resolver with the transpiler as a library:

var transpiler = require('es6-module-transpiler');
var Container = transpiler.Container;
var FileResolver = transpiler.FileResolver;
var NPMFileResolver = require('es6-module-transpiler-npm-resolver');
var formatters = require('es6-module-transpiler/lib/formatters');
var Formatter = formatters[formatters.DEFAULT];

var container = new Container({
  resolvers: [new FileResolver(['lib/']), new NPMFileResolver([process.cwd()])],
  formatter: new Formatter()
});

container.getModule('index');
container.write('out/mylib.js');

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Thanks, and enjoy living in the ES6 future!