Skip to content

Commit 755b9a7

Browse files
committed
feat: toggle package type
- evanw/esbuild#2026 - https://github.com/flex-development/toggle-pkg-type Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
1 parent fc2d542 commit 755b9a7

8 files changed

Lines changed: 90 additions & 51 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
"@flex-development/mlly": "1.0.0-alpha.13",
8585
"@flex-development/pathe": "1.0.3",
8686
"@flex-development/pkg-types": "2.0.0",
87+
"@flex-development/toggle-pkg-type": "1.1.1",
8788
"@flex-development/tsconfig-utils": "1.1.2",
8889
"@flex-development/tutils": "6.0.0-alpha.10",
8990
"colorette": "2.0.19",

src/__tests__/make.spec.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@
55

66
import { ErrorCode, type NodeError } from '@flex-development/errnode'
77
import pathe from '@flex-development/pathe'
8-
import consola from 'consola'
98
import testSubject from '../make'
109

1110
vi.mock('#src/utils/fs')
1211

1312
describe('unit:make', () => {
13+
let configfile: boolean
14+
1415
beforeAll(() => {
15-
consola.mockTypes(() => vi.fn())
16+
configfile = false
1617
})
1718

1819
it('should return build results', async () => {
1920
// Act
20-
const results = await testSubject({ configfile: false })
21+
const results = await testSubject({ configfile })
2122

2223
// Expect
2324
expect(results).to.be.an('array').that.is.not.empty
@@ -33,7 +34,7 @@ describe('unit:make', () => {
3334

3435
// Act
3536
try {
36-
await testSubject({ configfile: false, cwd, write: true })
37+
await testSubject({ configfile, cwd })
3738
assert.fail('expected exception not thrown')
3839
} catch (e: unknown) {
3940
error = e as typeof error

src/internal/__snapshots__/esbuilder.integration.snap

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
exports[`integration:internal/esbuilder > esbuild > cjs > should create bundle in cjs format 1`] = `
44
[
55
{
6-
"entryPoint": "find-uniq.cts",
6+
"entryPoint": "buddy.js",
77
"errors": [],
8-
"outfile": "dist/find-uniq.cjs",
9-
"path": "\${process.cwd()}/__fixtures__/pkg/find-uniq/dist/find-uniq.cjs",
8+
"outfile": "dist/buddy.cjs",
9+
"path": "\${process.cwd()}/__fixtures__/pkg/buddy/dist/buddy.cjs",
1010
"warnings": [],
1111
},
1212
]
@@ -15,10 +15,10 @@ exports[`integration:internal/esbuilder > esbuild > cjs > should create bundle i
1515
exports[`integration:internal/esbuilder > esbuild > cjs > should do transpilation in cjs format 1`] = `
1616
[
1717
{
18-
"entryPoint": "find-uniq.cts",
18+
"entryPoint": "buddy.js",
1919
"errors": [],
20-
"outfile": "dist/find-uniq.cjs",
21-
"path": "\${process.cwd()}/__fixtures__/pkg/find-uniq/dist/find-uniq.cjs",
20+
"outfile": "dist/buddy.cjs",
21+
"path": "\${process.cwd()}/__fixtures__/pkg/buddy/dist/buddy.cjs",
2222
"warnings": [],
2323
},
2424
]
@@ -107,10 +107,10 @@ exports[`integration:internal/esbuilder > esbuild > esm > should do transpilatio
107107
exports[`integration:internal/esbuilder > esbuild > iife > should create bundle in iife format 1`] = `
108108
[
109109
{
110-
"entryPoint": "buddy.js",
110+
"entryPoint": "find-uniq.cts",
111111
"errors": [],
112-
"outfile": "dist/buddy.js",
113-
"path": "\${process.cwd()}/__fixtures__/pkg/buddy/dist/buddy.js",
112+
"outfile": "dist/find-uniq.js",
113+
"path": "\${process.cwd()}/__fixtures__/pkg/find-uniq/dist/find-uniq.js",
114114
"warnings": [],
115115
},
116116
]
@@ -119,10 +119,10 @@ exports[`integration:internal/esbuilder > esbuild > iife > should create bundle
119119
exports[`integration:internal/esbuilder > esbuild > iife > should do transpilation in iife format 1`] = `
120120
[
121121
{
122-
"entryPoint": "buddy.js",
122+
"entryPoint": "find-uniq.cts",
123123
"errors": [],
124-
"outfile": "dist/buddy.js",
125-
"path": "\${process.cwd()}/__fixtures__/pkg/buddy/dist/buddy.js",
124+
"outfile": "dist/find-uniq.js",
125+
"path": "\${process.cwd()}/__fixtures__/pkg/find-uniq/dist/find-uniq.js",
126126
"warnings": [],
127127
},
128128
]

src/internal/__tests__/esbuilder.integration.spec.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
import type { Entry } from '#src/interfaces'
77
import loaders from '#src/utils/loaders'
88
import type { Mock } from '#tests/interfaces'
9+
import getPackageJson from '#tests/utils/get-package-json'
910
import * as mlly from '@flex-development/mlly'
1011
import * as pathe from '@flex-development/pathe'
12+
import type { PackageJson } from '@flex-development/pkg-types'
1113
import * as esbuild from 'esbuild'
1214
import testSubject from '../esbuilder'
1315

14-
vi.mock('#src/plugins/write/plugin')
16+
vi.mock('#src/utils/fs')
1517
vi.mock('esbuild')
1618

1719
describe('integration:internal/esbuilder', () => {
@@ -34,20 +36,22 @@ describe('integration:internal/esbuilder', () => {
3436
let absWorkingDir: string
3537
let entry: Partial<Entry>
3638
let format: esbuild.Format
39+
let pkg: PackageJson
3740

38-
beforeAll(() => {
39-
entry = { cwd: '__fixtures__/pkg/find-uniq' }
41+
beforeAll(async () => {
42+
entry = { cwd: '__fixtures__/pkg/buddy' }
4043
absWorkingDir = pathe.resolve(entry.cwd!) + pathe.sep
4144
format = entry.format = 'cjs'
45+
pkg = await getPackageJson(pathe.join(absWorkingDir, 'package.json'))
4246
})
4347

4448
it('should create bundle in cjs format', async () => {
4549
// Arrange
4650
const bundle: boolean = true
47-
const source: string = 'find-uniq.cts'
51+
const source: string = 'buddy.js'
4852

4953
// Act
50-
const [, results] = await testSubject({ ...entry, bundle, source })
54+
const [, results] = await testSubject({ ...entry, bundle, source }, pkg)
5155

5256
// Expect
5357
expect(results).toMatchSnapshot()
@@ -74,11 +78,14 @@ describe('integration:internal/esbuilder', () => {
7478

7579
it('should do transpilation in cjs format', async () => {
7680
// Arrange
77-
const pattern: string = 'find-uniq.cts'
81+
const pattern: string = 'buddy.js'
7882
const source: string = '.'
7983

8084
// Act
81-
const [, results] = await testSubject({ ...entry, pattern, source })
85+
const [, results] = await testSubject(
86+
{ ...entry, pattern, source },
87+
pkg
88+
)
8289

8390
// Expect
8491
expect(results).toMatchSnapshot()
@@ -214,15 +221,15 @@ describe('integration:internal/esbuilder', () => {
214221
let format: esbuild.Format
215222

216223
beforeAll(() => {
217-
entry = { cwd: '__fixtures__/pkg/buddy' }
224+
entry = { cwd: '__fixtures__/pkg/find-uniq' }
218225
absWorkingDir = pathe.resolve(entry.cwd!) + pathe.sep
219226
format = entry.format = 'iife'
220227
})
221228

222229
it('should create bundle in iife format', async () => {
223230
// Arrange
224231
const bundle: boolean = true
225-
const source: string = 'buddy.js'
232+
const source: string = 'find-uniq.cts'
226233

227234
// Act
228235
const [, results] = await testSubject({ ...entry, bundle, source })
@@ -252,7 +259,7 @@ describe('integration:internal/esbuilder', () => {
252259

253260
it('should do transpilation in iife format', async () => {
254261
// Arrange
255-
const pattern: string = 'buddy.js'
262+
const pattern: string = 'find-uniq.cts'
256263
const source: string = '.'
257264

258265
// Act

src/internal/__tests__/esbuilder.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import type { Entry } from '#src'
77
import testSubject from '../esbuilder'
88

9-
vi.mock('#src/plugins/write/plugin')
9+
vi.mock('#src/utils/fs')
1010

1111
describe('unit:internal/esbuilder', () => {
1212
let entry: Entry

src/internal/esbuilder.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import loaders from '#src/utils/loaders'
1212
import { EXT_DTS_REGEX } from '@flex-development/ext-regex'
1313
import * as mlly from '@flex-development/mlly'
1414
import * as pathe from '@flex-development/pathe'
15+
import type { PackageJson } from '@flex-development/pkg-types'
16+
import togglePkgType from '@flex-development/toggle-pkg-type'
1517
import * as esbuild from 'esbuild'
1618
import regexp from 'escape-string-regexp'
1719
import fg from 'fast-glob'
@@ -25,12 +27,14 @@ import { omit } from 'radash'
2527
* @async
2628
*
2729
* @param {Partial<Entry>} entry - Build entry object
28-
* @param {Partial<FileSystemAdapter>} [fs=fsa] - Custom file system methods
30+
* @param {PackageJson?} [pkg={}] - Relevant `package.json`
31+
* @param {Partial<FileSystemAdapter>?} [fs=fsa] - Custom file system methods
2932
* @return {Promise<[esbuild.Metafile, Result[]]>} Metafile and build results
3033
* @throws {esbuild.BuildFailure}
3134
*/
3235
const esbuilder = async (
3336
entry: Partial<Entry>,
37+
pkg: PackageJson = {},
3438
fs: Partial<FileSystemAdapter> = fsa
3539
): Promise<[esbuild.Metafile, Result[]]> => {
3640
const {
@@ -66,6 +70,17 @@ const esbuilder = async (
6670
const absWorkingDir: string =
6771
pathe.resolve(cwd).replace(/\/$/, '') + pathe.sep
6872

73+
/**
74+
* Boolean indicating {@linkcode pkg.type} should be disabled before building
75+
* source files.
76+
*
77+
* @see https://github.com/evanw/esbuild/issues/2026
78+
* @see https://github.com/flex-development/toggle-pkg-type
79+
*
80+
* @const {boolean} pkgtype
81+
*/
82+
const pkgtype: boolean = pkg.type === 'module' && format === 'cjs'
83+
6984
/**
7085
* Relative paths to source files.
7186
*
@@ -126,6 +141,9 @@ const esbuilder = async (
126141
Reflect.deleteProperty(options, 'stdin')
127142
Reflect.deleteProperty(options, 'watch')
128143

144+
// disable package type
145+
pkgtype && togglePkgType(null, absWorkingDir)
146+
129147
// build source files
130148
const { errors, metafile, outputFiles, warnings } = await esbuild.build({
131149
...options,
@@ -167,6 +185,9 @@ const esbuilder = async (
167185
write: false
168186
})
169187

188+
// reset package type
189+
pkgtype && togglePkgType(null, absWorkingDir)
190+
170191
return [
171192
metafile,
172193
outputFiles

src/make.ts

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -116,26 +116,21 @@ async function make({
116116
/**
117117
* `package.json` data.
118118
*
119-
* @var {Nullable<PackageJson>} pkg
119+
* @const {Nullable<PackageJson>} pkg
120120
*/
121-
let pkg: Nullable<PackageJson> = null
122-
123-
// get package.json
124-
if (options.write) {
125-
pkg = mlly.readPackageJson(cwd)
126-
127-
// throw if package.json was not found
128-
if (!pkg) {
129-
throw new ERR_MODULE_NOT_FOUND(
130-
pathe.join(cwd, 'package.json'),
131-
fileURLToPath(import.meta.url),
132-
'module'
133-
)
134-
}
121+
const pkg: Nullable<PackageJson> = mlly.readPackageJson(cwd)
122+
123+
// throw if package.json was not found
124+
if (!pkg) {
125+
throw new ERR_MODULE_NOT_FOUND(
126+
pathe.join(cwd, 'package.json'),
127+
fileURLToPath(import.meta.url),
128+
'module'
129+
)
135130
}
136131

137132
// print build start info
138-
options.write && consola.info(color.cyan(`Building ${pkg!.name}`))
133+
options.write && consola.info(color.cyan(`Building ${pkg.name}`))
139134

140135
// push empty object to infer entry from config if initial array is empty
141136
if (entries.length === 0) entries.push({})
@@ -146,7 +141,7 @@ async function make({
146141
* @const {Entry[]} tasks
147142
*/
148143
const tasks: Entry[] = entries.map(entry => {
149-
const { peerDependencies = {} } = pkg ?? /* c8 ignore next */ {}
144+
const { peerDependencies = {} } = pkg
150145

151146
const {
152147
bundle = options.bundle,
@@ -208,7 +203,7 @@ async function make({
208203
build.set(index, [])
209204

210205
// build source files
211-
const [, results] = await esbuilder(entry, fs)
206+
const [, results] = await esbuilder(entry, pkg, fs)
212207

213208
// add build results to build results map
214209
for (const result of results) build.get(index)!.push(result)
@@ -224,7 +219,7 @@ async function make({
224219
let bytes: number = 0
225220

226221
// print build done info
227-
consola.success(color.green(`Build succeeded for ${pkg!.name}`))
222+
consola.success(color.green(`Build succeeded for ${pkg.name}`))
228223

229224
// print build analysis
230225
for (const [index, outputs] of build.entries()) {

yarn.lock

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,7 @@ __metadata:
12291229
"@flex-development/mlly": "npm:1.0.0-alpha.13"
12301230
"@flex-development/pathe": "npm:1.0.3"
12311231
"@flex-development/pkg-types": "npm:2.0.0"
1232+
"@flex-development/toggle-pkg-type": "npm:1.1.1"
12321233
"@flex-development/tsconfig-utils": "npm:1.1.2"
12331234
"@flex-development/tutils": "npm:6.0.0-alpha.10"
12341235
"@graphql-eslint/eslint-plugin": "npm:3.16.0"
@@ -1367,6 +1368,19 @@ __metadata:
13671368
languageName: node
13681369
linkType: hard
13691370

1371+
"@flex-development/toggle-pkg-type@npm:1.1.1":
1372+
version: 1.1.1
1373+
resolution: "@flex-development/toggle-pkg-type@npm:1.1.1::__archiveUrl=https%3A%2F%2Fnpm.pkg.github.com%2Fdownload%2F%40flex-development%2Ftoggle-pkg-type%2F1.1.1%2F536f86b58a650e7b830ee3d88c750c3771b6dbc5"
1374+
dependencies:
1375+
"@flex-development/mlly": "npm:1.0.0-alpha.13"
1376+
"@flex-development/pathe": "npm:1.0.3"
1377+
"@flex-development/tutils": "npm:6.0.0-alpha.10"
1378+
bin:
1379+
toggle-pkg-type: dist/cli.mjs
1380+
checksum: 6ae31dfc2a193139b52fba42d9424eca4b4fdf6eb74621fd29d20508ae0148f7fae141f122995d1ec864dade8fd847a747cc24525ea6ad203e766e5692d3311a
1381+
languageName: node
1382+
linkType: hard
1383+
13701384
"@flex-development/tsconfig-types@npm:3.2.0":
13711385
version: 3.2.0
13721386
resolution: "@flex-development/tsconfig-types@npm:3.2.0::__archiveUrl=https%3A%2F%2Fnpm.pkg.github.com%2Fdownload%2F%40flex-development%2Ftsconfig-types%2F3.2.0%2Fe49eb5faa42583a340fdff7c9ec64ac5feaf4529"
@@ -8792,13 +8806,13 @@ __metadata:
87928806
languageName: node
87938807
linkType: hard
87948808

8795-
"typescript@npm:5.0.0-dev.20230222":
8796-
version: 5.0.0-dev.20230222
8797-
resolution: "typescript@npm:5.0.0-dev.20230222"
8809+
"typescript@npm:5.0.0-dev.20230224":
8810+
version: 5.0.0-dev.20230224
8811+
resolution: "typescript@npm:5.0.0-dev.20230224"
87988812
bin:
87998813
tsc: bin/tsc
88008814
tsserver: bin/tsserver
8801-
checksum: e22533927607fb9a52c7dbd2d2f530f268f2a7a03596ed9f76e5b20e9a49fe093c55441650d9d0f566e5f850be4a15d0e3f193aa987e62788d2860f7a909f1a2
8815+
checksum: 2c30ee32a78f64844b877032db0a974e1666033b867181158770b0283c210f6e2ba8576f5dca5c2e8e95a4239c6733bd5a9d15d67fbade3580d38125606ce26d
88028816
languageName: node
88038817
linkType: hard
88048818

0 commit comments

Comments
 (0)