Search code examples
webpackbabeljsmithril.js

Configuring webpack, babel and mithril.js


I'm trying to get webpack and babel working (I'm new to both). When I run the webpack-dev-server compilation goes fine but content from my imported module isn't working. Here is how I configure my project:

// package.json
{
  "name": "wtf",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "dev": "webpack-dev-server --open",
    "start": "webpack -d --watch",
    "build": "webpack -p",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.7.2",
    "@babel/preset-env": "^7.7.1",
    "babel-loader": "^8.0.6",
    "webpack": "^4.41.2",
    "webpack-cli": "^3.3.10",
    "webpack-dev-server": "^3.9.0"
  },
  "dependencies": {
    "mithril": "^2.0.4"
  }
}

// webpack.config.js
const path = require('path')
module.exports = {
  mode: 'development',
  entry: './src/index.js',
  output: {
    filename: 'app.js',
    path: path.resolve(__dirname, 'dist')
  },
  devtool: 'inline-source-map',
  devServer: {
    contentBase: './dist',
    hot: true
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /\/node_modules\//,
        use: { loader: 'babel-loader' }
      }
    ]
  }
}

// babel.rc
{
  "presets": [
    "@babel/preset-env"
  ],
  "sourceMaps": true
}

Application is this modest one:

const m = require('mithril');
import welcome from './ui/welcome.js';
var wtf = (function() {
  return {
    view: function(vnode) {
      return m("div", [
        m("div", welcome)
      ]);
    }
  }
})();
m.mount(document.body, wtf);

and the imported welcome module is here:

const m = require('mithril');
var welcome = (function() {
  return {
    view: function(vnode) {
      return m("div", "welcome!");
    }
  }
})();
export default welcome;

When I run npm run dev, webpack-dev-server compiles the code without errors or warnings and open the browser on a blank page. Exploring the code this is what I get:

<!DOCTYPE html>
<html>
  <head>
    ...
  </head>
  <body>
    <div>
      <div view="function view(vnode) {
            return m("div", "welcome!");
          }"></div>
    </div>
  </body>
</html>

Can't get why the module is interpreted that way, what am I missing?


Solution

  • As Isaih Meadows pointed in comment. New to node, webpack and babel I was looking for a configuration error, I totally missed that I was using mithril in a wrong way. To fix this issue I just change my index.js to this:

    const m = require('mithril');
    import welcome from './ui/welcome.js';
    var wtf = (function() {
      return {
        view: function(vnode) {
          return m("div", [
            m(welcome)
          ]);
        }
      }
    })();
    m.mount(document.body, wtf);