OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

JavaScript: Evaluating export code directly from string

  • Thread starter Thread starter Jay Bienvenu
  • Start date Start date
J

Jay Bienvenu

Guest
How does one run and resolve a JavaScript export module directly from a string?

Here's a concrete example: The module vue-route-generator generates export code that returns a Vue Router configuration object. So when I issue this command:

Code:
const routes = require('vue-route-generator').generateRoutes(...);

routes is a string containing code, something like this:

Code:
function index() {
  return import('/home/jay/Documents/industrial/resources/js/sections/index.vue');
}
function people() {
  return import('/home/jay/Documents/industrial/resources/js/sections/people.vue');
}

export default [
  { name: 'index', path: '/', component: index },
  { name: 'people', path: '/people', component: people },
];

How do I run this code and get the object without saving this code to a separate file? I'm using Webpack as my builder but a solution that works for other builders would be appreciated.

<p>How does one run and resolve a JavaScript export module directly from a string?</p>

<p>Here's a concrete example: The module <code>vue-route-generator</code> generates export code that returns a Vue Router configuration object. So when I issue this command:</p>

<pre><code>const routes = require('vue-route-generator').generateRoutes(...);
</code></pre>

<p><code>routes</code> is a string containing code, something like this:</p>

<pre><code>function index() {
return import('/home/jay/Documents/industrial/resources/js/sections/index.vue');
}
function people() {
return import('/home/jay/Documents/industrial/resources/js/sections/people.vue');
}

export default [
{ name: 'index', path: '/', component: index },
{ name: 'people', path: '/people', component: people },
];
</code></pre>

<p>How do I run this code and get the object without saving this code to a separate file? I'm using Webpack as my builder but a solution that works for other builders would be appreciated.</p>
 

Latest posts

Online statistics

Members online
0
Guests online
4
Total visitors
4
Top