Skip to content

Standardize synchronous circular requires #6

@jsinterface

Description

@jsinterface

Synchronous circular requires (commonjs equivalent of static default imports)
are individually edited in some sources (CSSOM, JSDOM),
but a generic pattern can be recognized.

The edits are always a delayed reassignment from dynamic import
(const a=require("a") -> let a;setTimeout(async function expect_hoisted_a()
{import("a").then(({default:module})=>a=module)},3000)
- note that dynamic import is only supported for requires in already async scopes; see issue #9 ),
supported by an equal timeout before the default export of module.exports in the
entry module since the dynamic imports become promise resolutions of hoisted variables
of co-dependent modules stacked lower in the module scope when bundled:

// entry module: 
await new Promise(resolve=>setTimeout(resolve,3000));
export default module.exports;

// dependent module: 
var codependent;
setTimeout(async function expect_hoisted_dependency()
{import("codependent").then(({default:module})=>codependent=module)
},3000);
export default module.exports;

// codependent module: 
import dependent from "dependent";
module.exports={dependent}
export default module.exports;

// bundle: 
var codependent;
setTimeout(async function expect_hoisted_a()
{Promise.resolve(dependency).then(({default:module})=>codependent=module)
},3000);
var dependent={default};
var dependency={default:dependent};
await new Promise(resolve=>setTimeout(resolve,3000));
export default module.exports;

Collecting and awaiting the dynamic import reassignments instead of the arbitrary delay at the end of bundle,
as well as gaining access to rollup's hoisted variable name to recursively ascertain the local reassignments,
or potential modifications to the module hierarchy could be further improvements to this generalization.

An illustrative subject circular dependency graph (CSSOM):
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions