Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Commit

Permalink
feat: TrextOptions#absolute
Browse files Browse the repository at this point in the history
  • Loading branch information
unicornware committed Oct 29, 2021
1 parent 763a305 commit 7ee867a
Show file tree
Hide file tree
Showing 20 changed files with 135 additions and 27 deletions.
4 changes: 3 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ module.exports = {
'dirix',
'errno',
'filenames',
'promisify',
'trext',
'trextel'
'trextel',
'tutils'
]
}
]
Expand Down
45 changes: 38 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ imports to use `.mjs` extensions.
```typescript
import type { TrextOptions } from '@flex-development/trext'
import { trext } from '@flex-development/trext'
import { inspect } from 'util'
import { inspect } from 'node:util'

/**
* @file Examples - Basic Usage
Expand All @@ -69,6 +69,37 @@ trext('esm/', TREXT_OPTIONS)

### Advanced Usage

#### Absolute Imports

By default, all absolute imports are ignored.

To transform extensions in absolute imports, set `absolute` to `true` or a
`RegExp` object filter:

```typescript
import type { TrextOptions } from '@flex-development/trext'
import { trext } from '@flex-development/trext'
import { inspect } from 'node:util'

/**
* @file Examples - Absolute Imports
* @module docs/examples/absolute
*/

const TREXT_OPTIONS: TrextOptions<'js', 'mjs'> = {
absolute: /@flex-development/,
from: 'js',
to: 'mjs'
}

trext('esm/', TREXT_OPTIONS)
.then(results => console.info(inspect(results, false, null)))
.catch(error => console.error(inspect(error, false, null)))
```

A regex filter is recommended unless all of your call expressions, exports,
and/or imports use the same file extension.

#### Babel Transform

`trext` implements a [custom Babel plugin][7] to update `export`, `import`, and
Expand All @@ -78,7 +109,7 @@ specify additional transform options using the `babel` property:
```typescript
import type { TrextOptions } from '@flex-development/trext'
import { trext } from '@flex-development/trext'
import { inspect } from 'util'
import { inspect } from 'node:util'

/**
* @file Examples - Babel Transform Options
Expand Down Expand Up @@ -114,7 +145,7 @@ import {
TrextOptions
} from '@flex-development/trext'
import Trextel from '@flex-development/trext/plugins/esm/trextel.plugin'
import { inspect } from 'util'
import { inspect } from 'node:util'

/**
* @file Examples - Dynamic File Extensions
Expand Down Expand Up @@ -177,7 +208,7 @@ specify a custom file extension search `pattern`:
```typescript
import type { TrextOptions } from '@flex-development/trext'
import { trext } from '@flex-development/trext'
import { inspect } from 'util'
import { inspect } from 'node:util'

/**
* @file Examples - Custom File Extension Search Pattern
Expand Down Expand Up @@ -222,7 +253,7 @@ directory index lookup location:
```typescript
import type { TrextOptions } from '@flex-development/trext'
import { trext } from '@flex-development/trext'
import { inspect } from 'util'
import { inspect } from 'node:util'

/**
* @file Examples - Ignoring Directory Indexes
Expand Down Expand Up @@ -250,7 +281,7 @@ specified][11]. Set `mandatory` to `false` to disable transformations for all
```typescript
import type { TrextOptions } from '@flex-development/trext'
import { trext } from '@flex-development/trext'
import { inspect } from 'util'
import { inspect } from 'node:util'

/**
* @file Examples - Disabling Mandatory File Extensions
Expand All @@ -274,7 +305,7 @@ You can also disable transformations by [`TrextNode`][12] type:
```typescript
import type { TrextOptions } from '@flex-development/trext'
import { trext } from '@flex-development/trext'
import { inspect } from 'util'
import { inspect } from 'node:util'

/**
* @file Examples - Disabling Mandatory File Extensions (By Node)
Expand Down
2 changes: 1 addition & 1 deletion __mocks__/glob.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { promisify } from 'util'
import { promisify } from 'node:util'

/**
* @file Node Module Mock - glob
Expand Down
2 changes: 1 addition & 1 deletion __tests__/__fixtures__/stats.fixture.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs from 'fs'
import fs from 'node:fs'
import { FILENAME } from './trext-file-result.fixture'

/**
Expand Down
2 changes: 1 addition & 1 deletion __tests__/__fixtures__/trext-file-result.fixture.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import DEFAULTS from '@trext/config/defaults.config'
import type { TrextFileResult } from '@trext/types'
import fs from 'fs'
import fs from 'node:fs'
import FILENAMES from './filenames.fixture'

/**
Expand Down
18 changes: 18 additions & 0 deletions docs/examples/absolute.example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { TrextOptions } from '@flex-development/trext'
import { trext } from '@flex-development/trext'
import { inspect } from 'node:util'

/**
* @file Examples - Absolute Imports
* @module docs/examples/absolute
*/

