Is it possible to utilize CommonJS, TypeScript, and React together without the need for bundling

Having attempted to utilize CommonJS, TypeScript, and React together, I encountered an issue with the initial code loading:

Index.html

  <script type="text/javascript">
          function loadScript(url)
          {    
              var head = document.getElementsByTagName('head')[0];
              var script = document.createElement('script');
              script.type = 'text/javascript';
              script.src = url;
              head.appendChild(script);
          }


          loadScript('./Scripts/react.js');

          loadScript('./Scripts/react-dom.js');

          loadScript('./Scripts/require.js');

          loadScript('./Scripts/index.js');

    </script>

The loading file: index.ts

        var rootElement;

        function _onLoad() {
            rootElement = rootElement || document.getElementById('root');
            ReactDOM.render(React.createElement(DataSample, null), rootElement);
        }
        function _onUnload() {
            if (rootElement) {
                ReactDOM.unmountComponentAtNode(rootElement);
            }
        }
        var isReady = document.readyState === 'interactive' || document.readyState === 'complete';
        if (isReady) {
            _onLoad();
        }
        else {
            window.onload = _onLoad;
        }
        window.onunload = _onUnload;

Error: *Uncaught ReferenceError: require is not defined at index.tsx:1 import * as React from 'react';*

I have webpack installed and can create bundles, but for development purposes, I prefer not to. Is there another loader I should consider using? Currently, I am using requirejs, which I know is primarily designed for AMD.

Thank you in advance.

Answer №1

Absolutely, yes!

In my humble opinion, it is possible (and advisable) to forego using bundlers if there are no specific requirements dictating their necessity.

Currently, most browsers do not support module loading by default, so utilizing a module loader is essential. I personally recommend using systemjs over requirejs due to its wider compatibility and ability to handle various module types effortlessly. You can find an instructional guide on how to set up systemjs here, along with a similar query answered on Stack Overflow.

On a side note, consider using JSPM instead of directly implementing SystemJS. JSPM simplifies the configuration process for different modules while leveraging SystemJS internally. Take a look at this example to learn more.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to close an ajax page within the main page using a button?

I am dealing with a situation where I have 2 pages. The first page contains a div called 'ajaxicall', which loads another page inside it. The challenge I am facing is figuring out how to close the second page when I click on the "close" button w ...

Ways to extract input values from a specific row in a textbox as users input data

On a button click, I am dynamically adding data to an HTML table using the loadbooks function. When the user clicks on the button, the table is populated with data. <button id="button" onclick="loadbooks()"></button> function loadbooks() { ... ...

Using Angular: connecting $viewContentLoaded to manually initiate app bootstrapping

I have an Angular module that I am bootstrapping manually and attempting to access its $rootScope to add an event listener for $viewContentLoaded. Below is the code snippet: angular.bootstrap(el, [appname]); //////////////////////////// Fixing links cons ...

Utilizing Regex Patterns to Manipulate CSS Attributes

I am dealing with a string containing CSS properties and their values: str = "filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#cccccc', endColorstr='#000000'); background: -webkit-linear-gradient(top, black, wh ...

My CSS files are not being included by grunt-bower-install

As someone who is relatively new to bower and grunt, I'm struggling with some basic tasks. After running bower install --save bootstrap, my goal is to use grunt-bower-install to update my js & css files as per the instructions on their documentat ...

Is it possible to handle both ajax form submissions and browser post submissions in express.js?

On my website, I have implemented a contact form using express.js (4.0). I am contemplating how to manage the scenario where a user disables JavaScript. If the last part of my routing function looks like this: res.render('contact.jade', { tit ...

Uploading an image in Typescript on IE11 using Angular 4

I'm having an issue uploading an image to my web application using Angular 4. I convert the input file using readAsBinaryString() and extract the ASCII code using btoa() before passing it to a backend service. While this process works smoothly on Chro ...

Adding HTML content to routes in ExpressOrEnhancing routes

Recently, I've been working with a basic index.html file. My goal is to have the routes file append an HTML button when requested by the browser without actually modifying the original file. This may seem like a peculiar idea, but my intention is to c ...

Ways to ensure text fits nicely within a container: HTML & CSS

Below is a code snippet: @import url('https://fonts.googleapis.com/css?family=Merriweather|Open+Sans'); .square { max-width: 460px; background: white; border-radius: 4px; box-shadow: 0px 5px 20px #D9DBDF; -webkit-transition: all 0. ...

Learn how to retrieve specific user information from a JSON file by implementing a button click feature within a custom directory using Angular.js

I am just starting to learn angular.js and I'm trying to figure out how to retrieve JSON data from a custom directory. My goal is to display the information of a specific user when a particular button is clicked, but so far I haven't been success ...

Fixing the Jquery Clone Bug

Recently, I came across a jQuery clone plugin that is designed to fix the values of cloned textarea and select elements. This code snippet showcases how it works: (function (original) { jQuery.fn.clone = function () { var result = ori ...

Mobile website includes a share button that activates share dialogs for iOS and Android systems

Can a share button be created on my website that will trigger share dialogs on iOS and Android devices? Specifically, I am referring to the dialogs shown below for each system: https://i.sstatic.net/dw759.jpg https://i.sstatic.net/NS8W3.png I am lookin ...

Meteor, enhanced with the dynamic Iron Router module and integrated

I am working on a project using Meteor (Meteor.com) and I want to incorporate iron-router for the page routing along with the pre-existing accounts-ui package for login functionality. Previously, my {{loginButtons}} functioned properly, but ever since I i ...

Cannot find JS variable after loop has completed

I am struggling to understand why the value of my variable is not changing in my function. Here is my code snippet: var count = function(datain){ let temparr = [], countobj = {}; $.each(datain, function(key, val) { console.log(temparr); cou ...

Tips for storing information in a database when a user decides to exit

Is there a proper method to achieve this task? I aim to store in the database the number of minutes that a user has watched in a video until they close the page, and then resume the video from where they left off when they revisit the same video. I attempt ...

Modify the original source data of an HTML table

I have written the following code: <table class="table table-hover"> <thead> <tr> <th>Status</th> <th>MPK</th> <th>Full Name</th> <th>Phone Number</th> <th>Proj ...

Manipulate the curvature of a spline using three.js

In my current scene, I have a camera following a spline path. This spline is generated using the CatmullRomCurve3() method in three.js. However, there are bumps appearing automatically before and after the curve descends. I am looking to eliminate these ...

Unlocking the Power of Calculations with VueJS

I am looking to transform a string representing an IP address, like 10.120.0.1, into another string representing an ISIS Network ID, such as 49.0001.0101.2000.0001.00. The section in the middle 010 1.20 00.0 001 corresponds to the first string (I have adde ...

Obtain the reference to the child element of the nativeElement

As a newcomer to Angular, I am currently working with Angular 7. Within our project, we have implemented a button component labeled as "Send Email." When this button is clicked on the webpage where it is displayed, it triggers the default email sending win ...

What is the best way to pass props down to grandchildren components in React?

I'm trying to pass some props from a layout to its children. The issue is, it works fine when the child component is directly nested inside the layout, but it doesn't work when the child component is wrapped in another element like a div. Exampl ...