Skip to content

Commit

Permalink
fix(dynamicInstantiate): works on Windows
Browse files Browse the repository at this point in the history
closes #39
  • Loading branch information
yoursunny committed May 14, 2020
1 parent 9b6a779 commit 7a98c38
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"pretry": "",
"try": "node --loader ./out/dist/index.js --experimental-specifier-resolution=node ./test/",
"prepublishOnly": "npm run build",
"prettier": "prettier --config .prettierrc --check 'src/**/*ts' 'bin/**/*.ts'"
"prettier": "prettier --config .prettierrc --check \"src/**/*ts\" \"bin/**/*.ts\""
},
"author": {
"email": "me@kristianjones.dev",
Expand Down
33 changes: 16 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,38 +69,37 @@ export async function resolve(
* @param url fileURL given by Node.JS
*/
export async function dynamicInstantiate(url: string) {
const moduleUrl = new URL(url);
const urlParts = url.split('/node_modules/');

const pathParts = moduleUrl.pathname.split('node_modules/');
const specifier = pathParts.pop()!;
const nodeModulesBase = pathParts.join('node_modules/');
// Extract the module name after node_modules.
const moduleName = urlParts.pop()!;

const nodeModuleUrl = new URL('node_modules', pathToFileURL(nodeModulesBase));
// With NPM, this is just top-level node_modules.
// With PNPM, this is the innermost node_modules.
const nodeModulesPath = urlParts.join('/node_modules/');

// Create a Node.JS Require using the `node_modules` folder as the base URL.
const require = createRequire(nodeModuleUrl);
// Create a require function next to node_module, and import the CommonJS module.
const require = createRequire(`${nodeModulesPath}/noop.js`);
let dynModule = require(moduleName);

// Import the module file path
let dynModule = require(specifier);

/**
* This is needed to allow for default exports in CommonJS modules.
*/
if (dynModule.default && dynModule !== dynModule.default)
// Adapt to default exports in CommonJS module.
if (dynModule.default && dynModule !== dynModule.default) {
dynModule = {
...dynModule.default,
...dynModule,
};
}

// Export as ES Module.
const linkKeys = Object.keys(dynModule);
const exports = dynModule.default ? linkKeys : [...linkKeys, 'default'];

return {
exports,
execute: (module: any) => {
module.default.set(dynModule);
// For all elements in the import set the module's key.
for (const linkKey of linkKeys) module[linkKey].set(dynModule[linkKey]);
for (const linkKey of linkKeys) {
module[linkKey].set(dynModule[linkKey]);
}
},
};
}
Expand Down

0 comments on commit 7a98c38

Please sign in to comment.