challenges encountered while trying to incorporate browserify

I have a situation where I have two .js files located in the same folder.

File app.js :

import {foo} from 'custom';
foo();

File custom.js :

function foo(){
  $('.nav.navbar-nav > li').on('click', function(e) {
    $('.nav.navbar-nav > li').removeClass('active');
    $(this).addClass('active');
  });
}

Now, my goal is to bundle these files together using gulpfile.js with Babel included in the process.

gulp.task('app-scripts', function() {
  return gulp.src('src/js/app.js')
    .pipe(babel({
        presets: ["es2015"]
    }))
    .pipe(uglify())
    .pipe(gulp.dest('build/js'));
 });

I've been trying to implement Browserify into this task as well. I've read the documentation but I'm unsure how to apply it correctly in this specific scenario.

I also attempted the following code but it didn't work:

const gulp           = require('gulp');
const babelify       = require('babelify');
const browserify     = require('browserify');
const source         = require('vinyl-source-stream');
const buffer         = require('vinyl-buffer');
const sourcemaps     = require('gulp-sourcemaps');

var config = {
  src:      './src/js/app.js',
  out:      './build/js/app.js',
  srcmaps:  '/build/maps'
}

gulp.task('app-scripts', function() {
 const bundler = browserify(config.src, {debug: true})
.transform('babelify', {presets: ['es2015']})

return bundler.bundle()
.pipe(source(config.out))
.pipe(buffer())
.pipe(sourcemaps.init())
.pipe(sourcemaps.write(config.srcmaps, {addComment: false}))
.pipe(gulp.dest('./build/js'));
});

Answer №1

Despite the method mentioned in the documentation not working for some reason, I found another method that worked perfectly. This alternative method is also taken from the same documentation:

gulp.task('app-scripts', function() {
const bundler = browserify('./src/js/app.js', {debug: true})
    .transform(babelify.configure({presets: ["es2015"]}));

return bundler.bundle()
    .pipe(source('app.min.js'))
    .pipe(buffer())
    .pipe(uglify())
    .pipe(sourcemaps.init())
    .pipe(sourcemaps.write('./', {addComment: false}))
    .pipe(gulp.dest('./build/js'));
});

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

How can one easily retrieve the callback function arguments from outside the function?

Here is a snippet of my code: var jenkins = require('jenkins')('http://192.168.1.5:8080'); var job_name = undefined; jenkins.job.list(function doneGetting(err, list) { if (err) throw err; job_name = list[0].name; }); jenkins. ...

Yarn combined with Webpack fails to execute post-compilation tasks

When using yarn and running yarn prod, I encountered the following error: https://i.stack.imgur.com/2emFk.jpg It seems to be stuck at this particular part of the code: mix.then(() => { execSync(`npm run rtlcss ${__dirname}/Assets/css/admin.css ${__dir ...

Having trouble with the installation of Parcel bundler via npm

Issue encountered while trying to install Parcel bundler for my React project using npm package manager. The terminal displayed a warning/error message during the command npm i parcel-bundler: npm WARN deprecated [email protected]: core-js@<3 is ...

How can you retrieve command line variables within your code by utilizing npm script in webpack?

I'm trying to access command line parameters from an npm script in my "constants.js" file. While I have been able to access parameters in the webpack.config.js file using process.env, it seems to be undefined in my app source files. The scenario con ...

Stopping npm private organization from releasing public packages

Is there a method to restrict the publication of public packages within an npm organization? It appears that this scenario would often arise (ensuring that no member of an organization accidentally publishes a package as public when it should be private b ...

What is the process for displaying node_modules directories in a json/javascript document?

Exploring ways to showcase the dependencies within my d3.js tree, I am curious if it's possible to dynamically list the dependencies' names in a JSON file or directly within the javascript file. I'm puzzled by how JavaScript can access folde ...

What are the steps to generating and sharing 'npm create' scripts?

I am looking to develop and release a script on npm that functions similarly to: npm create qwik@latest or yarn create next-app --typescript However, I am unsure where to begin. Despite searching extensively online, I have not been able to find any helpf ...

The eslint script encountered errors when running with meteor npm eslint command

After setting up Eslint, I encountered some errors that I couldn't quite figure out. When running meteor npm run lint, an error was thrown after completing the linting process. To fix this issue, I added the exit 0 attribute to gracefully exit the ...

Whenever I try to import a directory that contains modules, Webpack encounters an error

I am currently in the process of developing a small npm library to streamline API interaction. Here is an overview of my folder structure... dist/ index.js src/ index.js endpoints/ endpoint1.js package.json webpack.config.js Inside my src/index ...

Error: Unable to access the 'version' property of null

Having trouble installing any software on my computer, I've attempted various solutions suggested here but none have been successful. $ npm install axios npm ERR! Cannot read property '**version**' of null npm ERR! A complete log of this ru ...

Tips for customizing babel's preset plugin configurations

I've integrated babel-preset-react-app into my project with the following .babelrc configuration: { "presets": ["react-app"], "plugins": [ "transform-es2015-modules-commonjs", "transform-async-generator-functions" ] } Currently, I&apos ...

What steps do I need to follow in order to incorporate and utilize an npm package within my Astro component

I have been working on integrating KeenSlider into my project by installing it from npm. However, I am encountering an error message that says Uncaught ReferenceError: KeenSlider is not defined whenever I try to use the package in my Astro component. Belo ...

Encountering ReferenceError while running production build with Webpack

After upgrading to webpack 3.10.0 and Babel 6.26, I managed to fix my dev build but encountered issues with the prod build that I can't seem to resolve. This is the error message I am seeing: ERROR in ./src/index.js Module build failed: ReferenceErr ...

Tips on separating/callback functions based on earlier variables

Breaking Down Callback Functions Based on Previous Variables I am trying to figure out how to efficiently break down callback functions that depend on variables defined earlier in the code. Currently, my code resembles a "callback hell" due to my lack of ...

Encountering a 304 status error in the HTTP GET response after deploying a React app on Netlify

After deploying my react application on Netlify, I used the npm run build command to create the local scripts and manually deployed them in production mode on Netlify. The build scripts were generated on my local machine and then uploaded to the Net ...

After installing Ember and running the Ember server, I noticed that the node_modules directory appears to be empty. This issue is occurring on my Windows 10 x64 PC

Welcome to the command console: C:\Users\Documents\emberjs>ember server node_modules seem to be empty, consider running `npm install` Ember-cli version: 2.14.2 Node version: 6.11.2 Operating System: Windows 32-bit x64 I'm a beg ...

Gulp and Vinyl-fs not functioning properly when trying to save in the same folder as the source file due to issues with

After exploring a variety of solutions, I have yet to find success in modifying a file in place using Gulp.js with a globbing pattern. The specific issue I am facing can be found here. This is the code snippet I am currently working with: var fstrm = re ...

Tips for implementing dependency injection in AngularJs with ES6

I have integrated the yeoman angular-fullstack boilerplate into my project. 'use strict'; class LoginController { // Implementing login functionality. } angular.module('myApp') .controller('LoginController', LoginCont ...

Is it possible to incorporate a combination of es5 and es2015 features in an AngularJS application?

In my workplace, we have a large AngularJS application written in ES5 that is scheduled for migration soon. Rather than jumping straight to a new JS framework like Angular 2+ or React, I am considering taking the first step by migrating the current app to ...

Using ngTable within an AngularJS application

While working on my angularjs application, I encountered an issue with ngtable during the grunt build process. It seems that the references are missing, resulting in the following error: Uncaught Error: [$injector:modulerr] Failed to instantiate module pa ...