marko-js / marko

A declarative, HTML-based language that makes building web apps fun
https://markojs.com/
MIT License
13.36k stars 643 forks source link

Short question: require('marko/node-require'); or require('marko/node-require').install(); ? #1163

Closed basickarl closed 5 years ago

basickarl commented 5 years ago

Short question! I've seen both of the below lines of code here and there. I'd just like to know which one I should be using:

require('marko/node-require');
require('marko/node-require').install();
ravitadi commented 5 years ago

@basickarl please use require('marko/node-require').install(); This will enable the repo to see *.marko files.

This can just go in your root index.js and don't need to change it anymore once you set that line.

balupton commented 5 years ago

It should be noted that this page: https://markojs.com/docs/installing/

Has this code:

On the server

Require Marko views

Marko provides a custom Node.js require extension that allows you to require Marko views exactly like a standard JavaScript module. Take the following example server.js:

hello.marko
switch syntax
div -- Hello ${input.name}!
server.js
// The following line installs the Node.js require extension
// for `.marko` files.  This should be called once near the start
// of your application before requiring any `*.marko` files.
require('marko/node-require');

In fact, the syntax that is not recommended is widespread throughout the docs.

DylanPiercey commented 5 years ago

@basickarl both are fine and here is when to use either:

require('marko/node-require') Is used when you just want to allow requiring Marko files in your node server with the default config.

require('marko/node-require').install(options) Allows you to provide additional options for the Marko files that are compiled.

The option defaults look like:

{
  extensions: ["marko"],
  compilerOptions: DEFAULT_COMPILER_OPTIONS
}

Thanks for creating the issue, hopefully this answer is sufficient 😄.

basickarl commented 5 years ago

Perfect guys, thanks!