silverstripe-webpack/webpack.config.common.js

177 lines
4.7 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
const HardSourceWebpackPlugin = require('hard-source-webpack-plugin');
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
2019-04-11 00:15:29 +02:00
const includes = {};
2019-05-23 13:02:32 +02:00
const modules = [
2020-02-12 21:31:59 +01:00
path.resolve(__dirname, 'public'),
path.resolve(__dirname, conf.APPDIR, 'client', 'src'),
path.resolve(__dirname, conf.APPDIR, 'client', 'src', 'js'),
path.resolve(__dirname, conf.APPDIR, 'client', 'src', 'scss'),
path.resolve(__dirname, conf.APPDIR, 'client', 'src', 'img'),
path.resolve(__dirname, conf.APPDIR, 'client', 'src', 'thirdparty'),
path.resolve(__dirname, conf.APPDIR, 'client', 'node_modules'),
path.resolve(__dirname, 'node_modules'),
2019-05-23 13:02:32 +02:00
];
2019-04-11 00:15:29 +02:00
2020-02-12 21:31:59 +01:00
const _addAppFiles = theme => {
const dirPath = path.resolve(__dirname, theme);
const themeName = path.basename(theme);
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',
);
}
modules.push(path.join(dirPath, 'client', 'src', 'js'));
modules.push(path.join(dirPath, 'client', 'src', 'scss'));
modules.push(path.join(dirPath, 'client', 'src', 'img'));
modules.push(path.join(dirPath, 'client', 'src', 'thirdparty'));
const _getAllFilesFromFolder = function(dir, includeSubFolders = true) {
const dirPath = path.resolve(__dirname, dir);
let results = [];
filesystem.readdirSync(dirPath).forEach(file => {
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 => {
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 => {
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
2020-02-12 21:31:59 +01:00
commonVariables.themes.forEach(theme => {
_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,
devtool: 'source-map',
externals: {
jquery: 'jQuery',
},
optimization: {
namedModules: true, // NamedModulesPlugin()
splitChunks: {
// CommonsChunkPlugin()
name: 'vendor',
minChunks: 2,
},
noEmitOnErrors: true, // NoEmitOnErrorsPlugin
concatenateModules: true, //ModuleConcatenationPlugin
},
module: {
rules: [
{
test: /\.jsx?$/,
//exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'], //Preset used for env setup
plugins: [
['@babel/transform-react-jsx'],
['react-hot-loader/babel'],
],
cacheDirectory: true,
cacheCompression: false,
2020-02-12 21:31:59 +01:00
},
},
},
/*{
2020-02-12 21:31:59 +01:00
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
{
test: /\.coffee?$/,
use: 'coffee-loader',
},*/
2020-02-12 21:31:59 +01:00
{
test: /\.worker\.js$/,
use: {
loader: 'worker-loader',
},
},
],
},
resolve: {
modules: modules,
alias: {
jquery: require.resolve('jquery'),
jQuery: require.resolve('jquery'),
},
},
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery',
Popper: ['popper.js', 'default'],
Util: 'exports-loader?Util!bootstrap/js/dist/util',
Alert: 'exports-loader?Alert!bootstrap/js/dist/alert',
Button: 'exports-loader?Button!bootstrap/js/dist/button',
Carousel: 'exports-loader?Carousel!bootstrap/js/dist/carousel',
Collapse: 'exports-loader?Collapse!bootstrap/js/dist/collapse',
Dropdown: 'exports-loader?Dropdown!bootstrap/js/dist/dropdown',
Modal: 'exports-loader?Modal!bootstrap/js/dist/modal',
Tooltip: 'exports-loader?Tooltip!bootstrap/js/dist/tooltip',
Popover: 'exports-loader?Popover!bootstrap/js/dist/popover',
Scrollspy: 'exports-loader?Scrollspy!bootstrap/js/dist/scrollspy',
Tab: 'exports-loader?Tab!bootstrap/js/dist/tab',
}),
new HardSourceWebpackPlugin(),
2020-02-12 21:31:59 +01:00
],
};