How can I reduce the webpack bundle size for a Vue.js / Nuxt.js project incorporating AWS SDK? - vue.js

Summary:
I have created projects, with Vue.js and Nuxt.js, where I have installed aws-amplify (which automatically installs aws-sdk) in order that I can implement authentication with AWS Cognito.
In both cases, this works very nicely, but the problems come when I build production versions.
In both cases, I end up with massive bundle sizes which (thanks to webpack-bundle-analyzer) I can immediately see are caused by the aws-sdk which appears to contain code to implement every AWS service, under the sun, despite the fact that I am only importing AWS Cognito's: "Auth" (import { Auth } from 'aws-amplify')
I have tried creating a custom AWS SDK for JavaScript, which only includes the service: AWS.CognitoIdentity, but despite incorporating that (presumably incorrectly), I still end up with the same bundle size (and aws-sdk files) when I build the projects.
As I say, this is happening in both Nuxt and Vue project, but in order to simplify this, I for now just want to find the solution to a very basic sample project created with Vue.
I think I must be doing something dumb, but I can't work out what that is.
Any help would be greatly appreciated! :-)
Steps to reproduce:
Create a basic Vue.js project with defaults. Run: vue create vue-aws-sdk-inv
[Note: Steps 2 - 4, are not crucial to reproduce issue, but install webpack-bundle-analyzer which provides useful extra info.]
In the new project, install webpack-bundle-analyzer. Run: npm install --save-dev webpack-bundle-analyzer
Create root file: vue.config.js
Add the following code to vue.config.js:
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer")
.BundleAnalyzerPlugin;
module.exports = {
configureWebpack: {
plugins: [new BundleAnalyzerPlugin()]
}
};
As a benchmark, build the project. Run: npm run build
At this stage, the project will build (with no console warnings) and webpack-bundle-analyzer will launch (in the browser) showing the file: chunk-vendors..js, at the top of the tree, containing a bunch of other .js files, all of acceptable size.
Install AWS Amplify (and by default aws-sdk). Run: npm i aws-amplify
Open src/components/HelloWorld.vue and add the following under the tag: import { Auth } from "aws-amplify";
Build the project. Run: npm run build
At this stage, the project will build WITH console warnings regarding the following files being too large:
File Size Gzipped
dist/js/chunk-vendors.013ac3f0.js 3055.78 KiB 550.49 KiB
dist/js/app.fa2a21c4.js 4.67 KiB 1.67 KiB
dist/css/app.53019c4c.css 0.33 KiB 0.23 KiB
If installed, webpack-bundle-analyzer should launch (in the browser) showing an inflated: chunk-vendors..js, due to a hefty: aws-sdk.
aws-sdk will include api: .json files and lib: .js files for every AWS service I can think of!
The attempt to rectify:
Navigate to: https://sdk.amazonaws.com/builder/js/
Clear all services.
Select just: AWS.CognitoIdentity
Download "Minified" aws-sdk-.js
Download "Default" aws-sdk-.min.js
[Note: the following are the steps I am guessing I'm getting wrong?...]
In the project, search the node_modules directory for aws-sdk.js and aws-sdk.min.js.
They were found in /node_modules/aws-sdk/dist
Replace both files with the downloaded files (renaming to aws-sdk.js and aws-sdk.min.js respectively.)
Build the project. Run: npm run build
Project will build with same console warnings and same massive aws-sdk, as before, containing all the same .js and .json files for a bunch of services that are not actually imported in the application.
Final pieces of analysis:
Remove aws-sdk.js and aws-sdk.min.js from project's: /node_modules/aws-sdk/dist
Build the project. Run: npm run build
Project is built without even referencing these files.
Rename /node_modules/aws-sdk to /node_modules/TEMP_aws-sdk and attempt to build the project.
Build fails, and this proves (I think) that I was at least trying to add the custom versions, of aws-sdk.js and aws-sdk.min.js, somewhere in the correct directory!
Source Code:
vue.config.js:
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer")
.BundleAnalyzerPlugin;
module.exports = {
configureWebpack: {
plugins: [new BundleAnalyzerPlugin()]
}
};
src/components/HelloWorld.vue:
import { Auth } from "aws-amplify";
As said before, any help would be greatly appreciated! :-)

