Files
dspace-angular/webpack/helpers.js
2019-05-31 13:24:04 +02:00

124 lines
2.5 KiB
JavaScript

const path = require('path');
const fs = require('fs');
const projectRoot = (relativePath) => {
return path.resolve(__dirname, '..', relativePath);
};
const buildRoot = (relativePath) => {
if (process.env.DSPACE_BUILD_DIR) {
return path.resolve(projectRoot(process.env.DSPACE_BUILD_DIR), relativePath);
} else {
return path.resolve(projectRoot('src'), relativePath);
}
};
// const theme = '';
const theme = 'mantis';
const themePath = path.normalize(path.join(__dirname, '..', 'themes', theme));
const globalCSSImports = [
path.resolve(__dirname, '..', 'src/styles/_variables.scss'),
path.resolve(__dirname, '..', 'src/styles/_mixins.scss'),
];
const themeReplaceOptions =
{
multiple: [
{
search: '$themePath$/',
replace: (themePath.length ? themePath + '/' : ''),
flags: 'g'
},
{
search: '@import \'~/',
replace: '@import \'' + projectRoot('./') + '/',
flags: 'g'
}
]
};
const srcPath = projectRoot('src');
const getThemedPath = (componentPath, ext) => {
const parsedPath = path.parse(componentPath);
const relativePath = path.relative(srcPath, parsedPath.dir);
return path.join(themePath, relativePath, `${parsedPath.name}.${ext}`);
};
const themedTest = (origPath, extension) => {
if (/\.component.ts$/.test(origPath)) { // only match components
const themedPath = getThemedPath(origPath, extension);
return fs.existsSync(themedPath);
} else {
return false;
}
};
const themedUse = (resource, extension) => {
const origPath = path.parse(resource);
const themedPath = getThemedPath(resource, extension);
return [
//'debug-loader',
{
loader: 'string-replace-loader',
options: {
search: `\.\/${origPath.name}\.${extension}`,
replace: themedPath,
flags: 'g'
}
}
]
};
const cssLoaders = [
{
loader: 'raw-loader',
options: {
sourceMap: true
}
},
{
loader: 'postcss-loader',
options: {
sourceMap: true
}
},
{
loader: 'resolve-url-loader',
options: {
sourceMap: true
}
},
];
const scssLoaders = [
...cssLoaders,
{
loader: 'sass-loader',
options: {
sourceMap: true
}
},
{
loader: 'string-replace-loader',
options: themeReplaceOptions
}
];
module.exports = {
projectRoot,
buildRoot,
theme,
getThemedPath,
themedTest,
themedUse,
cssLoaders,
scssLoaders,
globalCSSImports,
themeReplaceOptions
};