| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- 'use strict';
- const path = require('path'),
- webpack = require('webpack'),
- HtmlWebpackPlugin = require('html-webpack-plugin'),
- MiniCssExtractPlugin = require("mini-css-extract-plugin");
- const PROJECT_ROOT = path.resolve(__dirname, "..");
- module.exports = (env) => {
- const isDev = env === 'development';
- const isTest = env === 'test';
- const isProd = !isDev && !isTest;
- const getAppEntry = () => {
- const appEntry = path.resolve(PROJECT_ROOT, "src/app/entry.jsx");
- if(isDev) {
- return [
- 'react-hot-loader/patch',
- 'webpack-dev-server/client?http://localhost:9000',
- 'webpack/hot/only-dev-server',
- appEntry
- ]
- } else {
- return [appEntry]
- }
- };
- const getPlugins = () => {
- // common plugins
- let plugins = [
- // Global variables
- new webpack.DefinePlugin({
- 'process.env.NODE_ENV': JSON.stringify(env),
- '__DEV__': isDev,
- '__PROD__': isProd,
- '__TEST__': isTest,
- }),
- // ignore moment locale files
- new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
- // extract styles to css file
- new MiniCssExtractPlugin({
- filename: "[name].[contenthash].css",
- chunkFilename: "[id].css",
- disable: isDev,
- }),
- // makes index.html
- new HtmlWebpackPlugin({
- template: path.resolve(PROJECT_ROOT, 'src/index.ejs'),
- })
- ];
- // development plugins
- if(isDev) {
- plugins.push(
- // Hot Reload (HMR)
- new webpack.HotModuleReplacementPlugin(),
- // Named Modules
- new webpack.NamedModulesPlugin()
- );
- }
- // production plugins
- if(isProd) {
- plugins.push(
- new webpack.optimize.ModuleConcatenationPlugin()
- );
- }
- return plugins;
- };
- return {
- target: 'web',
- mode: isProd ? "production" : "development",
- context: PROJECT_ROOT,
- entry: {
- app: getAppEntry()
- },
- output: {
- path: path.resolve(PROJECT_ROOT, isProd ? 'dist' : 'build'),
- filename: isProd ? '[name].[chunkhash:8].js' : '[name].js',
- publicPath: '/',
- sourceMapFilename: '[file].map',
- chunkFilename: isProd ? '[name].[chunkhash:8].js' : '[name].js',
- pathinfo: isDev
- },
- devtool: isProd ? "hidden-sourcemap" : 'eval',
- resolve: {
- extensions: ['.js', '.jsx', '.ts', '.tsx'],
- modules: ["node_modules", "src"],
- alias: {}
- },
- module: {
- rules: [
- // JS / JSX files
- {
- test: /\.(js|jsx)$/,
- exclude: /(node_modules)/,
- use: [
- {
- loader: 'babel-loader',
- options: {
- cacheDirectory: true
- }
- }
- ]
- },
- // SASS files
- {
- test: /\.scss$/,
- exclude: /(node_modules|bower_components)/,
- use: [
- isDev ? "style-loader" : MiniCssExtractPlugin.loader,
- {
- loader: "css-loader",
- options: {
- importLoaders: 2,
- url: true,
- import: false,
- sourceMap: isDev
- }
- },
- {
- loader: "postcss-loader",
- options: {
- sourceMap: isDev,
- plugins: isDev ? [] : [
- require("autoprefixer"),
- require("cssnano")({
- safe: true,
- zindex: false,
- discardComments: {
- removeAll: true
- }
- })
- ]
- }
- },
- {
- loader: "sass-loader",
- options: {
- sourceMap: isDev,
- includePaths: [".", path.join(process.cwd(), "src/app/core/styles")]
- }
- }
- ]
- },
- // Plain CSS files
- {
- test: /\.css$/,
- use: [
- isDev ? "style-loader" : MiniCssExtractPlugin.loader,
- {
- loader: "css-loader",
- options: {
- importLoaders: 1,
- url: true,
- import: false,
- sourceMap: isDev
- }
- },
- {
- loader: "postcss-loader",
- options: {
- sourceMap: isDev,
- plugins: isDev ? [] : [
- require("autoprefixer"),
- require("cssnano")({
- safe: true,
- zindex: false,
- discardComments: {
- removeAll: true
- }
- })
- ]
- }
- }
- ]
- },
- // images loader
- {
- test: /\.(png|jpe?g|gif|svg|ico)(\?.*)?$/,
- use: [
- {
- loader: 'file-loader',
- options: {
- name: "[name].[ext]",
- outputPath: isProd ? "../images/" : "images/"
- }
- }
- ]
- },
- // font loader
- {
- test: /\.(woff|woff2|ttf|eot)(\?.*)?$/,
- use: [
- {
- loader: 'file-loader',
- options: {
- name: "[name].[ext]",
- publicPath: isProd ? "" : "/webpack/",
- useRelativePath: isProd,
- outputPath: isProd ? "../fonts/" : "fonts/"
- }
- }
- ]
- }
- ]
- },
- plugins: getPlugins(),
- node: {
- __filename: true,
- __dirname: true,
- fs: 'empty',
- vm: 'empty',
- net: 'empty',
- tls: 'empty',
- }
- };
- };
|