为本地相关需求修复 Browserify 未找到的模块

Fixing Module Not Found with Browserify for Local Relative Requires

我有一个结构如下的演示 NPM 框架:

./src/child.咖啡

module.exports = class Child

./src/parent.咖啡

Child = require "./child"
module.exports = class Parent

./gulp.js

require('coffee-script/register');
require('./gulpfile.coffee');

./gulp.coffee

gulp = require "gulp"

util = require "gulp-util"
coffee = require "gulp-coffee"
browserify = require "gulp-browserify"

gulp.task "build", ->
  gulp.src("./src/**/*.coffee")
    .pipe(coffee().on("error", util.log))
    .pipe(gulp.dest("./lib/"))

如果我 运行 gulp build,一切都会很好。但是,我试图通过将以下内容添加到 build 任务来引入 Browserify:

gulp.task "build", ->
  gulp.src("./src/**/*.coffee")
    .pipe(coffee().on("error", util.log))
    .pipe(browserify())
    .pipe(gulp.dest("./lib/"))

有了这个,框架内的相关要求就失败了:

events.js:85 throw er; // Unhandled 'error' event ^ Error: module "./child" not found from "/Users/kevin/Desktop/demo/src/fake_d1543b04.js" at notFound (/Users/kevin/Desktop/demo/node_modules/gulp-browserify/node_modules/browserify/index.js:803:15) at /Users/kevin/Desktop/demo/node_modules/gulp-browserify/node_modules/browserify/index.js:754:23 at /Users/kevin/Desktop/demo/node_modules/gulp-browserify/node_modules/browserify/node_modules/browser-resolve/index.js:185:24 at /Users/kevin/Desktop/demo/node_modules/gulp-browserify/node_modules/browserify/node_modules/resolve/lib/async.js:36:22 at load (/Users/kevin/Desktop/demo/node_modules/gulp-browserify/node_modules/browserify/node_modules/resolve/lib/async.js:54:43) at /Users/kevin/Desktop/demo/node_modules/gulp-browserify/node_modules/browserify/node_modules/resolve/lib/async.js:60:22 at /Users/kevin/Desktop/demo/node_modules/gulp-browserify/node_modules/browserify/node_modules/resolve/lib/async.js:16:47 at FSReqWrap.oncomplete (fs.js:95:15)

如何在框架内正确设置相关需求并让它与 gulp 和 coffeescript 一起工作?更改 package.json(即 Lodash = require "lodash")依赖项的要求允许 gulp 构建。此外,从 coffeescript 更改为常规 javascript 也可以解决问题。

问题可能出在 .coffee 扩展名上。您需要指示 browserify 查找该扩展名。参见 opts.extensions。我不知道你如何用 gulp-browserify 做到这一点,但你最好还是不要使用它(它被 gulp 列入黑名单,如果这对你很重要,我相信它是未维护)。