Webpack: prebuild lifecycle hook for libraries - javascript

I'm the author of a library and I need a script to be run each time before webpack bundles my library into the user's app code.
My library's package.json would be something like this:
{
"name": "some-js-library",
"version": "0.1.0",
"scripts": {
"prebuild": "./path/to/my-libs-prebuild-script.js"
}
}
The user's package.json could be anything, for example:
{
"scripts": {
"//": "or however the user uses webpack",
"build": "webpack"
},
"dependencies": {
"some-js-library": "^0.1.0"
}
}
I don't have any control over my users' package.json, so I'm left to wonder if it's possible to have my-libs-prebuild-script.js executed every time before webpack starts building?
Ideally this would be a standard that every bundler agrees on, but a webpack only solution would be fine.
Thanks!

Related

Yarn install Lerna monorepo directory with local dependencies

I have a monorepo that has 20 visual components built using Lerna. This component library is being used by another app repo and is included in it's package.json
I'm pulling in a single component from the component library into the app using a Yarn 2+ feature in package.json:
"#author/component-a": "git+ssh://git#github.server.com:Author/component-lib.git#head=feature/updated-component&workspace=#author/component-a",
We want to push to a branch on Github rather having to publish to npmjs every time we need a change in the app. This reason we want to take this approach (rather than something like yarn link) is because we have a staging environment that we want to build with the changes in the branch.
The component library file structure looks like:
packages
component-a
package.json
component-b
package.json
package.json
Where packages/component-a/package.json looks like:
{
"name": "#author/component-a",
"version": "1.1.0-alpha.1",
"description": "Component A",
"author": "Author",
"license": "MIT",
"main": "dist/index.js",
"files": [
"dist"
],
"dependencies": {
"#author/component-b": "1.1.0-alpha.1"
}
}
and packages/component-b/package.json looks like:
{
"name": "#author/component-b",
"version": "1.1.0-alpha.1",
"description": "Component B",
"author": "Author",
"license": "MIT",
"main": "dist/index.js",
"files": [
"dist"
]
}
I've been trying ways to get the build to refer to the local files rather than the ones published on NPMjs:
packages/component-a/package.json
"dependencies": {
"#author/component-b": "file:../component-b"
}
Which works fine locally (building in the component library, but downstream building in the app, via a branch it breaks.
Question: How can I get a downstream app to pull in other components defined defined in the same repo from a monorepo without publishing the packages (to NPMjs) and working only via a branch.

Lambda function with additional dependencies

I'm starting a CDK lambda project which gets the source code like this:
code: lambda.Code.fromAsset("resources"),
handler: "synthetic_test.main",
There's a single javascript file synthetic_test.js in that folder.
This seems to work but I can't figure out how to make it so that I could do:
const axios = require("axios");
in that file.
For some reason it seems to be able to import:
const AWS = require("aws-sdk");
but nothing else.
I did yarn add axios which added it to the package.json of my CDK project. But that does not really seem to help the lambda a lot.
The AWS Lambda runtime environment includes native language libraries and the relevant language-specific AWS SDK.
It does not contain arbitrary third-party packages. You need to either package those dependencies with your code or create a Lambda Layer that includes the dependencies and configure your Lambda function to use the Lambda Layer.
To package CDK app dependencies, see #aws-cdk/aws-lambda-nodejs and here.
I went with packaging dependencies with my code
My cdk went to
// 👇 define PUT account function
const putAccountLambda = new lambda.Function(this, "put-account-lambda", {
runtime: lambda.Runtime.NODEJS_14_X,
handler: "main.handler",
code: lambda.Code.fromAsset(path.join(__dirname, "/../src/put-account/dist")),
environment: {
REGION,
ADMINS_TABLE,
ADMINS_TABLE_PARTITION_KEY,
HASH_ALG,
}
})
With dist being the folder with a packed main.js file. And this file has a handler entrypoint. I had to update the package.json of these lambdas with packed dependencies.
{
"name": "put-account",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "webpack --mode=production --env env=prod",
"build:dev": "webpack --mode=development --env env=dev"
},
"author": "",
"license": "ISC",
"devDependencies": {
"webpack": "^5.66.0",
"webpack-cli": "^4.9.1",
"webpack-merge": "^5.8.0"
},
"dependencies": {
"aws-sdk": "^2.1058.0",
"crypto": "^1.0.1",
"uuid": "^8.3.2"
}
}
And I updated the package.json of my cdk project to these scripts.
"build": "tsc && npm run build:webpack",
"build:webpack": "for file in ./src/*; do (cd $file && npm i && npm run build) & done",
"build:beta": "tsc && npm run build:webpack:beta",
"build:webpack:beta": "for file in ./src/*; do (cd $file && npm i && npm run build:dev) & done",
Notice that my file structure is as follows:
./
bin
lib
src
package.json
With src holding the source code for my project's lambdas.
I am not sure if you are familiar with webpack, but I have divided my webpack configuration in common, dev, prod.
A dev webpack configuration is specially useful for debugging because otherwise you lose line numbers among other useful information when something goes wrong on runtime.

