Skip to content

Commit

Permalink
fix: mfsu should handle native module and APP_ROOT properly (umijs#6932)
Browse files Browse the repository at this point in the history
  • Loading branch information
leftstick authored Jul 9, 2021
1 parent 326920f commit b4140f4
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"main": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "";
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { winPath } from '@umijs/utils';
import { join } from 'path';
import { getFilePath } from './getFilePath';
import { getFilePath, getProperCwd } from './getFilePath';

const fixtures = join(__dirname, 'fixtures', 'getFilePath');

Expand All @@ -9,6 +9,19 @@ function format(path: string | null) {
return path.replace(winPath(fixtures), '$CWD$');
}

let oldAppRoot = process.env.APP_ROOT;
let oldCwd = process.cwd();

beforeEach(() => {
oldAppRoot = process.env.APP_ROOT;
oldCwd = process.cwd();
});

afterEach(() => {
process.env.APP_ROOT = oldAppRoot;
process.chdir(oldCwd);
});

test('file exists', () => {
expect(
format(getFilePath(join(fixtures, 'js-file-first', 'foo.js'))),
Expand Down Expand Up @@ -44,3 +57,19 @@ test('directory index mjs', () => {
format(getFilePath(join(fixtures, 'directory-index-mjs', 'foo'))),
).toEqual(`$CWD$/directory-index-mjs/foo/index.mjs`);
});

test('cwd should be APP_ROOT', () => {
process.chdir(join(fixtures, 'package-json-in-approot'));
process.env.APP_ROOT = join(fixtures, 'package-json-in-approot', 'app');
expect(format(getProperCwd(process.env.APP_ROOT))).toEqual(
`$CWD$/package-json-in-approot/app`,
);
});

test('cwd should be process.cwd()', () => {
process.chdir(join(fixtures, 'package-json-notin-approot'));
process.env.APP_ROOT = join(fixtures, 'package-json-notin-approot', 'app');
expect(format(getProperCwd(process.env.APP_ROOT))).toEqual(
`$CWD$/package-json-notin-approot`,
);
});
12 changes: 12 additions & 0 deletions packages/preset-built-in/src/plugins/features/mfsu/getFilePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,15 @@ export function getFilePath(path: string) {

return null;
}

// 当 APP_ROOT 有值,但 依赖(package.json) 仍然安装在 process.cwd() 时
// 返回正常可用的 cwd
export function getProperCwd(cwd: string) {
const rawCwd = process.cwd();
const pkgLocationWithCwd = existsSync(join(cwd, 'package.json'));

if (pkgLocationWithCwd) {
return cwd;
}
return rawCwd;
}
15 changes: 13 additions & 2 deletions packages/preset-built-in/src/plugins/features/mfsu/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from 'fs';
import { isAbsolute, join } from 'path';
import { getDepReExportContent } from './getDepReExportContent';
import { getFilePath } from './getFilePath';
import { getFilePath, getProperCwd } from './getFilePath';

export const copy = (fromDir: string, toDir: string) => {
try {
Expand Down Expand Up @@ -43,9 +43,20 @@ export const figureOutExport = async (
): Promise<string> => {
const absImportFrom = isAbsolute(importFrom)
? importFrom
: join(cwd, 'node_modules', importFrom);
: join(getProperCwd(cwd), 'node_modules', importFrom);
const filePath = getFilePath(absImportFrom);

// @ts-ignore
const isNodeBuiltinModule = !!process.binding('natives')[importFrom];

// useful while running with target = electron-renderer
if (isNodeBuiltinModule) {
return Promise.resolve(`
const _ = require('${importFrom}');
module.exports = _;
`);
}

assert(filePath, `filePath not found of ${importFrom}`);

const content = readFileSync(filePath, 'utf-8');
Expand Down

0 comments on commit b4140f4

Please sign in to comment.