@a-la/import
![npm version](https://badge.fury.io/js/%40a-la%2Fimport.svg)
@a-la/import
is a a set of rules for alamode
to transpile an import
statement into require
in Node.js.
À La Mode is a RegExp-based transpiler which works faster than AST-based transpilers such as @babel
, and occupies only 100KB of disk space.
yarn add -E @a-la/import
Table Of Contents
API
The ALaImport is the default export and an array containing a sequence of rules for Replaceable.
import ALaImport from '@a-la/import'
ALaImport
Sequence
The rule set exported as an array by ALaImport
has multiple regexes and replacer functions to match all possible cases.
import { Replaceable } from 'restream'
import ALaImport from '@a-la/import'
const STRING = `import aLaMode from 'alamode'
import ALaImport from "@a-la/import"
import App from 'koa'
`
;(async () => {
const stream = new Replaceable([
...ALaImport,
])
await new Promise((r, j) => {
stream.end(STRING, r)
stream.on('error', j)
})
stream.pipe(process.stdout)
})()
const aLaMode = require('alamode')
const ALaImport = require("@a-la/import")
const App = require('koa')
Rules
The replacement sequence consists of a number of rules, which produce the most reliable result when put together. See Rule Type
in documentary for more info about how rules work.
Default
Rule
Allows to import the default export.
import helloWorld from 'hello-world'
Click to Show Details.
|
Example |
import examplePackage from 'examplePackage'
|
|
RegExp |
/^\s*import ([\w\d]+) from (["'])(.+?)\2/gm
|
|
Rule |
const ImportDefaultRule = {
re: /^\s*import ([\w\d]+) from (["'])(.+?)\2/gm,
replacement(match, name, src) {
const s = `const ${name} = require('${src}')`
return s
},
}
|
|
Output |
const examplePackage = require('examplePackage')
|
Copyright
(c) A La Mode 2018