Skip to content

Commit

Permalink
refactor: use cjs-module-lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
merceyz committed Dec 22, 2020
1 parent d523789 commit a7110bc
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 14 deletions.
10 changes: 10 additions & 0 deletions .pnp.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
1 change: 1 addition & 0 deletions packages/yarnpkg-pnp/package.json
Expand Up @@ -16,6 +16,7 @@
"@yarnpkg/builder": "workspace:^2.1.3",
"@yarnpkg/libzip": "workspace:^2.2.1",
"@yarnpkg/monorepo": "workspace:0.0.0",
"cjs-module-lexer": "^1.0.0",
"enhanced-resolve": "^5.4.1",
"rollup": "^2.35.1",
"typescript": "4.1.0-beta",
Expand Down
2 changes: 1 addition & 1 deletion packages/yarnpkg-pnp/sources/esm-loader/built-loader.js

Large diffs are not rendered by default.

39 changes: 26 additions & 13 deletions packages/yarnpkg-pnp/sources/esm-loader/loader.ts
@@ -1,6 +1,7 @@
import {parse, init} from 'cjs-module-lexer';
import {ResolverFactory, CachedInputFileSystem} from 'enhanced-resolve';
import fs from 'fs';
import {builtinModules, createRequire} from 'module';
import {builtinModules} from 'module';
import path from 'path';
import {fileURLToPath, pathToFileURL, URL} from 'url';

Expand Down Expand Up @@ -149,6 +150,16 @@ export async function getFormat(resolved: string, context: any, defaultGetFormat
throw new Error(`Unable to get module type of '${resolved}'`);
}

let parserInit: Promise<void> | null = init().then(() => {
parserInit = null;
});

async function parseExports(filePath: string) {
const {exports} = parse(await readFile(filePath));
// https://github.com/nodejs/node/blob/4d22dd329b5890387356806e45cc007b7004ee65/lib/internal/modules/esm/translators.js#L186-L188
return exports.includes(`default`) ? exports : [`default`, ...exports];
}

export async function getSource(urlString: string, context: any, defaultGetSource: any) {
const url = new URL(urlString);
if (url.protocol !== `file:`) return defaultGetSource(url, context, defaultGetSource);
Expand All @@ -161,30 +172,32 @@ export async function getSource(urlString: string, context: any, defaultGetSourc
};
}

const fakeModulePath = path.join(path.dirname(urlString), `noop.js`);
if (parserInit !== null) await parserInit;

const require = createRequire(fakeModulePath);
const dynModule = require(urlString);
const exports = await parseExports(urlString);

let exportStrings: Array<string> = [];
if (dynModule.__esModule === true) {
exportStrings = Object.getOwnPropertyNames(dynModule).map(propKey => {
if (propKey === `default`) {
return `export default cjs['default']`;
let exportStrings = ``;

if (exports.length !== 0) {
for (const exportName of exports) {
if (exportName === `default`) {
exportStrings += `export default cjs['default']\n`;
} else {
return `const __${propKey} = cjs['${propKey}'];\n export { __${propKey} as ${propKey} }`;
exportStrings += `const __${exportName} = cjs['${exportName}'];\n export { __${exportName} as ${exportName} }\n`;
}
});
}
} else {
exportStrings = [`export default cjs`];
exportStrings = `export default cjs`;
}

const fakeModulePath = path.join(path.dirname(urlString), `noop.js`);

const code = `
import {createRequire} from 'module';
const require = createRequire('${fakeModulePath.replace(/\\/g,`/`)}');
const cjs = require('${urlString.replace(/\\/g,`/`)}');
${exportStrings.join(`\n`)}
${exportStrings}
`;

return {
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Expand Up @@ -6300,6 +6300,7 @@ __metadata:
"@yarnpkg/fslib": "workspace:^2.4.0"
"@yarnpkg/libzip": "workspace:^2.2.1"
"@yarnpkg/monorepo": "workspace:0.0.0"
cjs-module-lexer: ^1.0.0
enhanced-resolve: ^5.4.1
rollup: ^2.35.1
tslib: ^1.13.0
Expand Down Expand Up @@ -8772,6 +8773,13 @@ __metadata:
languageName: node
linkType: hard

"cjs-module-lexer@npm:^1.0.0":
version: 1.0.0
resolution: "cjs-module-lexer@npm:1.0.0"
checksum: 5460ba5ac64ab7014b2263a2743a2b5e29a9add758fe22a84407581b59d916cc25758e34fb17d56d9911e27403aa7f090b474eb6e69f86c8df4aa77cb6aff908
languageName: node
linkType: hard

"class-utils@npm:^0.3.5":
version: 0.3.6
resolution: "class-utils@npm:0.3.6"
Expand Down

0 comments on commit a7110bc

Please sign in to comment.