Since version 4.0.0-alpha.19, the code in babylonjs-materials npm page:
import * as BABYLON from 'babylonjs';
import 'babylonjs-materials';
does not work when using with webpack. It will trigger a constructor undefined error in browser.
Example code:
package.json
{
"name": "demo",
"main": "demo.js",
"scripts": {
"build": "webpack --env.production true"
},
"devDependencies": {
"webpack": "^4.29.5",
"webpack-cli": "^3.3.0",
"webpack-dev-server": "^3.2.1"
},
"dependencies": {
"babylonjs": "^4.0.0-alpha.16",
"babylonjs-materials": "^4.0.0-alpha.16"
}
}
webpack.config.js
module.exports = function(env) {
return {
entry: {
demo: 'demo.js'
},
resolve: {
extensions: ['.js'],
modules: ['.', 'node_modules'],
}
};
};
demo.js
import * as BABYLON from 'babylonjs';
import 'babylonjs-materials';
var scene = new BABYLON.Scene(new BABYLON.NullEngine());
var waterMaterial = new BABYLON.WaterMaterial('w',scene);
index.html
<!doctype html>
<html><head><script src="dist/demo.js"></script></head></html>
I found that changine demo.js to this works:
import * as BABYLON from 'babylonjs';
import {WaterMaterial} from 'babylonjs-materials';
var scene = new BABYLON.Scene(new BABYLON.NullEngine());
var waterMaterial = new WaterMaterial('w',scene);