Skip to content

Commit

Permalink
fix: package entrypoint resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
tbossi committed Nov 16, 2023
1 parent c03f0d0 commit a810cc1
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 10 deletions.
10 changes: 9 additions & 1 deletion tests/samples/acceptance_sample_dependency_installation.cjs
Expand Up @@ -5,11 +5,19 @@ module.exports = {
},
deps: [
{name: 'jimp'},
{name: 'is-odd', alias: 'check-odd'}
{name: 'is-odd', alias: 'check-odd'},
{name: 'knex'},
{name: '@aws-sdk/core'},
{name: 'pg'},
{name: 'esbuild'},
],
run: async (args, options) => {
const _jimp = await gushio.import('jimp')
const _checkOdd = await gushio.import('check-odd')
const _knex = await gushio.import('knex')
const _awsSdkCore = await gushio.import('@aws-sdk/core')
const _pg = await gushio.import('pg')
const _esbuild = await gushio.import('esbuild')

console.log(`Written on console ${'after'.bold.yellow} requiring deps`)
},
Expand Down
8 changes: 8 additions & 0 deletions tests/samples/acceptance_sample_dependency_installation.mjs
Expand Up @@ -5,10 +5,18 @@ export const cli = {
export const deps = [
{name: 'jimp'},
{name: 'is-odd', alias: 'check-odd'},
{name: 'knex'},
{name: '@aws-sdk/core'},
{name: 'pg'},
{name: 'esbuild'},
]
export const run = async (args, options) => {
const _jimp = await gushio.import('jimp')
const _checkOdd = await gushio.import('check-odd')
const _knex = await gushio.import('knex')
const _awsSdkCore = await gushio.import('@aws-sdk/core')
const _pg = await gushio.import('pg')
const _esbuild = await gushio.import('esbuild')

console.log(`Written on console ${'after'.bold.yellow} requiring deps`)
}
27 changes: 23 additions & 4 deletions tests/utils/dependenciesUtils.test.js
Expand Up @@ -121,17 +121,33 @@ describe('dependenciesUtils', () => {

test.each([
{},
{main: 'index.js'},
{main: '/'},
{exports: 'index.js'},
{exports: '/'},
{exports: {import: 'index.js'}},
{exports: {import: '/'}},
{exports: {default: 'index.js'}},
{exports: {default: '/'}},
{exports: {'.': 'index.js'}},
{exports: {'.': '/'}},
{exports: {'.': {import: 'index.js'}}},
{exports: {'.': {import: '/'}}},
{exports: {'.': {import: null}}},
{exports: {'.': {default: 'index.js'}}},
{exports: {'.': {default: '/'}}},
{exports: {'.': {default: null}}},
{exports: {'.': {require: 'index.js'}}},
{exports: {'.': {require: '/'}}},
{exports: {'.': {require: null}}},
{exports: 'index.js', module: 'other_ignored.js'},
{exports: 'index.js', main: 'other_ignored.js'},
{type: 'module', module: 'index.js'},
{type: 'module', module: 'index'},
{type: 'module', module: '/'},
{type: 'commonjs', main: 'index.js'},
{type: 'commonjs', main: 'index'},
{type: 'commonjs', main: '/'},
{type: 'commonjs', module: 'other_ignored.js', main: 'index.js'},
{main: 'index.js', module: 'other_ignored.js'},
])('found in local folder %p', async (pkgStructure) => {
const moduleMock = {default: 'theModule'}
const moduleMockName = URL.fileURLToPath(`file://${os.platform() === 'win32' ? '' : 'localhost/'}${folder}/node_modules/a-fake-module/index.js`)
Expand All @@ -145,6 +161,7 @@ describe('dependenciesUtils', () => {
expect(path).toBe(`${folder}/node_modules/a-fake-module/package.json`)
return pkgStructure
})
fsExtra.pathExists = jest.fn().mockImplementation((path) => path.endsWith('.js'))

expect(JSON.stringify(await patchedImport('a-fake-module'))).toStrictEqual(JSON.stringify(moduleMock))
// call again to assert caching
Expand Down Expand Up @@ -188,7 +205,8 @@ describe('dependenciesUtils', () => {
process.emit('log', 'warn', 'msg3', 'msg4')
process.emit('log', 'error', 'msg5', 'msg6')
process.emit('log', 'info', 'msg7', 'msg8')
process.emit('log', 'other', 'msg9', 'msg10')
process.emit('log', 'info', 'msg9', 'postinstall')
process.emit('log', 'other', 'msg11', 'msg12')
})
}
Arborist.mockReturnValueOnce(arborist)
Expand All @@ -206,6 +224,7 @@ describe('dependenciesUtils', () => {
expect(console.warn).toHaveBeenNthCalledWith(2, '[Gushio|Deps] %s', 'msg3', 'msg4')
expect(console.error).toHaveBeenNthCalledWith(1, '[Gushio|Deps] %s', 'msg5', 'msg6')
expect(console.info).toHaveBeenNthCalledWith(1, '[Gushio|Deps] %s', 'msg7', 'msg8')
expect(console.verbose).toHaveBeenNthCalledWith(1, '[Gushio|Deps] %s', 'msg9', 'msg10')
expect(console.verbose).toHaveBeenNthCalledWith(1, '[Gushio|Deps] %s', 'msg9', 'postinstall')
expect(console.verbose).toHaveBeenNthCalledWith(2, '[Gushio|Deps] %s', 'msg11', 'msg12')
})
})
44 changes: 39 additions & 5 deletions utils/dependenciesUtils.js
Expand Up @@ -34,23 +34,53 @@ export const requireStrategy = {
const packageEntryPoint = async (pathToPackageFolder) => {
const pkg = await fsExtra.readJson(`${pathToPackageFolder}/package.json`)

let entryPoint = pkg.main
if (!entryPoint && pkg.exports) {
let entryPoint
if (pkg.exports) {
if (isString(pkg.exports)) {
entryPoint = pkg.exports
} else if (isString(pkg.exports['import'])) {
entryPoint = pkg.exports['import']
} else if (isString(pkg.exports['default'])) {
entryPoint = pkg.exports['default']
} else if (isString(pkg.exports['.'])) {
entryPoint = pkg.exports['.']
} else if (pkg.exports['.'] && isString(pkg.exports['.']['import'])) {
entryPoint = pkg.exports['.']['import']
} else if (pkg.exports['.']) {
if (isString(pkg.exports['.']['import'])) {
entryPoint = pkg.exports['.']['import']
} else if (isString(pkg.exports['.']['default'])) {
entryPoint = pkg.exports['.']['default']
} else if (isString(pkg.exports['.']['require'])) {
entryPoint = pkg.exports['.']['require']
}
}
} else if (pkg.type === 'module' && pkg.module) {
entryPoint = pkg.module
} else if (pkg.type === 'commonjs' && pkg.main) {
entryPoint = pkg.main
} else {
entryPoint = pkg.main
}

if (!entryPoint) {
entryPoint = 'index.js'
} else if (entryPoint.endsWith('/')) {
entryPoint += 'index.js'
} else if (!await fsExtra.pathExists(path.join(pathToPackageFolder, entryPoint))) {
if (await fsExtra.pathExists(path.join(pathToPackageFolder, `${entryPoint}.js`))) {
entryPoint += '.js'
} else if (await fsExtra.pathExists(path.join(pathToPackageFolder, `${entryPoint}.json`))) {
entryPoint += '.json'
} else if (await fsExtra.pathExists(path.join(pathToPackageFolder, `${entryPoint}.node`))) {
entryPoint += '.node'
}
} else {
try {
if ((await fsExtra.stat(path.join(pathToPackageFolder, entryPoint))).isDirectory()) {
entryPoint += '/index.js'
}
} catch (e) {
// suppressed
}
}

const completePath = path.join(pathToPackageFolder, entryPoint)
Expand Down Expand Up @@ -120,7 +150,11 @@ export const installDependencies = async (folder, dependencies, console) => {
break

case 'info':
console.info(GushioDepsLogFormat, ...args)
if (args.includes('postinstall')) {
console.verbose(GushioDepsLogFormat, ...args)
} else {
console.info(GushioDepsLogFormat, ...args)
}
break

default:
Expand Down

0 comments on commit a810cc1

Please sign in to comment.