Import and use 3rd Party Package with Webpack and Vue.JS - javascript

Good day,
I'm currently new in integration vue.js in webpack.
Basically, I'm trying to use a 3rd party package that I've already installed inside in my node_modules.
In this example, I installed a package called "Vuex Toast" (an alert for vue.js). NPM Package here
Here's my project structure.
.../js/main.build.js
.../src-modules/app.js (blank for now)
Normally, everytime I run webpack command, I generate my main.build.js
Can someone help me how to use that 3rd party package here?

The idea of webpack (and bundlers in general) is that you require code and webpack bundles it for you.
In your app.js do the following:
const vuexToast = require('vuex-toast');
Now you can use it. Webpack will know to check your node_modules/vuex-toast folder.

Related

Will electron-builder use dependencies that it doesn't need

I have been wondering this for a while and i haven't found a specific answer.
I am building a whole app using Electron and React to make the ui.
My question is if i should have a 2 complete different source code for each part of the app (electron and react) because i don't know very wekk how the package electron-builder works.
To be precise, since i have installed react (and react packages related) and some others that i just use for the ui part, when i build the app for distribution, will electron include the dependencies that doesn't require? Making the final bundle bigger!
While electron-builder has configuration options to specify which files are to be included, it is not a replacement for a tool like webpack.
electron-builder creates the installer/target artefact for your platform and packs the files you have specified.
It does not sift through your node_modules via tree shaking to create a minified script that only contains the code you need.
You can compare it a with the files array in the package.json that tells npm pack which files should be put into the tarball.
In the default configuration (and I don't know if you can overwrite that, but it is surely worth a try by setting specific node modules to ignore) it will include the entire production dependencies.
Another solution than manually ignoring the react dependencies would be to hold your react app in its own directory in your project and only include the build artefact.
gui/
|_build/
|_your built stuff
|_your react stuff
|_node_modules
|_package.json
main.js
node_modules
package.json
In this case you would configure electron-builder to include main.js and gui/build. With this electron-builder should only include the production dependencies of the outer project.

How do you add Node to the frontend?

I have been coding with a React frontend and a Node/Express backend. But sometimes, I only need some plain Javascript without React, but still want the benefit of NPM and other Node modules. What is a way to do this?
You'll need a module bundler of some kind. There are many options including Webpack, Browserify, Gulp, and Parcel.
For Webpack, for example, from their example docs, the process could be:
Install webpack with npm install webpack and install webpack-cli
Install a module you want to use on the frontend, eg lodash
In src/index.js, import lodash: import _ from 'lodash'; and use it as needed. (You can also import other modules from NPM or from other places in your source code)
Set up webpack.config.js if you need custom build configuration settings
Run webpack to build the project: npx webpack. A single bundled JavaScript file will be created which contains all your source code and the imported Lodash's source code.

Installing an NPM module without installing its dependencies

We have a project that uses NPM as its package manager. We use Webpack to build a Javascript app that is consumed by a Rails app. The app is fully self-contained and is built to a /dist directory that is included in the package. For reasons that are out of scope of this question, the Rails app consumes this App as a UMD module, loading it via a script tag and initialising it:
App({})
The problem is that the package has a large number of dependencies. When the Rails app installs the package, it also installs all the package's dependencies, despite the fact that it doesn't need or use any of these dependencies because everything it needs is present in the App bundle.
Is there any way the Rails app can install the package (and access the app in the /dist directory) without also installing all the package's dependencies?
Note that we don't want to move all dependencies to the app's devDependencies as that will be confusing (and we may well end up with multiple ways of distributing the app, including via es6 import). Also note that we are using Yarn as our package manager.
To clarify, we are using NPM as a means of sharing versioned releases with the Rails app, however the only thing in the package the Rails app cares about is the UMD module. It is very useful for the Rails app to be able to do yarn add Example#latest and immediately pull in the latest version of our App, however it doesn't need to resolve the package or its dependencies as they are already baked into the UMD module.

Deploying Angular2 quickstar project

I have cloned the Angular2 quickstart repo and build a simple app. I want to publish it to the web now. It runs locally but it references files directly inside the node_modules directory.
Is there a standard build process I can run that will copy all needed files to the output directory? Or do I have to create this myself from scratch with a task runner or something?
This is my first time to answer a question so bear with me if I didnt do it correctly.
If "bundling all angular 2 ts/js and other dependencies (core.js, rxjs, zone.js) into one js and create a script tag on index.html to reference the bundled js" close to the standard build process you mentioned and you want, my answer is yes, you probably need to npm install some other tools to do it.
Since the angular 2 quickstart is using systemjs to do ES module loading, the tool you can use is called "systemjs builder" https://github.com/systemjs/builder which helps you to do bundling (based on systemjs.config.js) and yes, you can use a task tunner (grunt or gulp) with systemjs builder plugins (gulp-systemjs-builder or grunt-systemjs-builder) to create a task to "build".
You can use this https://github.com/AngularClass/angular2-webpack-starter
And using npm run build:dev or npm run build:prod
It will build a dist folder and that's all you need.

Using stellar-lib api with Meteor

this is probably a silly question but am new to Meteor and struggling a bit. I want to build a stellar app that tweets when you get stellar. There is a nice Javascript API stellar-lib that works on node, but im unsure how to access the modules in Meteor...
I'm also building an app with Meteor and stellar-lib and have found two options:
1) You can manually copy over the built stellar-lib.js or stellar-lib-min.js from the build/ directory from either the github repo or the node_modules folder you installed it to when you ran the npm install command.
If you do this, you will have to copy the .js file to client/compatibility, otherwise stellar-lib will not work (note: this means you can only use Stellar on the client).
2) If you need it on the server, you can also have browserify wrap stellar-lib for you, then copy the output to lib/ in your Meteor app's root directory. I did this in my repo here with gulp.
Here's a short explanation to how 2) works:
.gulp is where I'll install my NPM modules where they will be ignored by the Meteor build environment (because the folder is hidden).
In deps.js, I require the modules I would want to use in my Meteor app as I would if I was using them in a traditional node.js app. package.json defines the modules I'll install with NPM and the gulpfile.js describes a build task that will resolve my require statements and output a single deps.js file that includes my dependencies to my Meteor app's lib/ folder.

Categories

Resources