diff --git a/src/index.js b/src/index.js index dfb566329..97cca7d69 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,10 @@ -const VirtualMachine = require('./virtual-machine'); +import VirtualMachine from './virtual-machine.js'; -module.exports = VirtualMachine; +import ArgumentType from './extension-support/argument-type.js'; +import BlockType from './extension-support/block-type.js'; + +export default VirtualMachine; +export { + ArgumentType, + BlockType +}; diff --git a/src/virtual-machine.js b/src/virtual-machine.js index 64ad7934f..750d76feb 100644 --- a/src/virtual-machine.js +++ b/src/virtual-machine.js @@ -1,28 +1,30 @@ +import EventEmitter from 'events'; +import JSZip from 'jszip'; +import {Buffer} from 'buffer'; +import centralDispatch from './dispatch/central-dispatch.js'; +import ExtensionManager from './extension-support/extension-manager.js'; +import log from './util/log.js'; +import MathUtil from './util/math-util.js'; +import Runtime from './engine/runtime.js'; +import StringUtil from './util/string-util.js'; +import * as formatMessage from 'format-message'; +import Variable from './engine/variable.js'; +import newBlockIds from './util/new-block-ids.js'; +import {loadCostume} from './import/load-costume.js'; +import {loadSound} from './import/load-sound.js'; +import {serializeSounds, serializeCostumes} from './serialization/serialize-assets.js'; +import 'canvas-toBlob'; +import validate from 'scratch-parser'; +import * as scratchSb1Converter from 'scratch-sb1-converter'; +import sb3 from './serialization/sb3.js'; +import sb2 from './serialization/sb2.js'; + let _TextEncoder; if (typeof TextEncoder === 'undefined') { _TextEncoder = require('text-encoding').TextEncoder; } else { _TextEncoder = TextEncoder; } -const EventEmitter = require('events'); -const JSZip = require('jszip'); - -const Buffer = require('buffer').Buffer; -const centralDispatch = require('./dispatch/central-dispatch'); -const ExtensionManager = require('./extension-support/extension-manager'); -const log = require('./util/log'); -const MathUtil = require('./util/math-util'); -const Runtime = require('./engine/runtime'); -const StringUtil = require('./util/string-util'); -const formatMessage = require('format-message'); - -const Variable = require('./engine/variable'); -const newBlockIds = require('./util/new-block-ids'); - -const {loadCostume} = require('./import/load-costume.js'); -const {loadSound} = require('./import/load-sound.js'); -const {serializeSounds, serializeCostumes} = require('./serialization/serialize-assets'); -require('canvas-toBlob'); const RESERVED_NAMES = ['_mouse_', '_stage_', '_edge_', '_myself_', '_random_']; @@ -317,7 +319,6 @@ class VirtualMachine extends EventEmitter { } const validationPromise = new Promise((resolve, reject) => { - const validate = require('scratch-parser'); // The second argument of false below indicates to the validator that the // input should be parsed/validated as an entire project (and not a single sprite) validate(input, false, (error, res) => { @@ -326,7 +327,7 @@ class VirtualMachine extends EventEmitter { }); }) .catch(error => { - const {SB1File, ValidationError} = require('scratch-sb1-converter'); + const {SB1File, ValidationError} = scratchSb1Converter; try { const sb1 = new SB1File(input); @@ -462,7 +463,6 @@ class VirtualMachine extends EventEmitter { * @return {string} Serialized state of the runtime. */ toJSON (optTargetId) { - const sb3 = require('./serialization/sb3'); return StringUtil.stringify(sb3.serialize(this.runtime, optTargetId)); } @@ -495,11 +495,9 @@ class VirtualMachine extends EventEmitter { const deserializePromise = function () { const projectVersion = projectJSON.projectVersion; if (projectVersion === 2) { - const sb2 = require('./serialization/sb2'); return sb2.deserialize(projectJSON, runtime, false, zip); } if (projectVersion === 3) { - const sb3 = require('./serialization/sb3'); return sb3.deserialize(projectJSON, runtime, zip); } // TODO: reject with an Error (possible breaking API change!) @@ -589,7 +587,6 @@ class VirtualMachine extends EventEmitter { } const validationPromise = new Promise((resolve, reject) => { - const validate = require('scratch-parser'); // The second argument of true below indicates to the parser/validator // that the given input should be treated as a single sprite and not // an entire project @@ -632,8 +629,6 @@ class VirtualMachine extends EventEmitter { */ _addSprite2 (sprite, zip) { // Validate & parse - - const sb2 = require('./serialization/sb2'); return sb2.deserialize(sprite, this.runtime, true, zip) .then(({targets, extensions}) => this.installTargets(targets, extensions, false)); @@ -647,7 +642,6 @@ class VirtualMachine extends EventEmitter { */ _addSprite3 (sprite, zip) { // Validate & parse - const sb3 = require('./serialization/sb3'); return sb3 .deserialize(sprite, this.runtime, zip, true) .then(({targets, extensions}) => this.installTargets(targets, extensions, false)); @@ -1247,8 +1241,6 @@ class VirtualMachine extends EventEmitter { * @return {!Promise} Promise that resolves when the extensions and blocks have been added. */ shareBlocksToTarget (blocks, targetId, optFromTargetId) { - const sb3 = require('./serialization/sb3'); - const copiedBlocks = JSON.parse(JSON.stringify(blocks)); newBlockIds(copiedBlocks); const target = this.runtime.getTargetById(targetId); @@ -1583,4 +1575,4 @@ class VirtualMachine extends EventEmitter { } } -module.exports = VirtualMachine; +export default VirtualMachine;