2018-05-28 08:09:49 +02:00
|
|
|
import { basename } from 'path';
|
|
|
|
|
2019-03-25 15:55:39 +01:00
|
|
|
interface Loader {
|
|
|
|
test: string;
|
|
|
|
use: string;
|
|
|
|
}
|
|
|
|
|
2018-05-28 08:09:49 +02:00
|
|
|
/* Return imports-loader format to the config
|
|
|
|
For example:
|
|
|
|
[
|
|
|
|
// Adds 'imports-loader?this=>widndow'
|
|
|
|
{path: './foler/my_module.js', args: '?this=>window'},
|
|
|
|
]
|
|
|
|
*/
|
2019-03-25 15:55:39 +01:00
|
|
|
interface ImportLoaderOptions {
|
|
|
|
path: string;
|
|
|
|
args: string;
|
2018-05-28 08:09:49 +02:00
|
|
|
}
|
2019-03-25 15:55:39 +01:00
|
|
|
function getImportLoaders(optionsArr: ImportLoaderOptions[]): Loader[] {
|
2019-03-24 11:54:17 +01:00
|
|
|
const importsLoaders = [];
|
|
|
|
for (var loaderEntry of optionsArr) {
|
2018-05-28 08:09:49 +02:00
|
|
|
importsLoaders.push({
|
|
|
|
test: require.resolve(loaderEntry.path),
|
2019-03-24 11:54:17 +01:00
|
|
|
use: "imports-loader?" + loaderEntry.args,
|
2018-05-28 08:09:49 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return importsLoaders;
|
|
|
|
}
|
|
|
|
/* 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-03-25 15:55:39 +01:00
|
|
|
function getExposeLoaders(optionsArr: ExportLoaderOptions[]): Loader[] {
|
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-03-24 11:54:17 +01:00
|
|
|
const useArr = [];
|
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
|
|
|
getImportLoaders,
|
|
|
|
};
|