0

I am using browserify with CommonJs.

I have file 2 with this structure.

module.exports = { value: 'bling bling' }; 

I have file 1 with this structure.

var file2 = require('./file2.js'); console.log('this is the file 2 object value', file2.value); 

So I run the following command in my terminal

$ browserify -g uglifyify ./file1.js | uglifyjs -c > bundle.js 

The bundle result would be.

!function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a="function"==typeof require&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}for(var i="function"==typeof require&&require,o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){var file2=require("./file2.js");console.log("this is the file 2 object value",file2.value)},{"./file2.js":2}],2:[function(require,module,exports){module.exports={value:"bling bling"}},{}]},{},[1]); 

I was wondering if there is any chance to get a different result for my bundle file with any transformation in order to get the following.

var file2 = { value: 'bling bling' }; console.log('this is the file 2 object value', file2.value); 

I just want my code in the final result without any extra coding of browserify or webpack or requirejs, may be I am just using the tool incorrectly, but this always happens to me when using each of this tools.

Some of this tools produce more or less coding, but I could not figure it out how to remove this extra code.

1
  • 1
    Try this one: github.com/rollup/rollup Browserify or webpack will always generate this extra code. Commented Mar 26, 2016 at 15:51

1 Answer 1

2

You can use rollup, which uses es6 modules by default. Es6 imports are static, so your bundle will have no require functions or anything in it. Your code would then be:
File 1

export default { value: 'bling bling' }; 

File 2

import file2 from './file2.js'; console.log('this is the file 2 object value', file2.value); 

In my project, I run the following command to use rollup:

./node_modules/rollup/bin/rollup file1.js

Here's more information about es6 modules.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.