简体   繁体   English

如何使用webpack导入aws-sdk

[英]How to use webpack import aws-sdk

I found this issues in the official, but it looks like they refused to answer. 我在官方发现了这个问题 ,但看起来他们拒绝回答。 So I can only ask questions on SO. 所以我只能在SO上提问。 Here is my Error&Warning Log: 这是我的错误和警告日志:

WARNING in ./~/aws-sdk/lib/util.js
Critical dependencies:
40:30-45 the request of a dependency is an expression
43:11-53 the request of a dependency is an expression
 @ ./~/aws-sdk/lib/util.js 40:30-45 43:11-53

WARNING in ./~/aws-sdk/lib ^\.\/.*$
Module not found: Error: Cannot resolve directory '.' in /Users/me/Documents/Sources/my-project/client/node_modules/aws-sdk/lib
 @ ./~/aws-sdk/lib ^\.\/.*$

WARNING in ./~/aws-sdk/lib/api_loader.js
Critical dependencies:
13:15-59 the request of a dependency is an expression
104:12-46 the request of a dependency is an expression
108:21-58 the request of a dependency is an expression
114:18-52 the request of a dependency is an expression
 @ ./~/aws-sdk/lib/api_loader.js 13:15-59 104:12-46 108:21-58 114:18-52

WARNING in ./~/aws-sdk/lib/region_config.json
Module parse failed: /Users/me/Documents/Sources/my-project/client/node_modules/aws-sdk/lib/region_config.json Line 2: Unexpected token :
You may need an appropriate loader to handle this file type.
| {
|   "rules": {
|     "*/*": {
|       "endpoint": "{service}.{region}.amazonaws.com"
 @ ./~/aws-sdk/lib ^\.\/.*$

ERROR in ./~/aws-sdk/lib/api_loader.js
Module not found: Error: Cannot resolve module 'fs' in /Users/me/Documents/Sources/my-project/client/node_modules/aws-sdk/lib
 @ ./~/aws-sdk/lib/api_loader.js 1:9-22

ERROR in ./~/aws-sdk/lib/services.js
Module not found: Error: Cannot resolve module 'fs' in /Users/me/Documents/Sources/my-project/client/node_modules/aws-sdk/lib
 @ ./~/aws-sdk/lib/services.js 1:9-22

There are three types: 有三种类型:

  1. Cannot resolve module 'fs' 无法解析模块'fs'

I only need to install fs can solve this. 我只需要安装fs就可以解决这个问题。

  1. need an appropriate loader 需要适当的装载机

Well, this will need to install json-loader , and set it in webpack.config.js , but also can solve. 好吧,这需要安装json-loader ,并在webpack.config.js设置,但也可以解决。

  1. Critical dependencies 关键依赖关系
  2. Module not found: Error: Cannot resolve directory '.' 找不到模块:错误:无法解析目录'。'

I webpack newbie.So, i don't know how to solve this. 我webpack newbie.So,我不知道如何解决这个问题。 Will someone help me? 有人会帮助我吗? thanks. 谢谢。

UPDATE: 更新:

  1. Module not found: Error: Cannot resolve directory '.' 找不到模块:错误:无法解析目录'。'

that is my fault, config file's extensions missing a . 这是我的错,配置文件的扩展缺少了.

I found this blog post that fixed it for me. 我找到了这篇博文 ,为我修好了。

Essentially you need to import the built version of the library. 基本上,您需要导入库的内置版本。

All credit goes to the author. 所有功劳都归功于作者。 Here is the code: 这是代码:

require('aws-sdk/dist/aws-sdk');
var AWS = window.AWS;

ES6 version: ES6版本:

import 'aws-sdk/dist/aws-sdk';
const AWS = window.AWS;

config: 配置:

module: {
  noParse: [
   /aws/
  ]
}

usage: 用法:

window.AWS to the reference of the global AWS object. window.AWS到全局AWS对象的引用。

Using the noParse method should work if you are creating a node package, as this is setting webpack to not apply any parsing/loaders. 如果要创建节点包,则使用no​​Parse方法应该有效,因为这会将webpack设置为不应用任何解析/加载器。 This did not work for me when creating a umd formatted output file/library. 在创建umd格式的输出文件/库时,这对我不起作用。

To create a umd formatted library I had to use loaders to Browserify aws-sdk and handle json files. 要创建一个umd格式的库,我必须使用加载器来浏览aws-sdk并处理json文件。

Install the loaders : 安装加载器

npm install json-loader --save-dev

npm install transform-loader brfs --save-dev

Webpack Config: Webpack配置:

module: {
  loaders: [
    { test: /aws-sdk/, loaders: ["transform?brfs"]},
    { test: /\.json$/, loaders: ['json']},
  ]
},
output: {
  library: 'LibraryName',
  libraryTarget: 'umd'
},
resolve: {
  extensions: ['', '.js']
}

Replace LibraryName with you own namespacing. 用您自己的命名空间替换LibraryName Currently the library would be used through a constructor as follows: 目前,库将通过构造函数使用,如下所示:

var libObj = new LibraryName();

AWS SDK从2.6.1版开始添加了对webpack支持,请参阅使用webpack和AWS SDK for JavaScript创建和捆绑应用程序 - 第1部分博客文章,描述如何将aws-sdk引入webpack bundle。

use npm install json-loader --save-dev add the following code to webpack.config.js 使用npm install json-loader --save-dev将以下代码添加到webpack.config.js

  module: {
loaders: [{
  test: /\.js$/,
  loaders: ['babel'],

  exclude: /node_modules/,
},
{
      test: /.json$/,
      loaders: ['json']
    }]

} }

Just import * as AWS from 'aws-sdk' 只需从'aws-sdk'导入*作为AWS

Notice that we specified a loader to tell webpack how to handle importing JSON files, in this case by using the json-loader we installed earlier. 请注意,我们指定了一个加载器来告诉webpack如何处理导入JSON文件,在这种情况下使用我们之前安装的json-loader。 By default, webpack only supports JavaScript, but uses loaders to add support for importing other file types as well. 默认情况下,webpack仅支持JavaScript,但也使用加载器添加对导入其他文件类型的支持。 The AWS SDK makes heavy use of JSON files, so without this extra configuration, webpack will throw an error when generating the bundle. AWS开发工具包大量使用JSON文件,因此如果没有这些额外配置,webpack将在生成捆绑包时抛出错误。

Update(2015-10-20): 更新(2015年10月20日):

aws-sdk fix this. aws-sdk解决了这个问题。 i can use it from npm. 我可以从npm使用它。

thanks, aws-sdk team. 谢谢,aws-sdk团队。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM