webpack-bootstrap-ui-kit/webpack.config.common.js

161 lines
4.6 KiB
JavaScript
Raw Normal View History

2021-01-20 16:31:34 +01:00
/*
* Common Environment
*/
2021-10-28 03:12:01 +02:00
const INDEX_NAME = 'app';
2021-05-03 19:00:50 +02:00
const YML_PATH = './webpack.yml';
2021-10-28 03:12:01 +02:00
const CONF_VAR = 'A2nt\\CMSNiceties\\Templates\\WebpackTemplateProvider';
2021-01-20 16:31:34 +01:00
const path = require('path');
2021-05-03 19:00:50 +02:00
const fs = require('fs');
const yaml = require('js-yaml');
const webpack = require('webpack');
2021-01-20 16:31:34 +01:00
2021-05-03 19:00:50 +02:00
/*
* Load webpack configuration from webpack.yml
*/
2021-08-02 22:03:17 +02:00
const yml = yaml.load(
2021-05-03 19:00:50 +02:00
fs.readFileSync(path.join(__dirname, YML_PATH), 'utf8'),
);
2021-09-12 23:19:05 +02:00
const conf = yml[CONF_VAR];
2021-05-03 19:00:50 +02:00
let themes = [];
// add themes
if (conf.THEMESDIR) {
2021-09-12 23:19:05 +02:00
const themeDir = conf.THEMESDIR;
const dir = path.resolve(__dirname, themeDir);
if (fs.existsSync(dir)) {
fs.readdirSync(dir).forEach((file) => {
filePath = path.join(themeDir, file);
const stat = fs.statSync(filePath);
if (stat && stat.isDirectory()) {
themes.push(filePath);
}
});
}
2021-05-03 19:00:50 +02:00
}
/* Setup Entries */
2021-01-20 16:31:34 +01:00
const includes = {};
const modules = [
2021-05-03 19:00:50 +02:00
path.resolve(__dirname, conf.APPDIR, conf.SRC),
path.resolve(__dirname, conf.APPDIR, conf.SRC, 'js'),
path.resolve(__dirname, conf.APPDIR, conf.SRC, 'scss'),
path.resolve(__dirname, conf.APPDIR, conf.SRC, 'img'),
path.resolve(__dirname, conf.APPDIR, conf.SRC, 'thirdparty'),
path.resolve(__dirname, 'node_modules'),
path.resolve(__dirname),
path.resolve(__dirname, 'public'),
2021-01-20 16:31:34 +01:00
];
const _addAppFiles = (theme) => {
2021-05-03 19:00:50 +02:00
const dirPath = './' + theme;
let themeName = path.basename(theme);
if (themeName == '.') {
2021-09-12 23:19:05 +02:00
themeName = 'app';
2021-05-03 19:00:50 +02:00
}
2021-08-09 19:41:14 +02:00
if (fs.existsSync(path.join(dirPath, conf.SRC, 'js', INDEX_NAME + '.js'))) {
2021-09-12 23:19:05 +02:00
includes[`${themeName}`] = path.join(dirPath, conf.SRC, 'js', INDEX_NAME + '.js');
2021-05-03 19:00:50 +02:00
} else if (
2021-08-09 19:41:14 +02:00
fs.existsSync(path.join(dirPath, conf.SRC, 'scss', INDEX_NAME + '.scss'))
2021-05-03 19:00:50 +02:00
) {
2021-09-12 23:19:05 +02:00
includes[`${themeName}`] = path.join(
dirPath,
conf.SRC,
'scss',
INDEX_NAME + '.scss',
);
2021-05-03 19:00:50 +02:00
}
modules.push(path.join(dirPath, conf.SRC, 'js'));
modules.push(path.join(dirPath, conf.SRC, 'scss'));
modules.push(path.join(dirPath, conf.SRC, 'img'));
modules.push(path.join(dirPath, conf.SRC, 'thirdparty'));
2021-09-12 23:19:05 +02:00
const _getAllFilesFromFolder = function (dir, includeSubFolders = true) {
2021-05-03 19:00:50 +02:00
const dirPath = path.resolve(__dirname, dir);
let results = [];
fs.readdirSync(dirPath).forEach((file) => {
if (file.charAt(0) === '_') {
2021-09-12 23:19:05 +02:00
return;
2021-05-03 19:00:50 +02:00
}
const filePath = path.join(dirPath, file);
const stat = fs.statSync(filePath);
if (stat && stat.isDirectory() && includeSubFolders) {
2021-09-12 23:19:05 +02:00
results = results.concat(
_getAllFilesFromFolder(filePath, includeSubFolders),
);
2021-05-03 19:00:50 +02:00
} else {
2021-09-12 23:19:05 +02:00
results.push(filePath);
2021-05-03 19:00:50 +02:00
}
2021-09-12 23:19:05 +02:00
});
2021-05-03 19:00:50 +02:00
return results;
2021-09-12 23:19:05 +02:00
};
2021-05-03 19:00:50 +02:00
// add page specific scripts
const typesJSPath = path.join(theme, conf.TYPESJS);
if (fs.existsSync(typesJSPath)) {
2021-09-12 23:19:05 +02:00
const pageScripts = _getAllFilesFromFolder(typesJSPath, true);
pageScripts.forEach((file) => {
includes[`${themeName}_${path.basename(file, '.js')}`] = file;
2021-05-03 19:00:50 +02:00
});
}
// add page specific scss
const typesSCSSPath = path.join(theme, conf.TYPESSCSS);
if (fs.existsSync(typesSCSSPath)) {
2021-09-12 23:19:05 +02:00
const scssIncludes = _getAllFilesFromFolder(typesSCSSPath, true);
scssIncludes.forEach((file) => {
includes[`${themeName}_${path.basename(file, '.scss')}`] = file;
2021-05-03 19:00:50 +02:00
});
}
2021-09-12 23:19:05 +02:00
};
2021-01-20 16:31:34 +01:00
_addAppFiles(conf.APPDIR);
// add themes
2021-05-03 19:00:50 +02:00
themes.forEach((theme) => {
_addAppFiles(theme);
2021-09-12 23:19:05 +02:00
});
2021-01-20 16:31:34 +01:00
module.exports = {
2021-05-03 19:00:50 +02:00
configuration: conf,
themes: themes,
webpack: {
entry: includes,
externals: {
// comment out jQuery if you don't use it to prevent bootstrap thinking that there's jQuery present
//jquery: 'jQuery',
react: 'React',
'react-dom': 'ReactDOM',
2021-09-12 23:19:05 +02:00
},
2021-05-03 19:00:50 +02:00
resolve: {
modules: modules,
2021-09-12 23:19:05 +02:00
extensions: ['.tsx', '.ts', '.js'],
2021-05-03 19:00:50 +02:00
alias: {
2021-09-12 23:19:05 +02:00
// comment out jQuery if you don't use it to prevent bootstrap thinking that there's jQuery present
/*'window.jQuery': require.resolve('jquery'),
$: require.resolve('jquery'),
jquery: require.resolve('jquery'),
2021-10-28 03:12:01 +02:00
jQuery: require.resolve('jquery'),
2021-09-12 23:19:05 +02:00
react: require.resolve('react'),
2021-10-28 03:12:01 +02:00
'react-dom': require.resolve('react-dom'),*/
2021-05-03 19:00:50 +02:00
},
2021-08-02 22:03:17 +02:00
fallback: {
2021-09-12 23:19:05 +02:00
path: false,
},
},
2021-05-03 19:00:50 +02:00
experiments: {
topLevelAwait: true,
2021-09-12 23:19:05 +02:00
},
},
};