Posted onEdited onInnode.js Symbols count in article: 189Reading time ≈1 mins.
The npm install --production command is used to install only the dependencies listed under the dependencies section in the package.json file, excluding the devDependencies. This is typically used in production environments where you want to avoid installing unnecessary development tools and libraries.
Here is a brief explanation of when and why you might use npm install --production:
Production Deployment: When deploying an application to a production environment, you often want to minimize the size of the deployment package and reduce the number of installed packages to only those necessary for running the application. This helps in improving performance and security.
Server Environments: In server environments where the application is running, you generally do not need development tools like testing frameworks, linters, or build tools. Using npm install --production ensures that only the essential packages are installed.
Docker Images: When building Docker images for your application, using npm install --production can help create smaller and more efficient images by excluding development dependencies.
Example usage:
1
npm install --production
This command will read the package.json file and install only the packages listed under dependencies, ignoring those under devDependencies.
Posted onEdited onInrxjs Symbols count in article: 432Reading time ≈2 mins.
Introduction
In RxJS, there is a timeout operator, it’s used to throw an error if the source observable does not emit a value within a specified timeout duration.
Use case
In Angular, the HttpClient service is used to make HTTP requests. Sometimes, we want to set a timeout for the request, if the request does not complete within the specified time, we want to cancel the request and show an error message to the user.
getData() { returnthis.http.get('https://example.com/api/data').pipe( timeout(10000), // 10 seconds catchError((error) => { // Handle timeout or other errors console.error('Request timed out or failed', error); returnthrowError(error); }) ); } }
But, wait, what if I want to add this timeout for all my requests? Do I need to add the timeout operator to every request? The answer is no, you can create an interceptor to add the timeout operator to all requests.
Now, all your HTTP requests will have a timeout of 10 seconds.
But, what if I want to set different timeout values for some specific requests? You can add a custom header to the request and check it in the interceptor, if custom header timeout exists, use the custom timeout value, otherwise use the default timeout value.
Because the first test case only reach the first if branch in getDuration function, it only call getIdleTime once. So there is no problem.
But the second test case need to reach the second if branch in getDuration function, but the mock only take effect in the first if branch, the second branch will still use the original implementation of getIdleTime function, so the test failed.
To fix this issue, you can use mockImplementation instead of mockImplementationOnce in the second test case.
Posted onEdited onInnx Symbols count in article: 409Reading time ≈1 mins.
Nx Library Types by functionality
Feature library
此类library主要负责和业务相关的组件和页面等等。
UI library
此类Library主要是负责和UI相关的功能。
Data access library
此类library主要负责和数据相关的功能,比如和后端API交互,数据处理等。
Utility library
此类library主要负责工具和辅助功能,比如一些通用的函数,服务等。
Nx library types by buildable and publishable
Workspace library(Normal library)
Create without any options, it’s a normal library.
1
nx g @nx/angular:lib libName
No ng-packagr file generated.
No package.json file generated .
No targets/build section in project.json file.
This type of libraries is intended to be used within the monorepo. It was imported by apps or other libraries in the same monorepo. It can’t be builded or published independently.
Buildable library
Create by adding buildable option.
1
nx g @nx/angular:lib libName --buildable
Add ng-packagr file to root of the library.
Add package.json file to root of the library.
name property in package.json is the libName.
Add targets/build section in project.json file.
Executor of build is: "executor": "@nx/angular:ng-packagr-lite"
Buildable libraries are similar to “publishable libraries” described above. Their scope however is not to distribute or publish them to some external registry. Thus they might not be optimized for bundling and distribution.
Buildable libraries are mostly used for producing some pre-compiled output that can be directly referenced from an Nx workspace application without the need to again compile it. A typical scenario is to leverage Nx’s incremental building capabilities.
Publishable library
Create by adding publishable and importPath option. importPath is the path that the library will be imported from, will be used as name of the package.
1
nx g @nx/angular:lib libName --publishable --importPath=@myorg/libName
Add ng-packagr file to root of the library.
Add package.json file to root of the library.
name property in package.json is the importPath.
Add targets/build section in project.json file.
Executor of build is: "executor": "@nx/angular:package"
Publishable libraries is intended to be published outside of the monorepo, and can be imported by other projects.
Default: The longest common path of all non-declaration input files. If composite is set, the default is instead the directory containing the tsconfig.json file.
When TypeScript compiles files, it keeps the same directory structure in the output directory as exists in the input directory.
Importantly, rootDir does not affect which files become part of the compilation. It has no interaction with the include, exclude, or files tsconfig.json settings.
Note that TypeScript will never write an output file to a directory outside of outDir, and will never skip emitting a file. For this reason, rootDir also enforces that all files which need to be emitted are underneath the rootDir path.
重点看这句:rootDir also enforces that all files which need to be emitted are underneath the rootDir path. 也就是说,所有需要被编译的文件都必须在rootDir的路径下,否则就会报错。
ng add vs npm install - ng add is a schematic that can install packages and run additional code to set up the package. npm install is just installing the package.
Generate module based component after Angular 17. Start from Angular 17, Angular CLI use standalone component by default, however, you can still generate module based application with the following command.
1
ng g app my-app --standalone=false
Why Angular use Decorator instead of Abstract Class for Component and Service?
Android No matching client found for package name ‘com.jiyuzhai.caoquanbei’, update package name in file app/src/google-services.json to match your application id.
一个Angular文件内可以定义多个组件。
Event loop and browser rendering是如何交互的,也就是Browser rendering是在Event loop哪个阶段进行的?
You should never use function in angular template. - url
Directive composition API - search on angular.dev, this is a new feature in angular 15, why this is useful?
javascript, generator functions
default import syntax in ES6.
WeakMap is not iterable, why? TypedArray is not array.
Angular BehaviorSubject vs Subject - BehaviorSubject needs an initial value, Subject does not.
Angular aot can be enabled/disabled in angular.json file.
it.each in Jest - This need real example from project.
Why Jest need transformer? Because Jest only understand JavaScript, so it needs transformer to transform other file types to JavaScript. for example ts -> js, tsx/jsx -> js vue/angular -> js, by default Jest use babel-jest as transformer, you can also use angular-jest-preset for Angular project.
Proxy.revocable(target, handler).
What is cross-site request forgery (CSRF) attack? How to prevent it?
Website defacement - what’s this?
steps to use web component.
Grid layout.
HTML page will always show even it contains errors, the console won’t display html error, it only show javascript errors.
Posted onEdited onIntypescript Symbols count in article: 248Reading time ≈1 mins.
Install TypeScript
This directive will install TypeScript globally.
1
npm install -g typescript
Init TypeScript project
This step will create a tsconfig.json file in under typescript-tsc-guide folder.
1 2 3
mkdir typescript-tsc-guide cd typescript-tsc-guide tsc --init # create tsconfig.json file
Create typescript files
Create a folder src under current folder. Then create a file index.ts under src folder.
1 2
mkdir src touch src/index.ts
input the following code to src/index.ts file.
1
constadd = (a, b) => a + b;
Compile TypeScript files
This step will compile the src/index.ts file to src/index.js file.
1 2
cd typescript-tsc-guide tsc
Compile options
outDir
Usually, we put the emitted files under dist folder. To do this, we need to modify the tsconfig.json file.
1 2 3 4 5
{ "compilerOptions":{ "outDir":"./dist"// output directory for the emitted files } }
Run tsc again, the emitted files will be under dist folder.
rootDir
We can also specify the source folder by using rootDir option, in this way, only files under the source folder will be compiled.
1 2 3 4 5 6
{ "compilerOptions":{ "outDir":"./dist", "rootDir":"./src"// specify the source folder } }
If you specify the rootDir option, you can’t put the source files outside the source folder. otherwise you’ll got the following error:
1
error TS6059: File '/typescript-tsc-guide/xxx.ts' is not under 'rootDir''/typescript-tsc-guide/src'. 'rootDir' is expected to contain all source files.
To test this, create a file test.ts under project root(same location as tsconfig.json) and run tsc command, you’ll get the error.
angular.json(for Angular project) or project.json(For Nx based mono repo)
1 2 3 4 5 6 7 8 9 10 11 12 13
{ "architect":{ "build":{ "builder":"@angular-builders/custom-webpack:browser",// change builder "options":{ "customWebpackConfig":{ "path":"./extra-webpack.config.js",// supply a path to the custom webpack config "mergeStrategies":{"externals":"replace"} } } } } }
How to choose?
If you are using Angular Module Federation, you should use ngx-build-plus, and it is installed automatically when you create a new Angular project with Angular Module Federation. This package has not been upgraded for a long time, but it is still working. ngx-build-plus support hooks.
If you are not using Angular Module Federation, you can choose either of them, but @angular-builders/custom-webpack is more popular. This package is upgraded frequently.