@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/importThe ÀLaImport is the default export and an array containing a sequence of rules for Replaceable.
importÀLaImportfrom'@a-la/import'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ÀLaContextfrom'@a-la/context'importÀLaImportfrom'@a-la/import'constSTRING=`import aLaMode from 'alamode'import ALaImport from "@a-la/import"import App from 'koa'import test from './test'`;(async()=>{constcontext=newÀLaContext(__filename)context.setConfig({import: {alamodeModules: ['alamode','@a-la/import'],},})const{ result }=awaitcontext.stream(ÀLaImport,STRING)console.log(result)})()constaLaMode=require('alamode');constALaImport=require("@a-la/import");letApp=require('koa');if(App&&App.__esModule)App=App.default;consttest=require('./test');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.
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.
importaLaModefrom'alamode'importKoafrom"koa"import{methodA,methodB}from'alamode'import{methodC,methodDasaliasD}from'alamode'importdefaultALaMode,{methodE,methodF,}from'alamode'importdef,*astestsfrom'./tests'constaLaMode=require('alamode');letKoa=require("koa");if(Koa&&Koa.__esModule)Koa=Koa.default;const{ methodA, methodB }=require('alamode');const{ methodC,methodD: aliasD}=require('alamode');constdefaultALaMode=require('alamode');const{
methodE, methodF,}=defaultALaMode;consttests=def=require('./tests');The transform will attempt to preserve lines as they are for easier generation of source maps by alamode. In future, this might change.
The named imports are only changed to replace as into :, otherwise the destructuring syntax is the same as for imports themselves.
import{test,test2,test3asalias3}from'package'const{ test, test2,test3: alias3}=require('package');When there is a default import along with named once, the line numbers will be respected.
importdef,{test,test2,test3asalias3,test4asalias4,}from'package'letdef=require('package');const{
test, test2,test3: alias3,test4
:
alias4,}=def;if(def&&def.__esModule)def=def.default;import defaultExport from "module-name"import * as name from "module-name";import { export } from "module-name";import { export as alias } from "module-name";import { export1 , export2 } from "module-name";import { export1 , export2 as alias2 , [...] } from "module-name";import defaultExport, { export [ , [...] ] } from "module-name";import defaultExport, * as name from "module-name";import "module-name";var promise = import(module-name);
- Better
from 'package'handling when matchers' logic is updated in therestream.
![]() | © Art Deco™ for À La Mode 2020 |
|---|
