我有一个问题,react-sortable-mixin 不尊重我的 package.json 文件中的 browserify-shim global:Sortable 声明,从而导致 Sortable 模块被打包在我的包中。我在其他模块上遇到了类似的问题,所以这很可能是我的配置问题。我创建了一个小测试包,它只需要 React 和 react-sortbable-mixin 来演示问题:
包.json
{
"name": "test",
"version": "1.0.0",
"description": "Just a test",
"main": "index.js",
"dependencies": {
"react": "^0.14.8",
"sortablejs": "^1.4.2"
},
"devDependencies": {
"browserify-shim": "^3.8.12"
},
"browserify": {
"transform": [
"browserify-shim"
]
},
"browserify-shim": {
"react": "global:React",
"sortablejs": "global:Sortable",
"sortablejs/react-sortable-mixin": {
"depends": "sortablejs:Sortable"
}
}
}
index.js
"use strict";
var React = require('react');
var SortableMixin = require('sortablejs/react-sortable-mixin');
react-sortable-mixin.js(只是模块的第一部分......)
(function (factory) {
'use strict';
if (typeof module != 'undefined' && typeof module.exports != 'undefined') {
module.exports = factory(require('./Sortable'));
}
else if (typeof define === 'function' && define.amd) {
define(['./Sortable'], factory);
}
else {
/* jshint sub:true */
window['SortableMixin'] = factory(Sortable);
}
})(function (/** Sortable */Sortable) { ...
这是 browserify 的诊断输出:
% BROWSERIFYSHIM_DIAGNOSTICS=1 browserify index.js -t browserify-shim -o bundle.js
{ file: '/home/jlafosse/test/index.js',
info:
{ package_json: '/home/jlafosse/test/package.json',
packageDir: '/home/jlafosse/test',
shim: undefined,
exposeGlobals: { react: 'React', sortablejs: 'Sortable' },
browser: undefined,
'browserify-shim':
{ react: 'global:React',
sortablejs: 'global:Sortable',
'sortablejs/react-sortable-mixin': { depends: 'sortablejs:Sortable' } },
dependencies: { react: '^0.14.8', sortablejs: '^1.4.2' } },
messages:
[ 'Resolved "sortablejs/react-sortable-mixin" found in package.json to "/home/jlafosse/test/sortablejs/react-sortable-mixin"',
'Found depends "sortablejs" as an installed dependency of the package',
{ resolved:
{ '/home/jlafosse/test/sortablejs/react-sortable-mixin':
{ exports: null,
depends: { sortablejs: 'Sortable' } } } } ] }
{ file: '/home/jlafosse/test/index.js',
info:
{ package_json: '/home/jlafosse/test/package.json',
packageDir: '/home/jlafosse/test',
resolvedPreviously: true,
shim: undefined,
exposeGlobals: { react: 'React', sortablejs: 'Sortable' },
browser: undefined,
'browserify-shim':
{ react: 'global:React',
sortablejs: 'global:Sortable',
'sortablejs/react-sortable-mixin': { depends: 'sortablejs:Sortable' } },
dependencies: { react: '^0.14.8', sortablejs: '^1.4.2' } },
messages: [] }
我知道全局匀场是有效的,因为 React 不包含在捆绑包中......但是如上所述,Sortable 被包含在内。此外,如果我只是 require('sortablejs') 而不是 index.js 文件中的 sortablejs/react-sortable-mixin,Sortable 将不会按预期包含在包中,所以我的想法是我的配置错误或模块react-sortable-mixin 的格式不尊重 browserify-shim。
任何帮助表示赞赏。