2020-07-03 16:30:46 +08:00
|
|
|
import * as path from 'path'
|
|
|
|
import * as webpack from 'webpack'
|
|
|
|
import MiniCssExtractPlugin from 'mini-css-extract-plugin'
|
|
|
|
import TerserJSPlugin from 'terser-webpack-plugin'
|
2020-11-04 11:34:36 +08:00
|
|
|
import CssMinimizerPlugin from 'css-minimizer-webpack-plugin'
|
2020-10-31 10:43:47 +08:00
|
|
|
import HtmlWebpackPlugin from 'html-webpack-plugin'
|
|
|
|
import HtmlWebpackEnhancementPlugin from './tools/HtmlWebpackEnhancementPlugin'
|
2018-07-29 15:31:54 +08:00
|
|
|
|
2019-03-17 23:10:40 +08:00
|
|
|
const devMode = !process.argv.includes('-p')
|
2018-07-29 15:31:54 +08:00
|
|
|
|
2020-10-31 10:43:47 +08:00
|
|
|
const htmlPublicPath = devMode ? '//localhost:8080/' : '{{ cdn_base }}/app/'
|
|
|
|
|
2020-07-03 16:30:46 +08:00
|
|
|
const config: webpack.Configuration = {
|
2019-03-15 11:42:41 +08:00
|
|
|
mode: devMode ? 'development' : 'production',
|
|
|
|
entry: {
|
2020-03-14 17:00:35 +08:00
|
|
|
app: ['react-hot-loader/patch', '@/index.tsx'],
|
2020-05-18 16:40:31 +08:00
|
|
|
style: ['@/styles/common.css'],
|
2020-06-24 16:12:01 +08:00
|
|
|
home: '@/scripts/homePage.ts',
|
2020-10-31 10:43:47 +08:00
|
|
|
'home-css': '@/styles/home.css',
|
2020-02-01 12:44:39 +08:00
|
|
|
spectre: [
|
|
|
|
'spectre.css/dist/spectre.min.css',
|
2020-03-14 17:00:35 +08:00
|
|
|
'@/fonts/minecraft.css',
|
|
|
|
'@/styles/spectre.css',
|
2020-02-01 12:44:39 +08:00
|
|
|
],
|
2019-03-15 11:42:41 +08:00
|
|
|
},
|
|
|
|
output: {
|
|
|
|
path: `${__dirname}/public/app`,
|
2020-06-11 19:27:15 +08:00
|
|
|
filename: devMode ? '[name].js' : '[name].[contenthash:7].js',
|
2019-03-15 11:42:41 +08:00
|
|
|
chunkFilename: devMode ? '[id].js' : '[id].[contenthash:7].js',
|
|
|
|
},
|
|
|
|
module: {
|
|
|
|
rules: [
|
2020-01-16 16:14:49 +08:00
|
|
|
{
|
|
|
|
test: /\.tsx?$/,
|
2020-03-12 11:06:23 +08:00
|
|
|
loader: 'ts-loader',
|
|
|
|
options: {
|
|
|
|
configFile: 'tsconfig.build.json',
|
2020-03-18 11:00:32 +08:00
|
|
|
transpileOnly: true,
|
2020-03-12 11:06:23 +08:00
|
|
|
},
|
2020-01-16 16:14:49 +08:00
|
|
|
},
|
2020-03-14 17:00:35 +08:00
|
|
|
{
|
2020-05-18 16:40:31 +08:00
|
|
|
test: /\.css$/,
|
2020-03-14 17:00:35 +08:00
|
|
|
use: [
|
|
|
|
devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
|
|
|
|
{
|
|
|
|
loader: 'css-loader',
|
|
|
|
options: {
|
2020-05-18 16:40:31 +08:00
|
|
|
importLoaders: 1,
|
2020-03-14 17:00:35 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
'postcss-loader',
|
2019-03-15 11:42:41 +08:00
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
2020-05-30 11:18:13 +08:00
|
|
|
test: /\.(png|webp)$/,
|
2019-03-15 11:42:41 +08:00
|
|
|
loader: 'url-loader',
|
|
|
|
options: {
|
|
|
|
limit: 8192,
|
2019-02-19 17:26:38 +08:00
|
|
|
},
|
2019-03-15 11:42:41 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.(svg|woff2?|eot|ttf)$/,
|
2019-03-25 22:01:57 +08:00
|
|
|
loader: devMode ? 'url-loader' : 'file-loader',
|
2019-03-15 11:42:41 +08:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
plugins: [
|
|
|
|
new MiniCssExtractPlugin({
|
2019-03-31 13:29:23 +08:00
|
|
|
filename: devMode ? '[name].css' : '[name].[contenthash:7].css',
|
|
|
|
chunkFilename: devMode ? '[id].css' : '[id].[contenthash:7].css',
|
2019-03-15 11:42:41 +08:00
|
|
|
}),
|
2020-10-31 10:43:47 +08:00
|
|
|
new HtmlWebpackPlugin({
|
|
|
|
templateContent: devMode
|
|
|
|
? ''
|
|
|
|
: `
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/react@17.0.1/umd/react.production.min.js" integrity="sha256-Ag0WTc8xFszCJo1qbkTKp3wBMdjpjogsZDAhnSge744=" crossorigin></script>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/react-dom@17.0.1/umd/react-dom.production.min.js" integrity="sha256-k8tzaSH8ucPwbsHEO4Wk5szE9zERNVz3XQynfyT66O0=" crossorigin></script>`,
|
|
|
|
templateParameters: ['app'],
|
|
|
|
filename: 'app.twig',
|
|
|
|
publicPath: htmlPublicPath,
|
|
|
|
}),
|
|
|
|
new HtmlWebpackPlugin({
|
|
|
|
templateContent: '',
|
|
|
|
templateParameters: ['style'],
|
|
|
|
filename: 'style.twig',
|
|
|
|
publicPath: htmlPublicPath,
|
|
|
|
}),
|
|
|
|
new HtmlWebpackPlugin({
|
|
|
|
templateContent: '',
|
|
|
|
templateParameters: ['home'],
|
|
|
|
filename: 'home.twig',
|
|
|
|
publicPath: htmlPublicPath,
|
|
|
|
}),
|
|
|
|
new HtmlWebpackPlugin({
|
|
|
|
templateContent: '',
|
|
|
|
templateParameters: ['home-css'],
|
|
|
|
filename: 'home-css.twig',
|
|
|
|
publicPath: htmlPublicPath,
|
|
|
|
}),
|
|
|
|
new HtmlWebpackPlugin({
|
|
|
|
templateContent: '',
|
|
|
|
templateParameters: ['spectre'],
|
|
|
|
filename: 'spectre.twig',
|
|
|
|
publicPath: htmlPublicPath,
|
|
|
|
}),
|
|
|
|
new HtmlWebpackEnhancementPlugin(),
|
2019-03-15 11:42:41 +08:00
|
|
|
],
|
|
|
|
resolve: {
|
2020-05-15 11:31:30 +08:00
|
|
|
extensions: ['.js', '.ts', '.tsx', '.json'],
|
2020-01-21 10:22:20 +08:00
|
|
|
alias: {
|
|
|
|
'react-dom': '@hot-loader/react-dom',
|
2020-02-07 09:59:10 +08:00
|
|
|
'@': path.resolve(__dirname, 'resources/assets/src'),
|
2020-05-18 16:40:31 +08:00
|
|
|
readline: '@/scripts/cli/readline.ts',
|
2020-01-21 10:22:20 +08:00
|
|
|
},
|
2019-03-15 11:42:41 +08:00
|
|
|
},
|
2020-05-18 16:40:31 +08:00
|
|
|
externals: Object.assign(
|
|
|
|
{ jquery: 'jQuery', bootstrap: 'bootstrap', 'admin-lte': 'adminlte' },
|
|
|
|
devMode
|
|
|
|
? {}
|
|
|
|
: {
|
|
|
|
react: 'React',
|
|
|
|
'react-dom': 'ReactDOM',
|
|
|
|
},
|
2020-07-03 16:30:46 +08:00
|
|
|
) as Record<string, string>,
|
2019-09-20 20:12:59 +08:00
|
|
|
optimization: {
|
2020-11-04 11:34:36 +08:00
|
|
|
minimizer: [new TerserJSPlugin({}), new CssMinimizerPlugin({})],
|
2019-09-20 20:12:59 +08:00
|
|
|
},
|
2019-03-15 11:42:41 +08:00
|
|
|
devtool: devMode ? 'cheap-module-eval-source-map' : false,
|
|
|
|
devServer: {
|
|
|
|
headers: {
|
|
|
|
'Access-Control-Allow-Origin': '*',
|
2019-02-19 17:26:38 +08:00
|
|
|
},
|
2019-03-15 11:42:41 +08:00
|
|
|
host: '0.0.0.0',
|
|
|
|
hot: true,
|
2019-09-20 19:43:51 +08:00
|
|
|
hotOnly: true,
|
2019-03-15 11:42:41 +08:00
|
|
|
stats: 'errors-only',
|
|
|
|
},
|
2020-03-26 10:13:40 +08:00
|
|
|
stats: 'errors-only',
|
2020-03-22 12:21:17 +08:00
|
|
|
node: {
|
|
|
|
child_process: 'empty',
|
|
|
|
fs: 'empty',
|
|
|
|
},
|
2019-03-15 11:42:41 +08:00
|
|
|
}
|
2018-08-08 18:17:11 +08:00
|
|
|
|
2019-03-15 11:42:41 +08:00
|
|
|
if (devMode) {
|
2020-07-03 16:30:46 +08:00
|
|
|
config.plugins!.push(new webpack.NamedModulesPlugin())
|
|
|
|
config.plugins!.push(new webpack.HotModuleReplacementPlugin())
|
2018-08-09 09:31:34 +08:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:30:46 +08:00
|
|
|
export default config
|