This repository was archived by the owner on Dec 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathserve.webpack.config.js
173 lines (152 loc) · 4.57 KB
/
serve.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*jslint node: true */
'use strict';
const fs = require('fs');
const path = require('path');
const util = require('util');
const open = require('open');
const logger = require('../../utils/logger');
const webpackMerge = require('webpack-merge');
const NamedModulesPlugin = require('webpack/lib/NamedModulesPlugin');
const LoaderOptionsPlugin = require('webpack/lib/LoaderOptionsPlugin');
const skyPagesConfigUtil = require('../sky-pages/sky-pages.config');
const hostUtils = require('../../utils/host-utils');
/**
* Returns the querystring base for parameters allowed to be passed through.
* PLEASE NOTE: The method is nearly duplicated in `runtime/params.ts`.
* @name getQueryStringFromArgv
* @param {Object} argv
* @param {SkyPagesConfig} skyPagesConfig
* @returns {string}
*/
function getQueryStringFromArgv(argv, skyPagesConfig) {
let found = [];
skyPagesConfig.skyux.params.forEach(param => {
if (argv[param]) {
found.push(`${param}=${encodeURIComponent(argv[param])}`);
}
});
if (found.length) {
return `?${found.join('&')}`;
}
return '';
}
/**
* Returns the default webpackConfig.
* @name getDefaultWebpackConfig
* @returns {WebpackConfig} webpackConfig
*/
function getWebpackConfig(argv, skyPagesConfig) {
/**
* Opens the host service url.
* @name WebpackPluginDone
*/
function WebpackPluginDone() {
const shorthand = {
l: 'launch',
b: 'browser'
};
let launched = false;
this.plugin('done', (stats) => {
if (!launched) {
const queryStringBase = getQueryStringFromArgv(argv, skyPagesConfig);
let localUrl = util.format(
'https://localhost:%s%s',
this.options.devServer.port,
this.options.devServer.publicPath
);
let hostUrl = hostUtils.resolve(
queryStringBase,
localUrl,
stats.toJson().chunks,
skyPagesConfig
);
logger.info('SKY UX builder is ready.');
launched = true;
// Process shorthand flags
Object.keys(shorthand).forEach(key => {
if (argv[key]) {
argv[shorthand[key]] = argv[key];
}
});
// Edge uses a different technique (protocol vs executable)
if (argv.browser === 'edge') {
const edge = 'microsoft-edge:';
argv.browser = undefined;
hostUrl = edge + hostUrl;
localUrl = edge + localUrl;
}
switch (argv.launch) {
case 'none':
break;
case 'local':
// Only adding queryStringBase to the message + local url opened,
// Meaning doesn't need those to communicate back to localhost
localUrl += queryStringBase;
logger.info(`Launching Local URL: ${localUrl}`);
open(localUrl, argv.browser);
break;
default:
logger.info(`Launching Host URL: ${hostUrl}`);
open(hostUrl, argv.browser);
break;
}
}
});
}
const common = require('./common.webpack.config').getWebpackConfig(skyPagesConfig);
return webpackMerge(common, {
watch: true,
module: {
rules: [
{
test: /\.ts$/,
use: [
{
loader: 'awesome-typescript-loader',
options: {
// Ignore the "Cannot find module" error that occurs when referencing
// an aliased file. Webpack will still throw an error when a module
// cannot be resolved via a file path or alias.
ignoreDiagnostics: [2307],
transpileOnly: true
}
},
{
loader: 'angular2-template-loader'
}
],
exclude: [/\.e2e\.ts$/]
}
]
},
devServer: {
compress: true,
inline: true,
contentBase: path.join(process.cwd(), 'src', 'app'),
headers: {
'Access-Control-Allow-Origin': '*'
},
historyApiFallback: {
index: skyPagesConfigUtil.getAppBase(skyPagesConfig)
},
stats: 'minimal',
https: {
key: fs.readFileSync(path.join(__dirname, '../../ssl/server.key')),
cert: fs.readFileSync(path.join(__dirname, '../../ssl/server.crt'))
},
publicPath: skyPagesConfigUtil.getAppBase(skyPagesConfig)
},
devtool: 'cheap-module-eval-source-map',
plugins: [
new NamedModulesPlugin(),
WebpackPluginDone,
new LoaderOptionsPlugin({
context: __dirname,
debug: true
})
]
});
}
module.exports = {
getWebpackConfig: getWebpackConfig
};