corenova / yang-js

YANG parser and composer
Apache License 2.0
56 stars 18 forks source link

[Question] Import on client side #111

Closed ohad182 closed 4 years ago

ohad182 commented 4 years ago

Hi,

I am using this library within Angular, When parsing a schema that doesn't contain import it works well.

I need to parse a schema with custom imports (bundled within my assets).

is it possible to do something like: Yang.import(other_yang_content)

or: Yang.addImport('ietf-yang-types','assets/yangModules/ietf-yang-types.yang')

those imports should grab the files using http get from the hosting server.

Is there any way to accomplish this?

Thanks!

sekur commented 4 years ago

Hi @ohad182

You can use require('my-schema.yang') instead of Yang.import to load schema and its associated dependencies. And if you use webpack or browserify you can bundle the schema and its dependencies into your application. For webpack you will need to use yang-loader.

Currently, there is no facility to support asynchronous dependency loading (such as dynamic fetching via HTTP GET for YANG schema files from a server).

ohad182 commented 4 years ago

Thanks for your comment,

How can i build the yang-loader library for web?

ohad182 commented 4 years ago

@saintkepha

sekur commented 4 years ago

Hi @ohad182 - please see below for using webpack along with custom loader (yang-loader) for building for web browser.

https://webpack.js.org/concepts/loaders/

ohad182 commented 4 years ago

Hi @saintkepha,

I've installed yang-loader and set it to .yang files in my web project. Now, since web production is a .js file and i have to import it as angular script - yang-loader fails since it performs: var Yang = require('yang-js'); (which is not installed as npm package). and i get error.

Module build failed (from ./node_modules/yang-loader/index.js):
Error: Cannot find module 'yang-js'

is there any solution to this? Thanks!

sekur commented 4 years ago

@ohad182 yang-loader has yang-js as a peerDependency. How are you using yang-js in your project without it being installed via npm or being placed inside node_modules/yang-js ?

ohad182 commented 4 years ago

@saintkepha I am using yang-js as mentioned in doc- clone -> build for web -> include the js file in angular.json

sekur commented 4 years ago

Got it.

First of all, the latest yang-js published package includes the browserified file inside the dist folder. You don’t need to build it yourself. Instead you can point it to yang-js/dist/yang.js for your angular project.

Secondly, using yang-loader along with webpack would ensure you do not need to use the browserified version at all. When you do require(“yang-js”) and other require(“my-schema.yang”) it will process it accordingly. You just need to make sure webpack will also process stuff inside node_modules/yang-js. Basically ensure it’s not excluded.

ohad182 commented 4 years ago

@saintkepha Thanks! you are right! it works! :)