tree-shaking | Build this project
kandi X-RAY | tree-shaking Summary
kandi X-RAY | tree-shaking Summary
Build this project by running.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of tree-shaking
tree-shaking Key Features
tree-shaking Examples and Code Snippets
Community Discussions
Trending Discussions on tree-shaking
QUESTION
I set up a small project with the following files
...ANSWER
Answered 2022-Mar-31 at 15:43I figured it out myself, auto-answer for the record :
The tsconfig.json was wrong, it wasn't preserving the ES6 module syntaxe so webpack couldn't treeshake properly.
More details on the correct configuration (optionally including Babel too) can be found here
QUESTION
Starting a new project with Vue.js v3 and Parcel.js v2. Everything went fine for setting up and launching a humble Hello World app except this warning in the browser's console:
...ANSWER
Answered 2021-Nov-23 at 17:43It looks like these warnings are generated when there are no global variables named __VUE_OPTIONS_API__
and __VUE_PROD_DEVTOOLS__
(see code).
In a webpack setup, you can inject global variables at build time with the Define Plugin. Unfortunately, I'm not aware of any parcel2 plugin (yet) that does this (although it would be pretty simple to write). However, you could just create them yourself by writing this near the beginning of your app's entry .js
file:
QUESTION
I am very new to Vue and I am trying to set up vue-i18n
and continue to get console warnings
ANSWER
Answered 2022-Jan-26 at 14:09The meaning of this warning message is that you need to specify the flag explicitly in the bundler when you do a production build.
Please refer to the following link for packages provided by vue-i18n for bundlers.
https://vue-i18n.intlify.dev/installation.html#with-a-bundler
One advice to help you out is to use the plugins for each bundler listed in the NOTE section of the above link, and they will configure them properly for you. I recommend that you check them out.
Checkout this issue listed in GitHub forum.
QUESTION
Using webpack-bundle-analyzer I detected the module StoreDevtoolsModule into my production bundle.
I though three shaking would removed it, but it does not...
Is there a way to build in production mode without it? (it is not used anyway in production):
...ANSWER
Answered 2022-Jan-21 at 12:23After discussing the problem with my peers, we figured that the reason for it to be in the bundle is that because environment.production
is evaluated at runtime, tree shaking will not remove the module from the bundle. Event though it is not really used.
There is a way around it though.
first create 2 files:
one to host the import array for non-prod (default), with the following contents
src/hat-trick/store-dev-tools-import.ts
QUESTION
We are using create-react-app
and Typescript within all of our projects and a slightly large module of commonly used React components has emerged. Trying to factor these out into its own NPM package (for easier maintenance and better reuse), here called PackageA
, we have arrived at a situation where the overall size of a test app, here called TestApp
, is larger than it was before (when the same code existed inside the code base of the same app). TestApp
is a VERY rudimentary app which basically just showcases some (but not all) parts of the components in PackageA
, formerly with the components inside the project itself, and now with this part removed and instead imported from privately published PackageA
.
Sizes of initial JS chunks before factoring out PackageA
with components from TestApp
("main" is assumed to be code from the project itself and the other chunk is believed to hold dependencies):
- Gzipped: "main" chunk ~ 31 kB, chunk with external dependencies ~ 193 kB (from output after building the app)
- Unzipped: "main" chunk ~ 93 kB, chunk with external dependencies ~ 653 kB (from browser)
Sizes after factoring out PackageA
from TestApp
:
- Gzipped: main chunk ~ 22 kB, chunk with external dependencies ~ 215 kB (from output after building the app)
- Unzipped: main chunk ~ 57 kB, chunk with external dependencies ~ 745 kB (from browser)
As can be seen, the overall size increases with 13 kB gzipped and 56 kB unzipped. This corresponds to an increase of ~ 6% gzipped and ~ 8% unzipped. This is not insanely much but I would still expect them to be somewhat similar.
Further information- The contents of
PackageA
is published as es6 modules to allow for tree-shaking which seem to work properly since unused parts ofPackageA
are not emitted into the output chunks ofTestApp
. - Minification of
PackageA
is done by UglifyJS prior to publishing using both--compress
and--mangle
options. - Source maps are not included in the source files of
PackageA
but only available separately. - The only packages listed under
dependencies
inPackageA
'spackage.json
are NOT used anywhere else inTestApp
and were subsequently removed from theTestApp
'spackage.json
at the same time as they were added toPackageA
'spackage.json
. The same versions were used. All other dependencies ofPackageA
are listed underpeerDependencies
. - All sizes were verified after deleting the
node_modules
folder and running a fresh install of dependencies. - The contents of
PackageA
is exactly the same as the deleted folder fromTestApp
with the exception of an addedindex.ts
page which exports the contents of the other files. This file is < 3 kB unzipped in size.
What could be possible sources of this increase in size and where could we start looking? Perhaps this increase might lie in how the code is transpiled in the package and that create-react-app
is somehow able to do this more efficiently for code included in the project itself than for imported code. I know this is a tough question that might have many answers and that is hard to reply to.
This is the tsconfig.json
used in the factored out PackageA
:
ANSWER
Answered 2021-Nov-14 at 16:35I understand that this questions might be highly dependant on the situation you're in but still thought I'd share my process.
Measuring sizesUsually when you build, your framework outputs the sizes of the built artifacts. In the case of create-react-app
, these sizes seem to be the size of the gzipped versions.
You may also check your browser dev tools and note the sizes of the downloaded bundles which are typically NOT the gzipped sizes but the actual sizes.
When factoring something out or doing some sort of comparison, take notice of the different sizes before the change and after the change. Given some sort of intuition as to how these sizes should change evaluate if this is the case or not.
Analyzing the bundleWith NextJS you may use @next/bundle-analyzer
, in most other cases you may use source-map-explorer
. The latter uses source maps to map bundled code to sources; if the source itself has source maps, then these sources are used, otherwise you will see references to node_modules
. With source-map-explorer
, it might be worth both looking into the visual representation as well as the json representation since, with a lot of sources, the visual representation might suppress the participation of a certain component or library which the json representation will not.
With source-map-explorer
you may make some valuable comparisons between different versions of an app.
After establishing with, for example, source-map-explorer
that some source code expands more in some context that in another, you may want to research the issue further. Using source maps (either in development mode or in production mode if you have them available there), you may want to track the imprint of a certain portion of source code in the output bundle. A lot of browsers support mapping bundled code to source code (given source maps) but not all allow you to do the opposite. Firefox, however, has this feature and it allows you to, given bundled code, view the source code and vice versa. This is very useful when you're trying to track what some source code actually results in in the output.
Well, the following is a list of what caused the increased size in my case:
I imported from an index in my package and the index imported all of the exports of the package. This should not be a problem with esm modules, but without the
sideEffects: false
flag inpackage.json
there were some ambiguities. Even though the unused sources from MY package were not added to the output bundle, some indirect dependencies could not be ruled out as not having side effects, so these were added to the bundle, even though the component that imported them were not even used. AddingsideEffects: false
to thepackage.json
of my package solved this and reduced the package size.When keeping the components in my main project that was bundled using
create-react-app
, some convenience methods added by Babel in all transpiled files were removed thanks to the presence of@babel/plugin-transform-runtime
plugin. This plugin removes these convenience methods, given that some Babel runtime was guaranteed to be present when running these files (from which they could be imported instead). When the same configuration was used for the factored out components, the size shrank even more.Finally, at the time of this writing, the
tsc
compiler seem to output more verbose code than Babel (see TypeScript: tsc transpiles jsx to more verbose code than babel). When transpiling the package I first usedtsc
and after started using Babel, the size shrank even more.
All in all, with the above changes, the output ended up being just 1.7 kB larger (unzipped) than before the refactoring which was a both understandable and acceptable result.
QUESTION
I'm using https://shoelace.style (in my Svelte project), and following the example config in shoelace docs, I added a copy()
plugin to my rollup.config.js
, copying it to public/vendor/shoelace
:
ANSWER
Answered 2021-Oct-30 at 14:07I'm using svelte with codeigniter as well and there is a simpler way to include your css framework:
- List item add the stylesheet to the view (layout) header and the script to its footer just above the build of svelte's script file (if you are going to use shoelace in the whole project).
- add both to
if you want to target only one page (even though you can also do that from the controller before loading the view to target certain pages).
- you can add it by installing
npm i svelte-preprocess node-scss
then include the scss file of you css framework and just inport the js file (and also you can do that using your global stylesheet using scss).
QUESTION
Like lodash, the library @yamato-daiwa/es-extensions suggests the common functions for both BrowserJS and NodeJS, but unlike lodash it has single main file index.js
with re-exports.
This library:
- Must work in browser environment with Webpack (reached ✔️)
- Must make available the Webpack's tree shaking (by other words, the cutting off of the unused functionality on production building) for BrowserJS where each kilobyte on count (reached ✔️).
- Must work in NodeJS environment.
- Must work with ts-node
The conflicting point is the modules type. To make the Webpack's tree shaking available, the ES modules are required, but currently NodeJS supports CommonJS modules only, and in ts-node the ES modules support is limited (especially in the library exporting case).
What we have is:
Modules type BrowserJS (Webpack) Tree shaking NodeJS ts-node CommonJS Yes No Yes Yes ES20XX Yes Yes No LimitedBecause the tree shaking is critical for BroswerJS, it's already been decided to distribute the library by ES2020 modules. But this way, the support for NodeJS and ts-node will be lost.
Even if to build the NodeJS application with Webpack where it's not recommended to bundle the NodeJS libraries (webpack node modules externals is being used to exclude them), application will crush if don't add the @yamato-daiwa/es-extensions
with it's ES modules to excluding of webpack node modules externals.
In this repro, npm run "Webpack:ProductionBuild"
will build the files BrowserJS.js
and NodeJS.js
for appropriate environment. Because the source code using isUndefined
function of "@yamato-daiwa/es-extensions"
library only, in BrowserJS.js
must not be any other functionality (Webpack's tree shaking):
ANSWER
Answered 2021-Oct-29 at 21:17The "main"
entry in a package.json
file should always be in commonjs format. The "module"
entry should always be es modules. Right now, you have "main"
pointing to es modules, which will not be resolved properly (your ts-node
error, for example).
Generally, if you want to give consumers the option, you would create 2 builds in the distributable.
(you'll need to remove the comments in these json files)
QUESTION
Some things to know:
- I understand how to make a HTML / CSS / JS website.
- I understand how to make a Node JS app and host it on Heroku
I encountered a problem that was very confusing and I still working it out. I am making a firebase project using their latest tree-shaking V9 SDK
import { initializeApp } from 'firebase/app';
I understand Node JS is meant for backend (it can't be run in a browser) so how am I supposed to "build" this into something that I can reference in a script tag? I've heard of webpack but that requires you to run npm run build
so how is that practical in any way? That would mean every change I make I would have to build it?
How would one live preview this Node JS app on a localhost
if Node JS can't be run in a browser? I would live to be able to preview this Node JS app and quickly make changes if that's possible.
ANSWER
Answered 2021-Oct-26 at 01:18I've heard of webpack but that requires you to run npm run build so how is that practical in any way? That would mean every change I make I would have to build it?
Yes, that's the general idea. You make changes to script files on your local machine (or network), and then in order to see the changes take effect, you rebuild the app so that a new bundle is generated. This bundle can then be hosted on your web server or development server, and once it's there, you can refresh the page to see the differences.
But almost all of this can be automated, resulting in it not really being much of a chore at all.
- Nodemon is a popular tool that watches a directory for changes and can run Node scripts when a change is detected. With it, you could, for example, make a change to a file, and then it'll automatically rebuild your app.
- For Webpack in particular, there's webpack-dev-server, which can automatically refresh a page when the app gets rebuilt.
So, during development, the workflow can be as simple as - make a change, then alt-tab to your browser (hosting the app on localhost) to look at the changes.
Bundles built for the purpose of eventually hosting them on the front-end can easily incorporate Node modules - the build process can take the code from the module, and the bundle produced can include that code. This sort of thing is extraordinarily common.
QUESTION
Unfortunately, there were not the working solution for the question How to setup the TypeScript compiler for the library so that the unused modules will be cut off by Webpack in the dependents projects? (for the Sep. 2021). The solution suggested by Mr. Craig Hicks works only locally and can not be used for the distributable library (I'll explan the details below).
However, from the Mr. Hicks's answer I knew that to make Webpack three-shaking available it's required to distribute the library as ES modules. It makes the issues for usage with Node.js native and ts-node, but I afraid I will be downoved if try to consider all of these problems in single question.
TargetThe target library @yamato-daiwa/es-extensions has a lot of functionality and even in big applications a part of it will not be used. Theoretically, the Webpack's three shaking could cut off the unused functionality, but actually it works with huge limitations as it will be shown in experiments below.
In the sample below, I am expecting that only isUndefined
function will be bundled and any other functionality will NOT be:
ANSWER
Answered 2021-Sep-29 at 05:33It appears that declaring "sideEffects": false
is the responsibility of each package an application consumes, so setting it in the application's root package.json isn't enough.
The package.json of @yamato-daiwa/es-extensions
needs to include "sideEffects": false
, so applications know that package is side-effect free. I tried this in your first example repo, and the final build gave me the expected:
QUESTION
These days I learned the concept of enum
and const enum
in Typescript and I got confused. I understood that const enum
will be transpiled into simple values while the other not. I also understood that some feature differences between using string union type and plain enum
(like iteration).
But I can't comprehend when and why I should (not) use (const
) enum
. I think iteration on enum
is not that general, and it's hard to predict whether they'll use it or not.
There is a post (which is written in Korean so sorry for that) that "tree-shaking cannot optimize transpiled enum
since they are IIFE". (Link)
Than should I just use const enum
without any hesitation, or is there any more pros and cons using the other alternative: string union type.
TLDR
- If
const enum
is more lightweighted thanenum
, is there any practical reason to useenum
? - I already know
Babel
can't work withconst enum
. In that case, why someone usesenum
rather than string union type even if they don't iterate over it.
ANSWER
Answered 2021-Aug-14 at 11:17I believe that enums
camed to TypeScript from C#
.
In general numerical enums
in TypeScript are unsafe. Consider this example:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install tree-shaking
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page