From a0c6b54d5545a7468936539368bb053eaffeb5d8 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Sat, 3 Nov 2018 15:54:05 +0000 Subject: [PATCH] Override babel module loading more generally, to ensure proper state is picked up for the Syndicate extensions --- packages/syntax-playground/src/ticker.js | 4 +--- packages/syntax/TODO.md | 3 ++- packages/syntax/src/index.js | 22 +++++++++++++++++++++- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/packages/syntax-playground/src/ticker.js b/packages/syntax-playground/src/ticker.js index 71e4d53..63c8102 100644 --- a/packages/syntax-playground/src/ticker.js +++ b/packages/syntax-playground/src/ticker.js @@ -16,8 +16,7 @@ // along with this program. If not, see . //--------------------------------------------------------------------------- -const Timer = activate require("@syndicate-lang/driver-timer"); -const TimeLaterThan = Timer.TimeLaterThan; +const { TimeLaterThan } = activate require("@syndicate-lang/driver-timer"); spawn named 'ticker' { field this.counter = 0; @@ -34,4 +33,3 @@ spawn named 'ticker' { stop on (this.counter == 5); } - diff --git a/packages/syntax/TODO.md b/packages/syntax/TODO.md index a1deb5a..10e5860 100644 --- a/packages/syntax/TODO.md +++ b/packages/syntax/TODO.md @@ -1,12 +1,13 @@ - [DONE] `during/spawn` - [DONE] `during` + - [DONE] `let { TimeLaterThan } = activate require("@syndicate-lang/driver-timer");` - [DONE] `react` - [DONE] `spawn*` or similar - looks like `spawn on start { ... }` will do the trick - [DONE] activation - [DONE] remove ground dataspace syntax + - `defer` statement - `define/query` - - `let { TimeLaterThan } = activate require("@syndicate-lang/driver-timer");` - some kind of `stop facet` statement - put a Symbol on the fields blob? - web pack diff --git a/packages/syntax/src/index.js b/packages/syntax/src/index.js index 390dc8f..58a96c1 100644 --- a/packages/syntax/src/index.js +++ b/packages/syntax/src/index.js @@ -98,6 +98,7 @@ BabelParser.__setParser(require("./parser").default); // This is mostly optional, unless for some reason we want only the // syntax extension but not the transform (e.g. if the plugin omitted // its `visitor`). +var Generator = require("@babel/generator"); // needed for _load override, below var Generators = require("@babel/generator/lib/generators"); var SyndicateGenerators = require("./generators"); Object.keys(SyndicateGenerators).forEach((f) => { @@ -105,5 +106,24 @@ Object.keys(SyndicateGenerators).forEach((f) => { }); //--------------------------------------------------------------------------- -// (5) At this point, we should (?) be able to load and use Babel +// (5) Ensure that, no matter where we are when some module `require`s +// one of our patched modules, we give them the patched version. This +// is ultra disgusting. + +(function () { + const Module = require('module'); + const _oldLoad = Module._load; + Module._load = function (request, parent) { + if (!parent) return _oldLoad.apply(this, arguments); + switch (request) { + case "@babel/parser": return BabelParser; + case "@babel/types": return Types; + case "@babel/generator": return Generator; + default: return _oldLoad.apply(this, arguments); + } + } +})(); + +//--------------------------------------------------------------------------- +// (6) At this point, we should (?) be able to load and use Babel // somewhat normally.