-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebpack.config.js
159 lines (152 loc) · 4.25 KB
/
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const dotenv = require('dotenv');
const path = require('path');
const process = require('process');
const webpack = require('webpack');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const InlineEnvironmentVariablesPlugin = require('inline-environment-variables-webpack-plugin');
const ProgressBarPlugin = require('progress-bar-webpack-plugin');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
const ZipPlugin = require('zip-webpack-plugin');
// load ".env"
dotenv.config();
const buildPath = 'build';
module.exports = (env = {}) => {
// see https://github.com/webpack/webpack/issues/2537 for details
const isProduction = process.argv.indexOf('-p') !== -1 || env.production;
if (!process.env.SCRIVITO_TENANT || process.env.SCRIVITO_TENANT === 'your_scrivito_tenant_id') {
throw('Environment variable "SCRIVITO_TENANT" is not defined!' +
' Check if the ".env" file with a proper SCRIVITO_TENANT is set.' +
' See ".env.example" for an example.'
);
}
const plugins = [
new ProgressBarPlugin(),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: isProduction && JSON.stringify('production'),
},
}),
new InlineEnvironmentVariablesPlugin('SCRIVITO_TENANT'),
new CopyWebpackPlugin([
{ from: '../public' },
{ from: '../node_modules/scrivito/scrivito/index.html', to: 'scrivito/index.html' },
]),
new ExtractTextPlugin({
filename: '[name]',
}),
new webpack.optimize.ModuleConcatenationPlugin(),
];
if (isProduction) {
plugins.unshift(new CleanWebpackPlugin([buildPath], { verbose: false }));
plugins.push(
new UglifyJSPlugin({
cache: true,
parallel: true,
uglifyOptions: {
ie8: false,
ecma: 5,
},
})
);
plugins.push(
new ZipPlugin({
filename: 'build.zip',
path: '../',
pathPrefix: 'build/',
})
);
} else {
plugins.push(
new webpack.SourceMapDevToolPlugin({})
);
}
return {
context: path.join(__dirname, 'src'),
entry: {
index: './index.js',
scrivito_extensions: './scrivito_extensions.js',
'index.css': './assets/stylesheets/index.scss',
},
module: {
rules: [
{
test: /\.js$/,
include: [path.join(__dirname, 'src')],
use: [
{
loader: 'babel-loader',
options: {
presets: [
'@babel/preset-react',
['@babel/preset-env', {
debug: false,
modules: false,
shippedProposals: true,
targets: { browsers: ['last 2 versions'] },
useBuiltIns: 'usage',
}],
],
cacheDirectory: 'tmp/babel-cache',
},
},
{
loader: 'eslint-loader',
options: {
emitWarning: !isProduction,
},
},
],
},
{
test: /\.s?css$/,
use: ExtractTextPlugin.extract({
use: [
{
loader: 'css-loader',
options: {
minimize: isProduction,
},
}, {
loader: 'sass-loader',
},
],
}),
},
{
test: /\.(jpg|png|eot|svg|ttf|woff|woff2|gif|html)$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[hash].[ext]',
},
},
],
},
],
},
output: {
filename: '[name].js',
path: path.join(__dirname, buildPath),
},
plugins: plugins,
resolve: {
extensions: ['.js'],
modules: ['src', 'node_modules'],
},
devServer: {
port: 8080,
historyApiFallback: {
rewrites: [
{ from: /^\/scrivito/, to: '/scrivito/index.html' },
{ from: /./, to: '/index.html' },
],
},
headers: {
'Access-Control-Allow-Origin': '*',
},
},
};
};