15

Given the example below.

import { QueryClientContract, TransactionClientContract } from '@ioc:Adonis/Lucid/Database'

The use of the colon has confused me and never understood where the imported files could be referenced from.

myckhel
  • 800
  • 3
  • 15
  • 29
  • 6
    It's not part of JavaScript; it's something relevant for whatever module "bundler" you're using. JavaScript just says that the `from` value has to be a string constant; it says nothing about the semantics of the string contents. – Pointy Mar 06 '21 at 15:55
  • And do you have idea of which bundler could be able to translate that? And what does that mean for the bundler? – myckhel Mar 06 '21 at 16:04
  • not 100% familar with `adonis.js`, but I suspect it has something to do with scoped packages . Some tooling might have installed the packages as scoped packages, and node (I am assuming node?) can resolve the package. – pandamakes Mar 06 '21 at 16:08
  • I don't recognize that, sorry. Where did you see it? – Pointy Mar 06 '21 at 16:08

3 Answers3

1

Prefixes for modules have existed for a while, though until recently they didn't affect the module functionality at all (you can read more about this development here). It looks like this specific prefix on these modules was supplied by the installation process for Adonis.js and given that the toolkit uses custom commands for things like building production versions of your project, it may have some custom processes for e.g. loading minimised versions of a dependency specifically for production builds.

There is not currently a standardised meaning or effect of using a prefix like this, outside of node: (which restricts imports to core packages and prevents you from accidentally downloading a malicious package by mistake, due to a typo). Some projects use this for internal purposes and it will depend on the specific project what effects the prefix may or may not have.

Dakeyras
  • 1,829
  • 5
  • 26
  • 33
0

In TypeScript, the : colon is used as a type annotation syntax to specify the type of a variable, parameter, or return value. However, in the context you provided, ':' is not specifically related to type annotations. It is used in the AdonisJS framework to define a declaration merging syntax.

In AdonisJS, the declaration declare module '@ioc:Adonis/Core/Route' is used to extend or augment the existing types or interfaces defined in the @ioc:Adonis/Core/Route module.

For example, you might have a custom route macro that you want to add to the existing Route module. By using the declare module syntax, you can add your custom methods or properties to the Route module, making them available for use throughout your application.

Here's an example of how you might use it:

declare module '@ioc:Adonis/Core/Route' {
  interface RouteContract {
    myCustomMethod: () => void;
  }
}

In the above code, we are declaring that the RouteContract interface in the @ioc:Adonis/Core/Route module should include a new method called myCustomMethod.

This syntax allows you to extend or modify the existing types or interfaces provided by AdonisJS or other libraries, enabling you to add your own custom functionality.

I hope this clarifies the usage of ':' in the AdonisJS declaration merging syntax. Let me know if you have any further questions!

myckhel
  • 800
  • 3
  • 15
  • 29
-5

In import statement you are importing ambient module. I recommend you to read following documentation for more details.

https://www.typescriptlang.org/docs/handbook/modules.html#ambient-modules

Hem Joshi
  • 1
  • 1