bpmn-js with Rails 5.1 webpacker 无法解析模块'fs'



我是新来的,使用rails 5.1与webpacker gem,在试图配置我的环境以使用 bpmn-js库时遇到了这个问题。我使用纱线安装了bpmn-js软件包,但我仍然需要添加BPMN-JS示例项目中的一些必需文件才能在project/app/javascript/packs/application.js中正常工作。问题是application.js使用FS模块创建一个新图,如下所示:

project/app/javascript/packs/application.js

import 'bpmn-js'
import 'diagram-js'
import 'bpmn-moddle'

import 'bootstrap/dist/css/bootstrap'
import 'bootstrap/dist/css/bootstrap-theme'
import 'bpmn-js/assets/bpmn-font/css/bpmn'
import 'bpmn-js/assets/bpmn-font/css/bpmn-embedded'
//import 'diagram-js/assets/diagram-js'
//import ModelerIndex from 'bpmn_stuff/modeler_index.js';
console.log('Hello World from webpacker')
'use strict';
var $ = require('jquery');
var BpmnModeler = require('bpmn-js/lib/Modeler');
var container = $('#js-drop-zone');
var canvas = $('#js-canvas');
var modeler = new BpmnModeler({ container: canvas });
var newDiagramXML = fs.readFileSync(__dirname + '/../resources/newDiagram.bpmn', 'utf-8');


function createNewDiagram() {
    openDiagram(newDiagramXML);
}
function openDiagram(xml) {
    modeler.importXML(xml, function(err) {
        if (err) {
            container
                .removeClass('with-diagram')
                .addClass('with-error');
            container.find('.error pre').text(err.message);
            console.error(err);
        } else {
            container
                .removeClass('with-error')
                .addClass('with-diagram');
        }

    });
}
function saveSVG(done) {
    modeler.saveSVG(done);
}
function saveDiagram(done) {
    modeler.saveXML({ format: true }, function(err, xml) {
        done(err, xml);
    });
}
function registerFileDrop(container, callback) {
    function handleFileSelect(e) {
        e.stopPropagation();
        e.preventDefault();
        var files = e.dataTransfer.files;
        var file = files[0];
        var reader = new FileReader();
        reader.onload = function(e) {
            var xml = e.target.result;
            callback(xml);
        };
        reader.readAsText(file);
    }
    function handleDragOver(e) {
        e.stopPropagation();
        e.preventDefault();
        e.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.
    }
    container.get(0).addEventListener('dragover', handleDragOver, false);
    container.get(0).addEventListener('drop', handleFileSelect, false);
}

////// file drag / drop ///////////////////////
// check file api availability
if (!window.FileList || !window.FileReader) {
    window.alert(
        'Looks like you use an older browser that does not support drag and drop. ' +
        'Try using Chrome, Firefox or the Internet Explorer > 10.');
} else {
    registerFileDrop(container, openDiagram);
}
// bootstrap diagram functions
//$(document).on('ready', function() {
    $('#js-create-diagram').click(function(e) {
        e.stopPropagation();
        e.preventDefault();
        createNewDiagram();
    });
    var downloadLink = $('#js-download-diagram');
    var downloadSvgLink = $('#js-download-svg');
    $('.buttons a').click(function(e) {
        if (!$(this).is('.active')) {
            e.preventDefault();
            e.stopPropagation();
        }
    });
    function setEncoded(link, name, data) {
        var encodedData = encodeURIComponent(data);
        if (data) {
            link.addClass('active').attr({
                'href': 'data:application/bpmn20-xml;charset=UTF-8,' + encodedData,
                'download': name
            });
        } else {
            link.removeClass('active');
        }
    }
    var _ = require('lodash');
    var exportArtifacts = _.debounce(function() {
        saveSVG(function(err, svg) {
            setEncoded(downloadSvgLink, 'diagram.svg', err ? null : svg);
        });
        saveDiagram(function(err, xml) {
            setEncoded(downloadLink, 'diagram.bpmn', err ? null : xml);
        });
    }, 500);
    modeler.on('commandStack.changed', exportArtifacts);
//});

您不能在不是节点环境中使用" FS"库。这线是问题:

var newDiagramXML = fs.readFileSync(__dirname + '/../resources/newDiagram.bpmn', 'utf-8');

您不能在不是节点环境中使用'FS'库。我必须用另一种方法代替它。查看了一些示例后,我可以更改此行以直接打开图XML。

更改行:

var newDiagramXML = fs.readFileSync(__dirname + '/../resources/newDiagram.bpmn', 'utf-8');

为此:

var newDiagramXML = '<?xml version="1.0" encoding="UTF-8"?>n'+
'<bpmn:definitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" targetNamespace="http://bpmn.io/schema/bpmn" id="Definitions_1">n'+
'  <bpmn:process id="Process_1" isExecutable="false">n'+
'    <bpmn:startEvent id="StartEvent_1"/>n'+
'  </bpmn:process>n' +
'  <bpmndi:BPMNDiagram id="BPMNDiagram_1">n' +
'    <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_1">n' +
'      <bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">n' +
'        <dc:Bounds height="36.0" width="36.0" x="173.0" y="102.0"/>n' +
'      </bpmndi:BPMNShape>n' +
'    </bpmndi:BPMNPlane>n' +
'  </bpmndi:BPMNDiagram>' +
'</bpmn:definitions>';

相关内容

  • 没有找到相关文章

最新更新