【问题标题】:WebPack: How to look both for bower.json and .bower.jsonWebPack:如何同时查找 bower.json 和 .bower.json
【发布时间】:2015-03-20 09:32:55
【问题描述】:

我已经能够让 webpack 在 bower_components 中查找组件。当 bower 组件仅包含 .bower.json 而不是 bower.json 文件时,我遇到了问题(请注意,第一个文件名前面有 .)。

我的webpack.config.js 文件的简化版本如下所示:

var ResolverPlugin = require('webpack/lib/ResolverPlugin');

// webpack configuration
module.exports = {
    entry: {
        app: 'app.js'
    },
    output: {
        filename: '[name].js'
    },
    resolve: {
        modulesDirectories: ['node_modules', 'web_modules', 'bower_components', 'static']
    },
    plugins: [
        new ResolverPlugin(
            new ResolverPlugin.DirectoryDescriptionFilePlugin('bower.json', ['main'])
        )
    ]
};

我正在尝试使用 jquery.selectBoxIt 作为凉亭组件。另一个问题可能是它的.bower.json 文件中缺少main 字段。对于它的价值,我在下面包含了该文件:

// .bower.json

{
  "name": "jquery.selectBoxIt",
  "homepage": "https://github.com/gfranko/jquery.selectBoxIt.js",
  "version": "3.8.1",
  "_release": "3.8.1",
  "_resolution": {
    "type": "version",
    "tag": "v3.8.1",
    "commit": "ffa615b25ebf4fac392726b17c857d429acf244c"
  },
  "_source": "git://github.com/gfranko/jquery.selectBoxIt.js.git",
  "_target": "~3.8.1",
  "_originalSource": "jquery.selectBoxIt",
  "_direct": true
}

【问题讨论】:

    标签: node.js bower webpack


    【解决方案1】:

    .bower.json 是 Bower 在依赖解析期间生成的文件。它不等同于bower.json,通常不应使用,因为它是一个可能会更改的internal 文件。
    如果您只看到 .bower.json 文件,则意味着该软件包没有 bower.json(Bower 实际上可以在没有它的情况下工作)。在这种情况下,这意味着您将无法找到“主要”属性。
    例如,jquery.selectBoxIt.js 包仅在其 master 分支中具有 bower.json 文件,但在其发布标签中没有(它可能尚未发布)。当您解决它时,Bower 将使用 3.8.1 git 标记,如您在 .bower.json 文件中所见。在这个标签中没有 bower.json 文件。
    对于此类包,您可能需要考虑其他选项,例如 CommonsJS。

    【讨论】:

    • 到目前为止,我已经在我的 webpack 配置中添加了一个额外的插件。这着眼于.bower.json_originalSource 字段。它有效,但你会说这是一个有风险的解决方案吗?如果你这么认为,我会求助于使用 CommonJS
    • _originalSource 不等同于 main 属性,所以我不确定使用它是否是个好主意。在没有 bower.json 的情况下,您无法知道主文件是什么。在这种情况下,我认为您应该尝试使用包含此信息的另一个描述符。此外,.bower.json 文件的结构不是公开的,将来可能会更改(或可能不会)。
    猜你喜欢
    • 2016-12-01
    • 2015-11-09
    • 2014-12-15
    • 1970-01-01
    • 2017-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-13
    相关资源
    最近更新 更多