New project: SwiftJS

Hello everybody,

I just create a new project. A cross platform Javascript engine for Swift.

8 Likes

I am trying to load a module from npm

let registry = URL(string: "https://registry.npmjs.org/")!

let context = JSContext()

context.global["npm_module_loader"] = JSObject(newFunctionIn: context) { context, this, arguments in
    
    guard arguments.count == 1, let module = arguments[0].stringValue else {
            return .failure(JSObject(newErrorFromMessage: "Invalid arguments", in: context))
    }
    
    guard let module_url = URL(string: module, relativeTo: registry) else {
        return .failure(JSObject(newErrorFromMessage: "Invalid module", in: context))
    }
    
    guard let module_contents = try? String(contentsOf: module_url) else {
        return .failure(JSObject(newErrorFromMessage: "Invalid module", in: context))
    }
    
    return .success(JSObject(string: module_contents, in: context))
}

try context.evaluateScript("""

function require(module) {
    
    var desc = JSON.parse(npm_module_loader(module))
    var latest_version = desc['dist-tags'].latest
    
    var package = desc.versions[latest_version]
    
    return package
}

""")

try print(context.evaluateScript("require('@babel/core')").dictionary)

then, I got the package.json

**["_npmVersion": "lerna/3.19.0/node@v13.11.0+x64 (linux)", "gitHead": "8d5e422be27251cfaadf8dd2536b31b4a5024b02", "engines": [object Object], "license": "MIT", "_nodeVersion": "13.11.0", "main": "lib/index.js", "devDependencies": [object Object], "name": "@babel/core", "maintainers": [object Object],[object Object],[object Object],[object Object],[object Object],[object Object], "version": "7.9.0", "author": [object Object], "_npmOperationalInternal": [object Object], "dist": [object Object], "_hasShrinkwrap": false, "publishConfig": [object Object], "directories": [object Object], "_npmUser": [object Object], "keywords": 6to5,babel,classes,const,es6,harmony,let,modules,transpile,transpiler,var,babel-core,compiler, "funding": [object Object], "browser": [object Object], "_id": "@babel/core@7.9.0", "dependencies": [object Object], "repository": [object Object], "description": "Babel compiler core.", "homepage": "https://babeljs.io/"]**

however, I don't known how the npm works. There is no lib folder under the repository https://github.com/babel/babel/tree/master/packages/babel-core

You should post a new thread (maybe in #swift-users ?). That'd attract the better group of people to ask. Though I'm not sure if it's swift-related.

Terms of Service

Privacy Policy

Cookie Policy