Fix dynamic import() for cjs format in rollup + get rid of getPackageAndTask usage outside target-graph package (#480)

* fixing real imports with rollup as well as getting rid of getPackageAndTask implementation details

* revert changes to basic test

* fixing test

* Change files
This commit is contained in:
Kenneth Chau 2022-11-09 11:32:46 -08:00 коммит произвёл GitHub
Родитель 9a11cfd194
Коммит 735b0483e4
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
13 изменённых файлов: 96 добавлений и 25 удалений

Просмотреть файл

@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fixing real imports with rollup as well as getting rid of getPackageAndTask implementation details",
"packageName": "@lage-run/lage",
"email": "kchau@microsoft.com",
"dependentChangeType": "patch"
}

Просмотреть файл

@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "get rid of getPackageAndTask references",
"packageName": "@lage-run/reporters",
"email": "kchau@microsoft.com",
"dependentChangeType": "patch"
}

Просмотреть файл

@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fixing real imports with rollup as well as getting rid of getPackageAndTask implementation details",
"packageName": "@lage-run/scheduler",
"email": "kchau@microsoft.com",
"dependentChangeType": "patch"
}

Просмотреть файл

@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "marking getPackageAndTask as internal - it was an unintentional export.",
"packageName": "@lage-run/target-graph",
"email": "kchau@microsoft.com",
"dependentChangeType": "patch"
}

Просмотреть файл

@ -96,7 +96,7 @@ describe("transitive task deps test", () => {
expect(indices[getTargetId("b", "transpile")]).toBeUndefined();
expect(indices[getTargetId("c", "transpile")]).toBeUndefined();
// repo.cleanup();
repo.cleanup();
});
it("only runs direct dependencies for ^ prefix dependencies -- ", () => {

Просмотреть файл

@ -3,6 +3,7 @@ import commonjs from "@rollup/plugin-commonjs";
import json from "@rollup/plugin-json";
import { terser } from "rollup-plugin-terser";
import alias from "@rollup/plugin-alias";
import { retainDynamicImport } from "./scripts/retain-dynamic-import-plugin.js";
export default [
{
@ -30,10 +31,11 @@ export default [
ignoreDynamicRequires: true,
}),
json(),
retainDynamicImport(),
terser(),
],
external: ["fsevents"],
inlineDynamicImports: true
inlineDynamicImports: true,
},
{
input: "./index.js",
@ -55,7 +57,7 @@ export default [
json(),
terser(),
],
inlineDynamicImports: true
inlineDynamicImports: true,
},
{
input: "@lage-run/scheduler/lib/workers/targetWorker.js",
@ -75,8 +77,9 @@ export default [
ignoreDynamicRequires: true,
}),
json(),
retainDynamicImport(),
terser(),
],
inlineDynamicImports: true
inlineDynamicImports: true,
},
];

Просмотреть файл

@ -0,0 +1,19 @@
export function retainDynamicImport() {
return {
name: 'retain-dynamic-import',
resolveDynamicImport(specifier) {
if (typeof specifier === 'string') {
return null;
}
return false;
},
renderDynamicImport(entry) {
if (!entry.targetModuleId) {
return {
left: 'import(',
right: ')'
};
}
}
};
}

Просмотреть файл

