Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save brendanmoore/0336e84444237ad1a8d41d3968f22cf4 to your computer and use it in GitHub Desktop.

Select an option

Save brendanmoore/0336e84444237ad1a8d41d3968f22cf4 to your computer and use it in GitHub Desktop.

Revisions

  1. brendanmoore revised this gist Jan 23, 2018. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions babel-plugin-dynamic-import-node-sync.js
    Original file line number Diff line number Diff line change
    @@ -23,14 +23,16 @@ export default function ({ template, types: t }) {
    }
    return {
    then: function(cb, errCb) {
    if (e) {
    if (e && errCb) {
    errCb(e);
    } else {
    cb(m);
    }
    },
    catch: function(cb) {
    cb(err);
    if (e) {
    cb(e);
    }
    }
    };
    })(SOURCE);`);
  2. brendanmoore revised this gist Jan 23, 2018. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion babel-plugin-dynamic-import-node-sync.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,10 @@
    // Based on https://github.com/airbnb/babel-plugin-dynamic-import-node
    /**
    * Please note this code is just a proof of concept and should not
    * be used in production!
    *
    * Based on https://github.com/airbnb/babel-plugin-dynamic-import-node
    */

    import syntax from 'babel-plugin-syntax-dynamic-import';

    export default function ({ template, types: t }) {
  3. brendanmoore revised this gist Jan 23, 2018. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions babel-plugin-dynamic-import-node-sync.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,13 @@
    // Based on https://github.com/airbnb/babel-plugin-dynamic-import-node
    import syntax from 'babel-plugin-syntax-dynamic-import';

    export default function ({ template, types: t }) {
    // Uses a hacky "thennable" return object that invokes the callbacks synchronusly,
    // Will not work if the import function does follow up `.then/.catch`
    //
    // i.e.
    // import().then(...) // <-- Should be fine
    // import().then(...).then(...) // <-- Nope
    const buildImport = template(` (function(modulePath) {
    var e;
    try {
  4. brendanmoore created this gist Jan 23, 2018.
    48 changes: 48 additions & 0 deletions babel-plugin-dynamic-import-node-sync.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    import syntax from 'babel-plugin-syntax-dynamic-import';

    export default function ({ template, types: t }) {
    const buildImport = template(` (function(modulePath) {
    var e;
    try {
    var m = require(modulePath);
    } catch (err) {
    e = err;
    }
    return {
    then: function(cb, errCb) {
    if (e) {
    errCb(e);
    } else {
    cb(m);
    }
    },
    catch: function(cb) {
    cb(err);
    }
    };
    })(SOURCE);`);

    return {
    inherits: syntax,

    visitor: {
    Import(path) {
    const importArguments = path.parentPath.node.arguments;
    const isString = t.isStringLiteral(importArguments[0])
    || t.isTemplateLiteral(importArguments[0]);
    if (isString) {
    t.removeComments(importArguments[0]);
    }
    const newImport = buildImport({
    SOURCE: (isString)
    ? importArguments
    : t.templateLiteral([
    t.templateElement({ raw: '', cooked: '' }),
    t.templateElement({ raw: '', cooked: '' }, true),
    ], importArguments),
    });
    path.parentPath.replaceWith(newImport);
    },
    },
    };
    }