const TREXT_OPTIONS: TrextOptions<'js', 'mjs'> = {
absolute: /@flex-development/,
from: 'js',
to: 'mjs'
}

trext('esm/', TREXT_OPTIONS)
.then(results => console.info(inspect(results, false, null)))
.catch(error => console.error(inspect(error, false, null)))
2 changes: 1 addition & 1 deletion docs/examples/babel.example.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { TrextOptions } from '@flex-development/trext'
import { trext } from '@flex-development/trext'
import { inspect } from 'util'
import { inspect } from 'node:util'

/**
* @file Examples - Babel Transform Options
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/basic-usage.example.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { TrextOptions } from '@flex-development/trext'
import { trext } from '@flex-development/trext'
import { inspect } from 'util'
import { inspect } from 'node:util'

/**
* @file Examples - Basic Usage
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/dynamic.example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
TrextOptions
} from '@flex-development/trext'
import Trextel from '@flex-development/trext/plugins/trextel.plugin'
import { inspect } from 'util'
import { inspect } from 'node:util'

/**
* @file Examples - Dynamic File Extensions
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/mandatory-by-node.example.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { TrextOptions } from '@flex-development/trext'
import { trext } from '@flex-development/trext'
import { inspect } from 'util'
import { inspect } from 'node:util'

/**
* @file Examples - Disabling Mandatory File Extensions (By Node)
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/mandatory.example.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { TrextOptions } from '@flex-development/trext'
import { trext } from '@flex-development/trext'
import { inspect } from 'util'
import { inspect } from 'node:util'

/**
* @file Examples - Disabling Mandatory File Extensions
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/pattern.example.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { TrextOptions } from '@flex-development/trext'
import { trext } from '@flex-development/trext'
import { inspect } from 'util'
import { inspect } from 'node:util'

/**
* @file Examples - Custom File Extension Search Pattern
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/src.example.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { TrextOptions } from '@flex-development/trext'
import { trext } from '@flex-development/trext'
import { inspect } from 'util'
import { inspect } from 'node:util'

/**
* @file Examples - Ignoring Directory Indexes
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
"glob": "7.2.0",
"mkdirp": "1.0.4",
"path-type": "5.0.0",
"resolve-cwd": "3.0.0",
"tslib": "2.3.1"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions src/config/defaults.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { TrextDefaults } from '@trext/types'
*/