@ -1,5 +1,4 @@
import { formatDuration, hrToSeconds } from "@lage-run/format-hrtime";
import { getPackageAndTask } from "@lage-run/target-graph";
import { isTargetStatusLogEntry } from "./isTargetStatusLogEntry.js";
import { LogLevel } from "@lage-run/logger";
import chalk from "chalk";
@ -202,15 +201,19 @@ export class AdoReporter implements Reporter {
if (failed && failed.length > 0) {
for (const targetId of failed) {
const { packageName, task } = getPackageAndTask(targetId);
const taskLogs = this.logEntries.get(targetId);
const target = targetRuns.get(targetId)?.target;
this.logStream.write(`##[error] [${chalk.magenta(packageName)} ${chalk.cyan(task)}] ${chalk.redBright("ERROR DETECTED")}\n`);
if (target) {
const { packageName, task } = target;
const taskLogs = this.logEntries.get(targetId);
if (taskLogs) {
for (const entry of taskLogs) {
// Log each entry separately to prevent truncation
this.logStream.write(`##[error] ${entry.msg}\n`);
this.logStream.write(`##[error] [${chalk.magenta(packageName)} ${chalk.cyan(task)}] ${chalk.redBright("ERROR DETECTED")}\n`);
if (taskLogs) {
for (const entry of taskLogs) {
// Log each entry separately to prevent truncation
this.logStream.write(`##[error] ${entry.msg}\n`);
}
}
}
}

Просмотреть файл

@ -1,5 +1,4 @@
import { formatDuration, hrtimeDiff, hrToSeconds } from "@lage-run/format-hrtime";
import { getPackageAndTask } from "@lage-run/target-graph";
import { isTargetStatusLogEntry } from "./isTargetStatusLogEntry.js";
import { LogLevel } from "@lage-run/logger";
import ansiRegex from "ansi-regex";
@ -256,19 +255,23 @@ export class LogReporter implements Reporter {
if (failed && failed.length > 0) {
for (const targetId of failed) {
const { packageName, task } = getPackageAndTask(targetId);
const failureLogs = this.logEntries.get(targetId);
const target = targetRuns.get(targetId)?.target;
this.print(`[${colors.pkg(packageName ?? "<root>")} ${colors.task(task)}] ${colors[LogLevel.error]("ERROR DETECTED")}`);
if (target) {
const { packageName, task } = target;
const failureLogs = this.logEntries.get(targetId);
if (failureLogs) {
for (const entry of failureLogs) {
// Log each entry separately to prevent truncation
this.print(entry.msg);
this.print(`[${colors.pkg(packageName ?? "<root>")} ${colors.task(task)}] ${colors[LogLevel.error]("ERROR DETECTED")}`);
if (failureLogs) {
for (const entry of failureLogs) {
// Log each entry separately to prevent truncation
this.print(entry.msg);
}
}
}
this.hr();
this.hr();
}
}
}

Просмотреть файл

@ -2,6 +2,7 @@ import path from "path";
import { getStartTargetId } from "@lage-run/target-graph";
import type { Target } from "@lage-run/target-graph";
import type { TargetRunner } from "@lage-run/scheduler-types";
import { pathToFileURL } from "url";
export interface TargetRunnerPickerOptions {
[key: string]: { script: string; options: any };
@ -23,7 +24,13 @@ export class TargetRunnerPicker {
const config = this.options[target.type];
const { script, options } = config;
const runnerModule = await import(script);
let importScript = script;
if (!importScript.startsWith("file://")) {
importScript = pathToFileURL(importScript).toString();
}
const runnerModule = await import(importScript);
const base = path.basename(script);
const runnerName = base.replace(path.extname(base), "");

Просмотреть файл

@ -1,4 +1,5 @@
import type { TargetRunner, TargetRunnerOptions } from "@lage-run/scheduler-types";
import { pathToFileURL } from "url";
export interface WorkerRunnerOptions {
taskArgs: string[];
@ -53,7 +54,13 @@ export class WorkerRunner implements TargetRunner {
throw new Error('WorkerRunner: "script" configuration is required - e.g. { type: "worker", script: "./worker.js" }');
}
const scriptModule = await import(scriptFile);
let importScript = scriptFile;
if (!importScript.startsWith("file://")) {
importScript = pathToFileURL(importScript).toString();
}
const scriptModule = await import(importScript);
const runFn = typeof scriptModule.default === "function" ? scriptModule.default : scriptModule;
if (typeof runFn !== "function") {

Просмотреть файл

@ -3,6 +3,6 @@ export type { TargetGraph } from "./types/TargetGraph.js";
export type { TargetConfig } from "./types/TargetConfig.js";
export { sortTargetsByPriority } from "./sortTargetsByPriority.js";
export { getPackageAndTask, getTargetId, getStartTargetId } from "./targetId.js";
export { getTargetId, getStartTargetId } from "./targetId.js";
export { detectCycles } from "./detectCycles.js";
export { TargetGraphBuilder } from "./TargetGraphBuilder.js";

Просмотреть файл

@ -14,6 +14,7 @@ export function getTargetId(pkgName: string | undefined, task: string) {
*
* If the packageName is //, that means that the task is meant to be run at the repo root level.
*
* @internal
* @param targetId
* @returns
*/