Skip to content

Commit

Permalink
fix: upgrade core & filter empty input & cjs eval polyfill (#45)
Browse files Browse the repository at this point in the history
* fix: upgrade core & filter empty input & cjs eval polyfill

* fix: remove unused field
  • Loading branch information
shulandmimi authored Oct 27, 2024
1 parent facbedc commit 5176cc7
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-llamas-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'farmup': patch
---

upgrade core & filter empty input & cjs eval polyfill
1 change: 1 addition & 0 deletions packages/core/farm.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default defineConfig({
output: {
targetEnv: 'node',
format: 'esm',
clean: true,
},
script: {
nativeTopLevelAwait: true,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"email": "[email protected]"
},
"dependencies": {
"@farmfe/core": "^1.3.22",
"@farmfe/core": "^1.3.29",
"tmp": "^0.2.3"
}
}
28 changes: 15 additions & 13 deletions packages/core/src/node/cjs/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ ipc.onMessage((data) => {
}

start();

ipc.close();
});

Expand All @@ -34,9 +33,10 @@ ipc.start(FARM_ESM_RESOURCE_MOCK_PORT!);
function createVmContextByModule(newM: Module) {
return vm.createContext(
{
__farm__filename: newM.filename,
__farm__dirname: path.dirname(newM.filename),
__farm_m: newM,
__filename: newM.filename,
__dirname: path.dirname(newM.filename),
module: newM,
exports: newM.exports,
},
{},
);
Expand Down Expand Up @@ -105,13 +105,13 @@ function createContext(_ctx: vm.Context, require: NodeRequire) {
}

function executeCode(code: string, ctx: ModuleContext) {
vm.runInNewContext(
`
(function(exports, require, module, __filename, __dirname) {
${code}
})(__farm_m.exports, require, __farm_m, __farm__filename, __farm__dirname);`.trim(),
ctx.context,
{
ctx.context.eval = (code: string) => {
return executeCode(code, ctx);
};

try {
return vm.runInNewContext(`${code}`.trim(), ctx.context, {
filename: ctx.filename,
// @ts-ignore
async importModuleDynamically(specifier, script, importAttributes) {
let exports: Record<string, unknown> | undefined;
Expand Down Expand Up @@ -143,8 +143,10 @@ function executeCode(code: string, ctx: ModuleContext) {
return m;
}
},
},
);
});
} catch (error) {
console.error(error);
}
}

function nativeModuleCache(m: Module, r: NodeRequire, importer?: string) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/node/esm/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class Register {

resource(url: string) {
const absolutePath = path.join(this.resources.root, this.resources.outputDir);

const relativePath = url.startsWith('file://') ? path.relative(absolutePath, fileURLToPath(url)) : url;

return this.resources?.resources[relativePath];
}

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/plugins/auto-execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default function autoExecute(options: CommonOptions = {}, logger = defaul
return;
}

const resourceOutputEntry = Object.keys(normalizeOption.options.entry)[0];
const resourceOutputEntry = Object.entries(normalizeOption.options.entry).filter((item) => item[1])[0]?.[0];

if (!resourceOutputEntry) {
logger.error('output entry is not found');
Expand All @@ -48,7 +48,7 @@ export default function autoExecute(options: CommonOptions = {}, logger = defaul
executer = new Executer(normalizeOption.options.execute, logger, normalizeOption.options);
}

const nameWithoutExt = path.parse(resourceOutputEntry).name;
const nameWithoutExt = path.parse(resourceOutputEntry).base;

if (normalizeOption.options.execute.type === ExecuteMode.Node && options.experienceScript) {
if (!ipcServer) {
Expand Down
147 changes: 145 additions & 2 deletions pnpm-lock.yaml

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

0 comments on commit 5176cc7

Please sign in to comment.