this is SUPER late but in case anybody else lands here, I think the best way to solve this would be to generate a separate asset for every srv.js
file, which can be done with a simple loader like so. You can then use these assets in your html just like you would include any other css or js file.
However, if you really want to only generate a single asset, you can do it like so:
- create a plugin that injects a function to the loader context, so the loaders can pass info back to the plugin (more info)
- create a loader for
srv.js
files that extracts the data and passes it to the plugin from step 1
- using the same plugin from step 1, aggregate all the data from the loaders and generates a single asset file (more info)
some rough code to show what I mean:
Plugin
class MyPlugin {
apply(compiler) {
var allData = "";
// inject a function to loaderContext so loaders can pass back info
compiler.hooks.compilation.tap('MyPlugin', compilation =>
compilation.hooks.normalModuleLoader.tap(
'MyPlugin',
(loaderContext, module) => {
loaderContext.myExportFn = (data) => {
allData += data;
}
}
)
);
// at the end, generate an asset using the data
compiler.hooks.emit.tapAsync(
'MyPlugin',
(compilation, callback) => {
compilation.assets['myfile.js'] = {
source: () => allData,
size: () => allData.length
};
callback();
}
);
}
}
Loader
module.exports = function(content) {
// use the injected function, pass back some data
this.myExportFn(`loader called for filepath: ${this.resourcePath}`);
return content; // return the source file unchanged
};