I'm currently working on a React project using typescript and the bundler ParcelJs. I'm using a tsconfig.json file to configure typescript but for some reason that I don't know, Parcejs doesn't consider it ignoring all the errors on build.
For example, I wrote this code that is invalid in typescript:
const message: number = "Hello world!";
My eslint trigger this error correctly:
Type '"Hello world!"' is not assignable to type 'number'
But when I build this file using parcel ./src/index.tsx it compiles without errors.
According to the official documentation and this issue I need to declare a Validator in the ParcelJs configuration file .parcelrc:
{
"extends": "#parcel/config-default",
"validators": {
"*.{ts,tsx}": ["#parcel/validator-typescript"]
}
}
The parcel validator need to be installed in package.json:
...
"dependencies": {
"#parcel/validator-typescript": "2.0.0-nightly.112",
...
So at the end my project structure looks like this:
├── .cache/
├── dist/
├── src/
│ ├── index.tsx
├── .parcelrc
├── package.json
├── tsconfig.json
Note:
If I write invalid json data in the .parcelrc file, ParcelJs still succeeds to build without any problem as if the configuration file was ignored
Does anyone have an idea how to get ParcelJs to take into account the typescript configuration?
I think that 2.0.0-nightly.112 was published before I made this fix. There's also this issue that bit me when I accidentally installed an older version of parcel. Can you try installing the latest nightly release of both parcel and #parcel/validator-typescript, deleting your .parcel-cache folder and trying again? If that doesn't work, would you mind sharing a repo where I could try reproducing the issue?
Another thing to try is to make sure there is a yarn.lock file in the same directory that contains the tsconfig.json. From looking at the code, I know that parcel sometimes uses this to detect the root of the project (and the tsconfig.json file). If parcel can't find a tsconfig.json file, it won't typecheck due to this issue. (If this is the root cause, it's probably something we should fix with Parcel, but it would be a good thing to test).
One other current issues with #parcel/validator-typescript that I'm working on fixing is #4204, but based on what you described, I don't think it is the cause of what you're hitting.
Related
I am trying to share enums and dtos between my backend and frontend (NestJS at the back, React at the front. Both use typescript).
To do so, I tried to add my "shared" files to the client's tsconfig.json > compilerOptions > paths like so:
"paths": {
"#project/shared/*": [
"../server/src/common/*"
]
}
Unfortunately, this doesn't seem to work. When I run the react client, it throws "webpack Module not found."
This is my file structure:
App.tsx consumes the TestEnum from the server's common directory.
What you are trying to achieve is essentially a monorepo. The best way to solve the dependency issue is of monorepo is to refer each package as a separate package; it's clear in the comment that you do not want to do so but it's de facto standard already.
It's because importing what's outside the workspace(where the package.json lies) is really really bad idea. I've tried this but not recommend it; the package managers, bundlers and IDEs(let's say "the tools") will work strangely due to conflict issues:
the file's root has it's own config such as tsconfig.json, package.json which can conflict with project's setting. it is not clear which one they should follow.
the tools usually have their boundary due to security issues; if the tool can leave the project root, it can do something sneaky, such as compromising internal system files.
when deploying the apps in the clouds, they(client/server) have to stick together all the time. it becomes devOps issue.
Solution
the best way to deal with this, is to use workspace feature of package managers. here the term workspace means separate packages inside monorepo. npm's explanation of workspace clearly fits to your settings.
Workspaces is a generic term that refers to the set of features in the npm cli that provides support to managing multiple packages from your local file system from within a singular top-level, root package.
npm and yarn both support workspace features.
npm workspace
yarn workspace
from now on, the project structure should be perceived as this.
project root --> place workspace setting here
|-package root(client) --> workspace
|-package root(server) -> workspace too
most workspace features support 'hoist' feature. without publishing the actual packages, your packages in different folders can be imported and referred as if they are published, via softlink(symlink, something like shortcut in windows). it's same as typescript config's path alias but it actually creates a real file so that the tool won't have any side effects.
to do so, setup workspace config at your project root so that the tool can perceive this packages as workspaces.
# create shared package directory first
$(projectRoot) mkdir shared
// projectRoot/package.json
{
"name": "project",
"workspaces": [
"client",
"server",
"shared"
]
}
// projectRoot/client/package.json
{
"name": "#project/client",
"private": true, // set this to private so that this won't get published by mistake
"packages": {
// your default packages
"#project/shared": "1.0.0" // <-- version has to match
}
// ...
}
// projectRoot/server/package.json
{
"name": "#project/server",
"private": true
// ...
}
// projectRoot/shared/package.json
{
"name": "#project/shared",
"private": false, // make it public so that you can publish it later
"version": "1.0.0"
// ...
}
# remove all `node_modules` folder.
$(projectRoot) rm -rf client/node_modules
$(projectRoot) rm -rf server/node_modules
# reinstall node_modules so that the projects can be symlinked as packages.
$(projectRoot) npm i
then it'll work as workspace from now on and the packages will be linked. it's that simple!
Caveat
as you can see, cleaning up and bootstrapping the package is bit of a work. that's where monorepo tool(i.g. lerna) comes handy; try expand your monorepo knowledge from here.
if it's deployed to AWS, it'll probably work. however, it's best to publish the #project/shared package to npm in the end. many cloud instances don't support symlinks(vercel, netlify...) still, symlink is useful in local/dev environment.
Edit
here's working demo:https://github.com/rabelais88/monorepo-workspace-npm
check readme before proceed. after typing npm install, symlinked hollow modules are seen. as far as they are there, scripts will run fine
edit 2: after setting up the workspace, some dependencies or all the dependencies will be placed at project root instead of each workspace. it is intentional by design, to avoid having duplicated packages in each workspace. this might surprise some but actually very useful in maintaining bigger systems.
I want to share what I had to do in my projects and file structure to share models, types, enums, entities, etc., between the server (NestJS) and the client (React).
First of all, I started setting up npm workspaces to achieve a monorepo, as #sungryeol described in his detailed answer (thanks for it).
Unfortunately, it was only half of the way to making it work. So here is a solution I came up with (remarks are welcome) in addition to workspaces:
Client (react)
create react app is designed to compile code only from the src directory. Because I am using typescript in my shared code, I need to compile it to make it work. To make CRA compile my shared code, I used react-app-rewired to modify the webpack config with the following config-overrides.js file:
const path = require('path');
module.exports = function override(config, env) {
const babelLoaderModule = require.resolve('babel-loader');
const rule = config.module.rules[1].oneOf.find(rule => rule.loader === babelLoaderModule);
rule.include = [
rule.include,
path.resolve(__dirname, '../shared')
];
return config;
}
React finally started accepting my shared code without any issues.
Server (NestJS)
Since NestJS uses tsc as the compiler, I had to specify the directory of the shared code in the server's tsconfig.json as a path.
"paths": {
"#managed-businesses/shared/*": [
"../shared/*"
]
}
This made my shared code compile when I started the nest app, but it couldn't find the entry point, as it compiled the code in the following structure
dist
├── server
│ ├── src
│ ├── main.d.ts
│ └── main.js
├── shared
│ ├── dtos
│ └── enums
└── tsconfig.build.tsBuildInfo
while Nest lookup for the src/main.js file to be at the root of the dist directory.
To fix that, I had to tweak the nest-cli.json file to lookup the file in the dist/server/src directory. Here is my new config for nest-cli.
{
"collection": "#nestjs/schematics",
"sourceRoot": "server/src"
}
After all, the server finally works with the shared code, and I don't need to duplicate it between the projects. I am probably missing something or did something incorrectly, and I'll be happy to know if there is a better way, but at the bottom line, it works.
create-react-app version
react: v17.0.1
react-scripts: v4.0.1
storybook version
#storybook/react: v6.1.6
#storybook/addon-docs: v6.1.6
#storybook/core: v6.1.6
and i could run yarn start to run react app and could run start-storybook -p 9009 -s public to start storybook.
when it comes to build react app , it comes issue. look below.
when run "yarn run build", it actually run react-app-rewired start.
it shows below
There might be a problem with the project dependency tree.
It is likely not a bug in Create React App, but something you need to fix locally.
The react-scripts package provided by Create React App requires a dependency:
"babel-loader": "8.1.0"
Don't try to install it manually: your package manager does it automatically.
However, a different version of babel-loader was detected higher up in the tree:
/Users/yejinlei/Documents/playground/personal/react-temp/node_modules/babel-loader (version: 8.2.1)
Manually installing incompatible versions is known to cause hard-to-debug issues.
If you would prefer to ignore this check, add SKIP_PREFLIGHT_CHECK=true to an .env file in your project.
That will permanently disable this message but you might encounter other issues.
To fix the dependency tree, try following the steps below in the exact order:
1. Delete package-lock.json (not package.json!) and/or yarn.lock in your project folder.
2. Delete node_modules in your project folder.
3. Remove "babel-loader" from dependencies and/or devDependencies in the package.json file in your project folder.
4. Run npm install or yarn, depending on the package manager you use.
In most cases, this should be enough to fix the problem.
If this has not helped, there are a few other things you can try:
5. If you used npm, install yarn (http://yarnpkg.com/) and repeat the above steps with it instead.
This may help because npm has known issues with package hoisting which may get resolved in future versions.
6. Check if /Users/yejinlei/Documents/playground/personal/react-temp/node_modules/babel-loader is outside your project directory.
For example, you might have accidentally installed something in your home folder.
7. Try running npm ls babel-loader in your project folder.
This will tell you which other package (apart from the expected react-scripts) installed babel-loader.
If nothing else helps, add SKIP_PREFLIGHT_CHECK=true to an .env file in your project.
That would permanently disable this preflight check in case you want to proceed anyway.
P.S. We know this message is long but please read the steps above :-) We hope you find them helpful!
I run npm ls babel-loader and see below show
react-temp#0.1.0 /Users/yejinlei/Documents/playground/personal/react-temp
├─┬ #storybook/addon-docs#6.1.6
│ └─┬ #storybook/core#6.1.6
│ └── babel-loader#8.2.1 deduped
└── babel-loader#8.2.1
and i follow the instruction and run yarn add babel-loader#8.1.0. when it done i get below :
react-temp#0.1.0 /Users/yejinlei/Documents/playground/personal/react-temp
├─┬ #storybook/addon-docs#6.1.6
│ └─┬ #storybook/core#6.1.6
│ └── babel-loader#8.2.1
└── babel-loader#8.1.0
then run yarn run build, which actually run react-app-rewired build and get below error
playground/personal/react-temp/node_modules/react-scripts/scripts/build.js:19
throw err;
^
TypeError: aGeneratedCode.split is not a function
at Function.SourceNode_fromStringWithSourceMap [as fromStringWithSourceMap] (playground/personal/react-temp/node_modules/source-map/lib/source-node.js:64:41)
can not build
so then i target to the code node_modules/source-map/lib/source-node.js
and see the error code:
/node_modules/source-map/lib/source-node.js
64 var remainingLines = aGeneratedCode.split(REGEX_NEWLINE);
I run and analysis the code. the the aGeneratedCode will be "obejct", so i change the code as below.
var remainingLines = typeof(aGeneratedCode) === "string" ? aGeneratedCode.split(REGEX_NEWLINE) : [];
then i could run build / start of the cra app and storybook now.
but why and how to permantly fix it ?
I was able to work it around with "resolutions" property
(https://classic.yarnpkg.com/en/docs/package-json/#toc-resolutions)
"resolutions": {
"babel-loader": "8.1.0"
},
Currently, I am developing a package and going to publish it in npm, I wrote it with TypeScript but faced some problems in package bundling.
I put my codes (TypeScript/Less files) in src and the structure shows below:
src
├── components
│ ├── table.less
│ └── table.tsx
├── index.tsx
└── utils
└── mock.tsx
Since I want to publish it in npm, so I need it be compiled to JavaScript/CSS files (in lib folder) so that other developers can import it directly (without extra compiling in their project), the structure should be like this:
lib
├── components
│ ├── table.css
│ └── table.js
├── index.js
└── utils
└── mock.js
But I faced some problems:
If I use tsc command, tsx files can be compiled to js files rightly, but less files will be ignored;
If I use webpack commands rather that tsc, the result will be bundled in one file, and lost it's original structure, and it will confuse package users;
I think I need to make it works by:
Compile all files from src to lib one by one(Keep the same folder structure);
tsx files to js files;
less files to css files;
add declaration files such as index.js.d.ts and index.css.d.ts;
modify some writing styles such as import styles from './index.less' to import styles from './index.css'; Or inject stylesheets into js files directly; (I am not sure about this step)
Bundle one js file with all of things in it (with webpack), as well as minimized version;
The package contains JSX grammar since I used React in it.
As I know, I need to use Babel in compiling TS/JS codes, and webpack in compiling less and other assets, but I am confused about how to combine them in working together.
So any suggestions on how to combine cool tools in solving my problem? I looked through really a lot tutorials but most of them are React/Less/TypeScript Project (not package development) or TypeScript package (without using less/css).
Thanks really a lot.
This question has been around for a long time, I hope my answer can still help people who click in to find the answer:
As you said, .tsx files can be compiled by tsc into .ts files, but .scss or .less not. so you need to use node-sass or less to process them.
Based on your directory structure above, you can write the following commands under scripts in package.json:
if you use less:
"scripts": {
"build:css": "lessc src/components/table.less lib/components/table.css"
}
or node-sass:
"scripts": {
"build:css": "node-sass -r src -o lib"
}
Yes, node-sass scans the root folder and automatically compiles the .scss files in it into .css files in the corresponding directory. Using less may require you to spend more time exploring the useful methods.
But just converting to css files doesn't help, because the style files introduced in the js files still have .less or .scss suffixes, we need to replace this part of the .js file, you may think of using node to read each .js file, and then use the regular match, this idea is generally no problem, but the global matching of the regular replacement can cause some hidden problems (although almost impossible), so I use AST to do the replacement.
Yes, I wrote an easy-to-use command line tool for this, you just need to:
npm i tsccss -D
and add npm script:
"scripts": {
"build:css": "node-sass -r src -o lib",
"compile": "tsc -p tsconfig.json && tsccss -o lib",
"build": "npm run build:css && npm run compile"
}
Yes, as you can see, tsccss -o lib is completely enough.
Hope you enjoy it, here is github/repo: tsccss
After upgrading to the newest version of create-react-app I've run into a bit of a big problem where web pack no longer watches for changes.
In my project I use node_modules to keep all of my project source code, and while I can import code from here without problems, web pack no longer watches this folder which means any changes made require restarting the project which is very frustrating.
bellow is an example of what my project structure looks like:
my-app/
node_modules/
package.json
.gitignore
public/
favicon.ico
index.html
src/
node_modules/
my_app/
components/
ui/
resources/
styles/
images/
App.js
index.js
Again, creating an consuming a new component is no problem, but making changes no longer get updated live in the browser, and I can't seem to find what the solution is.
In react-scripts in create-react-app, there is a configuration for webpackDevServer; webpackDevServer.config.js
There the directory node_modules is ignored. Could you remove this and try out?
I am trying to introduce Go vendoring (storing dependencies in a folder called vendor) to an existing App Engine project. I have stored all dependencies in the vendor folder (using Godep as a helper) and it looks right, but running the application locally I get the following error:
go-app-builder: Failed parsing input: package "golang.org/x/net/context" is imported from multiple locations: "/Users/erik/go/src/github.com/xyz/abc/vendor/golang.org/x/net/context" and "/Users/erik/go/src/golang.org/x/net/context"
I believe the two locations should resolve to the same location, as Go applications should look in the vendor folder first. Is there a way to make Appengine understand that both dependencies are the same?
Your project directory (where app.yaml is) is probably in the GOPATH/src.
It shouldn't be.
The go-app-builder will take everything in the app.yaml folder (and below) and additionally merge your GOPATH into it, meaning now you have it twice.
The solution is to move app.yaml out of the GOPATH/src folder.
Additionally you'll find that goapp test works differently from goapp serve and goapp deploy when it comes to resolving dependencies.
So this is the solution I have been using (haven't used golang app engine in a while already) and it's the only setup I've found to work properly for all the goapp commands and for govendor to work properly (not sure about godep)
/GOPATH
├──/appengine
| ├── app.yaml
| └── aeloader.go
└──/src
└── /MYPROJECT
├── main.go
├── /handler
| └── handler.go
└── /vendor
details:
file: GOPATH/appengine/aeloader.go (NOTE the init function is necessary, probably a bug though)
package mypackage
import (
_ "MYPROJECT"
)
func init() {
}
now run goapp serve and goapp deploy from ../GOPATH/appengine/ and goapp test ./... from ../GOPATH/src/MYPROJECT
P.S. I find the global GOPATH thing silly and simply set my GOPATH to current project folder (in the example above /GOPATH) and check the whole thing into version control.
I use a Makefile to move the vendor directory to a temporary GOPATH:
TMPGOPATH := $(shell mktemp -d)
deploy:
mv vendor $(TMPGOPATH)/src
GOPATH=$(TMPGOPATH) gcloud app deploy
mv $(TMPGOPATH)/src vendor
I store this Makefile at the root of my service near the vendor directory and simply use make deploy to deploy manually or from the CI.
It works with Glide, Godeps or any tool that respects the Go vendor spec.
Please note, that you really need to move the vendor directory out of the build directory, otherwise the GoAppEngine compiler will try to build the vendor dependencies, potentially causing compile errors.
I just ran into this issue myself actually. The problem occurs when you're using the App Engine tools to build any package which imports something that is using vendoring, but the package you're trying to run doesn't have the import within it's vendor directory.
So, for example, if I'm trying to run package foo, which imports package bar, and both of which use the github.com/gorilla/mux library, if the bar repository has a vendor/ directory that contains gorilla/mux, but the foo package doesn't have gorilla mux in it's vendor/ directory, this error will occur.
The reason this happens is that the bar package will prioritize it's own vendor package over the one in the GOPATH, which is what foo will be using, causing a difference in the actual location of the imported paths.
The solution I found to this issue is to make sure that the foo directory is in the GOPATH and has the vendor directory properly installed. It's important to note that the vendor/ convention only works from within the GOPATH.
I managed to resolve this error using govendor instead of Godeps. The root cause appears to have been that vendored references with their own vendored references was not resolved correctly by Godeps.
The answer provided by Su-Au Hwang is also correct - you do have to keep app.yaml separate from your source.
Also got the same problem.
In the docs Google suggests the following:
For best results, we recommend the following:
Create a separate directory in your app's directory for each service.
Each service's directory should contain the service's app.yaml file and one or more .go files.
Do not include any subdirectories in a service's directory.
Your GOPATH should specify a directory that is outside your app's directory and contain all the dependencies that your app imports.
But this messes up my project structure, which looks like this:
GOPATH/
└── src
└── github.com
└── username
└── myproject
├── app.yaml
├── cmd
│ └── myproject
│ └── main.go
├── handlers
│ └── api.go
├── mw
│ ├── auth.go
│ └── logger.go
└── vendor
Where the myproject directory is a git project and the vendor folder contains all dependencies.
Running gcloud deploy from the myproject directory where app.yaml file lives doesn't work because first, main.go file is not in the same directory and second (from the same doc):
you must be careful not to place your source code at or below your app's directory where the app.yaml file is located
What I ended up doing is building my own custom runtime instead, which turned out to be a very clean solution.
Simply generate the Dockerfile with the following command:
gcloud beta app gen-config --custom
Modify it, then specify runtime: custom in your app.yaml and deploy normally.
The trick here is of course that you're in control what gets copied where.
Here is my Dockerfile:
# Dockerfile extending the generic Go image with application files for a
# single application.
FROM gcr.io/google-appengine/golang
ENV GOPATH /go
# The files which are copied are specified in the .dockerignore file
COPY . /go/src/github.com/username/myproject/
WORKDIR /go/src/github.com/username/myproject/
RUN go build -o dist/bin/myproject ./cmd/myproject
# All configuration parameters are passed through environment variables and specified in app.yaml
CMD ["/go/src/github.com/username/myproject/dist/bin/myproject"]
Don't forget that App Engine expects your application listening on port 8080. Check out Building Custom Runtimes doc for more details.