It looks like import { Auth } from "aws-amplify"; doesn't currently allow for tree shaking according to this issue.
Reading through several related issues, it appears that:
import Auth from '#aws-amplify/auth';
is the best you can currently do. I suspect that over time, the AWS team will figure out a way to better separate the internals.
For readers looking for a way to reduce bundle sizes for the aws-sdk package, see this section of the docs.
In my case:
import S3 from 'aws-sdk/clients/s3';
import AWS from 'aws-sdk/global';
cut the bundle size down by quite a lot. That gets it down to ~57k gz to use S3.
Also, for anyone using nuxt you can just run nuxt build -a to get the build analyzer.

Related

webpack-resolve-alias to resolve dependecy's sub-dependency

I'm sort of stuck with one problem and googling it for about three hours brought me nowhere.
Here's the problem. I'm trying to develop my own custom PDF viewer based on PDF.JS lib. Officially it is distributed on npm as pdfjs-dist package. However I need to extend a few classes that are not accessible in pdfjs-dist. So I npm install'ed or yarn add'ed the original repo https://github.com/mozilla/pdf.js.git. This way I have access to classes I need. Inside pdf.js there are core lib that is stored in pdf.js/src/ and a pdf-viewer app that is stored in pdf.js/web/.
Inside that pdf.js/web/ app core lib (pdf.js) is referenced via pdfjs-lib alias that is resolved to pdf.js/src/ during pdf.js inner build process by gulp.
For example pdf.js/web/base_viewer.js:
import { AnnotationLayerBuilder } from './annotation_layer_builder';
import { createPromiseCapability } from 'pdfjs-lib';
import { PDFPageView } from './pdf_page_view';
So now I'm trying to import that pdf.js/web/base_viewer.js in my app that is using latest Webpack 4 (I guess), and this pdfjs-lib sub-dependency is not resolved.
I've tried webpack's resolve-alias mechanism (https://webpack.js.org/configuration/resolve/):
resolve: {
alias: {
'pdfjs-lib': path.resolve(__dirname, './node_modules/pdf.js/src/')
},
}
...but looks like it resolves dependencies only of my own App, but not sub-dependencies of my dependency.
Just in case I'm building Vue 3.0 app using vue-cli and access webpack config this way: https://cli.vuejs.org/guide/webpack.html, but don't think it matters.
Any help from Webpack gurus here?
Thanks.

process.env.NODE_ENV is not working with webpack3 [duplicate]

I've got an existing code base in which Vue.js has performance problems. I also see this notice in the browser console:
so I guess an easy fix could be to put Vue into production mode.
In the suggested link I try to follow the instructions for webpack. We're on Webpack version 2.7 (current stable version is 4.20). In the instructions it says that in Webpack 3 and earlier, you’ll need to use DefinePlugin:
var webpack = require('webpack')
module.exports = {
// ...
plugins: [
// ...
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production')
})
]
}
So in my package.json I've got a build script defined:
To build for production I run yarn run build and it runs a build.js file (paste here) which in turn calls webpack.base.conf.js (paste here) and webpack.prod.conf.js (paste here).
As you can see in the paste I use the DefinePlugin as suggested by the docs.
I also found a file called vue-loader.conf.js (paste here) and to be sure I also added the DefinePlugin in there as well.
I can run yarn run build which ends without errors, but when serve the site over Apache and open the browser it still shows the notification that we're in development mode.
To be sure it actually uses the files created by webpack I completely removed the folder /public/webpack/ and checked that the webinterface didn't load correctly without the missing files and then built again to see if it loaded correctly after the command finished. So it does actually use the files built by this webpack process. But Vue is actually not created in production mode.
What am I doing wrong here?
The problem may be in your 'webpack.base.conf.js' as i suspected, thank you for sharing it, upon searching i've found an issue resolving your 'production not being detected' problem on github here
The solution requires that you change 'vue$': 'vue/dist/vue' to 'vue$': vue/dist/vue.min in production.
You will find the original answer as:
#ozee31 This alias 'vue$': 'vue/dist/vue' cause the problem, use vue/dist/vue.min in production environment.

After bundling my aurelia app I get a: No PLATFORM.Loader error

