Browserify-require()一个组合字符串不会将模块构建到输出脚本中

Browserify - require() a conbimed string will not build the module into output script

本文关键字:构建 模块 脚本 输出 一个 组合 Browserify-require 字符串      更新时间:2023-09-26

if require()是一个组合字符串路径。该模块路径将不包括在输出脚本中类似:

require("./"+"b" );
//or
var path="./";
require(path+"b");

我有a.js

module.exports="a";

b.js

module.exports="b";

如果使用var b = require('./'+'b');,结果将是:

(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
module.exports="a";
},{}],2:[function(require,module,exports){
var a = require('./a');
var b = require('./'+'b');
},{"./a":1}]},{},[2]);

如果使用var b = require('./b');,结果将是:

(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
module.exports="a";
},{}],2:[function(require,module,exports){
module.exports="b";
},{}],3:[function(require,module,exports){
var a = require('./a');
var b = require('./b');
},{"./a":1,"./b":2}]},{},[3]);

我打开问题:https://github.com/substack/node-browserify/issues/883

一种解决方案是使用别名。