Leveraging npm in vanilla JavaScript applications

Because of limitations set by the governance of my current project, I am unable to utilize many of the modern JS libraries and frameworks. Therefore, for our MVP, we are resorting to using vanilla JS directly loaded to the client (un-minified), which is not ideal. Despite this approach highlighting to management that it's not the best solution, I still need to meet deadlines. However, we do have NPM and Node installed on our development machines, and I would like to leverage this to optimize our JS by bundling it into a combined file with a hash to prevent caching.

My question is, how can I gather various separate JS files and integrate them into a new NPM project without having webpack or prettify mangle the expected variables, objects, and functions?

I attempted to simply copy the JS files into the src directory and add the typical import/export statements, but I encountered numerous "identifier not found" errors in the console, indicating that much of the code was being altered.

While I have experience using create-react-app for React projects, this has shielded me from the challenges of setting up a project manually, and now I'm facing the consequences of my lack of expertise.

EDIT: To clarify, I am looking to bundle several existing js files into one package while keeping the same interface. If this is not feasible, please advise accordingly. Otherwise, any guidance on where to begin would be greatly appreciated.

Answer №1

In the days before advanced bundling tools like browserify, babel, webpack, grunt, or gulp, developers would manually concatenate and minify files to optimize their code. Even without module bundlers, the global objects in the files allowed for smooth functionality when combining them.

For Linux users, a simple way to concatenate and minify files is:

cat a.js b.js c.js | uglifyjs -cm > bundle.js

It was common practice to start each file with a semicolon to prevent errors during concatenation.

After configuring babel, integrating it into the workflow becomes effortless:

cat a.js b.js c.js | babel | uglifyjs -cm > bundle.js

More information on using babel from the command line can be found here.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

A guide on parsing a nested JSON file with ExtJS 4

Here is the content of my JSON file named "jobInfo.json": { "job": { "id": 1, "name": "Job 1", "description": "bla bla bla", "locations": { "type": "FeatureCollection", "features": [{ "id": 1, "t ...

What is the proper way to designate a manifest.json link tag on a limited-access website controlled by Apache shibboleth?

The issue arises when attempting to access the manifest.json file. It has been declared as follows: <link href="manifest.json" rel="manifest"/> Is it possible to declare the manifest tag inline, or what would be the most effective way to declare it ...

Retrieve the heading from a pop-up box

This jQuery tooltip allows for popups from another HTML page to be created. UPDATE: I have provided an example HERE The issue arises when trying to retrieve the title from the popup. Currently, using document.title displays the title of the current page ...

Configuring the registry and proxy for Maven and NPM using the exec-maven-plugin

Executing Maven and npm install commands I am looking to configure npm registry and proxy settings using the plugin below. Is there a way to run npm config set registry and npm config set proxy commands within this plugin? <plugin> <gr ...

Use two queries to apply filters to entries in NextJS

Greetings to all! I am currently working on a project in NextJS that involves showcasing a portfolio of works generated from JSON data. [ { "title": "WordPress Plugin for Yandex Recommender Widget", "image" ...

JavaScript Website Struggling to Make Postman REST API Call to Parse Server

I have set up a Parse API Server on Azure and can successfully make REST API calls to it using Postman. However, when I try to replicate this process in my web app using the jQuery AJAX code snippet generated by Postman, I encounter a 400 (bad request) err ...

Having trouble retrieving data from getters in the Vue store while inside a template

Within my component, I have set up computed properties that are supposed to display text for blopp and blipp. However, while blopp is working fine, blipp is not showing anything. The end goal is to have blipp generate a list of strings from the store state ...

What is the reason for the malfunction of this JavaScript code designed for a simple canvas operation?

I'm having trouble with the code below. It's supposed to display a black box on the screen, but for some reason it's not working properly. The page is titled Chatroom so at least that part seems to be correct... <html> <head> &l ...

Click on the hyperlinks one by one that trigger ajax events

There is a feature on the popular social media platform reddit.com where you can load more comments by clicking a link. I understand that comments may be hidden for performance reasons, but I would like to automatically expand all comments without having t ...

Utilize JQuery to implement fading effects for clicked elements in a webpage

I've been using a rollover JavaScript plugin to create smooth transitional effects when users hover over clickable page elements. Everything was going well until I decided to switch to making ajax calls instead of page loads for dynamic content. The p ...

Ways to inform a subelement that a task is complete?

I have a child component that displays a list of orders passed from the parent. I want to include a "Reload" button inside the child component that, when clicked, triggers a function in the parent component to reload the orders. Upon clicking the reload b ...

Troubleshooting node-sass with npm rebuild command stuck

Node version : 8.16 npm: 6.4.1 yarn: 1.17.3 Having an issue with postinstall: npm rebuild node-sass that hangs I have tried several solutions, but none seem to work. ...

What is the best way to notify my form that a payment has been successfully processed?

I'm working on a form that has multiple fields, including a PayPal digital goods button. Clicking on this button takes the user out of the website's workflow and into a pop-up window for payment processing. Once the payment is completed, the retu ...

Implementing Fullpage.js with persistent elements throughout slides

Can I keep certain elements fixed between slides? I found that the only way to accomplish this was by placing the text element outside of the slide div. <div class="section" id="section1"> <div class="intro"> <h1> ...

What happens when Google Polymer platform is used without defining _polyfilled?

My attempt at creating a simple example using Google Polymer's platform.js is running into an error message that says: Uncaught TypeError: Cannot read property '_polyfilled' of undefined This is what I'm attempting to achieve: <cur ...

In order to have the bot repeat a structure for every user, I would need to utilize both mongoose and discord.js

I am utilizing MongoDB (mongoose) to establish a database for storing user notes in my Discord bot, which is being developed with Discord.JS. This is my "Guild.js" file: const { Schema, model } = require('mongoose'); const Guild = Schema({ i ...

What is the best way to import a geojson file into Express.js?

I'm currently trying to read a geojson file in Node.js/express.js. The file I am working with is named "output.geojson". I want to avoid using JSON.parse and instead load it using express.js (or at least render it as JSON within this function). var o ...

What is the best way to prevent automatic trimming in EJS variable assignment in Node.js?

When I attempt to write a variable from the database into an EJS table, it is being displayed with default trimming by the EJS template. However, I would like to display the variable from the database without any default trimming. I consulted the EJS temp ...

"Encountering issue when trying to initiate npm start

After installing KrakenJs on my Windows 7 system, I created a project using the command : Yo Kraken When attempting to run the project, I encountered the following error (even after restarting my system) : E:\nodejs\test\kraken\D ...

Is it time to advance to the next input field when reaching the maxLength?

In my Vue form, I have designed a combined input field for entering a phone number for styling purposes. The issue I am facing is that the user needs to press the tab key to move to the next input field of the phone number. Is there a way to automaticall ...