What does `webpack` function do? - node.js

I have use Webpack for a long time and i always tangled with Webpack configurations. Recently, i try something to make VSCode show intellisense with Webpack and i found out webpack is a function.
That's great because now i can enable intellisense without reading complicated boring documents. But when it runs, it's not wonderful as i respect. The type checking warning wrong. I tried to run webpack({}) in console and it return something like Compiler schemas or default configuration. Although I found another way to enable intellisense using /** #type {webpack.Configuration} */ but still wonder; what does webpack() function really do and do we need to care about it?
Thanks, Sincerely!

webpack function enables a programmatic use of Webpack.
As Webpack Node.js API documentation states,
The imported webpack function is fed a webpack Configuration Object and runs the webpack compiler if a callback function is provided
<...>
If you don’t pass the webpack runner function a callback, it will return a webpack Compiler instance. This instance can be used to manually trigger the webpack runner or have it build and watch for changes, much like the CLI.
It isn't needed in webpack.config.js because this is already handled by Webpack CLI.

Related

How to enable decorators in NodeJs lambda function that are built and deployed using AWS CDK?

I have refactored my lambda function by applying DI principles using the tsyringe library. Unfortunately, after my lamdas are built to the cloud, services have not been injected properly and dependencies are undefined.
The issue is probably connected to esBuild because I could replicate this issue locally if I used it for code compilation. If I compile this code locally using TSC this issue does not occur and dependencies are properly injected.
How can I tinker the build process so I can still use AWS CDK and dependencies will be injected?
It looks like tsyringe requires the emitDecoratorMetadata option, which isn't supported natively by esbuild.
You have a couple of options to make it work:
Set the preCompilation option to true. This will run your build with tsc instead of esbuild. It'll be a bit slower, but you'll be able to emit the decorators.
Use a plugin like esbuild-plugin-tsc.
For Lambda, option #1 here is probably fine.

Express JS in Altassian Forge causing "process.cwd is not a function" error

I am new to development on Jira Cloud and am currently exploring creating apps using the Atlassian Forge. I was trying to use Express JS in the code as a middleware. Turns out that during deployment, it keeps giving this error "process.cwd is not a function."
The error received while deploying the app.
I tried installing process module (via npm i process; which was successfully done) and updating the webpack version but none of it worked.
Can someone please tell what could be causing this error?
If possible please suggest the alternative for using Express JS in Altassian Forge?
Thanks in advance.
Apoorva
From the documentation:
When a Forge app is invoked, the JavaScript code is executed within the app sandbox. This environment differs from a traditional Node.js environment you might already be familiar with.
You'll need to write your functions in a subset of Node.js. Some globals are not exposed, for example:
process (except for process.env)
queueMicrotask
which means that some NPM packages that depend on these may not function correctly.
If Express JS depends on process.cwd(), that would explain the error. You may be able to work around this if it depends on it in theory but not in practice:
If process.cwd() is only used in test cases or example code, you can delete it or make sure it's not bundled.
If it is used, but using a dummy string like "/" would work, you could stub out the call using DefinePlugin or similar.

Usage .env in React(CRA) with Typescript in [Runtime]

I want to use env variables in a React project CRA-4.0.3 with Typescript.
I did try many approaches(here, here, here , and some other) using dotenv, webpack and suggestions from different sources but I still get undefined.
But, I still get undefined, can someone point me in the right direction? I already spend a reasonable amount of time.
Update: I forgot to mention that I want them to be available in runtime, so, I can use them for E2E Test with Playwright and Jest as runner.
I'm using them in a structure like this /main/e2e/tests/file.spec.ts
.env
REACT_APP_ADMINUSER=user
REACT_APP_ADMINPASS=pass
Thanks in advance
If you're using CRA as described here you just have to create a .env file in the root of your project, and populated it with something like REACT_APP_NOT_SECRET_CODE=abcdef.
Then in your app you can use that variable by using
console.log(process.env.REACT_APP_NOT_SECRET_CODE)
You don't need to add Webpack or other modules to the default configuration that comes with CRA
UPDATE:
I see the edit of the question.
Have the env variables exposed in runtime make a lot of difference since as default they're injected in build time.
I found this module https://github.com/kHRISl33t/runtime-env-cra that seems to make more easy the method described in the offical doc to have the variables in runtime

Why doesn't jest support classProperties syntax?

I have a node program running on node12 and I don't have babel in my project because I don't like compiling the code. When I use jest: ^24.9.0 to test my code I get this error message:
Support for the experimental syntax 'classProperties' isn't currently enabled.
Below is the code causes this error. However, it runs very well on node. I have searched this error and many people say to configure the babel for that. I don't really want to bring babel to my project. I don'er understand why jest doesn't work with the syntax which is supported by node?
class Server {
static INTERVAL = 2000;

Babel setup on ES7

I have been upgrading my project to use ES7. I have changed some codes, made use of classes.
But there is a problem.
class Example {
change = async (params) => {
const job = await Some.job();
}
}
Everytime I wanted to debug it gives me Unexpected token problem. Even if I run it with Babel, it fails. I know a project where people use this kind of syntax and it works. I could not a valid solution on the internet, a couple github issues but nothing solid, so asking here.
What is the problem here? How should I setup the Babel or the project? Below the error from the console and my config file screenshots.
Just check out this link. Installing Babel V6.x ES7 Async/Await on Node.js v6.2.0 with Nodemon
(You can also check this if you want to install plugin: https://babeljs.io/docs/plugins/transform-async-to-generator/ )
Alternatively search Google "babel async await support" and see the results.
Babel does not give you whole things supported out of the box, you have to make some configurations, installing presets/plugins etc.
In my situation I needed to install stage-0 preset and/or transform plugins to make sure async keywords are supported. In the link above it says stage-3 but you can install stage-0 also, it includes all the plugins up to stage-3.
Babel needs to be documented much better, there is no way you could just get to documentation and set things up. There is not a straight one way Getting started a Project setup that shows you things in an ordered way. Hope they woul add it.

Resources