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

@a-la/import

v1.12.0

Published

RegExp rules to transpile import statements into require calls (used in ÀLaMode).

Downloads

75

Readme

@a-la/import

npm version

@a-la/import is a a set of rules for alamode to transpile import statements into require calls in Node.JS.

ÀLaMode is a RegExp-based transpiler which works faster than AST-based transpilers such as @babel, has no dependencies and occupies less disk space.

yarn add @a-la/import

Table Of Contents

API

The ÀLaImport is the default export and an array containing a sequence of rules for Replaceable.

import ÀLaImport from '@a-la/import'

ÀLaImport Sequence

The rule set exported as an array by ÀLaImport has multiple regexes and replacer functions to match all possible cases. The replacer functions expect to see the markers property on the context, which is set by alamode to access cut out strings. The transform can be run using @a-la/context which is a lightweight version of alamode which mimics its stream functionality.

/* yarn example/ */
import ÀLaContext from '@a-la/context'
import ÀLaImport from '@a-la/import'

const STRING = `import aLaMode from 'alamode'
import ALaImport from "@a-la/import"
import App from 'koa'
import test from './test'
`

;(async () => {
  const context = new ÀLaContext(__filename)
  context.setConfig({
    import: {
      alamodeModules: ['alamode', '@a-la/import'],
    },
  })
  const { result } = await context.stream(ÀLaImport, STRING)
  console.log(result)
})()
const aLaMode = require('alamode');
const ALaImport = require("@a-la/import");
let App = require('koa'); if (App && App.__esModule) App = App.default;
const test = require('./test');

Options

The transform accepts a number of options via the .alamoderc.

  • The replacement option is used to substitute the name or path of an imported module.
    {
      "env": {
        "test-build": {
          "import": {
            "replacement": {
              "from": "^((../)+)src",
              "to": "$1build"
            }
          }
        }
      }
    }
  • The esCheck option is used to always enforce the if (mod.__esModule) check — by default, this check is switched off for local imports, but is added when requiring external packages to make it compatible with Babel and TypeScript.
    {
      "env": {
        "test-build": {
          "import": {
            "esCheck": "always",
          }
        }
      }
    }
  • The alamodeModules array contains packages known to be compiled with ÀLAMode, or traditional packages that didn't use Babel for compilation, so that the if (mod.__esModule) is not required.

If esCheck is not set, and alamodeModules does not contain the module that is being imported, the transform will attempt to find its package.json file, and see if it has the alamode property set to true, in which case no esCheck will be appended.

Output Example

The set of rules changes import to require statements. When importing a default export from a module, a check will included to see if it was transpiled with Babel which is indicated by the presence of the __esModule property, and if it was, then the default property is reassigned to the variable.

import aLaMode from 'alamode'
import Koa from "koa"

import { methodA, methodB } from 'alamode'
import { methodC, methodD as aliasD } from 'alamode'
import defaultALaMode, {
  methodE, methodF,
} from 'alamode'

import def, * as tests from './tests'
const aLaMode = require('alamode');
let Koa = require("koa"); if (Koa && Koa.__esModule) Koa = Koa.default;

const { methodA, methodB } = require('alamode');
const { methodC, methodD: aliasD } = require('alamode');
const defaultALaMode = require('alamode'); const {
  methodE, methodF,
} = defaultALaMode;

const tests = def = require('./tests');

Lines Preservation

The transform will attempt to preserve lines as they are for easier generation of source maps by alamode. In future, this might change.

Named Imports

The named imports are only changed to replace as into :, otherwise the destructuring syntax is the same as for imports themselves.

import { test, test2,
  test3 as alias3 }
from 'package'
const { test, test2,
  test3: alias3 }
= require('package');

Named & Default

When there is a default import along with named once, the line numbers will be respected.

import def, {
  test, test2,
  test3 as alias3,
  test4
    as
  alias4,
}
  from
  'package'
let def = require('package'); const {
  test, test2,
  test3: alias3,
  test4
    :
  alias4,
}
  =
  def; if (def && def.__esModule) def = def.default;

Checklist

  • [x] import defaultExport from "module-name"
  • [x] import * as name from "module-name";
  • [x] import { export } from "module-name";
  • [x] import { export as alias } from "module-name";
  • [x] import { export1 , export2 } from "module-name";
  • [x] import { export1 , export2 as alias2 , [...] } from "module-name";
  • [x] import defaultExport, { export [ , [...] ] } from "module-name";
  • [x] import defaultExport, * as name from "module-name";
  • [ ] import "module-name";
  • [ ] var promise = import(module-name);

TODO

  • [ ] Better from 'package' handling when matchers' logic is updated in the restream.

Copyright