Skip to content

Commit

Permalink
feat: --no-comments. (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
morganney committed Aug 11, 2023
2 parents db998e4 + 2cd8b93 commit d80177c
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 21 deletions.
1 change: 1 addition & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ jobs:
uses: JS-DevTools/[email protected]
with:
token: ${{ secrets.NPM_AUTH_TOKEN }}
tag: ${{ contains(github.ref, '-') && 'next' || 'latest' }}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ Options:
--out-file-extension [extmap] Use a specific extension for esm/cjs files. [esm:.js,cjs:.cjs]
--keep-file-extension Preserve the file extensions of the input files.
--no-cjs-dir Do not create a subdirectory for the CJS build in --out-dir.
--no-comments Remove comments from generated code.
--source-maps Generate an external source map.
--minified Save as many bytes when printing (false by default).
--copy-files When compiling a directory copy over non-compilable files.
Expand Down
1 change: 1 addition & 0 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ await import(
JSON.stringify({
'--out-dir': 'dist',
'--no-cjs-dir': '',
'--no-comments': '',
files: ['src/*.js']
})
)}`
Expand Down
6 changes: 6 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ export default [
ignores: ['**/build.js'],
rules: {
'no-console': 'error',
'no-unused-vars': [
'error',
{
ignoreRestSiblings: true
}
],
'n/shebang': [
'error',
{
Expand Down
11 changes: 5 additions & 6 deletions package-lock.json

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

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "babel-dual-package",
"version": "1.0.0",
"version": "1.1.0",
"description": "CLI for building a dual ESM and CJS package with Babel.",
"type": "module",
"main": "dist",
Expand Down Expand Up @@ -30,7 +30,6 @@
"license": "MIT",
"dependencies": {
"@babel/core": "^7.22.8",
"@babel/parser": "^7.22.7",
"@babel/preset-env": "^7.22.7",
"@babel/preset-react": "^7.22.5",
"@babel/preset-typescript": "^7.22.5",
Expand Down
18 changes: 15 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,25 @@ const babelDualPackage = async (moduleArgs) => {

if (ctx) {
const { args, babelProjectConfig } = ctx
const { targets, plugins, presets } = babelProjectConfig.options
const {
targets,
plugins,
presets,
babelrc: unused,
...options
} = 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']
const outFileExtension = args.values['out-file-extension']
const noCjsDir = args.values['no-cjs-dir']
const sourceMaps = args.values['source-maps']
const minified = args.values.minified || (options.minified ?? false)
const noComments =
args.values['no-comments'] ||
(Object.hasOwn(options, 'comments') ? !options.comments : false)
const sourceMaps = args.values['source-maps'] || (options.sourceMaps ?? false)
const copyFiles = args.values['copy-files']
const { minified, extensions } = args.values
const { extensions } = args.values

if (!presets.length) {
addDefaultPresets(presets, extensions)
Expand All @@ -52,6 +62,7 @@ const babelDualPackage = async (moduleArgs) => {
const startTime = performance.now()
const build = async (filename, positional) => {
const { code, map } = await transform(filename, {
...options,
targets,
presets,
plugins,
Expand All @@ -60,6 +71,7 @@ const babelDualPackage = async (moduleArgs) => {
sourceMaps,
ast: false,
sourceType: 'module',
comments: !noComments,
// Custom options
esmPresets,
esmPlugins,
Expand Down
5 changes: 5 additions & 0 deletions src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ const init = async (moduleArgs, onError = () => {}) => {
type: 'boolean',
default: false
},
'no-comments': {
type: 'boolean',
default: false
},
'keep-file-extension': {
type: 'boolean',
default: false
Expand Down Expand Up @@ -156,6 +160,7 @@ const init = async (moduleArgs, onError = () => {}) => {
logHelp(
'--no-cjs-dir \t\t\t Do not create a subdirectory for the CJS build in --out-dir.'
)
logHelp('--no-comments \t\t\t Remove comments from generated code.')
logHelp('--source-maps \t\t\t Generate an external source map.')
logHelp('--minified \t\t\t Save as many bytes when printing (false by default).')
logHelp(
Expand Down
26 changes: 16 additions & 10 deletions src/transform.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { readFile } from 'node:fs/promises'

import { parse } from '@babel/parser'
import { transformAsync } from '@babel/core'
import { transformAsync, parseAsync } from '@babel/core'
import babelTraverse from '@babel/traverse'
import MagicString from 'magic-string'

Expand All @@ -17,16 +16,19 @@ const transform = async (filename, opts) => {
esmPresets,
esmPlugins,
cjsPresets,
comments,
outFileExtension,
keepFileExtension,
...rest
} = opts
const baseOpts = { ...rest, configFile: false }
const baseOpts = { ...rest, comments, configFile: false }
const specifierOpts = { esm, cjs, outFileExtension, source }
const ast = parse(source, {
sourceType: 'module',
allowAwaitOutsideFunction: true,
plugins: ['typescript', 'jsx']
const ast = await parseAsync(source, {
parserOpts: {
sourceType: 'module',
allowAwaitOutsideFunction: true,
plugins: ['typescript', 'jsx']
}
})

if (!keepFileExtension) {
Expand All @@ -47,11 +49,13 @@ const transform = async (filename, opts) => {

const { code: esmCode, map: esmMap } = await transformAsync(esm.toString(), {
...baseOpts,
generatorOpts: { comments },
plugins: esmPlugins,
presets: esmPresets
})
const { code: cjsCode, map: cjsMap } = await transformAsync(cjs.toString(), {
...baseOpts,
generatorOpts: { comments },
presets: isEsModuleFile(filename) ? esmPresets : cjsPresets
})

Expand All @@ -65,9 +69,11 @@ const updateDtsSpecifiers = async (filename, outFileExtension) => {
const source = (await readFile(filename)).toString()
const esm = new MagicString(source)
const cjs = new MagicString(source)
const ast = parse(source, {
sourceType: 'module',
plugins: [['typescript', { dts: true }]]
const ast = await parseAsync(source, {
parserOpts: {
sourceType: 'module',
plugins: [['typescript', { dts: true }]]
}
})
const opts = { esm, cjs, outFileExtension }

Expand Down
7 changes: 7 additions & 0 deletions test/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { existsSync } from 'node:fs'
import { rm, mkdir, cp, rename } from 'node:fs/promises'
import { fileURLToPath } from 'node:url'
import { dirname, resolve } from 'node:path'
import { execSync } from 'node:child_process'

/* eslint-disable no-undef */
const __filename = fileURLToPath(import.meta.url)
Expand Down Expand Up @@ -279,4 +280,10 @@ describe('babel-dual-package', () => {
assert.ok(!existsSync(resolve(dist, '.babelrc.json')))
assert.ok(!existsSync(resolve(dist, 'ignored.mjs')))
})

it('works as a cli script', async () => {
const resp = execSync('./src/index.js --help', { cwd: resolve(__dirname, '..') })

assert.ok(resp.toString().indexOf('Options:') > -1)
})
})

0 comments on commit d80177c

Please sign in to comment.