This repository has been archived by the owner on Mar 8, 2023. It is now read-only.
/
webpack.config.js
200 lines (181 loc) · 5.66 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* Copyright (C) 2017-2018 HERE Europe B.V.
* Licensed under Apache 2.0, see full license in LICENSE
* SPDX-License-Identifier: Apache-2.0
*/
const merge = require("webpack-merge");
const path = require("path");
const glob = require("glob");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const prepareOnly = process.env["PREPARE_ONLY"] === "true";
const harpMapThemePath = path.dirname(require.resolve("@here/harp-map-theme/package.json"));
const harpFontResourcesPath = path.dirname(require.resolve("@here/harp-fontcatalog/package.json"));
function resolveOptional(path, message) {
try {
return require.resolve(path);
} catch (error) {
if (!message) {
message = "some examples may not work";
}
console.log(`warning: unable to find '${path}': ${message}`);
return undefined;
}
}
const commonConfig = {
context: __dirname,
devtool: prepareOnly ? undefined : "source-map",
externals: [
{
three: "THREE",
fs: "undefined"
},
function(context, request, callback) {
return /three\.module\.js$/.test(request) ? callback(null, "THREE") : callback();
}
],
resolve: {
extensions: [".webpack.js", ".web.ts", ".ts", ".tsx", ".web.js", ".js"]
},
module: {
rules: [
{
test: /\.tsx?$/,
loader: "ts-loader",
exclude: /node_modules/,
options: {
configFile: path.join(__dirname, "tsconfig-build.json"),
onlyCompileBundledFiles: true,
projectReferences: true
}
}
]
},
output: {
path: path.join(process.cwd(), "dist/examples"),
filename: "[name].bundle.js"
},
performance: {
hints: false
},
stats: {
all: false,
timings: true,
exclude: "/resources/",
errors: true,
entrypoints: true,
warnings: true
},
mode: process.env.NODE_ENV || "development"
};
const decoderConfig = merge(commonConfig, {
target: "webworker",
entry: {
decoder: "./decoder/decoder"
}
});
const webpackEntries = glob
.sync(path.join(__dirname, "./src/*.{ts,tsx}"))
.filter(name => !name.includes(".d.ts"))
.reduce((result, entry) => {
const name = path.basename(entry).replace(/.tsx?$/, "");
if (name.startsWith("common")) {
return result;
}
result[name] = entry;
return result;
}, {});
const htmlEntries = glob.sync(path.join(__dirname, "./src/*.html")).reduce((result, entry) => {
result[path.basename(entry).replace(/.html$/, "")] = entry;
return result;
}, {});
function filterExamples(pattern) {
function filterEntries(entries) {
Object.keys(entries).forEach(entryName => {
if (entryName.indexOf(pattern) == -1) {
delete entries[entryName];
}
});
}
filterEntries(webpackEntries);
filterEntries(htmlEntries);
}
// Uncomment and adapt to filter built examples and speed up the build significantly
//
//filterExamples("hello");
const browserConfig = merge(commonConfig, {
entry: webpackEntries,
output: {
filename: "[name]_bundle.js"
},
optimization: {
splitChunks: {
chunks: "all",
minSize: 1000,
name: "common"
}
}
});
const exampleBrowserConfig = merge(commonConfig, {
entry: {
"example-browser": "./example-browser.ts"
}
});
const codeBrowserConfig = merge(commonConfig, {
entry: {
codebrowser: "./codebrowser.ts"
}
});
browserConfig.plugins = Object.keys(browserConfig.entry).map(
chunk =>
new HtmlWebpackPlugin({
template: "template/example.html",
chunks: ["common", chunk],
filename: `${chunk}.html`
})
);
const allEntries = Object.assign({}, webpackEntries, htmlEntries);
/**
* Geterate example definitions for 'index.html' in following form:
*
* {
* [examplePage: string]: string // maps example page to example source
* }
*/
const exampleDefs = Object.keys(allEntries).reduce(function(r, entry) {
r[entry + ".html"] = path.relative(__dirname, allEntries[entry]);
return r;
}, {});
const assets = [
{
from: __dirname + "/example-definitions.js.in",
to: "example-definitions.js",
transform: content => {
return content.toString().replace("{{EXAMPLES}}", JSON.stringify(exampleDefs, true, 4));
}
},
...[path.join(__dirname, "src")].map(srcDir => ({
from: path.join(srcDir, "*.{ts,tsx,html}"),
to: "src/[name].[ext]"
})),
path.join(__dirname, "index.html"),
{
from: path.join(__dirname, "src/*.html"),
to: "[name].[ext]"
},
path.join(__dirname, "codebrowser.html"),
{ from: path.join(__dirname, "resources"), to: "resources", toType: "dir" },
{ from: path.join(harpMapThemePath, "resources"), to: "resources", toType: "dir" },
{
from: path.join(harpFontResourcesPath, "resources"),
to: "resources/fonts",
toType: "dir"
},
require.resolve("three/build/three.min.js"),
resolveOptional("@here/harp.gl/dist/harp.js", "bundle examples require `yarn build-bundle`"),
resolveOptional("@here/harp.gl/dist/harp-decoders.js")
].filter(asset => asset); // ignore stuff that is not found
browserConfig.plugins.push(
new CopyWebpackPlugin(assets, { ignore: ["*.npmignore", "*.gitignore"] })
);
module.exports = [decoderConfig, browserConfig, codeBrowserConfig, exampleBrowserConfig];