[英]Using aws-sdk in a typescript / jquery / webpack app
Hello I have a typescript/jquery/webpack app all recent releases. 您好,我所有最近的发行版都有一个typescript / jquery / webpack应用程序。 Everything is working fine. 一切正常。 I wanted to introduce the aws-sdk into it. 我想将aws-sdk引入其中。 I followed the pattern I used to import other libraries like '_' which do work. 我遵循了以前导入有效的其他库(如“ _”)的模式。
import * as _ from '../node_modules/lodash-es/lodash';
what happens when i go to run it is i get a bunch of errors like this: 当我运行时会发生什么,我会收到很多这样的错误:
ERROR in .../code-projects/.../tsconfig.json
error TS2318: Cannot find global type 'Number'.
ERROR in .../code-projects/.../tsconfig.json
error TS2318: Cannot find global type 'Object'.
ERROR in .../code-projects/.../tsconfig.json
error TS2318: Cannot find global type 'RegExp'.
ERROR in .../code-projects/.../tsconfig.json
error TS2318: Cannot find global type 'String'.
I then followed the aws-sdk instructions for typescript. 然后,我按照aws-sdk的说明进行打字。
same result. 同样的结果。
The import i us is the following: 我们导入的内容如下:
import * as AWS from '../node_modules/aws-sdk/dist/aws-sdk';
and webstorm does not complain and the autocomplete works. 和webstorm不会抱怨,并且自动完成工作。
My webpack is included if that will help. 如果可以,则包括我的webpack。
Question: What is the proper way to include the aws-sdk into this type of framework. 问题:将aws-sdk包含在此类框架中的正确方法是什么?
const webpack = require('webpack');
const path = require('path');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: [
"bootstrap-webpack",
'./src/index.ts'
],
module: {
rules: [
// the url-loader uses DataUrls.
// the file-loader emits files.
{
test: /\.html$/,
loader: 'raw-loader',
options: {
minimize: true
}
},
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
},
{test: require.resolve("jquery"), use: "imports-loader?$=jquery"},
{
test: /\.(woff|woff2)(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url-loader?limit=10000&mimetype=application/font-woff'
},
{test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: 'url-loader?limit=10000&mimetype=application/octet-stream'},
{test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: 'file-loader'},
{test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: 'url-loader?limit=10000&mimetype=image/svg+xml'},
{
test: /\.png$/,
exclude: /node_modules/,
loader: 'url-loader'
},
{
test: /\.gif/,
exclude: /node_modules/,
loader: 'url-loader'
},
{
test: /\.jpg/,
exclude: /node_modules/,
loader: 'url-loader'
},
{
test: /\.css$/,
exclude: /node_modules/,
loader: "style-loader!css-loader"
},
{test: /\.ts$/, loader: 'ts-loader', exclude: /node_modules/},
{
test: /.json$/,
loaders: ['json']
}
]
},
resolve: {
extensions: ['.js', '.ts']
},
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html',
hash: true
}),
new webpack.LoaderOptionsPlugin({
debug: true
}),
new CleanWebpackPlugin(['dist/*.*'], {})
]
};
I tried a bunch of things with different results. 我尝试了一堆不同的结果。 I got to a point where I could compile my project but during runtime all constructors and methods would fail because they didn't exist. 我到了可以编译我的项目的地步,但是在运行时,所有构造函数和方法都会失败,因为它们不存在。 I assumed this was a types loading problem? 我以为这是类型加载问题? Not even sure how to describe it correctly. 甚至不确定如何正确地描述它。
However after reading about typescript modules and compiling module types, i got my answer from here . 但是,在阅读了有关打字稿模块和编译模块类型的内容之后,我从这里得到了答案。
it was a combination of things; 这是事物的结合; one from the article and one from the comments: 一个来自文章,另一个来自评论:
The description of the problem first... 首先说明问题...
Use the pre-built version of the aws-sdk. 使用aws-sdk的预构建版本。 Using it sidesteps these require() issues because all the dependencies have been included in a single file. 使用它回避了这些require()问题,因为所有依赖项都已包含在单个文件中。
In the aws-sdk node module, the pre-built file is located at dist/aws-sdk.js. 在aws-sdk节点模块中,预构建的文件位于dist / aws-sdk.js。 You can import it with require('aws-sdk/dist/aws-sdk'). 您可以使用require('aws-sdk / dist / aws-sdk')导入它。
There's one more problem - the pre-built file doesn't export any variables, but instead just adds AWS to the window object. 还有一个问题-预先构建的文件不会导出任何变量,而只会将AWS添加到window对象中。
and how I got it to work, because adding it to the window object did not work. 以及我如何使它工作,因为将其添加到窗口对象不起作用。
import '../node_modules/aws-sdk/dist/aws-sdk';
declare const AWS: any;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.