I am trying to migrate to Nx and setup a monorepo.
Currently running into issues with path alias imports.
#backend/kernel": ["libs/kernel/src/index.ts"],
#backend/kernel/*": ["libs/kernel/src/*"]
When issuing imports of the form
import { Something } from "#backend/kernel".
On the other hand, when doing imports that go deeper,
import { SomethingElse } from "#backend/kernel/somewhere/deeper" I get TS2307 when compiling. No issues in VSCode; all types are correctly resolved.
Anybody has some advice?
Have you export the code correctly from your lib in the root index of your library ?
If not, at the index.ts at the root of your library, just export your code like this :
export { Something } from 'your/path'
or if you already export inside your library you can use a wildcard *.
After that, remove the line
#backend/kernel/": ["libs/kernel/src/"]
from your tsconfig.base.json
Cheers
Related
I am confused by something on the react-redux documentation
https://redux-docs.netlify.com/recipes/configuring-your-store/
specifically, in attempting to build the prototype app (which is provided here https://github.com/reduxjs/redux/tree/master/examples/todos/src), I am confused about the line
import rootReducer from './reducers'
what is confusing is that in the app directory, there is no such file reducers.js, there is only a folder reducers/ which contains
index.js
todos.js
todos.spec.js
visibilityFilter.js
I see no file for rootReducer, so I am assuming (correctly?) that the ES6 syntax for the above code is importing the default export from
reducers/index.js
is that correct?
In that file, I see
export default combineReducers({
todos,
visibilityFilter
})
do I understand correctly the defaulted export anonymous function, exported from reducers/index.js, is then imported into index.js as rootReducer ?
Yes. That's ES6 "default export" syntax, combined with an index.js file. When an index.js file exists in a folder, you can specify just the folder name in the import statement, and the bundler knows to look at index.js automatically.
We're using Cypress for testing an app build with Create React App, and our CRA app is setting a custom import path in .env – NODE_PATH=src/ – so that we can import files "absolutely", e.g. import Foo from 'components/Foo' vs. import Foo from '../../components/Foo'
The problem is, if we import one of the files from our CRA into a Cypress test, and the imported file includes something like import routes from 'lib/routes' Cypress doesn't know how to process that path and the import fails.
Example
Let's say we have the following files:
/cypress/integration/test.js
import routes from '../../src/lib/routes';
// Tests here…
/src/lib/routes.js
import { routeHelpers } from 'lib/routes';
// Routing code here
In this scenario, when /cypress/integration/test.js imports /src/lib/routes.js it will encounter the absolute import of 'lib/routes' and have no idea how to resolve that path.
Is there a way to set custom paths for Cypress to include when searching for imports in this way? In the case of this arbitrary example, it would mean telling Cypress to use src as a directory to resolve imports from.
Easiest solution for this turned out to be simply running the cypress commands with NODE_PATH=src. So my package.json was simply updated to the following:
"scripts": {
"cypress:open": "NODE_PATH=src cypress open",
"cypress:run": "NODE_PATH=src cypress run",
},
I had a similar issue and I was using .env with NODE_PATH=src
Solution: I removed .env and created jsconfig.json for absolute imports.
{
"compilerOptions": {
"baseUrl": "src"
},
"include": ["src"]
}
This is the recommended approach in the CRA docs: https://create-react-app.dev/docs/importing-a-component/#absolute-imports
We've updated our project to use Angular & Rxjs 6 and all works fine.
We've also updated the code to use the pipe operators, so we would like to drop rxjs-compat.
The only issue is that one of our dependencies still uses the old import syntax for Observable and Subject.
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
Is there any way to provide our own minimal rxjs-compat just for these two classes?
The library does not do anything fancy with Observable and Subject, and doesn't use any operators, so it seems overkill to import the full rxjs-compat package.
I don't think you need to copy any files when using the paths option.
I'd try something like this:
"compilerOptions": {
"baseUrl": ".",
"paths": {
"rxjs/Observable": ["./node_modules/rxjs/internal/Observable"],
"rxjs/Subject": ["./node_modules/rxjs/internal/Subject"]
}
}
Note that baseUrl must be specified.
The question is whether or not the paths option affects other dependencies in node_modules. I'm not sure and I didn't try it.
Using paths as metioned by #cartant I managed to get it to compile without rxjs-compat, but the output main.js is even larger than using the full compat library...
With rxjs-compat = 691 KB
With patched library import = 671 KB (ideal size)
with rxjs-compat paths hack = 700 KB
I've probably done something wrong, but I don't have time to investigate this further.
Steps for the hack:
Create a folder src\rxjs-compat\node_modules (note the node_modules name, it's used to bypass a webpack bug)
Copy the d.ts and .js files you need from rxjs-compat (in my case Observable and Subject)
Edit tsconfig.app.json and add the following paths in the compilerOptions:
"paths": {
"rxjs-compat/Observable": [ "rxjs-compat/node_modules/Observable"],
"rxjs/Subject": [ "rxjs-compat/node_modules/Subject"]
}
How to import internals packages in Go ?
import (
"runtime/internal/atomic"
"runtime/internal/sys"
)
Like this without get a error:
imports runtime/internal/atomic: use of internal package not allowed
And use internal funcs in a main package ?
Background
Go encourages structuring a program as a collection of packages interacting using exported APIs. However, all packages can be imported. This creates a tension when implementing a library or command: it may grow large enough to structure as multiple packages, but splitting it would export the API used in those additional packages to the world. Being able to create packages with restricted visibility would eliminate this tension.
A rule proposed to Go 1.4
An import of a path containing the element “internal” is disallowed if the importing code is outside the tree rooted at the parent of the “internal” directory.
Short answer
You can't (at least easily) and you shouldn't.
I will show you how I use internal nettest package:
// I copied nettest to vendor with `dep ensure` I think. Then:
mkdir vendor-local
cp -r vendor/golang.org/x/net/internal/nettest ./vendor-local/nettest
vim ./vendor-local/nettest/stack.go and remove import comment // import "foo" [1]
// Use full import in your go file:
import "github.com/foo-user/bar-project/vendor-local/nettest"
[1]: https://github.com/golang/net/blob/a8b9294777976932365dabb6640cf1468d95c70f/internal/nettest/stack.go#L6
Docs about import comments
You may find all import comments in your internal package with grep -r "// import" ./vendor-local/nettest
Why can't I copy nettest to ./vendor and use shorter import
You can, but utils like dep ensure that don't support local packages will purge your copy.
I'm stuck on running the BeeGO app using "bee run" it says
The thing is I've already have setup properly my GOPATH to D:/Web Dev/GO/BeeGO/test-project/
and also routers path does exist and I've tried to manual build the file but it doesn't generate an .exe file.
Anyone knows how to fix this?
I'm using Windows 8.1 Pro (64-bit)
Thanks
GO expects the directory structure under $GOPATH in following ways as described in code organization:
$GOPATH/src <--- where your source code goes
/pkg
/bin
Instead of placing your source files directly under $GOPATH (D:/Web Dev/GO/BeeGO/test-project/ for your case), you want to move your code under $GOPATH/src.
D:/Web Dev/GO/BeeGO/test-project/src/main.go
D:/Web Dev/GO/BeeGO/test-project/src/quickstart/routers/routers.go
D:/Web Dev/GO/BeeGO/test-project/src/quickstart/controllers/controllers.go
import path should be always starting from $GOPATH/src. routers.go can be always imported as import "quickstart/routers" and controllers.go can be imported as import "quickstart/controllers".
That's not how you import a package.
The import path is relative to $GOPATH/src. use:
import "quickstart/routers"
Finally fixed the bug from the framework,
What I did:
in main.go import from
"D:/Web Dev/GO/BeeGO/test-project/quickstart/routers"
I changed it to _"../quickstart/routers" make sure to include _ this means to import the library even if it is not used,
Then in the routers/router.go I changed the import path
"D:/Web Dev/GO/BeeGO/test-project/quickstart/controllers" to "../controllers"
It seems BeeGO doesn't generate the template properly and caused the build to fail.
Another possiblity for this error, is when you copy-paste code from the internet,
and
import "quickstart/routers"
became
import "quickstart/routers "
due to bugs in some CMS/Blog systems (notice the space at the end before the closing quote...).