-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
170 lines (154 loc) · 4.25 KB
/
index.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
"use strict";
const path = require("path");
const webpack = require("webpack");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const HtmlWebpackTagsPlugin = require("html-webpack-tags-plugin");
let pnp;
try {
pnp = require("pnpapi");
} catch (error) {
// not in PnP; not a problem
}
let cesiumSource;
if (pnp) {
// Craco Cesium using Pnp
const topLevelLocation = pnp.getPackageInformation(pnp.topLevel)
.packageLocation;
cesiumSource = path.resolve(
pnp.resolveToUnqualified("cesium", topLevelLocation, {
considerBuiltins: false
}),
"Source"
);
} else {
// Craco Cesium using normal module
cesiumSource = "node_modules/cesium/Source";
}
let amd = false;
try {
const cesiumPackageJson = require(path.resolve(
cesiumSource,
"..",
"package.json"
));
const versionStr = cesiumPackageJson.version.split(".");
const version = parseFloat(versionStr[0] + "." + versionStr[1]);
amd = version < 1.63;
} catch (e) {
// ignore
}
module.exports = options => ({
overrideWebpackConfig: ({ webpackConfig, context: { env } }) => {
const { loadPartially, loadCSSinHTML, cesiumPath } = {
loadPartially: false,
loadCSSinHTML: true,
cesiumPath: "cesium",
...options
};
const prod = env === "production";
if (loadPartially) {
// https://github.com/AnalyticalGraphicsInc/cesium-webpack-example
// https://cesium.com/docs/tutorials/cesium-and-webpack/
if (prod) {
// Strip cesium pragmas
webpackConfig.module.rules.push({
test: /.js$/,
enforce: "pre",
include: path.resolve(__dirname, cesiumSource),
sideEffects: false,
use: [
{
loader: "strip-pragma-loader",
options: {
pragmas: {
debug: false
}
}
}
]
});
}
webpackConfig.resolve.alias = {
...webpackConfig.resolve.alias,
cesium$: "cesium/Cesium",
cesium: "cesium/Source"
};
webpackConfig.plugins.push(
new CopyWebpackPlugin([
{
from: path.join(cesiumSource, "../Build/Cesium/Workers"),
to: path.join(cesiumPath, "Workers")
},
{
from: path.join(cesiumSource, "../Build/Cesium/ThirdParty"),
to: path.join(cesiumPath, "ThirdParty")
},
{
from: path.join(cesiumSource, "Assets"),
to: path.join(cesiumPath, "Assets")
},
{
from: path.join(cesiumSource, "Widgets"),
to: path.join(cesiumPath, "Widgets")
}
]),
...(loadCSSinHTML
? [
new HtmlWebpackTagsPlugin({
append: false,
tags: [path.join(cesiumPath, "Widgets", "widgets.css")]
})
]
: []),
new webpack.DefinePlugin({
CESIUM_BASE_URL: JSON.stringify(cesiumPath)
})
);
if (amd) {
webpackConfig.output = {
...webpackConfig.output,
// Needed to compile multiline strings in Cesium
sourcePrefix: ""
};
webpackConfig.amd = {
...webpackConfig.amd,
// Enable webpack-friendly use of require in Cesium
toUrlUndefined: true
};
}
webpackConfig.node = {
...webpackConfig.node,
// Resolve node module use of fs
fs: "empty"
};
} else {
// https://resium.darwineducation.com/installation1
webpackConfig.plugins.push(
new CopyWebpackPlugin([
{
from: path.join(
cesiumSource,
`../Build/Cesium${prod ? "" : "Unminified"}`
),
to: cesiumPath
}
]),
new HtmlWebpackTagsPlugin({
append: false,
tags: [
...(loadCSSinHTML ? ["cesium/Widgets/widgets.css"] : []),
path.join(cesiumPath, "Cesium.js")
]
}),
new webpack.DefinePlugin({
CESIUM_BASE_URL: JSON.stringify(cesiumPath)
})
);
webpackConfig.externals = {
...webpackConfig.externals,
cesium: "Cesium"
};
}
return webpackConfig;
}
});