I'm trying to bundle the project with webpack 5.
There is a problem with dynamic import, and I can't fix it, so I decided to exclude this library from the bundle, and then it works fine.
But, In the production version, I'm removing node_modules because it's running on lambda.
So here is a question: Can we exclude library from bundle but use it without node_modules? If yes, how we can achieve this?
For example, copy the library from node_modules to the dist folder and then change import * from "library_name" to point to the import * from "./dist/library_name" directory.
Related
I was recently using a react pakage and importing like so:
import ReactRegionSelect from 'react-region-select';
Now how does react know in my case where to import react-region-select from ?
I.E. if i were building the plugin react-region-select, which can be found HERE.
do i need to build it in a way that webpack or guld or node knows where to pick it from , considering it will be in node_modules ?
or is it just because in my package.json file i have the property name: 'react-region-select', that webpack, gulp or node knows where to pick it from ?
It checks node_modules folder by default.
You would need to publish to npm to use it in another project remotely.
Yes, using the name. NPM uses the published name to download the module under the same name when resolving dependancies.
https://webpack.js.org/concepts/module-resolution/#module-paths
https://webpack.js.org/configuration/resolve/#resolvemodules
Is it possible to build a TS source into a single file, which would also contain node_modules imported packages' source?
That would be very helpful in a serverless project. I have done this before on a non-TS project but was using webpack (for another reason).
It seems this was briefly possible before but was due a bug https://github.com/Microsoft/TypeScript/issues/13414 ?
You will need to use a bundler such as webpack to bundle your compiled code and all your node_modules dependencies. The TypeScript compiler (tsc) just transforms TypeScript code into JavaScript, and won't deal with bundling.
I start learning webpack , node , and react and I am bit confused about some basic staff.
Does webpack bundle whole react.js + my javascript files into one output file, so when deploying to production I don't need to install node packages used in project (assuming that they are added to webpack.config.js file) ?
If above is right:
On my server I just need to place index.html + output from
webpack ( bundle.js) ? ( In simple scenario) ?
package.json will be used only on development side ?
You only need index.html and the bundle.js (or any name you gave the file) for the app to work, provided that you are not using any local assets. You don't need to include node modules. Package.json should tell you what to include in your project so that you don't have to include node modules whenever you want to upload your project along with few other decalarations.
The way Webpack works is that you specify one or more entry points and one or more output files. Webpack then reads the entry point and also traverses through the import / require statements recursively. It then generates final bundle file(s) which includes all the traversed files.
Yes, Webpack outputs everything in the the bundle.js file(s). You can configure multiple output bundles. So, you just need HTML and output bundle to deploy the app.
The package.json specifies the packages upon which the app depends, apart from several other things. While traversing through the entry points, webpack will also include the packages specified in import / require. Function of package.json is to tell npm to install those packages.
I've created a React project with create-react-app command, it uses webpack.
In order to use SASS I needed to eject with npm run eject command and to manually add SCSS loader inside loaders array, as explained here.
This is my first time with React and I'm using a per comonent style approach which consist in importing a .scss file per component.
I have a global variables file in ~/my-project-folder/src/assets/styles/_variables.scss and I want to import it from ~/my-project-folder/src/scenes/Auth/Login/styles/login.scss, of course I don't want to do something like #import '../../../assets/styles/_variables.scss'.
I've seen that I can refer to SASS files inside node_modules folder this way: #import "~bootstrap-sass/assets/stylesheets/bootstrap/variables"; so I'm wondering what is the way to refer to my project's root directory, i.e. ~/my-project-folder.
When you do this #import "~bootstrap-sass/assets/stylesheets/bootstrap/variables"; it will look for the bootstrap-sass in node_modules but when you want to import your global sass file in your other files then you have to give the relative path otherwise webpack will not able to resolve it for your. You can try to give public path in your webpack
Ive just published my first package (a react component) to npm but im having some trouble understanding the difference between what the lib directory is compared to the dist.
Currently I generate both lib and dist however my package "main" points to the dist unminified js file which has been built using webpack and output as UMD. The lib folder is built using babel taking the src and outputting to lib.
The dist folder contains both [unminified/minified].js files as well as [unminified/minified].css files.
My main confusion is with the lib folder since imports from there currently wouldn't work seeing as I just transform src -> lib meaning the scss references are still there and the scss files aren't transformed either.
I use CSS Modules (css-loader, styles-loader, postcss-loader etc) to generate my CSS files and this is where the confusion is since, wouldn't I also need to use webpack to generate my lib folder seeing as the scss files/import references need to be transformed to css?
Are you meant to have both lib and dist or is the UMD build in dist fulling the same purpose as that of having a lib folder?
If you are supposed to have both how would I achieve this, since I couldnt find any info regarding generating the lib folder when using CSS modules within your js files and still maintaing the same folder structure of that of src (while still generating dist)?
Usually the dist folder is for shipping a UMD that a user can use if they aren't using package management. The lib folder is what package.json main points to, and users that install your package using npm will consume that directly. The only use of the lib as opposed to src is to transform your source using babel and webpack to be more generally compatible, since most build processes don't run babel transforms on packages in node_modules.
As far as handling the style imports, it's probably a good idea to not import scss or css files in your source js that you export. This is because node can't import styles like that by default. If you have an example that demos your component, it makes sense to import the styles there. The common pattern is to publish minified and unminified css in the dist folder, and in your documentation tell the consumer to explicitly import the css file using whatever technique they prefer. I took this approach with redux bug reporter if you need an example. Hope that helps!
In general lib refers to libraries that are included in a package, dist on the other hand are distribution files for your project. As an example you could write a bunch of javascript and include jquery (which is a lib) and then when they're all bundled up you have a single dist file.
Ok think I found out how to do this. There is a babel plugin that allows you to use webpack loaders when running babel (babel-plugin-webpack-loaders). Thus my CSS mapping is inlined within the js file and the mapping hashes used are also the same as that used when building dist. Yay!