can't covert css to rtl using javascript library

624 views Asked by At

I am beginner in JavaScript and I am trying to use rtlcss library to conver css files from ltr to rtl. I am using this code but it displays 2 errors:

Uncaught SyntaxError: Illegal return statement
Uncaught ReferenceError: require is not defined

My code

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>convert css to rtl</title>
    <script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
    <script src="js/rtlcss/src/rtlcss.js"></script>
</head>
<body>
    <textarea id="source_textarea" placeholder="place your css" ></textarea>
    <button id="convert_btn">Convert</button>
    <textarea id="result_textarea"></textarea>
    <script>
        (function myfunction() {
            rtlcss = require('rtlcss');
            var output = rtlcss.process('css/main.css');
            console.log(output);
            $("#result_textarea").val(output);
        })();
    </script>
</body>
</html>

I believe i am doing something wrong, it's not library problem.. so anyone can help?

1

There are 1 answers

0
MK. On

This is a node package, as @haakym mentioned, you should be using NPM (node package manager). For more details on how to install and use NPM, follow the Getting Started guide.

If you want to use it in the browser, you can use Browserify; it lets you require('modules') in the browser by bundling up all of your dependencies.

After you have node/npm setup complete, do the following:

  • From the command line, run these commands to install RTLCSS and Browserify

    npm install rtlcss
    npm install -g browserify    
    
  • Create a file with the following contents and save it as main.js :

    rtlcss = require('rtlcss');
    
  • Run this command to create a browser usable bundle:

    browserify main.js -o bundle.js
    
  • Include the resulting script in your page:

    <script src="bundle.js"></script>
    

That's all, Now you'll be able to use it in the browser:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>convert css to rtl</title>
    <script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
    <script src="bundle.js"></script>   
</head>
<body>
    <textarea id="source_textarea" placeholder="place your css" ></textarea>
    <button id="convert_btn">Convert</button>
    <textarea id="result_textarea"></textarea>
    <script>
    $('button').click(function(){
      var output = rtlcss.process($('#source_textarea').val());
      $("#result_textarea").val(output);
    });
    </script>
</body>
</html>

Online Demo