

 [適用於 JavaScript 的 AWS SDK V3 API 參考指南](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/)詳細說明 第 3 版 適用於 JavaScript 的 AWS SDK (V3) 的所有 API 操作。

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 Webpack 綁定應用程式
<a name="webpack"></a>

在瀏覽器指令碼或 Node.js 中，Web 應用程式使用程式碼模組會建立相依性。這些程式碼模組可能會擁有自己的依存項目，成為一組您的應用程式運作所需的互連模組。若要管理相依性，您可以使用模組套件，例如 `webpack`。

`webpack` 模組套件會剖析您的應用程式程式碼、搜尋 `import`或 `require`陳述式，以建立套件，其中包含應用程式所需的所有資產。如此一來，即可透過網頁輕鬆提供資產。適用於 JavaScript 的 SDK 可以包含在 中`webpack`，做為要包含在輸出套件中的其中一個相依性。

如需 的詳細資訊`webpack`，請參閱 GitHub 上的 [Webpack 模組套件](https://webpack.github.io/)。

## 安裝 Webpack
<a name="webpack-installing"></a>

若要安裝`webpack`模組套件，您必須先安裝 Node.js 套件管理員 npm。輸入下列命令來安裝 `webpack` CLI 和 JavaScript 模組。

```
npm install --save-dev webpack
```

若要使用 `path`模組來使用使用 Webpack 自動安裝的檔案和目錄路徑，您可能需要安裝 Node.js `path-browserify`套件。

```
npm install --save-dev path-browserify
```

## 設定 Webpack
<a name="webpack-configuring"></a>

根據預設，Webpack `webpack.config.js`會在專案的根目錄中搜尋名為 的 JavaScript 檔案。此檔案能夠指定組態選項。以下是 WebPack 5.0.0 版和更新版本的`webpack.config.js`組態檔案範例。

**注意**  
Webpack 組態需求會根據您安裝的 Webpack 版本而有所不同。如需詳細資訊，請參閱 [Webpack 文件](https://webpack.js.org/configuration/)。

```
// Import path for resolving file paths
var path = require("path");
module.exports = {
  // Specify the entry point for our app.
  entry: [path.join(__dirname, "browser.js")],
  // Specify the output file containing our bundled code.
  output: {
    path: __dirname,
    filename: 'bundle.js'
  },
   // Enable WebPack to use the 'path' package.
   resolve:{
  fallback: { path: require.resolve("path-browserify")}
  }
  /**
  * In Webpack version v2.0.0 and earlier, you must tell 
  * webpack how to use "json-loader" to load 'json' files.
  * To do this Enter 'npm --save-dev install json-loader' at the 
  * command line to install the "json-loader' package, and include the 
  * following entry in your webpack.config.js.
  * module: {
    rules: [{test: /\.json$/, use: use: "json-loader"}]
  }
  **/
};
```

在此範例中， `browser.js` 指定為*進入點*。*進入點*是 檔案`webpack`用來開始搜尋匯入的模組。系統會指定輸出檔案名稱為 `bundle.js`，這個輸出檔案將包含應用程式需要執行的所有 JavaScript。如果進入點中指定的程式碼匯入或需要其他模組，例如適用於 JavaScript 的 SDK，則該程式碼會綁定，而不需要在組態中指定。

## 執行 Webpack
<a name="webpack-running"></a>

若要建置應用程式以使用 `webpack`，請將下列內容新增至 `package.json` 檔案中的 `scripts` 物件。

```
"build": "webpack"
```

以下是示範新增 的範例`package.json`檔案`webpack`。

```
{
  "name": "aws-webpack",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@aws-sdk/client-iam": "^3.32.0",
    "@aws-sdk/client-s3": "^3.32.0"
  },
  "devDependencies": {
    "webpack": "^5.0.0"
  }
}
```

若要建置您的應用程式，請輸入下列命令。

```
npm run build
```

然後，`webpack`模組綁定器會產生您在專案根目錄中指定的 JavaScript 檔案。

## 使用 webpack 套件
<a name="webpack-using-bundle"></a>

若要在瀏覽器指令碼中使用套件，您可以使用`<script>`標籤來整合套件，如下列範例所示。

```
<!DOCTYPE html>
<html>
    <head>
        <title>Amazon SDK with webpack</title>
    </head> 
    <body>
        <div id="list"></div>
        <script src="bundle.js"></script>
    </body>
</html>
```

## Node.js 套件
<a name="webpack-nodejs-bundles"></a>

您可以使用 在組態中指定 `node`為目標，`webpack`以產生在 Node.js 中執行的套件。

```
target: "node"
```

當您在磁碟空間有限的環境中執行 Node.js 應用程式時，這個做法十分實用。下方為 `webpack.config.js` 組態範例，而該組態會將 Node.js 指定為輸出目標。

```
// Import path for resolving file paths
var path = require("path");
module.exports = {
  // Specify the entry point for our app.
  entry: [path.join(__dirname, "browser.js")],
  // Specify the output file containing our bundled code.
  output: {
    path: __dirname,
    filename: 'bundle.js'
  },
  // Let webpack know to generate a Node.js bundle.
  target: "node",
   // Enable WebPack to use the 'path' package.
   resolve:{
  fallback: { path: require.resolve("path-browserify")}
   /**
   * In Webpack version v2.0.0 and earlier, you must tell 
   * webpack how to use "json-loader" to load 'json' files.
   * To do this Enter 'npm --save-dev install json-loader' at the 
   * command line to install the "json-loader' package, and include the 
   * following entry in your webpack.config.js.
   module: {
    rules: [{test: /\.json$/, use: use: "json-loader"}]
  }
  **/
};
```