LoginSignup
21
20

More than 5 years have passed since last update.

ブラウザ環境でcommonjs/requireを可能にするrequire1k

Last updated at Posted at 2015-01-30

Echo.jsで話題になってたので試してみた。

$ wget https://raw.githubusercontent.com/Stuk/require1k/master/require1k.min.js

index.htmlで読み込む

<html>
  <head>
    <title>try require1k</title>
    <script src="require1k.min.js" data-main="./main"></script>
  </head>
  <body>
  </body>
</html>

data-mainで指定したパスがエントリポイントになる。

main.jsではlodashは読み込んでみた。
どうやら、外部モジュールを呼び出す際にpackage.jsonのmainを見てないっぽくて、直接指定しなければならなかった。

var _ = require('lodash/index');
var marked = require('marked/index');
console.log(marked('# hello'));

パッケージがさらに入れ子になってるようなライブラリ(react)は呼び出せない模様。

サブセットなので browserify main.js で連結してもちゃんと動く。

どうせ環境作るなら非力だけど、mainまでサポートしたら開発環境用に使えなくもない気がした。

サンプルプロジェクト
https://github.com/mizchi-sandbox/try-require1k

以上です。

21
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
20