Skip to content

Commit

Permalink
fix: do not transform ignored files.
Browse files Browse the repository at this point in the history
  • Loading branch information
morganney committed Jul 27, 2023
2 parents db6c2c9 + 66cc8ca commit 00bec0c
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 24 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "babel-dual-package",
"version": "1.0.0-rc.4",
"version": "1.0.0-rc.5",
"description": "CLI for building a dual ESM and CJS package with Babel.",
"type": "module",
"main": "dist",
Expand Down
39 changes: 27 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ import {
getEsmPlugins,
getModulePresets,
getRealPathAsFileUrl,
getBabelFileHandling,
addDefaultPresets
} from './util.js'

const babelDualPackage = async (moduleArgs) => {
const ctx = await init(moduleArgs, logError)

if (ctx) {
const { args, babelConfig } = ctx
const { targets, plugins, presets } = babelConfig.options
const { args, babelProjectConfig } = ctx
const { targets, plugins, presets } = babelProjectConfig.options
const outDir = resolve(relative(cwd(), args.values['out-dir']))
const cjsOutDir = join(outDir, args.values['cjs-dir-name'])
const keepFileExtension = args.values['keep-file-extension']
Expand Down Expand Up @@ -105,7 +106,13 @@ const babelDualPackage = async (moduleArgs) => {
}

if (stats.isFile()) {
if (!dmctsRegex.test(posPath) && extensions.includes(extname(posPath))) {
const fileHandling = await getBabelFileHandling(posPath)

if (
!dmctsRegex.test(posPath) &&
extensions.includes(extname(posPath)) &&
fileHandling === 'transpile'
) {
await build(posPath)
numFilesCompiled++
}
Expand All @@ -115,28 +122,36 @@ const babelDualPackage = async (moduleArgs) => {
const files = allFiles.filter(
(file) => fileHasExpectedExt(file) && !dmctsRegex.test(file)
)
const nonCompilable = !copyFiles
const copyable = !copyFiles
? []
: allFiles.filter((file) => !fileHasExpectedExt(file))
: allFiles.filter(
(file) =>
!fileHasExpectedExt(file) &&
!/^(?:\.babelrc|babel\.config)/.test(basename(file))
)

for (const filename of files) {
await build(filename, posPath)
numFilesCompiled++
const fileHandling = await getBabelFileHandling(filename)

if (fileHandling === 'transpile') {
await build(filename, posPath)
numFilesCompiled++
}
}

// Copy any non-compilable files
for (const nonComp of nonCompilable) {
const relativeFn = relative(posPath, nonComp)
for (const copy of copyable) {
const relativeFn = relative(posPath, copy)
const outEsm = join(outDir, relativeFn)

await mkdir(dirname(outEsm), { recursive: true })
await copyFile(nonComp, outEsm)
await copyFile(copy, outEsm)

if (!noCjsDir) {
const outCjs = join(cjsOutDir, relativeFn)

await mkdir(dirname(outCjs), { recursive: true })
await copyFile(nonComp, outCjs)
await copyFile(copy, outCjs)
}
}
}
Expand Down Expand Up @@ -212,7 +227,7 @@ const babelDualPackage = async (moduleArgs) => {

if (tsFilesUpdated) {
logResult(
`Successfully copied and updated ${tsFilesUpdated} typescript declaration file${
`Successfully copied and updated ${tsFilesUpdated} TypeScript declaration file${
tsFilesUpdated === 1 ? '' : 's'
} in ${Math.abs(Math.round(tsUpdateTime - tsStartTime))}ms.`
)
Expand Down
8 changes: 4 additions & 4 deletions src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const init = async (moduleArgs, onError = () => {}) => {
const rootModes = ['root', 'upward', 'upward-optional']
let pkgJson = null
let args = null
let babelConfig = null
let babelProjectConfig = null

try {
const { values, positionals } = parseArgs({
Expand Down Expand Up @@ -128,7 +128,7 @@ const init = async (moduleArgs, onError = () => {}) => {
}

pkgJson = pkgJson.packageJson
babelConfig = await loadPartialConfigAsync({ rootMode: values['root-mode'] })
babelProjectConfig = await loadPartialConfigAsync({ rootMode: values['root-mode'] })
}
} catch (err) {
onError(err.message)
Expand Down Expand Up @@ -164,11 +164,11 @@ const init = async (moduleArgs, onError = () => {}) => {
logHelp(`--help \t\t\t\t Output usage information (this information).`)
}

if (args && pkgJson && babelConfig) {
if (args && pkgJson && babelProjectConfig) {
return {
args,
pkgJson,
babelConfig
babelProjectConfig
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { fileURLToPath, pathToFileURL } from 'node:url'
import { dirname, resolve, extname } from 'node:path'
import { readdir, realpath } from 'node:fs/promises'

import { createConfigItem } from '@babel/core'
import { createConfigItem, loadPartialConfigAsync } from '@babel/core'

const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
Expand Down Expand Up @@ -129,6 +129,11 @@ const getOutExt = (filename, outFileExtension, keepFileExtension, type = 'esm')

return outFileExtension[type]
}
const getBabelFileHandling = async (filename) => {
const conf = await loadPartialConfigAsync({ filename, showIgnoredFiles: true })

return conf.fileHandling
}

export {
logHelp,
Expand All @@ -143,5 +148,6 @@ export {
getOutExt,
getRealPathAsFileUrl,
getModulePresets,
getBabelFileHandling,
addDefaultPresets
}
6 changes: 6 additions & 0 deletions test/__fixtures__/copy/.babelrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"ignore": [
"**/test.js",
"*.mjs"
]
}
1 change: 1 addition & 0 deletions test/__fixtures__/copy/dir/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const test = true
1 change: 1 addition & 0 deletions test/__fixtures__/copy/ignored.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const ignored = true
13 changes: 9 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,11 @@ describe('babel-dual-package', () => {
assert.ok(spy.mock.calls[0].arguments[1].startsWith('Successfully compiled 4 files'))
assert.ok(
spy.mock.calls[1].arguments[1].startsWith(
'Successfully copied and updated 4 typescript declaration files'
'Successfully copied and updated 4 TypeScript declaration files'
)
)
assert.ok(existsSync(resolve(dist, 'cjs/file.d.cts')))
assert.ok(!existsSync(resolve(dist, '.babelrc.json')))
})

it('allows declaration files to have .d.ts extension in cjs dir', async (t) => {
Expand All @@ -229,7 +230,7 @@ describe('babel-dual-package', () => {
assert.ok(spy.mock.calls[0].arguments[1].startsWith('Successfully compiled 4 files'))
assert.ok(
spy.mock.calls[1].arguments[1].startsWith(
'Successfully copied and updated 4 typescript declaration files'
'Successfully copied and updated 4 TypeScript declaration files'
)
)
assert.ok(existsSync(resolve(dist, 'cjs/file.cjs.d.ts')))
Expand Down Expand Up @@ -257,13 +258,13 @@ describe('babel-dual-package', () => {

assert.ok(
spy.mock.calls[1].arguments[1].startsWith(
'Successfully copied and updated 1 typescript declaration file'
'Successfully copied and updated 1 TypeScript declaration file'
)
)
assert.ok(existsSync(resolve(dist, 'cjs/file.d.ts')))
})

it('can copy for non-compilable files', async (t) => {
it('copies all non-compilable files when using --copy-files', async (t) => {
const { babelDualPackage } = await import('../src/index.js')
const spy = t.mock.method(global.console, 'log')

Expand All @@ -273,5 +274,9 @@ describe('babel-dual-package', () => {
assert.ok(spy.mock.calls[0].arguments[1].startsWith('Successfully compiled 1 file'))
assert.ok(existsSync(resolve(dist, 'file.html')))
assert.ok(existsSync(resolve(dist, 'dir', 'file.json')))
assert.ok(!existsSync(resolve(dist, 'dir', 'test.js')))
assert.ok(!existsSync(resolve(dist, 'cjs', 'dir', 'test.js')))
assert.ok(!existsSync(resolve(dist, '.babelrc.json')))
assert.ok(!existsSync(resolve(dist, 'ignored.mjs')))
})
})

0 comments on commit 00bec0c

Please sign in to comment.