javascript - Removing bundle dead code -


i using browserify commonjs.

i have file 2 structure.

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

i have file 1 structure.

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

so run following command in terminal

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

the bundle result 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 file 2 object value",file2.value)},{"./file2.js":2}],2:[function(require,module,exports){module.exports={value:"bling bling"}},{}]},{},[1]); 

i wondering if there chance different result bundle file transformation in order following.

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

i want code in final result without coding of browserify or webpack or requirejs, may using tool incorrectly, happens me when using each of tools.

some of tools produce more or less coding, not figure out how remove code.

you can use rollup, uses es6 modules default. es6 imports static, bundle have no require functions or in it. code be:
file 1

export default {     value: 'bling bling' }; 

file 2

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

in project, run following command use rollup:

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

here's more information es6 modules.


Comments