try to running blockly on server-side for headless join block & code generate

72 views
Skip to first unread message

m lz

unread,
Oct 6, 2023, 11:58:16 AM10/6/23
to Blockly
I have an chrome extension help people generate block and send relevent xml to server,
after that server side combine the block to current workspace,export the final xml to client browser and render it in client side.
so I want to blockly running on server side base on nodejs bellow is my code ,but it not work give me an error
ReferenceError: goog is not defined
    at Object.<anonymous> (C:\Users\mlzboy\Downloads\blockly-blockly-v9.2.1\blockly-blockly-v9.2.1\blocks\math.js:13:1)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (C:\Users\mlzboy\Downloads\blockly-blockly-v9.2.1\blockly-blockly-v9.2.1\zzzz.js:8:1)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)


const { JSDOM } = require('jsdom');
// const Blockly = require('blockly');
const { window } = new JSDOM('<!DOCTYPE html><html><body></body></html>');
global.document = window.document;
global.window = window;
 
global.Blockly = require('./blockly_compressed.js');
require('./blocks/math.js');
require('./blocks/text.js');
require('./blocks/lists.js');
require('./generators/python.js');
require('./generators/python/math.js');
require('./generators/python/text.js');
require('./generators/python/lists.js');
require('./msg/messages.js');

var fs = require('fs');

// var xmlText = process.argv[2];
  // Define the XML representation of the block instance you want to insert
  var xmlText = '<xml xmlns="http://www.w3.org/1999/xhtml">' +
  '<block type="math_arithmetic" inline="true" x="400" y="400">' +
  '<field name="OP">ADD</field>' +
  '<value name="A">' +
  '<shadow type="math_number">' +
  '<field name="NUM">1</field>' +
  '</shadow>' +
  '</value>' +
  '<value name="B">' +
  '<shadow type="math_number">' +
  '<field name="NUM">2</field>' +
  '</shadow>' +
  '</value>' +
  '</block>' +
  '</xml>';
try {
    var xml = Blockly.Xml.textToDom(xmlText);
    // Create a headless workspace.
    // var workspace = new Blockly.Workspace();
    var workspace = Blockly.inject(document.body);
    Blockly.Xml.domToWorkspace(workspace, xml);
    var code = Blockly.Python.workspaceToCode(workspace);
    console.log(code);
} catch (e) {
    console.log(e);
}

Beka Westberg

unread,
Oct 6, 2023, 12:49:12 PM10/6/23
to Blockly
Hello,

We don't recommend importing Blockly's files directly. This will give you the `goog` errors, because references to closure compiler haven't been stripped out yet.

Instead you should import our exported modules:
```
const Blockly = require('blockly/core');
const {pythonGenerator} = require('blockly/python');
const libraryBlocks = require('blockly/blocks');
```

I believe those are the correct requires! But I'm used to ES6 style imports, not requires, so I might have messed something up =) Check out the package readme for more info.

Best wishes,
--Beka

Reply all
Reply to author
Forward
0 new messages