Skip to content
This repository has been archived by the owner on Mar 28, 2023. It is now read-only.

Commit

Permalink
Add(Pack): --format-outdirs
Browse files Browse the repository at this point in the history
  • Loading branch information
1aron committed Mar 8, 2023
1 parent 2af9932 commit 0a6f110
Show file tree
Hide file tree
Showing 16 changed files with 73 additions and 63 deletions.
22 changes: 11 additions & 11 deletions packages/aronrepo/README.md
Expand Up @@ -142,16 +142,16 @@ Simultaneously output `cjs`, `esm`, `iife`, `type declarations` respectively acc
"build": "aron pack",
"dev": "npm run build -- --watch"
},
"main": "dist/index.cjs",
"main": "dist/cjs/index.js",
"browser": "dist/index.browser.js",
"module": "dist/index.mjs",
"module": "dist/esm/index.js",
"types": "dist/index.d.ts",
"jsnext:main": "dist/index.mjs",
"esnext": "dist/index.mjs",
"jsnext:main": "dist/esm/index.js",
"esnext": "dist/esm/index.js",
"exports": {
".": {
"require": "./dist/index.cjs",
"import": "./dist/index.mjs",
"require": "./dist/cjs/index.js",
"import": "./dist/esm/index.js",
"types": "./dist/index.d.ts"
}
},
Expand Down Expand Up @@ -284,10 +284,10 @@ import '@master/style-element.react'
```json
{
"name": "externals",
"main": "dist/index.cjs",
"main": "dist/cjs/index.js",
"exports": {
".": {
"require": "./dist/index.cjs"
"require": "./dist/cjs/index.js"
}
},
"files": [
Expand All @@ -313,7 +313,7 @@ aron pack --platform node

<img width="568" alt="exclude-externals-pack" src="https://user-images.githubusercontent.com/33840671/204489494-10854837-be15-49fd-a1c8-0e02fb3e174a.png">

`@master/css.webpack` is bundled into `dist/index.cjs`, except for `@master/css` and `@master/style-element.react`.
`@master/css.webpack` is bundled into `dist/cjs/index.js`, except for `@master/css` and `@master/style-element.react`.

So if there is an external package that needs to be bundled, you just install it to `devDependencies` via `npm i <some-package> --save-dev`, then `aron pack` will not exclude it.

Expand Down Expand Up @@ -344,8 +344,8 @@ So if there is an external package that needs to be bundled, you just install it
"name": "externals",
"exports": {
".": {
"require": "./dist/index.cjs",
"import": "./dist/index.mjs"
"require": "./dist/cjs/index.js",
"import": "./dist/esm/index.js"
},
"./utils/exec": {
"require": "./dist/utils/exec.cjs",
Expand Down
19 changes: 14 additions & 5 deletions packages/aronrepo/src/commands/pack.ts
Expand Up @@ -32,6 +32,7 @@ peerDependencies && externalDependencies.push(...Object.keys(peerDependencies))

program.command('pack [entryPaths...]')
.option('-f, --format [formats...]', 'The output format for the generated JavaScript files `iife`, `cjs`, `esm`', ['cjs', 'esm'])
.option('-f, --format-outdirs [outdirs...]', 'The output file creates a corresponding directory according to its format', ['cjs', 'esm'])
.option('-w, --watch', 'Rebuild whenever a file changes', false)
.option('-s, --sourcemap', 'Emit a source map', process.env.NODE_ENV === 'production')
.option('-p, --platform <node,browser,neutral>', 'Platform target', 'browser')
Expand All @@ -42,11 +43,10 @@ program.command('pack [entryPaths...]')
.option('-kn, --keep-names', 'Keep JavaScript function/class names', false)
.option('--cjs-ext', 'Specify CommonJS default file extension', '.js')
.option('--iife-ext', 'Specify CommonJS default file extension', '.js')
.option('--esm-ext', 'Specify CommonJS default file extension', '.mjs')
.option('--esm-ext', 'Specify CommonJS default file extension', '.js')
.option('--srcdir <dir>', 'The source directory', 'src')
.option('--target', 'This sets the target environment for the generated JavaScript and/or CSS code.', 'esnext')
.option('--mangle-props', 'Pass a regular expression to esbuild to tell esbuild to automatically rename all properties that match this regular expression', '^_')
.option('--resolve-extensions', 'The resolution algorithm used by node supports implicit file extensions', ['.tsx', '.ts', '.jsx', '.js', '.mjs', '.cjs', '.css', '.json'])
.option('--no-bundle', 'OFF: Inline any imported dependencies into the file itself', true)
.option('--no-minify', 'OFF: Minify the generated code')
.option('--no-clean', 'OFF: Clean up the previous output directory before the build starts')
Expand All @@ -60,7 +60,13 @@ program.command('pack [entryPaths...]')
}
const buildTasks: BuildTask[] = []
const getFileSrcGlobPattern = (filePath: string, targetExt: string) => {
return path.changeExt(path.join(options.srcdir, path.relative(options.outdir, filePath)), targetExt)
let subFilePath /* esm/components/a.ts */ = path.relative(options.outdir, filePath)
const targetFormat = options.formatOutdirs.find((eachFormat) => subFilePath.startsWith(eachFormat + '/'))
if (targetFormat) {
subFilePath = /* components/a.ts */ path.relative(targetFormat, subFilePath)
}
const srcFilePath /* src/components/a.ts */ = path.join(options.srcdir, subFilePath)
return path.changeExt(srcFilePath, targetExt)
}
const addBuildTask = async (eachEntries: string[], eachOptions: { format: string, ext?: string, platform?: string, outFile?: string }) => {
const isCSSTask = eachOptions.format === 'css'
Expand Down Expand Up @@ -88,15 +94,18 @@ program.command('pack [entryPaths...]')
}
} : false,
logLevel: 'silent',
outdir: options.formatOutdirs.includes(eachOptions.format)
? path.join(options.outdir, eachOptions.format)
: options.outdir,
outbase: options.srcdir,
platform: eachOptions.platform || options.platform,
metafile: true,
format: isCSSTask ? undefined : eachOptions.format,
keepNames: options.keepNames,
mangleProps: options.mangleProps ? new RegExp(options.mangleProps) : undefined,
resolveExtensions: options.resolveExtensions,
target: options.target,
sourcemap: options.sourcemap
sourcemap: options.sourcemap,
conditions: ['import', 'require']
}

// 安全地同步選項給 esbuild
Expand Down
2 changes: 1 addition & 1 deletion packages/aronrepo/tests/args-externals/pack.test.ts
Expand Up @@ -3,7 +3,7 @@ import { expectFileIncludes } from '../../../../utils/expect-file-includes'

test('prevent bundling external packages by args', () => {
execSync('node ../../dist/bin/index pack --external @master/css @master/style-element.react', { cwd: __dirname, stdio: 'pipe' })
expectFileIncludes('dist/index.cjs', [
expectFileIncludes('dist/cjs/index.js', [
'require("@master/css")',
'require("@master/style-element.react")'
], { cwd: __dirname })
Expand Down
4 changes: 2 additions & 2 deletions packages/aronrepo/tests/args-externals/package.json
@@ -1,9 +1,9 @@
{
"name": "args-externals",
"main": "dist/index.cjs",
"main": "dist/cjs/index.js",
"exports": {
".": {
"require": "./dist/index.cjs"
"require": "./dist/cjs/index.js"
}
},
"files": [
Expand Down
2 changes: 1 addition & 1 deletion packages/aronrepo/tests/bin/pack.test.ts
Expand Up @@ -3,5 +3,5 @@ import { expectExist } from '../../../../utils/expect-exist'

it('build `bin` by `package.json`', () => {
execSync('../../dist/bin/index pack', { cwd: __dirname, stdio: 'pipe' })
expectExist(['dist/bin/index.js'])
expectExist(['dist/cjs/bin/index.js'])
})
2 changes: 1 addition & 1 deletion packages/aronrepo/tests/externals/pack.test.ts
Expand Up @@ -3,7 +3,7 @@ import { expectFileIncludes } from '../../../../utils/expect-file-includes'

test('prevent bundling deps and peerDeps by `package.json`', () => {
execSync('node ../../dist/bin/index pack --extra-external fake-external-package', { cwd: __dirname, stdio: 'pipe' })
expectFileIncludes('dist/index.cjs', [
expectFileIncludes('dist/cjs/index.js', [
'require("@master/css")',
'require("@master/style-element.react")'
], { cwd: __dirname })
Expand Down
4 changes: 2 additions & 2 deletions packages/aronrepo/tests/externals/package.json
@@ -1,9 +1,9 @@
{
"name": "externals",
"main": "dist/index.cjs",
"main": "dist/cjs/index.js",
"exports": {
".": {
"require": "./dist/index.cjs"
"require": "./dist/cjs/index.js"
}
},
"files": [
Expand Down
9 changes: 3 additions & 6 deletions packages/aronrepo/tests/multi-exports/pack.test.ts
Expand Up @@ -4,14 +4,11 @@ import { expectExist } from '../../../../utils/expect-exist'
it('exports multiple outputs', () => {
execSync('../../dist/bin/index pack', { cwd: __dirname, stdio: 'pipe' })
expectExist([
'dist/index.cjs',
'dist/index.mjs',
'dist/cjs/index.js',
'dist/esm/index.js',
'dist/index.browser.js',
'dist/index.browser.mjs',
'dist/index.default.js',
'dist/index.default.mjs',
'dist/index.browser.d.ts',
'dist/index.d.ts',
'dist/index.default.d.ts',
'dist/options.d.ts',
])
})
16 changes: 10 additions & 6 deletions packages/aronrepo/tests/multi-exports/package.json
@@ -1,16 +1,16 @@
{
"name": "multi-exports",
"main": "./dist/index.cjs",
"jsnext:main": "./dist/index.mjs",
"main": "./dist/cjs/index.js",
"jsnext:main": "./dist/esm/index.js",
"browser": "./dist/index.browser.js",
"esnext": "./dist/index.mjs",
"module": "./dist/index.mjs",
"esnext": "./dist/esm/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"node": {
"require": "./dist/index.cjs",
"import": "./dist/index.mjs"
"require": "./dist/cjs/index.js",
"import": "./dist/esm/index.js"
},
"browser": {
"import": "./dist/index.browser.mjs"
Expand All @@ -20,6 +20,10 @@
"import": "./dist/index.default.mjs"
},
"types": "./dist/index.d.ts"
},
"./options": {
"require": "./dist/options.js",
"import": "./dist/options.mjs"
}
},
"files": [
Expand Down
4 changes: 2 additions & 2 deletions packages/aronrepo/tests/options/package.json
@@ -1,7 +1,7 @@
{
"name": "test-options",
"main": "dist/index.cjs",
"module": "dist/index.mjs",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"sideEffects": false,
"files": [
"dist"
Expand Down
4 changes: 2 additions & 2 deletions packages/aronrepo/tests/options/test.ts
Expand Up @@ -5,14 +5,14 @@ import { expectFileIncludes } from '../../../../utils/expect-file-includes'
execSync('../../dist/bin/index pack --no-minify', { cwd: __dirname, stdio: 'pipe' })

it('mangle private', () => {
expectFileExcludes('dist/index.mjs', [
expectFileExcludes('dist/esm/index.js', [
'_fullAAAMembership'
], { cwd: __dirname })
})

it('tree shake and only bundle BBB', () => {
execSync('../../dist/bin/index pack src/tree-shaking.ts --no-minify --no-clean --no-soft-bundle', { cwd: __dirname, stdio: 'pipe' })
expectFileExcludes('dist/tree-shaking.mjs', [
expectFileExcludes('dist/esm/tree-shaking.js', [
'AAA'
], { cwd: __dirname })
})
2 changes: 1 addition & 1 deletion packages/aronrepo/tests/specified-ext/pack.test.ts
Expand Up @@ -3,5 +3,5 @@ import { expectExist } from '../../../../utils/expect-exist'

test('specified ext `.js` by "main": "dist/index.js"', () => {
execSync('../../dist/bin/index pack', { cwd: __dirname, stdio: 'pipe' })
expectExist(['dist/index.js'])
expectExist(['dist/cjs/index.js'])
})
2 changes: 1 addition & 1 deletion packages/aronrepo/tests/tsx/pack.test.ts
Expand Up @@ -3,5 +3,5 @@ import { expectExist } from '../../../../utils/expect-exist'

test('resolve `.tsx` with `package.json`', () => {
execSync('../../dist/bin/index pack', { cwd: __dirname, stdio: 'pipe' })
expectExist(['dist/index.cjs'])
expectExist(['dist/cjs/index.js'])
})
12 changes: 6 additions & 6 deletions packages/aronrepo/tests/tsx/package.json
@@ -1,14 +1,14 @@
{
"name": "tsx",
"main": "dist/index.cjs",
"module": "dist/index.mjs",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"types": "dist/index.d.ts",
"jsnext:main": "dist/index.mjs",
"esnext": "dist/index.mjs",
"jsnext:main": "dist/esm/index.js",
"esnext": "dist/esm/index.js",
"exports": {
".": {
"require": "./dist/index.cjs",
"import": "./dist/index.mjs",
"require": "./dist/cjs/index.js",
"import": "./dist/esm/index.js",
"types": "./dist/index.d.ts"
}
},
Expand Down
16 changes: 8 additions & 8 deletions packages/conventional-changelog-config/package.json
@@ -1,8 +1,8 @@
{
"name": "conventional-changelog-aron",
"scripts": {
"build:cjs": "esbuild src/index.ts --bundle --outfile=dist/index.cjs --format=cjs --minify --sourcemap --platform=node",
"build:esm": "esbuild src/index.ts --bundle --outfile=dist/index.mjs --format=esm --minify --sourcemap --platform=node",
"build:cjs": "esbuild src/index.ts --bundle --outfile=dist/cjs/index.js --format=cjs --minify --sourcemap --platform=node",
"build:esm": "esbuild src/index.ts --bundle --outfile=dist/esm/index.js --format=esm --minify --sourcemap --platform=node",
"build:type": "tsc --emitDeclarationOnly --preserveWatchOutput",
"build": "conc 'npm:build:*'",
"dev": "conc 'npm:build:* -- --watch'",
Expand Down Expand Up @@ -33,15 +33,15 @@
"aron",
"preset"
],
"main": "./dist/index.cjs",
"jsnext:main": "./dist/index.mjs",
"esnext": "./dist/index.mjs",
"module": "./dist/index.mjs",
"main": "./dist/cjs/index.js",
"jsnext:main": "./dist/esm/index.js",
"esnext": "./dist/esm/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"require": "./dist/index.cjs",
"import": "./dist/index.mjs",
"require": "./dist/cjs/index.js",
"import": "./dist/esm/index.js",
"types": "./dist/index.d.ts"
}
},
Expand Down
16 changes: 8 additions & 8 deletions packages/conventional-commits/package.json
Expand Up @@ -2,8 +2,8 @@
"name": "aron-conventional-commits",
"license": "MIT",
"scripts": {
"build:cjs": "esbuild src/index.ts --bundle --outfile=dist/index.cjs --format=cjs --minify --sourcemap --platform=node",
"build:esm": "esbuild src/index.ts --bundle --outfile=dist/index.mjs --format=esm --minify --sourcemap --platform=node",
"build:cjs": "esbuild src/index.ts --bundle --outfile=dist/cjs/index.js --format=cjs --minify --sourcemap --platform=node",
"build:esm": "esbuild src/index.ts --bundle --outfile=dist/esm/index.js --format=esm --minify --sourcemap --platform=node",
"build:type": "tsc --emitDeclarationOnly --preserveWatchOutput",
"build": "conc 'npm:build:*'",
"dev": "conc 'npm:build:* -- --watch'",
Expand Down Expand Up @@ -36,15 +36,15 @@
"aron",
"preset"
],
"main": "./dist/index.cjs",
"jsnext:main": "./dist/index.mjs",
"esnext": "./dist/index.mjs",
"module": "./dist/index.mjs",
"main": "./dist/cjs/index.js",
"jsnext:main": "./dist/esm/index.js",
"esnext": "./dist/esm/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"require": "./dist/index.cjs",
"import": "./dist/index.mjs",
"require": "./dist/cjs/index.js",
"import": "./dist/esm/index.js",
"types": "./dist/index.d.ts"
}
},
Expand Down

0 comments on commit 0a6f110

Please sign in to comment.