Source Assets di Webpack
Sekarang mari kita coba source assets. Mari kita buat agar konten dari file teks masuk ke dalam variabel saat diimpor:
export default {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve('dist'),
},
module: {
rules: [
{
test: /\.txt$/,
type: 'asset/source',
}
]
},
plugins: [
new HtmlWebpackPlugin(),
],
};
Mari kita buat sebuah file teks:
abcde
Mari hubungkan file ini ke entry point:
import text from './text.txt';
Konten dari file teks akan masuk
ke dalam variabel text:
console.log(text); // akan menampilkan 'abcde'
Hubungkan beberapa file teks ke entry point. Dapatkan kontennya ke dalam variabel.