4

私はこの答えを見ましたが、AFAICTは私にとってはうまくいきません。たぶん私は愚かなことをしています。

私はアーモンドgrunt-contrib-requirejsを使用しています。いろいろ試してみました

これが私のレイアウトです

.
├── Gruntfile.js
├── 3rdparty
│   ├── require.js
├── src
│   ├── lib.js
│   └── main.js
└── node_modules
    └── almond
        └── almond.js

そして、ここに私の grunt-contrib-requirejs 設定があります

requirejs: {
  full: {
    options: {
      baseUrl: "./",
      name: "node_modules/almond/almond.js",
      include: [ "src/main.js" ],
      out: "dist/app.js",
      optimize: "none",
    },
  },
},

main.js はこんな感じ

requirejs(['./lib',], function(lib) {
  lib.hello();
});

lib.js は次のようになります

define([], function() {
    return {
      hello: function() {
          console.log("hello from lib");
      },
    };
});

次のようにrequire.jsを使用するページを実行すると

<script src="3rdparty/require.js" data-main="src/main.js"></script>

それはうまくいきます。ここでライブを見ることができます。コンソールを確認すると、印刷されていることがわかりますhello from lib

だから私はうなり声を上げます。次に、を使用するページを実行するdist/app.jsと、エラーが発生します

Uncaught Error: undefined missing lib

こちらがライブページです。

生成されたものdist/app.jsを確認すると、libがこれに変わっていることがわかります

define('src/lib',[], function() {
   ...
});

そしてメインはこのようにそれを含めています

requirejs(['./lib'], function(lib) {
  ...
});

つまり、r.js が生成src/libした id は、main が参照している id と一致しません./lib

これは、r.js の非常に単純な例のように思えます。実質的に「ハローワールド」のように。

私は何を間違っていますか?

私が試したことの1つは、に変更するbaseUrlことです./src

requirejs: {
  full: {
    options: {
      baseUrl: "./src",
      name: "node_modules/almond/almond.js",
      include: [ "src/main.js" ],
      out: "dist/app.js",
      optimize: "none",
    },
  },
},

しかし今、私は得る

{ [Error: Error: ENOENT: no such file or directory, open '/Users/gregg/temp/grunt-contrib-requirejs-example/src/node_modules/almond/almond.js'
    at Error (native)
]
  originalError: 
   { [Error: ENOENT: no such file or directory, open '/Users/gregg/temp/grunt-contrib-requirejs-example/src/node_modules/almond/almond.js']
     errno: -2,
     code: 'ENOENT',
     syscall: 'open',
     path: '/Users/gregg/temp/grunt-contrib-requirejs-example/src/node_modules/almond/almond.js',
     fileName: '/Users/gregg/temp/grunt-contrib-requirejs-example/src/node_modules/almond/almond.js' } }

だから私はアーモンドパスを修正しようとします

requirejs: {
  full: {
    options: {
      baseUrl: "./src",
      name: "../node_modules/almond/almond.js",
      include: "main",
      out: "dist/app.js",
      optimize: "none",
    },
  },
},

でもそれも失敗

{ [Error: Error: ERROR: module path does not exist: ../node_modules/almond/almond.js for module named: ../node_modules/almond/almond.js. Path is relative to: /Users/gregg/temp/grunt-contrib-requirejs-example
    at /Users/gregg/temp/grunt-contrib-requirejs-example/node_modules/requirejs/bin/r.js:30214:35
]
  originalError: [Error: ERROR: module path does not exist: ../node_modules/almond/almond.js for module named: ../node_modules/almond/almond.js. Path is relative to: /Users/gregg/temp/grunt-contrib-requirejs-example] }

何が得られないのですか?

作業したい場合は、すべてがこちらの github にチェックインされています。

4

1 に答える 1