-
Notifications
You must be signed in to change notification settings - Fork 1
Import
Changes all import
statements into require
statements. Although the specification between the ECMAScript Modules and Modules is different, most developers will prefer to use import
just because of its neater syntax.
import argufy from 'argufy'
import restream, {
Replaceable,
makeMarkers, makeCutRule, makePasteRule,
} from 'restream'
import { resolve, join } from 'path'
import { version } from '../../package.json'
import erte, { c } from './erte'
const argufy = require('argufy');
let restream = require('restream'); const {
Replaceable,
makeMarkers, makeCutRule, makePasteRule,
} = restream; if (restream && restream.__esModule) restream = restream.default;
const { resolve, join } = require('path');
const { version } = require('../../package.json');
const erte = require('./erte'); const { c } = erte;
The options that can be set in the .alamoderc.json
are described below.
The if (dependency && dependency.__esModule) dependency = dependency.default;
check is there to make alamode
compatible with Babel and TypeScript that export default modules as the default
property of module.exports
object and set the __esModule
marker to true, e.g.,
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = method;
The check will only be added for external modules, and the Node.JS core dependencies as well as those that start with .
or /
will be required without the __esModule
check. To enforce the check for any file, the esCheck: always
should be set in the transform configuration. To disable the check for specific modules, they are added to the alamodeModules
directive.
{
"import": {
"esCheck": "always", // adds the check for each default import
"alamodeModules": ["restream"] // disables the check for packages
}
}
If neither esCheck
nor alamodeModules
are present, ÀLaMode will look up the package.json of the module and see if it includes the "alamode": true
property, and won't add the check if it does.
This transform supports an option to replace the path to the required file using a regular expression. This can be useful when running tests against the build directory, rather than source directory.
{
"import": {
"replacement": {
"from": "^((../)+)src",
"to": "$1build"
}
}
}
}
Source | Replaced Source |
---|---|
import myPackage from '../src'
(async () => {
await myPackage()
})() |
const myPackage = require('../build');
(async () => {
await myPackage()
})() |
This is how we have set up all our Zoroaster tests which natively use ÀLaMode: during the development, it's always good to require the source code, however just before the release, the test-build
command is run. If the package publishes build/index.js
as its main
field of package.json, it's even possible to specify the config just to point to the root of the project directory.
If the package publishes a single file compiled with Depack, whereas test will import library methods for unit tests also from src
dir, the $
must be added.
{
"env": {
"test-build": {
"import": {
"replacement": {
"from": "^((../)+)src$",
"to": "$1"
}
}
}
},
}
See @Depack/cache for an example of Depack-compiled project.
With the advent of Depack, the Node.JS source code compiler based off Google Closure Compiler, it became possible to assemble all internal and external dependencies into a single file. However, for bigger and more complex projects it might not be necessary to compile the actual source code, but rather create a packed library of all dependencies that it uses, and update the source code to point to it after build. The dev environment stays the same and is left intact for testing.
For example, Documentary defines the library in the following way:
import clearr from 'clearr'
import erte, { c, b } from 'erte'
import forkfeed from 'forkfeed'
import makepromise from 'makepromise'
// ...
module.exports = {
'clearr': clearr,
'c': c,
'b': b,
'erte': erte,
'forkfeed': forkfeed,
'makepromise': makepromise,
// ...
}
With the stdlib
option, ÀLaMode will source all dependencies from a single file. This means that all source files must name default imports consistently with the standard library that was compiled for the project. Read more about compilation on Depack's page.
Given the following rc file:
{
"env": {
"stdlib-wiki": {
"import": {
"stdlib": {
"path": "stdlib.js",
"packages": ["rexml", "example", "import"]
}
}
}
}
}
ÀLaMode will repoint all imports to the library file:
Source | Output |
---|---|
import defaultFn,
{ named } from 'rexml'
import { namedExample } from 'example'
import importFn from 'import'
import { nonStandard } from 'external' |
const
{ defaultFn, named } = require('../stdlib');
const { namedExample } = require('../stdlib');
const { importFn } = require('../stdlib');
const { nonStandard } = require('external'); |
The transform will preserve line numbers.