After bundling a simple aurelia application with jspm bundle-sfx I get the following error:
No PLATFORM.Loader is defined and there is neither a System API (ES6) or a Require API (AMD) globally available to load your app.
An example application: https://github.com/Baudin999/jspm-bundling-test
You can use: npm run setup:dev in a non windows env to switch back to the dev settings (which is just a comment/uncomment in the ./src/client/index.html) and you can use npm run setup:prod to switch back to the production environment, bundling will automatically be triggered. all other scripts can be found in the package.json.
I can't link to other questions because I haven't found any questions which relate to this problem. I "think" (which means absolutely nothing) that this might be related to the fact that aurelia needs a full loader even when bundling with bundle-sfx but I haven't found any ways to solve the error.
EDIT (25/01/2017 17:16): I've found out that the error is because I import the aurelia-bootstrapper.
As soon as I add: import * as bootstrapper from 'aurelia-bootstrapper'; I get the error
Please add the code how do you bootstrap your aurelia app.
There is nothing actually to import from bootstrapper apart from bootstrap function.
Which you would use in case of custom manual bootstrapping.
like in
import { bootstrap } from 'aurelia-bootstrapper'
const configure: (au: Aurelia) => {} = async function (au: Aurelia) {
au.use
.standardConfiguration();
await au.start()
au.setRoot() // or au.enchance()
})
bootstrap(configure)
in a happy path scenario with jspm - you System.import('aurelia-bootstrapper')
and it takes over finding the root node of your app and the script to configure Aurelia (main by default)
Have a look at Bootstrapping Aurelia in the docs
Oh.. and bundle-sfx is not supported there are other means to bundle aurelia apps using jspm

Aurelia javascript dependencies loading time [duplicate]

I wrote a minimal test page to try out Aurelia.
http://www.andrewgolightly.com/
GitHub: https://github.com/magician11/ag-landingpage
My last test, showed it took 55 seconds to load the page with 135 requests.
It seems I need to bundle the jspm_packages directory first so that the 543KB gets downloaded at once.. and not in pieces.
So given I followed this example: http://aurelia.io/get-started.html
How do I bundle the packages? It's not clear to me from https://github.com/jspm/jspm-cli/wiki/Production-Workflows
And then what do I update in my index.html file? And I'll still need to include the jspm_packages folder as I reference it in the head, right?
<link rel="stylesheet" href="jspm_packages/github/twbs/bootstrap#3.3.2/css/bootstrap.min.css">
<link rel="stylesheet" href="jspm_packages/npm/font-awesome#4.3.0/css/font-awesome.min.css">
Thanks.
Update
The team is working on bundling..
From Rob Eisenberg: "We aren’t finished with our bundling support just yet. We’re working on it."
This question was posted at a very early time but we do have a strategy now in place that works with jspm and system.js loader for bundling. As a note it's not that the framework is slow it's that the loading of assets was taking a while early on due to the high number of requests (and that you probably didn't have gzip enabled)
I've copied this from my blog post on the subject - http://patrickwalters.net/my-best-practices-for-aurelia-bundling-and-minification/
Requirements
Understand that a jspm bundle command is used to let system.js, our module loader, know to load the bundle
Understand this only covers the JavaScript files (for now)
Create a new bundle
Open your terminal and navigate to your skeleton-navigation folder.
Open your config.js in your text editor
Run this command -
jspm bundle '*' + aurelia-skeleton-navigation/* + aurelia-bootstrapper + aurelia-http-client + aurelia-dependency-injection + aurelia-router dist/app-bundle.js --inject --minify
Breakdown
// Create a bundle
jspm bundle
// Bundle all of these paths
// from my config.js
'*' +
aurelia-skeleton-navigation/* +
aurelia-bootstrapper +
aurelia-http-client +
aurelia-dependency-injection +
aurelia-router
// Create the bundle here
// with this name
dist/app-bundle.js
// These modifiers tell the bundle
// to both minify and then inject
// the bundle
--inject
--minify
Additional notes on bundling
If you are serving in production you may want to look at setting baseUrl in your config.js like that
To unbundle and serve files individually use jspm unbundle
Since we used the --inject modifier system.js should pick up on our bundle and serve it without changing our script path in index.html
You can add more files by using + {filename} in the bundle area
2016 Update
The official toolkit for bundling aurelia applications can be installed via npm using npm install --save-dev aurelia-bundler.
Once installed, you can set up a gulp task for handling the bundle / unbundle process. A basic example of a task looks like this:
build/tasks/bundle.js
var gulp = require('gulp');
var bundler = require('aurelia-bundler');
var config = {
bundles: {
'dist/app-build': {
includes: [
'**/*.js'
],
options: {
minify: true
}
}
}
};
gulp.task('bundle', ['build', 'unbundle'], function() {
return bundler.bundle(config);
});
gulp.task('unbundle', function() {
return bundler.unbundle(config);
});
I've written a more in-depth article here: http://www.foursails.co/blog/aurelia-bundling/
The official documentation can be found here: https://github.com/aurelia/bundler
There is a GitHub repository that includes an r.js bundling strategy for the Aurelia AMD target build, as well as sample projects for using the bundle in Visual Studio with TypeScript (including an aurelia.d.ts TypeScript type definition file).
repo
bundling with r.js
aurelia.d.ts
using this strategy should dramatically reduce your load time as it will be loading one file instead of many files.

