Panduan Referensi APIAWS SDK for JavaScript V3 menjelaskan secara rinci semua operasi API untukAWS SDK for JavaScript versi 3 (V3).
Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.
Penggunaan modul kode oleh aplikasi web dalam skrip browser atau Node.js menciptakan dependensi. Modul kode ini dapat memiliki dependensi sendiri, menghasilkan kumpulan modul yang saling berhubungan yang dibutuhkan aplikasi Anda untuk berfungsi. Untuk mengelola dependensi, Anda dapat menggunakan bundler modul sepertiwebpack.
Parameterwebpackmodul bundler mem-parsing kode aplikasi Anda, mencarinpm install --save-dev path-browserify0ataunpm install --save-dev path-browserify1pernyataan, untuk membuat bundel yang berisi semua aset yang dibutuhkan aplikasi Anda. Hal ini agar aset dapat dengan mudah dilayani melalui halaman web. SDK untuk JavaScript dapat dimasukkan dalamwebpacksebagai salah satu dependensi untuk memasukkan dalam bundel output.
Untuk informasi lebih lanjut tentangwebpack, lihatpaketan moduldi GitHub.
Menginstal webpack
Untuk menginstalwebpackbundler modul, Anda harus terlebih dahulu memiliki npm, manajer paket Node.js, diinstal. Ketik perintah berikut untuk menginstalwebpackCLI dan JavaScript modul.
npm install --save-dev webpackUntuk menggunakannpm install --save-dev path-browserify6modul untuk bekerja dengan file dan direktori path, yang diinstal secara otomatis dengan webpack, Anda mungkin perlu menginstal Node.jsnpm install --save-dev path-browserify7paket.
npm install --save-dev path-browserifyMengonfigurasi webpack
Secara default, Webpack mencari file JavaScript bernamanpm install --save-dev path-browserify8di direktori root proyek. File ini menentukan opsi konfigurasi Anda. Berikut ini adalah contohnpm install --save-dev path-browserify8file konfigurasi untuk WebPack versi 5.0.0 dan versi terbaru.
Persyaratan konfigurasi Webpack bervariasi tergantung pada versi Webpack yang Anda instal. Untuk informasi selengkapnya, lihatdokumentasi Webpack.
// 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"}] } **/ };Dalam contoh ini,// 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"}] } **/ };0ditentukan sebagaititik masuk. Parametertitik masukadalah filewebpackmenggunakan untuk mulai mencari modul impor. Nama file output ditentukan sebagai// 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"}] } **/ };2. File output ini akan berisi semua JavaScript aplikasi perlu dijalankan. Jika kode yang ditentukan dalam impor entry point atau memerlukan modul lain, seperti SDK untuk JavaScript, kode yang dibundel tanpa perlu menentukan dalam konfigurasi.
Menjalankan webpack
Membangun aplikasi yang akan digunakanwebpack, tambahkan yang berikut ini ke// 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"}] } **/ };4objek di// 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"}] } **/ };5berkas.
Berikut ini adalah contoh-contohnya// 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"}] } **/ };5file yang menunjukkan menambahkanwebpack.
{ "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" } }Untuk membangun aplikasi Anda, masukkan perintah berikut.
npm run buildParameterwebpackpackage modul kemudian menghasilkan file JavaScript yang Anda tentukan di direktori root proyek.
Menggunakan bundel webpack
Untuk menggunakan bundel dalam skrip browser, Anda dapat menggabungkan bundel menggunakan// 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"}] } **/ };9
Paketan untuk Node.js
Anda dapat menggunakanwebpackuntuk menghasilkan bundel yang berjalan di Node.js dengan menentukan"build": "webpack"1sebagai target dalam konfigurasi.
target: "node"Hal ini berguna ketika menjalankan aplikasi Node.js di lingkungan di mana ruang disk terbatas. Ini contohnyanpm install --save-dev path-browserify8konfigurasi dengan Node.js ditentukan sebagai target output.