const DEFAULTS: TrextDefaults = {
absolute: false,
babel: {},
mandatory: true,
pattern: /\..+$/,
Expand Down
11 changes: 11 additions & 0 deletions src/interfaces/trext-options.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ import type { TrextTo } from '@trext/types'
* @template T - New file extension name(s)
*/
interface TrextOptions<F extends string = string, T extends string = string> {
/**
* Transform extensions in absolute imports. To transform a select amount of
* extensions, pass a regex filter. Otherwise, pass `true`.
*
* A regex filter is recommended unless all of your call expressions, exports,
* and/or imports use the same file extension.
*
* @default false
*/
absolute?: RegExp | boolean

/**
* Babel transformation options.
*
Expand Down
28 changes: 26 additions & 2 deletions src/plugins/__tests__/trextel.plugin.functional.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ describe('functional:plugins/Trextel', () => {
expected: { value: pkg.name },
state: { opts: { from: 'js', to: 'mjs' } }
},
{
_arguments: [stringLiteral('@flex-development/tutils/cjs/guards')],
do: 'change extension in absolute require statement if enabled',
expected: { value: '@flex-development/tutils/cjs/guards/index.cjs' },
state: { opts: { absolute: true, from: 'js', to: 'cjs' } }
},
{
_arguments: [callExpression(identifier('getPackageName'), [])],
do: 'not change extension if require is not string literal',
Expand Down Expand Up @@ -115,11 +121,17 @@ describe('functional:plugins/Trextel', () => {
state: { opts: { from: 'js', to: 'mjs' } }
},
{
do: 'not change extension in absolute import',
do: 'not change extension in absolute export',
expected: { value: pkg.name },
source: stringLiteral(pkg.name),
state: { opts: { from: 'cjs', to: 'js' } }
},
{
do: 'change extension in absolute export if enabled',
expected: { value: '@flex-development/log/esm/utils/format.util.mjs' },
source: stringLiteral('@flex-development/log/esm/utils/format.util'),
state: { opts: { absolute: true, from: 'js', to: 'mjs' } }
},
{
do: 'not ignore export all declaration if export is partial dirix',
expected: { value: '../plugins/index.cjs' },
Expand Down Expand Up @@ -183,6 +195,12 @@ describe('functional:plugins/Trextel', () => {
source: stringLiteral(pkg.name),
state: { opts: { from: 'mjs', to: 'js' } }
},
{
do: 'change extension in absolute named export if enabled',
expected: { value: '@flex-development/grease/cjs/main.cjs' },
source: stringLiteral('@flex-development/grease/cjs/main'),
state: { opts: { absolute: true, from: 'js', to: 'cjs' } }
},
{
do: 'not ignore named export declaration if export is partial dirix',
expected: { value: './plugins/index.cjs' },
Expand Down Expand Up @@ -244,7 +262,13 @@ describe('functional:plugins/Trextel', () => {
do: 'not change extension in absolute import',
expected: { value: pkg.name },
source: stringLiteral(pkg.name),
state: { opts: { from: 'js', to: 'cjs' } }
state: { opts: { absolute: /@gar/, from: 'js', to: 'cjs' } }
},
{
do: 'change extension in absolute import if enabled',
expected: { value: '@gar/promisify/index.js' },
source: stringLiteral('@gar/promisify'),
state: { opts: { absolute: true, from: 'cjs', to: 'js' } }
},
{
do: 'not ignore import declaration if import is partial dirix',
Expand Down
28 changes: 22 additions & 6 deletions src/plugins/trextel.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import DEFAULTS from '@trext/config/defaults.config'
import { TrextelState } from '@trext/interfaces'
import { TrextNodePath } from '@trext/types'
import { isDirectorySync as isDirectory } from 'path-type'
import resolve from 'resolve-cwd'

/**
* @file Plugins - Trextel
Expand Down Expand Up @@ -98,11 +99,29 @@ class Trextel<F extends string = string, T extends string = string>
// Do nothing is missing source code
if (!code) return

// Merge options
const options = { ...DEFAULTS, ...state.opts }

// Get user options
const { from, mandatory, src, to } = { ...DEFAULTS, ...state.opts }
const { absolute, from, mandatory, src, to } = options

// Check for absolute import
const absimport = !/^\./.test(code)

// Ignore absolute imports
if (absimport) {
if (absolute === false) return
if (typeof absolute !== 'boolean' && !absolute.test(code)) return
}

// Check for directory entry point
const directory: boolean = ((): boolean => {
if (absimport) return !!resolve.silent(`${code}/index`)
return isDirectory(`${src}/${code.match(/\w.+/)?.[0]}`)
})()

// Ignore directory entry points if mandatory file extensions are disabled
if (isDirectory(`${src}/${code.match(/\w.+/)?.[0]}`)) {
if (directory) {
const { type } = node
const $m = mandatory as Exclude<TrextelState<F, T>, boolean>

Expand All @@ -113,10 +132,7 @@ class Trextel<F extends string = string, T extends string = string>
if ($m['import'] === false && type.startsWith('Import')) return

code = `${code}/index`
}

// Ignore absolute imports
if (!/^\./.test(code)) return
} else if (absimport && resolve.silent(`${code}/package.json`)) return

// Get output extension
let $to = typeof to === 'function' ? to(nodePath) : to
Expand Down
3 changes: 3 additions & 0 deletions src/types/trext-defaults.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import type { TrextOptions } from '@trext/interfaces'
* Default `trext` options.
*/
type TrextDefaults = {
/** {@link TrextOptions#absolute} */
absolute: Extract<NonNullable<TrextOptions['absolute']>, false>

/** {@link TrextOptions#babel} */
babel: Partial<Record<keyof NonNullable<TrextOptions['babel']>, never>>

Expand Down
3 changes: 2 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,7 @@ __metadata:
prettier-plugin-sh: 0.7.1
read-pkg: 7.0.0
replace-in-file: 6.2.0
resolve-cwd: 3.0.0
rimraf: 3.0.2
shelljs: 0.8.4
ts-jest: 27.0.5
Expand Down Expand Up @@ -8556,7 +8557,7 @@ __metadata:
languageName: node
linkType: hard

"resolve-cwd@npm:^3.0.0":
"resolve-cwd@npm:3.0.0, resolve-cwd@npm:^3.0.0":
version: 3.0.0
resolution: "resolve-cwd@npm:3.0.0"
dependencies:
Expand Down

0 comments on commit 7ee867a

Please sign in to comment.