@a-la/import 中文文档教程

发布于 5年前 浏览 25 项目主页 更新于 3年前

@a-la/import

npm version

@a-la/importalamode< 的一组规则/code>import 语句转换为 Node.JS 中的 require 调用。

ÀLaMode 是一个基于 RegExp 的转译器,比基于 AST 的转译器(例如 @babel)运行速度更快,没有依赖性并且占用更少的磁盘空间。

yarn add @a-la/import

Table Of Contents

API

ÀLaImport 是默认导出和包含 可更换

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

ÀLaImport Sequence

ÀLaImport 导出为数组的规则集有多个正则表达式和替换函数来匹配所有可能的情况。 替换函数期望在上下文中看到 markers 属性,该属性由 alamode 设置以访问剪切的字符串。 转换可以使用 @a-la/context 运行,它是 alamode 的轻量级版本,模仿其流功能。

/* 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

转换通过 .alamoderc 接受许多选项。

  • The replacement option is used to substitute the name or path of an imported module. json5 { "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. json5 { "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.

如果未设置 esCheck,并且 alamodeModules 不包含正在导入的模块,转换将尝试查找其 package.json 文件,并查看它是否将 alamode 属性设置为 true,在这种情况下,不会附加 esCheck

Output Example

规则集将 import 更改为 require 语句。 从模块导入默认导出时,将检查它是否使用 Babel 进行了转译,这由 __esModule 属性的存在指示,如果它是,然后将 default 属性重新分配给变量。

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

转换将尝试保留行,因为它们更容易通过 alamode 生成源映射。 将来,这可能会改变。

Named Imports

命名导入仅更改为将 as 替换为 :,否则解构语法与导入本身相同。

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

Named & Default

当默认导入和命名一次时,行号将被考虑。

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.
Art Deco© Art Deco™ for À La Mode 2020

@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. json5 { "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. json5 { "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.
Art Deco© Art Deco™ for À La Mode 2020

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文