2016-03-26 1 views
0

私はCommonJsでbrowserifyを使用しています。バンドルデッドコードの削除

私はこの構造のファイル2を持っています。

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

私は、この構造を持つファイル1を持っています。

var file2 = require('./file2.js'); 

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

だから私は、バンドルの結果は次のようになり

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

私のターミナルで次のコマンドを実行します。

!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]); 

以下を取得するために、任意の変換と私のバンドル・ファイルに別の結果を得るためにあらゆるチャンスがあるならば、私は思っていました。

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

私はちょうどbrowserifyまたはWebPACKのかrequirejsの余分なコーディングなしで、最終的な結果に私のコードをしたい、私がちょうど間違ってツールを使用しますが、このツールのそれぞれを使用している場合、これは常に私に起こっていても良いです。

このツールの中には多かれ少なかれコーディングがありますが、この余分なコードを削除する方法を理解できませんでした。

+1

これを試してみてください:https://github.com/rollup/rollup Browserifyやwebpackは常にこの余分なコードを生成します。 –

答えて

2

デフォルトではes6モジュールを使用するrollupを使用できます。 Es6のインポートは静的なので、あなたのバンドルには関数やその中の何も必要はありません。あなたのコードは次のようになります。私のプロジェクトで
ファイル1

export default { 
    value: 'bling bling' 
}; 

ファイル2

import file2 from './file2.js'; 

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

、私はロールアップを使用するには、次のコマンドを実行します。

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

ここmore informationですes6モジュールについて

+0

ちょうど試してみて、それは完璧に動作します、ありがとう! –