Skip to content

Commit e335014

Browse files
committed
Separate out Scratch specific webpack configuration
Our webpack config has grown to support our new scratch entry file. I've separated out the Scratch specific config needed for this entrypoint. As part of this I have also made sure the code from the scratch-gui is kept in a separate file from our code and not modified as part of our build process so we are better following the requirements of the Scratch license. Because Scratch has peer dependencies (react, react-dom, redux, redux-react) I've had to configure these as external dependencies and set them on window in the way that Scratch expects. These dependencies will load first, then Scratch and then our compiled scratch.jsx.
1 parent 86f0339 commit e335014

File tree

2 files changed

+82
-5
lines changed

2 files changed

+82
-5
lines changed

src/scratch.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,17 @@
5454
<body>
5555
<div id="scratch-loading">loading</div>
5656
<div id="app"></div>
57+
58+
<script src="<%= publicUrl %>vendor/react.production.min.js"></script>
59+
<script src="<%= publicUrl %>vendor/react-dom.production.min.js"></script>
60+
<script src="<%= publicUrl %>vendor/redux.min.js"></script>
61+
<script src="<%= publicUrl %>vendor/react-redux.min.js"></script>
62+
<script>
63+
window.react = window.React;
64+
window["react-dom"] = window.ReactDOM;
65+
window.redux = window.Redux;
66+
window["react-redux"] = window.ReactRedux;
67+
</script>
68+
<script src="<%= publicUrl %>vendor/scratch-gui.js"></script>
5769
</body>
5870
</html>

webpack.config.js

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,9 @@ const moduleRules = [
101101
},
102102
];
103103

104-
module.exports = {
104+
const mainConfig = {
105105
entry: {
106106
"web-component": path.resolve(__dirname, "./src/web-component.js"),
107-
scratch: path.resolve(__dirname, "./src/scratch.jsx"),
108107
PyodideWorker: path.resolve(__dirname, "./src/PyodideWorker.js"),
109108
},
110109
module: { rules: moduleRules },
@@ -185,21 +184,87 @@ module.exports = {
185184
filename: "web-component.html",
186185
chunks: ["web-component"],
187186
}),
187+
new CopyWebpackPlugin({
188+
patterns: [
189+
{ from: "public", to: "" },
190+
{ from: "src/projects", to: "projects" },
191+
],
192+
}),
193+
],
194+
stats: "minimal",
195+
};
196+
197+
const scratchConfig = {
198+
entry: {
199+
scratch: path.resolve(__dirname, "./src/scratch.jsx"),
200+
},
201+
module: { rules: moduleRules },
202+
resolve: {
203+
extensions: [".*", ".js", ".jsx", ".css"],
204+
},
205+
output: {
206+
path: path.resolve(__dirname, "./build"),
207+
filename: "[name].js",
208+
publicPath: publicUrl,
209+
},
210+
externals: [
211+
function ({ request }, callback) {
212+
if (request === "@scratch/scratch-gui") return callback(null, "GUI");
213+
if (request === "react") return callback(null, "React");
214+
if (request === "react-dom" || request.startsWith("react-dom/"))
215+
return callback(null, "ReactDOM");
216+
if (request === "redux") return callback(null, "Redux");
217+
if (request === "react-redux") return callback(null, "ReactRedux");
218+
callback();
219+
},
220+
],
221+
plugins: [
222+
new Dotenv({
223+
path: "./.env",
224+
systemvars: true,
225+
}),
188226
new HtmlWebpackPlugin({
189227
inject: "body",
190228
template: "src/scratch.html",
191229
filename: "scratch.html",
192230
chunks: ["scratch"],
231+
templateParameters: {
232+
publicUrl: publicUrl,
233+
},
193234
}),
194235
new CopyWebpackPlugin({
195236
patterns: [
196237
{ from: scratchStaticDir, to: "scratch-gui/static" },
197-
{ from: `${scratchStaticDir}/assets`, to: "static/assets" },
238+
{ from: `${scratchStaticDir}/assets`, to: "vendor/static/assets" },
198239
{ from: scratchChunkDir, to: "chunks" },
199-
{ from: "public", to: "" },
200-
{ from: "src/projects", to: "projects" },
240+
{
241+
from: "node_modules/react/umd/react.production.min.js",
242+
to: "vendor/react.production.min.js",
243+
},
244+
{
245+
from: "node_modules/react-dom/umd/react-dom.production.min.js",
246+
to: "vendor/react-dom.production.min.js",
247+
},
248+
{
249+
from: "node_modules/redux/dist/redux.min.js",
250+
to: "vendor/redux.min.js",
251+
},
252+
{
253+
from: "node_modules/react-redux/dist/react-redux.min.js",
254+
to: "vendor/react-redux.min.js",
255+
},
256+
{
257+
from: "node_modules/@scratch/scratch-gui/dist/scratch-gui.js",
258+
to: "vendor/scratch-gui.js",
259+
},
260+
{
261+
from: "node_modules/@scratch/scratch-gui/dist/scratch-gui.js.LICENSE.txt",
262+
to: "vendor/scratch-gui.js.LICENSE.txt",
263+
},
201264
],
202265
}),
203266
],
204267
stats: "minimal",
205268
};
269+
270+
module.exports = [mainConfig, scratchConfig];

0 commit comments

Comments
 (0)