Hello, guys! I try to use importmap
with Phaser 4 but I have this error on Plunker: VM535 +esm:29 Uncaught ReferenceError: Cannot access 'jn' before initialization
and null
on PlayCode.
Playgrounds:
index.html
<!doctype html>
<html>
<head>
<title>Example</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<canvas id="renderCanvas"></canvas>
<!-- Since importmap is not yet supported by all browsers, it is
necessary to add the polyfill es-module-shims.min.js -->
<script async src="https://unpkg.com/es-module-shims@0.1.7/dist/es-module-shims.min.js"></script>
<script type="importmap">
{
"imports": {
"phaser": "https://cdn.jsdelivr.net/npm/@phaserjs/phaser@0.2.2/+esm"
}
}
</script>
<script type="module" src="./js/main.js"></script>
</body>
</html>
js/main.js
import phaser from "phaser";
console.log(phaser);
I don’t need Phaser 4 because Phaser 3 supports ESM:
Sandboxes:
index.html
<!doctype html>
<html>
<head>
<title>Example</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<canvas id="renderCanvas"></canvas>
<!-- Since importmap is not yet supported by all browsers, it is
necessary to add the polyfill es-module-shims.min.js -->
<script async src="https://unpkg.com/es-module-shims@0.1.7/dist/es-module-shims.min.js"></script>
<script type="importmap">
{
"imports": {
"@box2d/core": "https://cdn.jsdelivr.net/npm/@box2d/core@0.10.0/+esm",
"phaser": "https://cdn.jsdelivr.net/npm/phaser@3.60.0/dist/phaser.esm.js"
}
}
</script>
<script type="module" src="./js/main.js"></script>
</body>
</html>
js/main.js
import { b2World } from "@box2d/core";
import { AUTO, Scene, Game } from "phaser";
// Box2D
const world = b2World.Create({ x: 0, y: 3 });
console.log(world.GetGravity());
// Phaser
console.log(Game);
Scripts to create debug and release bundles with Rollup (you don’t need to install Phaser locally):
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "rollup -cwm",
"del-bundle-map": "del /f /q /s .\\public\\js\\bundle.js.map",
"bundle": "rollup -cm",
"minify": "uglifyjs public/js/bundle.js -o public/js/bundle.js",
"release": "npm run bundle && npm run minify && npm run del-bundle-map"
},
rollup-config.js
export default {
input: "./src/main.js",
output: {
file: "public/js/bundle.js"
}
}