requirejs - 了解 r.js、almond 和相对路径

标签 requirejs r.js almond

我看到了this answer但 AFAICT 它对我不起作用。也许我在做一些愚蠢的事情。

我正在使用 almondgrunt-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>

它运行良好。 You can see it live it here .检查控制台,你会看到它打印出 hello from lib
于是我咕哝着跑。然后我运行一个使用 dist/app.js 的页面我得到了错误
Uncaught Error: undefined missing lib

这是 live page .

检查生成的dist/app.js我看到 lib 已经变成了这个
define('src/lib',[], function() {
   ...
});

主要是这样包含它
requirejs(['./lib'], function(lib) {
  ...
});

换句话说,r.js 生成的 id src/lib与 main 引用的 id 不匹配 ./lib .

对于 r.js,这似乎是一个非常直接的示例。就像实际上“ Hello World ”。

我究竟做错了什么?

我尝试过的一件事是更改 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] }

我没有得到什么?

The entire thing is checked into github here如果你想使用它。

最佳答案

所以这就是答案。

r.js 更喜欢模块名称而不是路径

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

关于requirejs - 了解 r.js、almond 和相对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35389879/

相关文章:

css - 通过r.js优化require.js/backbone app css文件是否包含cdn @imports

javascript - 有条件地组合 javascript 文件作为预编译步骤(比 r.js 更灵活)

requirejs - 使用r.js打包一个使用 'text'加载 View 的SPA应用程序

angularjs - 带有 angularJS 的简单 requireJS - Angular 未定义

node.js - 带有 grunt 的 Nodeunit 找不到模块 'tap'

javascript - 无需 require 即可使用 Requirejs 模块

javascript - r.js, almond : Is it possible for two . 包含杏仁共享依赖的js文件?

requirejs - 将 grunt requirejs 与 almond 结合使用会导致 "define is not defined"

javascript - Qlik Sense mashup RequireJS 添加 js 文件给出错误 'not a function'

TypeScript:如何用窗口对象替换导入?