2020-07-16 22:40:18 +02:00
|
|
|
import {RuleSetRule, RuleSetUseItem} from "webpack";
|
|
|
|
import {basename, resolve} from "path";
|
2019-03-25 15:55:39 +01:00
|
|
|
|
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
|
|
|
}
|
2019-06-01 23:35:05 +02:00
|
|
|
function getExposeLoaders(optionsArr: ExportLoaderOptions[]): RuleSetRule[] {
|
2019-03-24 11:54:17 +01:00
|
|
|
const exposeLoaders = [];
|
2019-11-02 00:06:25 +01:00
|
|
|
for (const loaderEntry of optionsArr) {
|
2019-03-24 11:54:17 +01:00
|
|
|
const path = loaderEntry.path;
|
2018-05-28 08:09:49 +02:00
|
|
|
let name = "";
|
2019-06-04 21:11:31 +02:00
|
|
|
const useArr = [cacheLoader];
|
2018-05-28 08:09:49 +02:00
|
|
|
// If no name is provided, infer it
|
2019-03-24 11:54:17 +01:00
|
|
|
if (!loaderEntry.name) {
|
2020-07-15 01:29:15 +02:00
|
|
|
name = basename(path, ".js");
|
|
|
|
useArr.push({loader: "expose-loader", options: name});
|
2018-05-28 08:09:49 +02:00
|
|
|
} else {
|
|
|
|
// If name is an array
|
2019-03-24 11:54:17 +01:00
|
|
|
if (Array.isArray(loaderEntry.name)) {
|
2019-11-02 00:06:25 +01:00
|
|
|
for (const exposeName of loaderEntry.name) {
|
2020-07-15 01:29:15 +02:00
|
|
|
useArr.push({loader: "expose-loader", options: exposeName});
|
2018-05-28 08:09:49 +02:00
|
|
|
}
|
2020-07-15 00:34:28 +02:00
|
|
|
// If name is a string
|
2018-05-28 08:09:49 +02:00
|
|
|
} else {
|
2020-07-15 01:29:15 +02:00
|
|
|
useArr.push({loader: "expose-loader", options: loaderEntry.name});
|
2018-05-28 08:09:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
exposeLoaders.push({
|
|
|
|
test: require.resolve(path),
|
2019-03-24 11:54:17 +01:00
|
|
|
use: useArr,
|
2018-05-28 08:09:49 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return exposeLoaders;
|
|
|
|
}
|
2020-07-15 00:34:28 +02:00
|
|
|
export {getExposeLoaders};
|