React and Vue without npm and build
When you start to learn React or Vue or another new frontend framework you know these magic commands: npm install, npm build. Only after them, we deploy our âpublicâ directory.
But what if I say there is a way you don't need to build anything and just insert JS code into your HTML page. Many new frontend developers even donât know that there is a way to use React and Vue without build step and npm.
What if we donât need to build and even npm?
Donât rush to hate me and write evil comments. I know that this is anti-pattern and weâll not have unit tests, components imports, bundles and etc.
But why we need it?
This week we have a new issue from the business customer. The company has its own ERP/BPM system which is Redmine with many plugins and modifications. Systems run on old Centos 6 VPS server with some microservices that grab stats from machines. A customer wants some minor UI modifications, so we donât have a budget and time to rewrite all microservices, compile new Rails and NPM to install webpack for old Rails. New Rails 6 have a webpack.
So, we decided to insert Vue or React just as JS import in the Rails template page.
Vue without npm
When using Vue without the build process you need to write a template for components in âdifferentâ way. The template must be a string that after compiles on render.
Sample Vue code:
<html>
<head>
<script src=âhttps://unpkg.com/vue@2.6.10/dist/vue.js"></script> <title>Stranger Vue things</title>
</head>
<body>
<div id=âvue-appâ></div>
<script type=âtext/javascriptâ>
const titleComponent = `<h1>{{ title }}</h1>`;
var app = new Vue(
{
el: â#vue-appâ,
template: titleComponent,
data: function () { return { title: âStranger Vue thingsâ }; }
});
</script>
</body>
</html>
Browser download size: 371 Kb, time: 590 ms
You can even import Vue components from .vue files using https://github.com/FranckFreiburger/http-vue-loader.
React without npm
Itâs a little complicated to do the same with React compared to Vue. For JSX support you need to import Babel. For DOM you need to import react-dom. So we need three imports compared to only one in Vue case.
Sample React code:
<html>
<head><title>React Stranger Things</title><script type=âapplication/javascriptâ src=âhttps://unpkg.com/react@16.0.0/umd/react.production.min.js"></script><script type=âapplication/javascriptâ src=âhttps://unpkg.com/react-dom@16.0.0/umd/react-dom.production.min.js"></script><script type=âapplication/javascriptâ src=âhttps://unpkg.com/babel-standalone@6.26.0/babel.js"></script></head>
<body>
<div id=ârootâ></div>
<script type=âtext/babelâ>const rootElement = document.getElementById(ârootâ)class TitleComponent extends React.Component {render() {
return (
<h1> {this.props.title}</h1>
);
}
}function App(){
return(
<div>
<TitleComponent title=âReact Stranger Thingsâ/>
</div>
)
}ReactDOM.render(
<App />,
rootElement
)
</script></body></html>
Browser download size: 542Kb, time: 589 ms
In comparing with Vue we donât need to wrap a component as a string. You just write the usual JSX code component.
What about Preact?
Preact â is a tiny React which only 3Kb in size and work without building too. In fact, when I got the issue when weâll need to use some frontend without npm and build â I remember Preact. But when I just started to read the docs, I have understood that React â Preact. And there is a huge problem.
To write in Preact without bundle you need to use htm. Itâs a big difference compared to JSX.
Conclusions
We choose to use React to solve the issue with old Redmine and Rails. Just because we write JSX.
Using React or Vue without npm and build process is a huge pain and we donât recommend to use this method if you have any other road. But we haven'tâŚ