Source assets in webpack
Let's try source assets now. Let's make it so that the contents of text files go into a variable when imported:
export default {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve('dist'),
},
module: {
rules: [
{
test: /\.txt$/,
type: 'asset/source',
}
]
},
plugins: [
new HtmlWebpackPlugin(),
],
};
Let's make a text file:
abcde
Let's connect this file to the entry point:
import text from './text.txt';
The variable text
will contain the contents of the text file:
console.log(text); // will bring out 'abcde'
Connect several text files to the entry point. Get their contents into variables.