silverstripe-webpack/webpack.config.serve.js

142 lines
4.4 KiB
JavaScript
Raw Normal View History

2020-12-25 00:18:14 +01:00
/*
* Development assets generation
*/
2021-05-03 19:11:13 +02:00
const common = require('./webpack.config.common.js');
const conf = common.configuration;
2020-12-25 00:18:14 +01:00
const path = require('path');
2021-05-03 19:11:13 +02:00
const fs = require('fs');
2020-12-25 00:18:14 +01:00
//const autoprefixer = require('autoprefixer');
const webpack = require('webpack');
2021-08-03 03:22:09 +02:00
const {
2021-09-12 22:50:08 +02:00
merge,
2021-08-03 03:22:09 +02:00
} = require('webpack-merge');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
2020-12-25 00:18:14 +01:00
const IP = process.env.IP || conf.HOSTNAME;
const PORT = process.env.PORT || conf.PORT;
const NODE_ENV = 'development'; //conf.NODE_ENV || process.env.NODE_ENV;
const COMPRESS = NODE_ENV === 'production' ? true : false;
const plugins = [
new webpack.ProvidePlugin(common['PROVIDES']),
new webpack.DefinePlugin(common['JSVARS']),
2021-03-29 09:55:04 +02:00
//new webpack.HotModuleReplacementPlugin(),
new MiniCssExtractPlugin(),
];
const indexPath = path.join(__dirname, conf.APPDIR, conf.SRC, 'index.html');
2021-05-03 19:11:13 +02:00
if (fs.existsSync(indexPath)) {
2021-09-12 22:50:08 +02:00
plugins.push(
new HtmlWebpackPlugin({
publicPath: '',
template: path.join(conf.APPDIR, conf.SRC, 'index.html'),
templateParameters: {
NODE_ENV: NODE_ENV,
GRAPHQL_URL: conf['GRAPHQL_URL'],
STATIC_URL: conf['STATIC_URL'],
REACT_SCRIPTS: NODE_ENV === 'production' ?
'<script crossorigin src="https://unpkg.com/react@17/umd/react.production.min.js"></script><script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"></script>' : '<script crossorigin src="https://unpkg.com/react@17/umd/react.development.js"></script><script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>',
2021-03-29 09:55:04 +02:00
},
}),
2021-09-12 22:50:08 +02:00
);
}
2020-12-25 00:18:14 +01:00
2021-05-03 19:11:13 +02:00
const config = merge(common.webpack, {
2021-03-29 09:55:04 +02:00
mode: 'development',
2020-12-25 00:18:14 +01:00
2021-03-29 09:55:04 +02:00
entry: {
2021-09-12 22:50:08 +02:00
/*hot: [
'react-hot-loader/patch',
'webpack-dev-server/?https://' + conf.HOSTNAME + ':' + conf.PORT,
'webpack/hot/only-dev-server',
],*/
2021-03-29 09:55:04 +02:00
},
2021-03-29 09:55:04 +02:00
output: {
path: path.join(__dirname),
filename: '[name].js',
// necessary for HMR to know where to load the hot update chunks
publicPath: `http${conf['HTTPS'] ? 's' : ''}://${conf['HOSTNAME']}:${
2021-05-03 19:11:13 +02:00
conf.PORT
}/`,
2021-09-12 22:50:08 +02:00
},
2021-03-29 09:55:04 +02:00
module: {
2021-09-13 00:24:40 +02:00
rules: [{
test: /\.(js|ts)x?$/,
2021-08-03 03:22:09 +02:00
//exclude: /node_modules/,
use: {
2021-09-13 00:24:40 +02:00
loader: 'babel-loader', //'@sucrase/webpack-loader',
2021-08-03 03:22:09 +02:00
options: {
2021-09-13 00:24:40 +02:00
//transforms: ['jsx']
presets: [
2021-08-03 03:22:09 +02:00
'@babel/preset-env',
'@babel/react',
{
plugins: [
'@babel/plugin-proposal-class-properties',
],
2021-09-13 00:24:40 +02:00
},
2021-08-03 03:22:09 +02:00
], //Preset used for env setup
plugins: [
2021-09-13 00:24:40 +02:00
'@babel/transform-react-jsx',
'@babel/plugin-transform-typescript',
2021-08-03 03:22:09 +02:00
],
cacheDirectory: true,
2021-09-13 00:24:40 +02:00
cacheCompression: true,
2021-09-12 22:50:08 +02:00
},
},
},
2021-08-03 03:22:09 +02:00
{
test: /\.s?css$/,
use: [{
loader: 'style-loader', //MiniCssExtractPlugin.loader,
2021-09-12 22:50:08 +02:00
},
2021-03-29 09:55:04 +02:00
{
2021-08-03 03:22:09 +02:00
loader: 'css-loader',
options: {
sourceMap: true,
2021-09-12 22:50:08 +02:00
},
},
2021-03-29 09:55:04 +02:00
{
2021-08-03 03:22:09 +02:00
loader: 'sass-loader',
options: {
sourceMap: true,
2021-09-12 22:50:08 +02:00
},
}, ],
2021-09-12 22:50:08 +02:00
},
2021-08-03 03:22:09 +02:00
{
test: /fontawesome([^.]+).(ttf|otf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/,
type: 'asset/resource',
2021-09-12 22:50:08 +02:00
},
2021-08-03 03:22:09 +02:00
{
test: /\.(gif|png|jpg|jpeg|ttf|otf|eot|svg|webp|woff(2)?)$/,
type: 'asset/resource',
}, ],
2021-09-12 22:50:08 +02:00
},
2021-03-29 09:55:04 +02:00
plugins: plugins,
devServer: {
host: IP,
port: PORT,
historyApiFallback: false,
static: path.resolve(__dirname, conf['APPDIR'], conf['SRC']),
https: conf['HTTPS'],
hot: false,
2021-05-03 19:11:13 +02:00
//injectClient: conf['injectClient'],
2021-03-29 09:55:04 +02:00
headers: {
'Access-Control-Allow-Origin': '*',
'Referrer-Policy': 'unsafe-url',
'service-worker-allowed': '/',
2021-09-12 22:50:08 +02:00
},
},
});
2020-12-25 00:18:14 +01:00
module.exports = config;