Moving nuxt 3 to JavaScript from TypeScript

When getting started with nuxt 3, it creates TypeScript project by default.
Is there a way to move to JavaScript?
I have tried:
Rename nuxt.config.ts to nuxt.config.js
delete tsconfig.json
add jsconfig.json
Contents of nuxt.config.js
export default {
};
contents of package.json
{
"private": true,
"scripts": {
"dev": "nuxi dev",
"build": "nuxi build",
"start": "node .output/server/index.mjs"
},
"devDependencies": {
"nuxt3": "latest"
}
}
Still the .nuxt folder creates TypeScript files
No, unfortunately in the current nuxt3 version (3.0.0-27268729.5b8e10f), there is no configuration to disable the output of those type declaration files.
While the generated .d.ts file are intended to help TypeScript developers, Nuxt does not require app source to be written in TypeScript. The .d.ts files should have no impact on your JavaScript development other than the slight delay in generating those files.

How to properly expose subpaths in package.json using the ”exports” key?

I’ve released a NPM package which is a plugin for a framework where only the main entry of my package.json is needed for usage in the framework and its environment. I also want to be able to use a subpath of the plugin in order for users to be able to use the plugin outside of this framework as well, which will require that the main entry point is never initialized as there are framework specific dependencies used there which I don't want to initialize when using this plugin outside of the framework.
My project structure looks like this:
.
├── index.js
├── submodule.js
└── package.json
Source code for the sake of this example looks like this:
// index.js
export default function () {
return "foo";
}
// submodule.js
export default function () {
return "bar";
}
// package.json
{
"name": "my-package",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"exports": {
".": "./index.js",
"./submodule": "./submodule.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "MIT"
}
According to Node.js documentation on this matter, this setup should allow me to use my package like this:
import myPackage from ’my-package’
import mySubModule from ’my-package/submodule’
In order to be able to test my package locally I run npm link in the project root of my npm package and then in another project i run npm link my-package. Now that I try to run the project (using parcel-bundler) that imports my-package and my-package/submodule like in the example above I get the following exception:
Cannot resolve dependency 'my-package/submodule'
I'm using NVM with Node v.12.18.4 and NPM v.7.15.0. I have also tried with Node v.14.17.0 but the issue persists. What am I missing?
It seems that the project setup is correct and that the problem lies in the parcel-bundler which does not support package.json#exports yet. There's currently an open issue on this matter.

Is it possible to get what package.json script has been called in NodeJS code?

Is it possible to get what package.json script has been called in NodeJS code?
My package.json file contains a script that builds the application:
{
"name": "notes-app",
"version": "0.0.1",
"license": "MIT",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build && node server.js"
},
etc, etc...
Can I then write in my server.js file an if statement like:
if (script === 'build') {
// Do something
}
Is there a way of knowing what script was called when starting the app. I would start this using the command npm run build. Also I'm using windows if that would make any difference.
I agree with yury, you must be detecting this for a reason, and maybe a more fit solution lies in that context.
However, when this is important (like for system administration) it is usually done by setting an environment variable in the scripts section and then testing process.env in the node js.

Categories

Resources