Is there way to import modules with alias?
E.g. I have angular service in folder common/services/logger/logger.ts.
How to make import looks like import {Logger} from "services" where "services" contains all my services?
I've looked at this and this but didn't understand how I can use it.
Create one file that contains all of your services and give it a name, e.g. services.ts. That file can be seen as a barrels, because it will contain and export all of your services. The file could look like this:
export * from './logger/logger';
export * from ...;
etc.
Now you can import this single file as follow:
import * as Services from 'common/services';
Then you can access your servies via Services.Logger, or directly import the service you want via:
import {Logger} from 'common/services';
Note, you have change the paths since this is just an example. For more information about this technique, have a look here.
To solve this problem I used webpack. If you want to import dependencies everywhere you want, you will need to add alias in webpack config. For example, you have folder shared in root with services folders in it. Folder services should have index.ts that exports all services you need (e.g Logger). So alias would be "services" => "shared/services".
To make autocomplete work in WebStorm you just need to mark shared folder as a "Resource root".
Related
My organization is fairly big, we use node and are looking into potentially using Cypress. We have like 100 different projects and each have their own Github repository, but we like to share functions and methods and import them. So my question is, is there a way to do that with Cypress custom commands so that it can be a dependency and you can just import them?
Is it as easy as just adding a Github URL in the dependency?
You will have to create a library/package that has all those commands implemented. And then in every project you should install Cypress and then import those commands. For example:
In the library you would have something like this in commands.ts if you're using Typescript or commands.js if you're using JavaScript.
Cypress.Commands.add('take', (testSelector) => {
return cy.get(`[data-test='${testSelector}']`);
});
Then, in the repo you want to use it you'll have to import it from the library.
In your cypress/support/index file you should add:
import '#organization/library-name/path/to/commands';
Note that the name of the package can depend on how you choose to generate it.
I've been trying JSPM for few days, but couldn't find an easy way to import module from folder.
As described in the node documentation require for the method, it is possible to require a folder. This would resolve to folder/index.js if the file exists.
Is there a way to do that with SystemJs/JSPM ?
I'm trying to use import a package for internal use, but I have been having some issues.
My directory structure looks like this:
app/
model/
file1.go
file2.go
...
main.go
When I try to build the program, I get an error that looks something like this:
/usr/local/go/src/pkg/model (from $GOROOT)
I want to be able to call the model programs in any of my other programs in the app simply using:
import "app/model"
What are my options when it comes to doing this?
You import from GOPATH level .. all of your packages should live there.
For example, assuming your application is here:
$GOPATH/src/dtrinh100/app/
..and your package you wish to import is here:
$GOPATH/src/github.com/other/package
Your import would be:
import "github.com/other/package"
You should review the literature around what the GOPATH environment variable is all about. When beginning Go, it is important you understand its purpose and initially, you should place all of your projects/packages inside of the GOPATH.
When you import a custom package, Go looks for its definition in each workspace listed in the GOPATH environment variable. Your custom package should be defined in a src subdirectory.
If you keep your code in a source repository somewhere, then you should use the root of that source repository as your base path. For instance, if you have a GitHub account at github.com/user, that should be your base path.
Note that you don't need to publish your code to a remote repository before you can build it. It's just a good habit to organize your code as if you will publish it someday. In practice you can choose any arbitrary path name, as long as it is unique to the standard library and greater Go ecosystem.
You should use github.com/user as our base path. Create a directory inside your workspace in which to keep source code:
$ mkdir -p $GOPATH/src/github.com/user
You can look at How to Write Go Code for more details.
I am creating an addon project.
I have a file in app/utils with the following import
import config from '../config/environment';
I want to move this to addon/utils but the above import does not work anymore and I get an error when doing ember serve. I need access to the consuming applications environment.
How do I do this from the addon folder?
You simply can't. The addon namespace is meant to be isolated from the app namespace and it is, that's why it won't let you import the environment config. If you want to access it it needs to be in the app namespace.
One way around this, is to keep it in the addon namespace, and require users to import it in their own app/utils and pass in the config themselves. It would look something like this:
// app/utils/their-util.js
import config from '../config/environment';
import yourUtil from 'the-addon/utils/your-util';
export default function() {
return yourUtil(config);
}
You could even do this in your addon's app namespace to have it included by default, and they could then override it if they wanted to do something custom.
Obviously this isn't an ideal solution but it's what's available today.
Go, Golang : does not make sense that I have to have files before import
I am trying to go to next step but keep getting errors
I have the package that I want to import ready.
All I need to do is to import the external package from github and be able to use it on any code.
So this is what I did.
mkdir $HOME/go
export GOPATH=$HOME/go
go get github.com/user/project
This runs successfully. I downloaded it onto here with source code files from github
/Users/user/go/src/github.com/user/project/project.go
So to use this package that I just import I do
go run /Users/user/Desktop/code.go
But I am getting the following errors
MacBook-Air:~ user$ go run /Users/user/Desktop/code.go
Desktop/code.go:32:8: cannot find package "project" in any of:
/usr/local/go/src/pkg/project (from $GOROOT)
/Users/user/go/src/project (from $GOPATH)
What should I do? AM I missing something? Thanks in advance and please help me. I wrote a lot of code but being very frustrated not being able to distribute it because of this.
The error message says at line 32 in your code.go it can't find package "goling".
Assuming that is a local package you want to use, you need to make sure it is in your GOPATH.
If you set GOPATH then you should develop your code within it, so moving the "goling" directory into /Users/user/go/src is the right thing to do.
Alternatively "goling" could be a typo, so check the imports in code.go. If you want to import an project from github the import should say
import "github.com/user/project"
And you then use the parts of project with a prefix of project.
If that doesn't help you get it working, then post the imports section of code.go.
It looks like you've got the external package in the same folder as your main package which uses it. In go, all packages must be in separate directories. It looks like the github project itself is actually doing that. If you separate the packages into different directories it should work properly.