So what you want to compile a second JS file that's output seperately from the main JS file? In this case, you'll have to adjust the webpack config. The webpack.config.js in the starter project doesn't specify an explicit entry point, which means the default of src/index.js is used. You can overwrite this by specyfing one or multiple entry points as per the documentation. Include something like this in your webpack.config.js:
config = {
// ...
entry: {
main: 'src/index.js',
myCustomComponent: `src/myCustomComponent.js`,
}
// ...
}
Then create the specified file src/myCustomComponent.js and put your page-specific JavaScript in there.
This should work without further adjustments since the default output config in the starter project already supports multiple entry points by using placeholders for the target filename:
filename: devMode ? "[name].js" : "[name].[fullhash].js",
With the additional entry points, the webpack build step should result in two output files:
/web/assets/dist/index.js
/web/assets/dist/myCustomComponent.js
Both of those files will be compiled using the same rules specified by the Webpack config. In production mode, the output filenames will include the content hash (e.g. index.abcd1234.js, myCustomComponent.efgh5678.js). Those filenames will be written to the manifest by the Webpack manifest plugin, so now you can simply use the rev function to get the current asset path for your custom asset:
{% js rev('myCustomComponent.js') at head with { defer: true } %}
As a sidenote, I would recommend not using a starter template if you're just starting out. Unless you understand everything it's doing and the benefits it brings, it will just introduce complexity into your project that you're unable to debug or work productively with. Besides, the starter blog is, in my opinion, way more complicated than it needs to be. The webpack build in particular is really overkill, I've built much more complicated projects with a much simpler Webpack config. My recommendation would be to start with the blank starter project and use the starter blog as inspiration to draw from if you get stuck.