Why does the alpha version of Aurelia load slowly?

I wrote a minimal test page to try out Aurelia.
http://www.andrewgolightly.com/
GitHub: https://github.com/magician11/ag-landingpage
My last test, showed it took 55 seconds to load the page with 135 requests.
It seems I need to bundle the jspm_packages directory first so that the 543KB gets downloaded at once.. and not in pieces.
So given I followed this example: http://aurelia.io/get-started.html
How do I bundle the packages? It's not clear to me from https://github.com/jspm/jspm-cli/wiki/Production-Workflows
And then what do I update in my index.html file? And I'll still need to include the jspm_packages folder as I reference it in the head, right?
<link rel="stylesheet" href="jspm_packages/github/twbs/bootstrap#3.3.2/css/bootstrap.min.css">
<link rel="stylesheet" href="jspm_packages/npm/font-awesome#4.3.0/css/font-awesome.min.css">
Thanks.
Update
The team is working on bundling..
From Rob Eisenberg: "We aren’t finished with our bundling support just yet. We’re working on it."
This question was posted at a very early time but we do have a strategy now in place that works with jspm and system.js loader for bundling. As a note it's not that the framework is slow it's that the loading of assets was taking a while early on due to the high number of requests (and that you probably didn't have gzip enabled)
I've copied this from my blog post on the subject - http://patrickwalters.net/my-best-practices-for-aurelia-bundling-and-minification/
Requirements
Understand that a jspm bundle command is used to let system.js, our module loader, know to load the bundle
Understand this only covers the JavaScript files (for now)
Create a new bundle
Open your terminal and navigate to your skeleton-navigation folder.
Open your config.js in your text editor
Run this command -
jspm bundle '*' + aurelia-skeleton-navigation/* + aurelia-bootstrapper + aurelia-http-client + aurelia-dependency-injection + aurelia-router dist/app-bundle.js --inject --minify
Breakdown
// Create a bundle
jspm bundle
// Bundle all of these paths
// from my config.js
'*' +
aurelia-skeleton-navigation/* +
aurelia-bootstrapper +
aurelia-http-client +
aurelia-dependency-injection +
aurelia-router
// Create the bundle here
// with this name
dist/app-bundle.js
// These modifiers tell the bundle
// to both minify and then inject
// the bundle
--inject
--minify
Additional notes on bundling
If you are serving in production you may want to look at setting baseUrl in your config.js like that
To unbundle and serve files individually use jspm unbundle
Since we used the --inject modifier system.js should pick up on our bundle and serve it without changing our script path in index.html
You can add more files by using + {filename} in the bundle area
2016 Update
The official toolkit for bundling aurelia applications can be installed via npm using npm install --save-dev aurelia-bundler.
Once installed, you can set up a gulp task for handling the bundle / unbundle process. A basic example of a task looks like this:
build/tasks/bundle.js
var gulp = require('gulp');
var bundler = require('aurelia-bundler');
var config = {
bundles: {
'dist/app-build': {
includes: [
'**/*.js'
],
options: {
minify: true
}
}
}
};
gulp.task('bundle', ['build', 'unbundle'], function() {
return bundler.bundle(config);
});
gulp.task('unbundle', function() {
return bundler.unbundle(config);
});
I've written a more in-depth article here: http://www.foursails.co/blog/aurelia-bundling/
The official documentation can be found here: https://github.com/aurelia/bundler
There is a GitHub repository that includes an r.js bundling strategy for the Aurelia AMD target build, as well as sample projects for using the bundle in Visual Studio with TypeScript (including an aurelia.d.ts TypeScript type definition file).
repo
bundling with r.js
aurelia.d.ts
using this strategy should dramatically reduce your load time as it will be loading one file instead of many files.