Skip to content

Commit

Permalink
feat: Modify jsMeta to metafile option (remix-run#6441)
Browse files Browse the repository at this point in the history
  • Loading branch information
19Qingfeng committed May 24, 2023
1 parent cc3059e commit 9c48eed
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
21 changes: 12 additions & 9 deletions packages/remix-dev/compiler/utils/analyzer.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { resolve } from 'path'
import { resolve, normalize } from 'path'
import fs from 'fs-extra'
import type { Metafile } from 'esbuild';

import type { Context } from "../context";
import invariant from "../../invariant";


const JS_META_FILE_NAME = 'remix-js-metafile.json';
const JS_META_FILE_NAME = 'browser-metafile.json';


const getMetaPath = (target: string, filename: string) => resolve(target, filename);
const getMetaPath = (target: string, filename: string) => normalize(resolve(target, filename));

/**
* Generate metafile for esbuild analyze
Expand All @@ -20,17 +20,20 @@ const createMetaFile = (ctx: Context) => {
config
} = ctx;
let {
browserMeta,
assetsBuildDirectory
metafile,
debugDirectory
} = config
return {
createBrowserMetaFile: (outputMeta?: Metafile) => {
if (browserMeta && outputMeta) {
if (metafile && outputMeta) {
try {
let output = getMetaPath(assetsBuildDirectory, JS_META_FILE_NAME);
return fs.writeFile(output, JSON.stringify(outputMeta));
let output = getMetaPath(debugDirectory, JS_META_FILE_NAME);
if (!fs.existsSync(debugDirectory)) {
fs.mkdirSync(debugDirectory)
}
return fs.writeFile(normalize(output), JSON.stringify(outputMeta));
} catch (e) {
invariant(e, `Failed to generate ${JS_META_FILE_NAME}.`);
invariant(e, `Failed to generate ${JS_META_FILE_NAME} in ${debugDirectory}.`);
}
}
}
Expand Down
28 changes: 23 additions & 5 deletions packages/remix-dev/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,15 @@ export interface AppConfig {
*/
assetsBuildDirectory?: string;

/**
* The path to the browser metafile build, relative to `remix.config.js`. Defaults * to "public/.debug".
*/
debugDirectory?: string

/**
* Whether to support generate browser js metafile for esbuild, default to false.
*/
browserMeta?: boolean;
metafile?: boolean;

/**
* The path to the browser build, relative to remix.config.js. Defaults to
Expand Down Expand Up @@ -273,11 +278,16 @@ export interface RemixConfig {
*/
assetsBuildDirectory: string;

/**
* The absolute path to the metafile build directory.
*/
debugDirectory: string

/**
* Whether to support generate browser js metafile for esbuild, default to false.
* When you set it to true, remix-js-metafile.json will be generated in the assetsBuildDirectory directory.
*/
browserMeta?: boolean;
metafile?: boolean;

/**
* the original relative path to the assets build directory
Expand Down Expand Up @@ -658,8 +668,6 @@ export async function readConfig(
? path.resolve(appDirectory, userEntryServerFile)
: path.resolve(defaultsDirectory, entryServerFile);

let browserMeta = !!appConfig.browserMeta

if (appConfig.browserBuildDirectory) {
warnOnce(browserBuildDirectoryWarning, "browserBuildDirectory");
}
Expand All @@ -669,11 +677,20 @@ export async function readConfig(
appConfig.browserBuildDirectory ||
path.join("public", "build");


let absoluteAssetsBuildDirectory = path.resolve(
rootDirectory,
assetsBuildDirectory
);

let absoluteDebugDirectory = path.resolve(
rootDirectory,
appConfig.debugDirectory || path.join("public", ".debugger")
);

let metafile = !!appConfig.metafile


let devServerPort =
Number(process.env.REMIX_DEV_SERVER_WS_PORT) ||
(await getPort({ port: Number(appConfig.devServerPort) || 8002 }));
Expand Down Expand Up @@ -762,7 +779,8 @@ export async function readConfig(
entryClientFilePath,
entryServerFile,
entryServerFilePath,
browserMeta,
debugDirectory: absoluteDebugDirectory,
metafile,
devServerPort,
devServerBroadcastDelay,
assetsBuildDirectory: absoluteAssetsBuildDirectory,
Expand Down

0 comments on commit 9c48eed

Please sign in to comment.