2019-06-06 16:09:49 +02:00
|
|
|
import { RuleSetRule, RuleSetUseItem } from 'webpack';
|
2019-10-30 20:13:53 +01:00
|
|
|
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 = {
|
2019-06-04 21:11:31 +02:00
|
|
|
loader: 'cache-loader',
|
|
|
|
options: {
|
|
|
|
cacheDirectory: resolve(__dirname, '../var/webpack-cache'),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
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 = [];
|
|
|
|
for (var loaderEntry of optionsArr) {
|
|
|
|
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) {
|
2018-05-28 08:09:49 +02:00
|
|
|
name = basename(path, '.js');
|
|
|
|
useArr.push({loader: 'expose-loader', options: name});
|
|
|
|
} else {
|
|
|
|
// If name is an array
|
2019-03-24 11:54:17 +01:00
|
|
|
if (Array.isArray(loaderEntry.name)) {
|
|
|
|
for (var exposeName of loaderEntry.name) {
|
|
|
|
useArr.push({loader: 'expose-loader', options: exposeName});
|
2018-05-28 08:09:49 +02:00
|
|
|
}
|
|
|
|
// If name is a string
|
|
|
|
} else {
|
|
|
|
useArr.push({loader: 'expose-loader', options: loaderEntry.name});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
export {
|
|
|
|
getExposeLoaders,
|
2019-03-24 11:54:17 +01:00
|
|
|
};
|