forked from oskariorg/oskari-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
157 lines (148 loc) · 5.67 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
const path = require('path');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const parseParams = require('./webpack/parseParams.js');
const { lstatSync, readdirSync } = require('fs');
const generateEntries = require('./webpack/generateEntries.js');
const proxyPort = 8081;
module.exports = (env, argv) => {
const isProd = argv.mode === 'production';
const {version, pathParam, publicPathPrefix} = parseParams(env);
const isDirectory = source => lstatSync(source).isDirectory();
const getDirectories = source => readdirSync(source).map(name => path.join(source, name)).filter(isDirectory);
const appsetupPaths = getDirectories(path.resolve(pathParam));
const {entries, plugins} = generateEntries(appsetupPaths, isProd, __dirname);
plugins.push(new MiniCssExtractPlugin({
filename: '[name]/oskari.min.css'
}));
const styleLoaderImpl = isProd ? {
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: '../'
}
} : 'style-loader';
// Common config for both prod & dev
const config = {
node: {
fs: 'empty'
},
mode: isProd ? 'production' : 'development',
entry: entries,
devtool: isProd ? 'source-map' : 'cheap-module-eval-source-map',
output: {
path: path.resolve(`dist/${version}/`),
publicPath: `${publicPathPrefix}Oskari/dist/${version}/`,
filename: '[name]/oskari.min.js'
},
module: {
rules: [
{
test: require.resolve('sumoselect'),
use: 'imports-loader?define=>undefined,exports=>undefined'
},
{
test: /\.(js|jsx)$/,
exclude: [/libraries/, /\.min\.js$/],
use: {
loader: 'babel-loader',
options: {
presets: [
[
require.resolve('@babel/preset-env'), // Resolve path for use from external projects
{
useBuiltIns: 'entry',
targets: '> 0.25%, not dead, ie 11'
}
],
require.resolve('@babel/preset-react') // Resolve path for use from external projects
],
plugins: [require.resolve('babel-plugin-transform-remove-strict-mode')] // Resolve path for use from external projects
}
}
},
{
test: /\.css$/,
use: [
styleLoaderImpl,
{ loader: 'css-loader', options: { minimize: true } }
]
},
{
test: /\.scss$/,
use: [
styleLoaderImpl,
{ loader: 'css-loader', options: { minimize: true } },
'sass-loader' // compiles Sass to CSS
]
},
{
test: /\.(ttf|png|jpg|gif|svg)$/,
use: [
{
loader: 'file-loader',
options: {
outputPath: 'assets/'
}
}
]
},
{
type: 'javascript/auto',
test: /minifierAppSetup.json$/,
use: [
{
loader: path.resolve(__dirname, './webpack/minifierLoader.js')
}
]
}
]
},
plugins,
resolveLoader: {
modules: [path.resolve(__dirname, 'node_modules'), 'node_modules'], // allow external projects to use loaders in oskari-frontend node_modules
extensions: ['.js', '.json'],
mainFields: ['loader', 'main'],
alias: {
'oskari-loader': path.resolve(__dirname, './webpack/oskariLoader.js'),
'oskari-lazy-loader': path.resolve(__dirname, './webpack/oskariLazyLoader.js')
}
},
resolve: {
extensions: ['.js', '.jsx'],
modules: [path.resolve(__dirname, 'node_modules'), 'node_modules'], // allow use of oskari-frontend node_modules from external projects
symlinks: false
}
};
// Mode specific config
if (isProd) {
config.optimization = {
minimizer: [
new UglifyJsPlugin({
sourceMap: true,
parallel: true
})
]
};
} else {
config.devServer = {
port: proxyPort,
proxy: [{
context: ['/transport/cometd'],
target: 'ws://localhost:8080',
secure: false,
changeOrigin: true,
ws: true
}, {
context: ['**', `!/Oskari/dist/${version}/**`, '!/Oskari/bundles/bundle.js'],
target: 'http://localhost:8080',
secure: false,
changeOrigin: true,
headers: {
'X-Forwarded-Host': 'localhost:' + proxyPort,
'X-Forwarded-Proto': 'http'
}
}]
};
}
return config;
};