2020-07-16 22:40:18 +02:00
|
|
|
import {basename, resolve} from "path";
|
2019-03-25 15:55:39 +01:00
|
|
|
|
2020-07-24 06:02:07 +02:00
|
|
|
import {RuleSetRule, RuleSetUseItem} from "webpack";
|
|
|
|
|
2019-06-06 16:09:49 +02:00
|
|
|
export const cacheLoader: RuleSetUseItem = {
|
2020-07-15 01:29:15 +02:00
|
|
|
loader: "cache-loader",
|
2019-06-04 21:11:31 +02:00
|
|
|
options: {
|
2020-07-15 01:29:15 +02:00
|
|
|
cacheDirectory: resolve(__dirname, "../var/webpack-cache"),
|
2019-06-04 21:11:31 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-05-28 08:09:49 +02:00
|
|
|
/* Return expose-loader format to the config
|
|
|
|
For example
|
|
|
|
[
|
|
|
|
// Exposes 'my_module' as the name
|
|
|
|
{path: './folder/my_module.js'},
|
|
|
|
|
|
|
|
// Exposes 'my_custom_name'
|
|
|
|
{path: './folder/my_module.js', name: 'my_custom_name'},
|
|
|
|
|
|
|
|
// Exposes 'name1' and 'name2'
|
|
|
|
{path: './folder/my_module.js', name: ['name1', 'name2']}
|
|
|
|
]
|
|
|
|
*/
|
2019-03-25 15:55:39 +01:00
|
|
|
interface ExportLoaderOptions {
|
|
|
|
path: string;
|
|
|
|
name?: string | string[];
|
2018-05-28 08:09:49 +02:00
|
|
|
}
|
2020-07-18 03:14:20 +02:00
|
|
|
|
|
|
|
export function getExposeLoaders(optionsArr: ExportLoaderOptions[]): RuleSetRule[] {
|
|
|
|
return optionsArr.map(({path, name}) => ({
|
|
|
|
test: require.resolve(path),
|
|
|
|
use: [
|
|
|
|
cacheLoader,
|
|
|
|
{
|
|
|
|
loader: "expose-loader",
|
|
|
|
options: {
|
|
|
|
// If no name is provided, infer it
|
|
|
|
exposes: name ?? basename(path, ".js"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}));
|
2018-05-28 08:09:49 +02:00
|
|
|
}
|