Marko

Marko + Webpack

The @marko/webpack/loader loader for Webpack will automatically compile all imported Marko templates during bundling. In addition, it will automatically bundle any template dependencies (including required CSS).

ProTip: Want to see it in action? Check out the marko-webpack demo repository. Or run npx @marko/create --template webpack to use this sample as a starting point for a new app.

Installation

npm install marko
npm install webpack @marko/webpack --save-dev

Client rendering

Let's say we have a simple view that we want to render in the browser: hello.marko

hello.marko
<h1>Hello ${input.name}</h1>
h1 -- Hello ${input.name}

First, let's create a client.js that requires the view and renders it to the body:

client.js
import MyTemplate from "./hello.marko";
 
MyTemplate.renderSync({ name: "Marko" }).appendTo(document.body);

Now, let's configure webpack to compile the client.js file and use @marko/webpack/loader for any *.marko files:

webpack.config.js
module.exports = {
  entry: "./client.js",
  output: {
    path: __dirname,
    filename: "static/bundle.js"
  },
  resolve: {
    extensions: [".js", ".marko"]
  },
  module: {
    rules: [
      {
        test: /\.marko$/,
        loader: "@marko/webpack/loader"
      }
    ]
  }
};

Run webpack from your terminal and you'll have a new static/bundle.js file created. Reference that from an html file and you're good to go.

index.html
<!DOCTYPE html>
<html>
  <body>
    <script src="static/bundle.js"></script> 
  </body>
</html>

Load up that page in your browser and you should see Hello Marko staring back at you.

Using CSS pre-processors

If you're using inline css with pre-processors, you must configure the appropriate loader.

pretty.marko
style.less {
    .pretty {
        color:@pretty-color;
    }
}
 
<div.pretty/>
style.less {
  .pretty {
    color: @pretty-color;
  }
}
 
div.pretty
webpack.config.js
//...
module: {
  rules: [
    //...
    {
      test: /\.less$/, // matches style.less { ... } from our template
      use: ["style-loader", "css-loader", "less-loader"]
    }
    //...
  ];
}
//...

Extracting CSS

It is recommended to configure the MiniCSSExtractPlugin so that you get a separate css bundle rather than it being included in the JavaScript bundle.

npm install mini-css-extract-plugin --save-dev
webpack.config.js
const CSSExtractPlugin = require("mini-css-extract-plugin");
 
module.exports = {
  entry: "./client.js",
  resolve: {
    extensions: [".js", ".marko"]
  },
  module: {
    rules: [
      {
        test: /\.marko$/,
        loader: "@marko/webpack/loader"
      },
      {
        test: /\.(less|css)$/,
        use: [CSSExtractPlugin.loader, "css-loader", "less-loader"]
      }
    ]
  },
  plugins: [
    // Write out CSS bundle to its own file:
    new CSSExtractPlugin({
      filename: "[name].css"
    })
  ]
};
EDIT

Contributors

Helpful? You can thank these awesome people! You can also edit this doc if you see any issues or want to improve it.