Skip to content

Commit

Permalink
added: babel-plugins to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
binjospookie committed Dec 29, 2023
1 parent 43c377b commit 688d10c
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 18 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ Allows to override the config values for package.
}
```

### `--babel-plugins, -p`

```diff
"scripts": {
"build": "webpack ./webpack.config.js",
- "check-exports": "pure-index",
+ "check-exports": "pure-index --babel-plugins typescript,classPrivateProperties",
"test": "vitest"
}
```

### `--batch, -b`

```diff
Expand Down
6 changes: 4 additions & 2 deletions bin/__tests__/getConfig/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ test('default value', async () => {
entry: 'src/index.tsx',
extensions: 'js,jsx',
collectUsages: 'package-a',
batch: 1
batch: 1,
babelPlugins: 'decorators-legacy,classPrivateProperties'
}
}))
}))
Expand All @@ -22,7 +23,8 @@ test('default value', async () => {
entry: 'src/index.tsx',
extensions: ['js', 'jsx'],
collectUsages: 'package-a',
batch: 1
batch: 1,
babelPlugins: ['decorators-legacy', 'classPrivateProperties']
})

vi.resetAllMocks()
Expand Down
5 changes: 3 additions & 2 deletions bin/__tests__/getConfig/config+cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ test('default value', async () => {
entry: 'src/main.js',
extensions: 'js,jsx,ts,tsx',
collectUsages: 'package-a',
batch: 1
batch: 1,
babelPlugins: 'decorators-legacy,classPrivateProperties'
}
}))
}))
Expand All @@ -33,7 +34,7 @@ test('default value', async () => {

expect(config).toStrictEqual({
...CONFIG,
babelPlugins: new Set([...CONFIG.babelPlugins, 'jsx']),
babelPlugins: ['decorators-legacy', 'classPrivateProperties'],
batch: 1,
collectUsages: 'package-a',
entry: 'src/main.js',
Expand Down
2 changes: 1 addition & 1 deletion bin/__tests__/getConfig/constants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const CONFIG = {
babelPlugins: new Set(['typescript']),
babelPlugins: ['typescript'],
batch: 100,
collectUsages: null,
entry: 'index.ts',
Expand Down
2 changes: 1 addition & 1 deletion bin/__tests__/getConfig/withconfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test('default value', async () => {

expect(config).toStrictEqual({
...CONFIG,
babelPlugins: new Set([...CONFIG.babelPlugins, 'jsx']),
babelPlugins: ['jsx'],
batch: 500,
entry: 'src/index.ts',
extensions: ['js', 'jsx'],
Expand Down
2 changes: 1 addition & 1 deletion bin/collectUsages.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createStatusAPI } from './utils/index.js'
/**
* @param {{
* config: {
* babelPlugins: Set<string>
* babelPlugins: Array<string>
* batch: number
* collectUsages: string
* exclude: Set<string>
Expand Down
2 changes: 1 addition & 1 deletion bin/fileTraversal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getFiles } from './getFiles.js'
* @param {{
* cmd: {function(_: string): void}
* config: {
* babelPlugins: Set<string>
* babelPlugins: Array<string>
* batch: number
* exclude: Set<string>
* extensions: Array<string>
Expand Down
2 changes: 1 addition & 1 deletion bin/fileTraversal/processBatch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { traversal } from './traversal.js'
* files: Array<string>
* cmd: {function(_: string): void}
* config: {
* babelPlugins: Set<string>
* babelPlugins: Array<string>
* }
* pkg: {
* name: string
Expand Down
4 changes: 2 additions & 2 deletions bin/fileTraversal/processBatch/traversal.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { readFile } from '../../utils/index.js'
* file: string
* cmd: {function(_: string): void}
* config: {
* babelPlugins: Set<string>
* babelPlugins: Array<string>
* }
* pkg: {
* name: string
Expand All @@ -19,7 +19,7 @@ const traversal = async ({ file, pkg, config, cmd }) => {

const ast = parse(code, {
sourceType: 'module',
plugins: [...config.babelPlugins]
plugins: config.babelPlugins
})

for (const node of ast.program.body) {
Expand Down
11 changes: 7 additions & 4 deletions bin/getConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { lilconfig } from 'lilconfig'
import meow from 'meow'

const BASE_CONFIG = {
babelPlugins: new Set(['typescript']),
babelPlugins: ['typescript'],
batch: 100,
collectUsages: null,
entry: 'index.ts',
Expand All @@ -12,12 +12,12 @@ const BASE_CONFIG = {
}

// todo: add exclude
// todo: add babelPlugins
const cli = meow(
`
Options
--entry, -e path to the package index file. relative to the package directory
--extensions, -x list of file extensions to be considered during the search
--babel-plugins, -p list of babel plugins that will be used when parsing files
--batch, -b number of files to be traversed in parallel
--collect-usages, -u outputs a list of all unique uses of the package
`,
Expand All @@ -28,6 +28,7 @@ const cli = meow(
flags: {
entry: { type: 'string', shortFlag: 'e' },
extensions: { type: 'string', shortFlag: 'x' },
babelPlugins: { type: 'string', shortFlag: 'p' },
batch: { type: 'number', shortFlag: 'b' },
collectUsages: { type: 'string', shortFlag: 'u' }
}
Expand All @@ -46,7 +47,7 @@ const getConfig = async () => {

const {
exclude = [],
babelPlugins = [],
babelPlugins = BASE_CONFIG.babelPlugins,
entry = BASE_CONFIG.entry,
batch = BASE_CONFIG.batch,
extensions = BASE_CONFIG.extensions
Expand All @@ -57,7 +58,9 @@ const getConfig = async () => {
: {
entry: cli.flags.entry || entry,
exclude: new Set([...BASE_CONFIG.exclude, ...exclude]),
babelPlugins: new Set([...BASE_CONFIG.babelPlugins, ...babelPlugins]),
babelPlugins: cli.flags.babelPlugins
? cli.flags.babelPlugins.split(',')
: babelPlugins,
batch: cli.flags.batch || batch,
collectUsages: cli.flags.collectUsages || BASE_CONFIG.collectUsages,
extensions: cli.flags.extensions
Expand Down
4 changes: 2 additions & 2 deletions bin/getExports.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { readFile, ObservableSet } from './utils/index.js'
/**
* @param {{
* config: {
* babelPlugins: Set<string>
* babelPlugins: Array<string>
* entry: string
* }
* pkg: {
Expand All @@ -22,7 +22,7 @@ const getExports = async ({ config, pkg }) => {

const ast = parser.parse(code, {
sourceType: 'module',
plugins: [...config.babelPlugins]
plugins: config.babelPlugins
})

for (const node of ast.program.body) {
Expand Down
2 changes: 1 addition & 1 deletion bin/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { createStatusAPI, readJSON } from './utils/index.js'
/**
* @param {{
* config: {
* babelPlugins: Set<string>
* babelPlugins: Array<string>
* batch: number
* entry: string
* exclude: Set<string>
Expand Down

0 comments on commit 688d10c

Please sign in to comment.