OpenWrt Luci - importing an external javascript file

49 Views Asked by At

I am attempting to extract a variable from my Luci module plugin into its own file so I can reuse it.

graph_objects.js:

'use strict';
'require baseclass';
'require uci';

const graph_objects = {...};

return graph_objects;

gauges.js (importing graph_objects here):

var graph_objects = L.resource('statistics/rrdtool/definitions/graph_objects.js');

My error in Luci:

TypeError
"statistics.rrdtool.definitions.graph_objects" factory yields invalid constructor

Other things I have tried:

export const myVariable = 'some value';
import { myVariable } from './config.js';

and

module.exports.myVariable = 'some value';
const { myVariable } = require('./config.js');

Thank you in advance to anyone who takes the time to help

1

There are 1 best solutions below

0
Sourabh Burse On

As far as I know L.resource() only lets you load js files which are subclass of Luci.js.

@kbessemer maybe you can store the variable in session storage like this and use it wherever you want.

You will have to write a onload function that will load the value in session storage, refer this

Or maybe I am misunderstanding your requirement, in that case please add some more code so it will be better to understand.