Skip to content

Instantly share code, notes, and snippets.

@uupaa
Last active August 29, 2015 13:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uupaa/9394739 to your computer and use it in GitHub Desktop.
Save uupaa/9394739 to your computer and use it in GitHub Desktop.
The own field add to package.json.

npm の設定ファイル package.json は、 https://www.npmjs.org/doc/files/package.json.html で定義されているフィールドの他にも、 貴方好みの独自のフィールドを追加した状態で、 npm publish が可能です(単にエラーにならないという話です)。

たとえば、npm モジュールの雛形 Xxx.js の package.json では、 "build": { ... } フィールドを追加しています。

{
  "name":           "uupaa.xxx.js",
  "version":        "0.8.3",
  "description":    "",
  "url":            "https://github.com/uupaa/Xxx.js",
  "keywords":       ["Xxx", "util", "uupaa"],
  "repository": {
    "type":         "git",
    "url":          "https://github.com/uupaa/Xxx.js.git"
  },
  "scripts": {
    "watch":        "node node_modules/uupaa.watch.js --verbose --action build ./lib/Xxx.js",
    "build":        "node node_modules/uupaa.minify.js --keep --output ./lib/Xxx.min.js ./lib/Xxx.js",
    "test":         "NODE_ENV=production NODE_PATH=lib node --harmony test/index.node.js; open test/index.html",
    "lint":         "plato --title Xxx.js --jshint .jshintrc -d lint/plato lib/Xxx.js; open lint/plato/index.html"
  },
  "build": {        ←ここね
    "all": {
      "lib/Xxx.js":     ""
    },
    "node": {
    },
    "worker": {
    },
    "browser": {
    }
  },
  "dependencies": {
  },
  "devDependencies": {
    "uupaa.help.js":    "",
    "uupaa.task.js":    "",
    "uupaa.test.js":    "",
    "uupaa.watch.js":   "",
    "uupaa.minify.js":  ""
  },
  "lib":            "./lib/",
  "main":           "./index.js",
  "author":         "uupaa <uupaa.js@gmail.com>",
  "license":        "MIT",
  "contributors":   []
}

Xxx.js では、各環境向けにビルドするファイルの一覧を "build": { ... } フィールドに列挙しておく。
といった使い方をしています。

@shibukawa
Copy link

jshintがこの技を使っていますよね。

jshintConfigがpackage.jsonにあったらそっちを使う(.jshintrcを無視する)というのでハマったことがあります。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment