1

coffeescript ファイルで import ステートメントが重複しないようにしています。

すべての *.coffee でこれら 3 つのファイルからインポートする必要があるとします。

#import "../node_modules/moment/moment.js"
#import "../testhelpers.js"
#import "../tuneup/tuneup.js"

コードの重複を避けるにはどうすればよいですか? 私は試した

  1. 別のヘルパー コーヒー ファイルを作成してインポートする
  2. 別のヘルパー JS ファイルを作成してインポートする

しかし、どちらもうまくいきませんでした。

これは Web アプリケーションではないため、JavaScript のサイズや不要な JS のロードは問題になりません。

4

1 に答える 1

1

node.jsを使用している場合は、requireを使用します。

moment = require "../node_modules/moment/moment.js"
testhelpers = require "../testhelpers.js"
tuneup = require "../tuneup/tuneup.js"

さらに、インポートするファイルでexportsオブジェクトを使用する必要があります。

たとえば、moment.jsの場合:

exports.somefunc = (foo) -> console.log(foo)

次に、インポートするとき:

moment = require "../node_modules/moment/moment.js"
moment.somefunc("hello world")

exportsrequireに電話すると、バインドされていないものにはアクセスできなくなります。

于 2013-02-24T19:44:42.100 に答える