Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(utilities): add possibility to import single functions by appending them to the import path. #454

Merged
merged 24 commits into from Oct 2, 2022
Merged

Conversation

imranbarbhuiya
Copy link
Contributor

@imranbarbhuiya imranbarbhuiya commented Sep 16, 2022

fixes #452

tsup was bunding the dts in one file and has no option to disable it. So I've used tsc to build the types files.

@imranbarbhuiya
Copy link
Contributor Author

imranbarbhuiya commented Sep 16, 2022

Not sure why build is failing with targe 2021

Copy link
Member

@favna favna left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Granted even CJS didn't work before my commit above this review, this PR still needs more work.

Create a file on the root of the repository called test.cjs with content:

/* eslint-disable @typescript-eslint/no-var-requires */
const { isFunction } = require('@sapphire/utilities/isFunction');

console.log(isFunction(() => undefined));

then run it with node test.cjs. This will return true as expected.

However doing the same with test.mjs and content:

import { isFunction } from '@sapphire/utilities/isFunction';

console.log(isFunction(() => undefined));

will give:

node file.mjs
file:///Users/favna/workspace/sapphire/utilities/file.mjs:1
import { isFunction } from '@sapphire/utilities/isFunction';
         ^^^^^^^^^^
SyntaxError: Named export 'isFunction' not found. The requested module '@sapphire/utilities/isFunction' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from '@sapphire/utilities/isFunction';
const { isFunction } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:123:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:189:5)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:528:24)
    at async loadESM (node:internal/process/esm_loader:91:5)
    at async handleMainPromise (node:internal/modules/run_main:65:12)

Node.js v18.8.0

And lastly, TS is also broken, again with a similar file this time file.ts:

import { isFunction } from '@sapphire/utilities/isFunction';

console.log(isFunction(() => undefined));

This gives a compiler error:

Cannot find module '@sapphire/utilities/isFunction' or its corresponding type declarations.ts(2307)

packages/decorators/src/base-decorators.ts Outdated Show resolved Hide resolved
packages/utilities/package.json Outdated Show resolved Hide resolved
@favna favna changed the title refactor: ability to import single functions in @sapphire/utilities feat(utilities): add possibility to import single functions by appending them to the import path. Sep 19, 2022
vladfrangu
vladfrangu previously approved these changes Oct 1, 2022
Copy link
Member

@favna favna left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blocking until #468 is merged

@favna favna merged commit 374c145 into sapphiredev:main Oct 2, 2022
@imranbarbhuiya imranbarbhuiya deleted the feat/gh-452 branch October 2, 2022 10:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

request: ability to import single functions in @sapphire/utilities
4 participants