silverstripe-webpack/webpack.config.common.js

119 lines
3.2 KiB
JavaScript
Raw Normal View History

2018-02-22 14:39:58 +01:00
/*
* Common Environment
*/
2018-04-21 05:36:06 +02:00
const webpack = require('webpack');
2019-05-23 13:02:32 +02:00
const commonVariables = require('./webpack.configuration');
const conf = commonVariables.configuration;
2018-02-22 14:39:58 +01:00
2018-04-21 05:36:06 +02:00
const path = require('path');
2019-04-11 00:15:29 +02:00
const filesystem = require('fs');
2018-02-05 12:11:01 +01:00
2020-12-28 00:29:43 +01:00
const UIInfo = require('./node_modules/@a2nt/ss-bootstrap-ui-webpack-boilerplate/package.json');
const UINAME = JSON.stringify(UIInfo.name);
const UIVERSION = JSON.stringify(UIInfo.version);
console.info(`%cUI Kit ${UINAME} ${UIVERSION}`, 'color:yellow;font-size:14px');
2019-04-11 00:15:29 +02:00
const includes = {};
2019-05-23 13:02:32 +02:00
const modules = [
2021-01-20 16:51:16 +01: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'),
2020-02-12 21:31:59 +01:00
path.resolve(__dirname, 'node_modules'),
path.resolve(__dirname),
path.resolve(__dirname, 'public'),
2019-05-23 13:02:32 +02:00
];
2019-04-11 00:15:29 +02:00
const _addAppFiles = (theme) => {
2020-02-12 21:31:59 +01:00
const dirPath = path.resolve(__dirname, theme);
2021-01-20 16:51:16 +01:00
let themeName = path.basename(theme);
if (themeName == '.') {
themeName = 'app';
}
2020-02-12 21:31:59 +01:00
if (filesystem.existsSync(path.join(dirPath, conf.SRC, 'js', 'app.js'))) {
includes[`${themeName}`] = path.join(dirPath, conf.SRC, 'js', 'app.js');
} else if (
filesystem.existsSync(path.join(dirPath, conf.SRC, 'scss', 'app.scss'))
) {
includes[`${themeName}`] = path.join(
dirPath,
conf.SRC,
'scss',
'app.scss',
);
}
2021-01-20 16:51:16 +01: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'));
2020-02-12 21:31:59 +01:00
const _getAllFilesFromFolder = function (dir, includeSubFolders = true) {
2020-02-12 21:31:59 +01:00
const dirPath = path.resolve(__dirname, dir);
let results = [];
filesystem.readdirSync(dirPath).forEach((file) => {
2020-02-12 21:31:59 +01:00
if (file.charAt(0) === '_') {
return;
}
const filePath = path.join(dirPath, file);
const stat = filesystem.statSync(filePath);
if (stat && stat.isDirectory() && includeSubFolders) {
results = results.concat(
_getAllFilesFromFolder(filePath, includeSubFolders),
);
} else {
results.push(filePath);
}
});
return results;
};
// add page specific scripts
const typesJSPath = path.join(theme, conf.TYPESJS);
if (filesystem.existsSync(typesJSPath)) {
const pageScripts = _getAllFilesFromFolder(typesJSPath, true);
pageScripts.forEach((file) => {
2020-02-12 21:31:59 +01:00
includes[`${themeName}_${path.basename(file, '.js')}`] = file;
});
}
// add page specific scss
const typesSCSSPath = path.join(theme, conf.TYPESSCSS);
if (filesystem.existsSync(typesSCSSPath)) {
const scssIncludes = _getAllFilesFromFolder(typesSCSSPath, true);
scssIncludes.forEach((file) => {
2020-02-12 21:31:59 +01:00
includes[`${themeName}_${path.basename(file, '.scss')}`] = file;
});
}
2018-02-05 12:11:01 +01:00
};
2019-04-11 00:15:29 +02:00
_addAppFiles(conf.APPDIR);
2018-02-21 11:19:51 +01:00
2019-04-11 00:15:29 +02:00
// add themes
commonVariables.themes.forEach((theme) => {
2020-02-12 21:31:59 +01:00
_addAppFiles(theme);
2019-05-23 13:02:32 +02:00
});
2018-02-05 12:11:01 +01:00
module.exports = {
2020-02-12 21:31:59 +01:00
entry: includes,
externals: {
jquery: 'jQuery',
},
resolve: {
modules: modules,
alias: {
2020-12-28 00:29:43 +01:00
'window.jQuery': require.resolve('jquery'),
$: require.resolve('jquery'),
2020-02-12 21:31:59 +01:00
jquery: require.resolve('jquery'),
jQuery: require.resolve('jquery'),
},
},
};