silverstripe-webpack/webpack.config.common.js

157 lines
5.1 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');
const conf = require('./webpack.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
2019-04-11 00:15:29 +02:00
const includes = {};
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');
}
const _getAllFilesFromFolder = function(dir, includeSubFolders = true) {
const dirPath = path.resolve(__dirname, dir);
let results = [];
2018-02-05 12:11:01 +01:00
2019-04-11 00:15:29 +02:00
filesystem.readdirSync(dirPath).forEach((file) => {
if (file.charAt(0) === '_') {
return;
}
2018-02-22 14:39:58 +01:00
2019-04-11 00:15:29 +02:00
const filePath = `${dirPath}/${file}`;
const stat = filesystem.statSync(filePath);
2018-02-05 12:11:01 +01:00
2019-04-11 00:15:29 +02:00
if (stat && stat.isDirectory() && includeSubFolders) {
results = results.concat(_getAllFilesFromFolder(filePath, includeSubFolders));
} else {
results.push(filePath);
}
});
2018-02-05 12:11:01 +01:00
2019-04-11 00:15:29 +02:00
return results;
};
2018-02-05 12:11:01 +01:00
2019-04-11 00:15:29 +02:00
// 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;
});
}
2018-02-05 12:11:01 +01:00
2019-04-11 00:15:29 +02:00
// 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
if (conf.THEMESDIR) {
const dir = path.resolve(__dirname, conf.THEMESDIR);
if (filesystem.existsSync(dir)) {
filesystem.readdirSync(dir).forEach((file) => {
filePath = `${dir}/${file}`;
const stat = filesystem.statSync(filePath);
if (stat && stat.isDirectory()) {
_addAppFiles(path.join(conf.THEMESDIR, file));
}
});
}
}
2018-02-05 12:11:01 +01:00
module.exports = {
2018-02-21 11:19:51 +01:00
entry: includes,
2018-04-21 05:36:06 +02:00
devtool: 'source-map',
2018-02-05 12:11:01 +01:00
externals: {
2018-04-21 05:36:06 +02:00
'jquery': 'jQuery',
2018-02-05 12:11:01 +01:00
},
module: {
rules: [{
test: /\.jsx?$/,
exclude: /node_modules/,
use: {
2018-04-21 05:36:06 +02:00
loader: 'babel-loader',
2018-02-05 12:11:01 +01:00
options: {
presets: [
2018-04-21 05:36:06 +02:00
['es2015', {
modules: false,
2018-02-05 12:11:01 +01:00
}],
2018-04-21 05:36:06 +02:00
['stage-2'],
2018-02-05 12:11:01 +01:00
],
plugins: [
2018-04-21 05:36:06 +02:00
['transform-react-jsx'],
['react-hot-loader/babel'],
],
2018-02-05 12:11:01 +01:00
},
2018-04-21 05:36:06 +02:00
},
2018-02-21 11:19:51 +01:00
}, {
test: /\.tsx?$/,
use: 'ts-loader',
2018-04-21 05:36:06 +02:00
exclude: /node_modules/,
2018-02-21 11:19:51 +01:00
}, {
test: /\.coffee?$/,
2018-04-21 05:36:06 +02:00
use: 'coffee-loader',
2018-02-05 12:11:01 +01:00
}, {
test: /\.worker\.js$/,
use: {
2018-04-21 05:36:06 +02:00
loader: 'worker-loader',
},
}],
2018-02-05 12:11:01 +01:00
},
2018-02-21 11:19:51 +01:00
resolve: {
2018-04-21 05:36:06 +02:00
modules: [
path.resolve(__dirname, 'public'),
2019-04-11 00:15:29 +02:00
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, 'node_modules')
2018-04-21 05:36:06 +02:00
],
2018-02-21 11:19:51 +01:00
alias: {
'jquery': require.resolve('jquery'),
'jQuery': require.resolve('jquery'),
2018-04-21 05:36:06 +02:00
},
2018-02-21 11:19:51 +01:00
},
2018-02-05 12:11:01 +01:00
plugins: [
new webpack.ProvidePlugin({
2018-04-21 05:36:06 +02:00
$: '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',
}),
2018-02-05 12:11:01 +01:00
],
};