Skip to content

Commit

Permalink
Merge pull request #59 from yoursunny/win32
Browse files Browse the repository at this point in the history
fix(dynamicInstantiate): works on Windows
  • Loading branch information
EntraptaJ authored May 14, 2020
2 parents 9b6a779 + 5e4a269 commit ce981c9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/Push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ on: [push, pull_request]
jobs:
Test:
name: Tests
runs-on: ubuntu-latest
strategy:
matrix:
node: ['13.9', '13.10', '13.11']
os: ['ubuntu-latest', 'windows-latest']
node: ['13.9', '13.10', '13.11', '14.x']
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- uses: actions/[email protected]
Expand Down
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": "[email protected]",
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 ce981c9

Please sign in to comment.