зеркало из https://github.com/microsoft/rnx-kit.git
fix(metro-serializer-esbuild): conditionally add lodash transformer (#391)
This commit is contained in:
Родитель
a131d68b77
Коммит
0de555701c
|
@ -33,8 +33,13 @@ jobs:
|
|||
git diff | GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }} yarn suggestion-bot
|
||||
- name: Bundle packages
|
||||
run: yarn bundle:ci
|
||||
- name: Bundle packages with esbuild
|
||||
run: yarn bundle+esbuild
|
||||
- name: Bundle test app with esbuild
|
||||
run: |
|
||||
sed -i'' 's/"experimental_treeShake": false/"experimental_treeShake": true/' package.json
|
||||
yarn bundle+esbuild
|
||||
sed -i'' 's/"experimental_treeShake": true/"experimental_treeShake": false/' package.json
|
||||
shell: bash
|
||||
working-directory: packages/test-app
|
||||
review:
|
||||
name: "Review"
|
||||
runs-on: ubuntu-latest
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"type": "patch",
|
||||
"comment": "Conditionally add lodash transformer",
|
||||
"packageName": "@rnx-kit/metro-serializer-esbuild",
|
||||
"email": "4123478+tido64@users.noreply.github.com",
|
||||
"dependentChangeType": "patch"
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"type": "patch",
|
||||
"comment": "Enable esbuild sourcemaps",
|
||||
"packageName": "@rnx-kit/metro-serializer-esbuild",
|
||||
"email": "4123478+tido64@users.noreply.github.com",
|
||||
"dependentChangeType": "patch"
|
||||
}
|
|
@ -2,7 +2,7 @@ import { info, warn } from "@rnx-kit/console";
|
|||
import type { MetroPlugin } from "@rnx-kit/metro-serializer";
|
||||
import type { BuildOptions, BuildResult, Plugin } from "esbuild";
|
||||
import * as esbuild from "esbuild";
|
||||
import type { Graph, Module, SerializerOptions } from "metro";
|
||||
import type { Dependencies, Graph, Module, SerializerOptions } from "metro";
|
||||
import type { SerializerConfigT } from "metro-config";
|
||||
import * as semver from "semver";
|
||||
|
||||
|
@ -43,6 +43,16 @@ function fixSourceMap(outputPath: string, text: string): string {
|
|||
return JSON.stringify({ ...sourcemap, sources });
|
||||
}
|
||||
|
||||
function isImporting(moduleName: string, dependencies: Dependencies): boolean {
|
||||
const iterator = dependencies.keys();
|
||||
for (let key = iterator.next(); !key.done; key = iterator.next()) {
|
||||
if (key.value.includes(moduleName)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function isRedundantPolyfill(modulePath: string): boolean {
|
||||
// __prelude__: The content of `__prelude__` is passed to esbuild with `define`
|
||||
// polyfills/require.js: `require` is already provided by esbuild
|
||||
|
@ -57,7 +67,7 @@ function outputOf(module: Module | undefined): string | undefined {
|
|||
* esbuild bundler for Metro.
|
||||
*/
|
||||
export function MetroSerializer(
|
||||
plugins: MetroPlugin[] = [],
|
||||
metroPlugins: MetroPlugin[] = [],
|
||||
buildOptions?: Options
|
||||
): SerializerConfigT["customSerializer"] {
|
||||
assertVersion(">=0.66.1");
|
||||
|
@ -68,7 +78,9 @@ export function MetroSerializer(
|
|||
graph: Graph,
|
||||
options: SerializerOptions
|
||||
): ReturnType<Required<SerializerConfigT>["customSerializer"]> => {
|
||||
plugins.forEach((plugin) => plugin(entryPoint, preModules, graph, options));
|
||||
metroPlugins.forEach((plugin) =>
|
||||
plugin(entryPoint, preModules, graph, options)
|
||||
);
|
||||
|
||||
const { dependencies } = graph;
|
||||
const metroPlugin: Plugin = {
|
||||
|
@ -159,8 +171,6 @@ export function MetroSerializer(
|
|||
},
|
||||
};
|
||||
|
||||
const lodashTransformer = require("esbuild-plugin-lodash");
|
||||
|
||||
// `outfile` is only meant to give esbuild a name it can use to generate
|
||||
// the sourcemap and insert it into `BuildResult["outputFiles"]`. We've
|
||||
// disabled writing to disk by setting `write: false`. Metro will handle
|
||||
|
@ -168,6 +178,12 @@ export function MetroSerializer(
|
|||
const outfile = "main.jsbundle";
|
||||
const sourcemapfile = outfile + ".map";
|
||||
|
||||
const plugins = [metroPlugin];
|
||||
if (isImporting("lodash", dependencies)) {
|
||||
const lodashTransformer = require("esbuild-plugin-lodash");
|
||||
plugins.push(lodashTransformer());
|
||||
}
|
||||
|
||||
return esbuild
|
||||
.build({
|
||||
bundle: true,
|
||||
|
@ -193,7 +209,7 @@ export function MetroSerializer(
|
|||
logLevel: buildOptions?.logLevel ?? "error",
|
||||
minify: buildOptions?.minify ?? !options.dev,
|
||||
outfile,
|
||||
plugins: [metroPlugin, lodashTransformer()],
|
||||
plugins,
|
||||
sourcemap: "external",
|
||||
write: false,
|
||||
})
|
||||
|
|
Загрузка…
Ссылка в новой задаче