silverstripe-webpack/webpack.config.common.js

159 lines
4.6 KiB
JavaScript
Raw Normal View History

2018-02-22 14:39:58 +01:00
/*
* Common Environment
*/
2021-05-03 19:11:13 +02:00
const YML_PATH = '/app/_config/webpack.yml';
2021-06-19 22:22:44 +02:00
const CONF_VAR = 'A2nt\\CMSNiceties\\Templates\\WebpackTemplateProvider';
2018-02-22 14:39:58 +01:00
2018-04-21 05:36:06 +02:00
const path = require('path');
2021-05-03 19:11:13 +02:00
const fs = require('fs');
const yaml = require('js-yaml');
const webpack = require('webpack');
/*
* Load webpack configuration from webpack.yml
*/
const yml = yaml.safeLoad(
fs.readFileSync(path.join(__dirname, YML_PATH), 'utf8'),
);
const conf = yml[CONF_VAR]
let themes = [];
// add themes
if (conf.THEMESDIR) {
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);
2018-02-05 12:11:01 +01:00
2021-05-03 19:11:13 +02:00
if (stat && stat.isDirectory()) {
themes.push(filePath);
}
});
}
}
/* Setup Entries */
2019-04-11 00:15:29 +02:00
const includes = {};
2019-05-23 13:02:32 +02:00
const modules = [
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'),
2019-05-23 13:02:32 +02:00
];
2019-04-11 00:15:29 +02:00
const _addAppFiles = (theme) => {
const dirPath = './' + theme;
let themeName = path.basename(theme);
if (themeName == '.') {
themeName = 'app';
}
2021-05-03 19:11:13 +02:00
if (fs.existsSync(path.join(dirPath, conf.SRC, 'js', 'app.js'))) {
includes[`${themeName}`] = path.join(dirPath, conf.SRC, 'js', 'app.js');
} else if (
2021-05-03 19:11:13 +02:00
fs.existsSync(path.join(dirPath, conf.SRC, 'scss', 'app.scss'))
) {
includes[`${themeName}`] = path.join(
dirPath,
conf.SRC,
'scss',
'app.scss',
);
}
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'));
const _getAllFilesFromFolder = function(dir, includeSubFolders = true) {
const dirPath = path.resolve(__dirname, dir);
let results = [];
2021-05-03 19:11:13 +02:00
fs.readdirSync(dirPath).forEach((file) => {
if (file.charAt(0) === '_') {
return;
}
const filePath = path.join(dirPath, file);
2021-05-03 19:11:13 +02:00
const stat = fs.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);
2021-05-03 19:11:13 +02:00
if (fs.existsSync(typesJSPath)) {
const pageScripts = _getAllFilesFromFolder(typesJSPath, true);
pageScripts.forEach((file) => {
includes[`${themeName}_${path.basename(file, '.js')}`] = file;
});
}
// add page specific scss
const typesSCSSPath = path.join(theme, conf.TYPESSCSS);
2021-05-03 19:11:13 +02:00
if (fs.existsSync(typesSCSSPath)) {
const scssIncludes = _getAllFilesFromFolder(typesSCSSPath, true);
scssIncludes.forEach((file) => {
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
2021-05-03 19:11:13 +02:00
themes.forEach((theme) => {
_addAppFiles(theme);
2019-05-23 13:02:32 +02:00
});
2018-02-05 12:11:01 +01:00
module.exports = {
2021-05-03 19:11:13 +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
2021-05-03 19:11:13 +02:00
//jquery: 'jQuery',
react: 'React',
'react-dom': 'ReactDOM',
},
resolve: {
modules: modules,
alias: {
// 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'),
jQuery: require.resolve('jquery'),*/
react: require.resolve('react'),
'react-dom': require.resolve('react-dom'),
},
2021-06-19 22:22:44 +02:00
fallback: {
path: false
},
},
2021-05-03 19:11:13 +02:00
experiments: {
topLevelAwait: true,
},
}
};