Skip to content

Commit

Permalink
refactor: Replace Babel strategy with Rollup strategy.
Browse files Browse the repository at this point in the history
  • Loading branch information
darkobits committed Jul 10, 2023
1 parent 512e5fa commit f9ca579
Show file tree
Hide file tree
Showing 11 changed files with 790 additions and 371 deletions.
833 changes: 671 additions & 162 deletions package-lock.json

Large diffs are not rendered by default.

9 changes: 2 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,24 @@
"prepare": "nr prepare"
},
"dependencies": {
"@babel/core": "^7.22.8",
"@babel/preset-env": "^7.22.7",
"@babel/preset-typescript": "^7.22.5",
"@babel/register": "^7.22.5",
"@darkobits/log": "^2.0.0-beta.15",
"@darkobits/valida": "^0.1.6",
"@esbuild-plugins/tsconfig-paths": "^0.1.2",
"@rollup/plugin-typescript": "^11.1.2",
"@types/yargs": "^17.0.24",
"babel-plugin-module-resolver-tsconfig": "^1.0.25",
"camelcase-keys": "^8.0.2",
"cosmiconfig": "^8.2.0",
"deepmerge": "^4.3.1",
"esbuild": "^0.18.11",
"fs-extra": "^11.1.1",
"node-version": "^3.0.0",
"read-pkg-up": "^10.0.0",
"resolve-pkg": "^2.0.0",
"rollup": "^3.26.2",
"tsconfck": "^2.1.1",
"yargs": "^17.7.2"
},
"devDependencies": {
"@darkobits/ts": "~0.18.4",
"@types/babel__core": "^7.20.1",
"@types/fs-extra": "^11.0.1"
}
}
61 changes: 34 additions & 27 deletions src/lib/configuration/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import { cosmiconfig, defaultLoaders } from 'cosmiconfig';
import merge from 'deepmerge';

import validators from 'etc/validators';
import { babelStrategy } from 'lib/configuration/strategies/babel';
import { babelRegisterStrategy } from 'lib/configuration/strategies/babel-register';
import { esbuildStrategy } from 'lib/configuration/strategies/esbuild';
import { rollupStrategy } from 'lib/configuration/strategies/rollup';
import log from 'lib/log';
import { getPackageInfo } from 'lib/package';

Expand Down Expand Up @@ -38,22 +37,25 @@ function getDefaultExport(module: any) {
* ESM or CJS, for the consumers of those applications to be written in ESM or
* CJS, and with configuration files written in ESM or CJS.
*/
async function ecmaScriptLoader(filePath: string /* , content: string */) {
async function ecmaScriptLoader(filePath: string /* , contents: string */) {
const prefix = log.prefix('config');
const errors: Array<Error> = [];

log.verbose(prefix, `Loading file: ${log.chalk.green(filePath)}`);

const pkgInfo = getPackageInfo({ cwd: path.dirname(filePath) });
if (!pkgInfo?.root) throw new Error(`${prefix} Unable to compute host package root directory.`);

/**
* Tracks errors produced by various strategies. If all strategies fail, an
* AggregateError will be thrown with this value.
*/
const errors: Array<Error> = [];


/**
* Strategy 1: Dynamic Import
*
* This will not perform any transpilation on code, and if the host project
* uses any custom path mappings, they will not work here. However, this
* strategy is the simplest and fastest, so we always try it first.
* uses any language features that require transpilation, this strategy will
* fail. However, this strategy is the simplest and fastest, so we always try
* it first.
*/
try {
const result = await import(filePath);
Expand All @@ -65,41 +67,46 @@ async function ecmaScriptLoader(filePath: string /* , content: string */) {


/**
* Strategy 2: esbuild
* Gather information about the package containing `filePath`, required for
* subsequent strategies.
*/
try {
const result = await esbuildStrategy(filePath, pkgInfo);
log.verbose(prefix, 'Used strategy:', log.chalk.bold('esbuild'));
return getDefaultExport(result);
} catch (err: any) {
errors.push(new Error(`${prefix} Failed to load file with ${log.chalk.bold('esbuild')}: ${err}`));
}
const pkgInfo = getPackageInfo({ cwd: path.dirname(filePath) });
if (!pkgInfo?.root) throw new Error(`${prefix} Unable to compute host package root directory.`);


/**
* Strategy 3: babel
* Strategy 2: esbuild
*
* This strategy will work for files that:
* - import nothing
* - only import other code that does not require a transpilation step
* - do not use any custom path mappings
*
* It is faster than Rollup and should cover the vast majority of cases where
* a simple dynamic import will not work.
*/
try {
const result = await babelStrategy(filePath, pkgInfo);
log.verbose(prefix, 'Used strategy:', log.chalk.bold('babel'));
const result = await esbuildStrategy(filePath, pkgInfo);
log.verbose(prefix, 'Used strategy:', log.chalk.bold('esbuild'));
return getDefaultExport(result);
} catch (err: any) {
errors.push(new Error(`${prefix} Failed to load file with ${log.chalk.bold('babel')}: ${err}`));
errors.push(new Error(`${prefix} Failed to load file with ${log.chalk.bold('esbuild')}: ${err}`));
}


/**
* Strategy 3: babel/register
* Strategy 3: Rollup
*
* This strategy will (likely) only need to be used when a configuration file
* relies on other files that also need to be transpiled.
* This is the slowest strategy, but the most robust. It will inline and
* transpile any code that the configuration file imports, allowing the
* output file to be imported as a standalone bundle.
*/
try {
const result = await babelRegisterStrategy(filePath, pkgInfo);
log.verbose(prefix, 'Used strategy:', log.chalk.bold('babel/register'));
const result = await rollupStrategy(filePath, pkgInfo);
log.verbose(prefix, 'Used strategy:', log.chalk.bold('rollup'));
return getDefaultExport(result);
} catch (err: any) {
errors.push(new Error(`${prefix} Failed to load file with ${log.chalk.bold('babel/register')}: ${err}`));
errors.push(new Error(`${prefix} Failed to load file with ${log.chalk.bold('rollup')}: ${err}`));
}


Expand Down
125 changes: 0 additions & 125 deletions src/lib/configuration/strategies/babel-register.ts

This file was deleted.

0 comments on commit f9ca579

Please sign in to comment.