@bleedingdev/modern-js-app-tools-extensions 3.9.0-ultramodern.19 → 3.9.0-ultramodern.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/dist/cjs/build-config/remote-address.js +12 -10
  2. package/dist/cjs/cloudflare-builder.js +5 -30
  3. package/dist/cjs/config.js +4 -0
  4. package/dist/cjs/deploy-output/plugin.js +8 -7
  5. package/dist/cjs/deploy-output/target.js +57 -1
  6. package/dist/cjs/release-envelope/plugin.js +5 -10
  7. package/dist/esm/build-config/remote-address.mjs +12 -10
  8. package/dist/esm/cloudflare-builder.mjs +6 -28
  9. package/dist/esm/config.mjs +1 -0
  10. package/dist/esm/deploy-output/plugin.mjs +9 -8
  11. package/dist/esm/deploy-output/target.mjs +50 -3
  12. package/dist/esm/release-envelope/plugin.mjs +5 -10
  13. package/dist/esm-node/build-config/remote-address.mjs +12 -10
  14. package/dist/esm-node/cloudflare-builder.mjs +6 -28
  15. package/dist/esm-node/config.mjs +1 -0
  16. package/dist/esm-node/deploy-output/plugin.mjs +9 -8
  17. package/dist/esm-node/deploy-output/target.mjs +50 -3
  18. package/dist/esm-node/release-envelope/plugin.mjs +5 -10
  19. package/dist/types/cloudflare-builder.d.ts +4 -8
  20. package/dist/types/config.d.ts +2 -1
  21. package/dist/types/deploy-output/plugin.d.ts +12 -18
  22. package/dist/types/deploy-output/target.d.ts +28 -5
  23. package/dist/types/release-envelope/plugin.d.ts +3 -7
  24. package/package.json +17 -16
  25. package/src/build-config/remote-address.ts +11 -9
  26. package/src/cloudflare-builder.ts +11 -55
  27. package/src/config.ts +2 -7
  28. package/src/deploy-output/plugin.ts +20 -37
  29. package/src/deploy-output/target.ts +97 -14
  30. package/src/release-envelope/plugin.ts +10 -40
  31. package/dist/cjs/templates/cloudflare-worker-mf-ssr-runtime-plugin.mjs +0 -14
  32. package/dist/esm/templates/cloudflare-worker-mf-ssr-runtime-plugin.mjs +0 -14
  33. package/dist/esm-node/templates/cloudflare-worker-mf-ssr-runtime-plugin.mjs +0 -14
  34. package/dist/types/templates/cloudflare-worker-mf-ssr-runtime-plugin.d.mts +0 -13
  35. package/src/templates/cloudflare-worker-mf-ssr-runtime-plugin.mjs +0 -14
package/src/config.ts CHANGED
@@ -4,13 +4,8 @@ export {
4
4
  resolveEffectTsgoCompiler,
5
5
  } from './build-config/public';
6
6
  export { createRemoteManifestUrl } from './build-config/remote-address';
7
-
8
- export type DeployTarget =
9
- | 'node'
10
- | 'vercel'
11
- | 'netlify'
12
- | 'ghPages'
13
- | 'cloudflare';
7
+ export type { DeployTarget } from './deploy-output/target';
8
+ export { resolveDeployTarget } from './deploy-output/target';
14
9
 
15
10
  export type CloudflareWorkerSecurityCspMode = 'enforce' | 'report-only' | 'off';
16
11
 
@@ -7,35 +7,28 @@ import {
7
7
  normalizeDeclaredPublicAssets,
8
8
  stageDeclaredPublicAssets,
9
9
  } from './public-assets';
10
- import {
11
- type DeployOutputConfig,
12
- resolveDeployTarget as defaultResolveDeployTarget,
13
- } from './target';
10
+ import { getDeployingTarget, type ResolvedDeployTarget } from './target';
11
+
12
+ export interface DeployOutputAppContext {
13
+ appDirectory: string;
14
+ metaName: string;
15
+ deployTarget?: ResolvedDeployTarget;
16
+ }
14
17
 
15
18
  export interface DeployOutputPluginApi {
16
- getAppContext(): { appDirectory: string; metaName: string };
17
- getNormalizedConfig(): DeployOutputConfig;
19
+ getAppContext(): DeployOutputAppContext;
18
20
  onAfterDeploy(handler: () => Promise<void>): void;
19
21
  }
20
22
 
21
- export const createDeployOutputAliasesPlugin = ({
22
- resolveDeployTarget = defaultResolveDeployTarget,
23
- }: {
24
- resolveDeployTarget?: (config: DeployOutputConfig) => string;
25
- } = {}) => ({
23
+ export const createDeployOutputAliasesPlugin = () => ({
26
24
  name: '@modern-js/deploy-output-aliases',
27
25
  setup(api: DeployOutputPluginApi) {
28
26
  api.onAfterDeploy(async () => {
29
- const { appDirectory, metaName } = api.getAppContext();
30
- const config = api.getNormalizedConfig();
31
- if (
32
- (metaName !== 'modern-js' &&
33
- !config.deploy?.target &&
34
- !process.env.MODERNJS_DEPLOY) ||
35
- resolveDeployTarget(config) !== 'node'
36
- ) {
27
+ const appContext = api.getAppContext();
28
+ if (getDeployingTarget(appContext) !== 'node') {
37
29
  return;
38
30
  }
31
+ const { appDirectory } = appContext;
39
32
 
40
33
  const entry = createRequire(__filename).resolve('@modern-js/prod-server');
41
34
  const identity = await readPackageIdentity(entry);
@@ -54,14 +47,12 @@ export const createDeployOutputAliasesPlugin = ({
54
47
  },
55
48
  });
56
49
 
57
- export interface DeployOutputPublicAssetsConfig extends DeployOutputConfig {
58
- deploy?: DeployOutputConfig['deploy'] & {
59
- node?: { publicAssets?: NodePublicAssetConfig[] };
60
- };
50
+ export interface DeployOutputPublicAssetsConfig {
51
+ deploy?: { node?: { publicAssets?: NodePublicAssetConfig[] } };
61
52
  }
62
53
 
63
54
  export interface DeployOutputPublicAssetsPluginApi {
64
- getAppContext(): { appDirectory: string; metaName: string };
55
+ getAppContext(): DeployOutputAppContext;
65
56
  getNormalizedConfig(): DeployOutputPublicAssetsConfig;
66
57
  onAfterDeploy(handler: () => Promise<void>): void;
67
58
  }
@@ -70,24 +61,16 @@ export interface DeployOutputPublicAssetsPluginApi {
70
61
  * Stage `deploy.node.publicAssets` into the Node deploy output. Cloudflare
71
62
  * stages `deploy.worker.publicAssets` inside its deploy preset.
72
63
  */
73
- export const createDeployOutputPublicAssetsPlugin = ({
74
- resolveDeployTarget = defaultResolveDeployTarget,
75
- }: {
76
- resolveDeployTarget?: (config: DeployOutputConfig) => string;
77
- } = {}) => ({
64
+ export const createDeployOutputPublicAssetsPlugin = () => ({
78
65
  name: '@modern-js/deploy-output-public-assets',
79
66
  setup(api: DeployOutputPublicAssetsPluginApi) {
80
67
  api.onAfterDeploy(async () => {
81
- const { appDirectory, metaName } = api.getAppContext();
82
- const config = api.getNormalizedConfig();
83
- if (
84
- (metaName !== 'modern-js' &&
85
- !config.deploy?.target &&
86
- !process.env.MODERNJS_DEPLOY) ||
87
- resolveDeployTarget(config) !== 'node'
88
- ) {
68
+ const appContext = api.getAppContext();
69
+ if (getDeployingTarget(appContext) !== 'node') {
89
70
  return;
90
71
  }
72
+ const { appDirectory } = appContext;
73
+ const config = api.getNormalizedConfig();
91
74
 
92
75
  await stageDeclaredPublicAssets({
93
76
  appDirectory,
@@ -1,10 +1,19 @@
1
- import { provider } from 'std-env';
1
+ import { getArgv } from '@modern-js/utils';
2
+ import { provider as detectedProvider } from 'std-env';
2
3
 
3
- export interface DeployOutputConfig {
4
- deploy?: { target?: string };
5
- }
4
+ export const DEPLOY_TARGETS = [
5
+ 'node',
6
+ 'vercel',
7
+ 'netlify',
8
+ 'ghPages',
9
+ 'cloudflare',
10
+ ] as const;
11
+
12
+ export type DeployTarget = (typeof DEPLOY_TARGETS)[number];
6
13
 
7
- const providerDeployTargets: Partial<Record<string, string>> = {
14
+ export const DEPLOY_TARGET_FLAG = '--deploy-target';
15
+
16
+ const providerDeployTargets: Partial<Record<string, DeployTarget>> = {
8
17
  vercel: 'vercel',
9
18
  netlify: 'netlify',
10
19
  cloudflare: 'cloudflare',
@@ -12,12 +21,86 @@ const providerDeployTargets: Partial<Record<string, string>> = {
12
21
  cloudflare_workers: 'cloudflare',
13
22
  };
14
23
 
15
- export const resolveDeployTarget = (
16
- config: DeployOutputConfig,
17
- envDeployTarget = process.env.MODERNJS_DEPLOY,
18
- detectedProvider = provider,
19
- ): string =>
20
- config.deploy?.target ||
21
- envDeployTarget ||
22
- (detectedProvider ? providerDeployTargets[detectedProvider] : undefined) ||
23
- 'node';
24
+ export interface ResolvedDeployTarget {
25
+ target: DeployTarget;
26
+ /** The flag, `deploy.target` or `MODERNJS_DEPLOY` chose the target. */
27
+ explicit: boolean;
28
+ }
29
+
30
+ export interface ResolveDeployTargetOptions {
31
+ argv?: readonly string[];
32
+ configTarget?: string;
33
+ env?: string;
34
+ provider?: string;
35
+ }
36
+
37
+ const readDeployTargetFlag = (argv: readonly string[]) => {
38
+ for (const [index, arg] of argv.entries()) {
39
+ if (arg === DEPLOY_TARGET_FLAG) {
40
+ const value = argv[index + 1];
41
+ if (value === undefined || value.startsWith('-')) {
42
+ throw new Error(`${DEPLOY_TARGET_FLAG} needs a value.`);
43
+ }
44
+ return value;
45
+ }
46
+ if (arg.startsWith(`${DEPLOY_TARGET_FLAG}=`)) {
47
+ return arg.slice(DEPLOY_TARGET_FLAG.length + 1);
48
+ }
49
+ }
50
+ return undefined;
51
+ };
52
+
53
+ const assertDeployTarget = (target: string, source: string): DeployTarget => {
54
+ if (!(DEPLOY_TARGETS as readonly string[]).includes(target)) {
55
+ throw new Error(
56
+ `Unknown deploy target '${target}' from ${source}. Use one of: ${DEPLOY_TARGETS.join(', ')}.`,
57
+ );
58
+ }
59
+ return target as DeployTarget;
60
+ };
61
+
62
+ /**
63
+ * The one deploy-target resolver: `--deploy-target` > `deploy.target` >
64
+ * `MODERNJS_DEPLOY` > detected CI provider > `node`. app-tools stores the
65
+ * result in the app context once; config files that branch on the target call
66
+ * it without `configTarget`.
67
+ */
68
+ export const resolveDeployTarget = ({
69
+ argv = getArgv(),
70
+ configTarget,
71
+ env = process.env.MODERNJS_DEPLOY,
72
+ provider = detectedProvider,
73
+ }: ResolveDeployTargetOptions = {}): ResolvedDeployTarget => {
74
+ const explicit = (
75
+ [
76
+ [readDeployTargetFlag(argv), DEPLOY_TARGET_FLAG],
77
+ [configTarget, 'deploy.target'],
78
+ [env, 'MODERNJS_DEPLOY'],
79
+ ] as const
80
+ ).find(([target]) => target);
81
+ if (explicit?.[0]) {
82
+ return {
83
+ target: assertDeployTarget(explicit[0], explicit[1]),
84
+ explicit: true,
85
+ };
86
+ }
87
+ return {
88
+ target: (provider && providerDeployTargets[provider]) || 'node',
89
+ explicit: false,
90
+ };
91
+ };
92
+
93
+ /**
94
+ * The target `modern deploy` stages. Apps under another CLI meta name deploy
95
+ * only when a flag, config or env chose the target explicitly.
96
+ */
97
+ export const getDeployingTarget = ({
98
+ deployTarget,
99
+ metaName,
100
+ }: {
101
+ deployTarget?: ResolvedDeployTarget;
102
+ metaName: string;
103
+ }): DeployTarget | undefined =>
104
+ deployTarget && (metaName === 'modern-js' || deployTarget.explicit)
105
+ ? deployTarget.target
106
+ : undefined;
@@ -5,6 +5,10 @@ import {
5
5
  normalizeDeclaredPublicAssets,
6
6
  resolveAddedDeclaredPublicAssetPaths,
7
7
  } from '../deploy-output/public-assets';
8
+ import {
9
+ getDeployingTarget,
10
+ type ResolvedDeployTarget,
11
+ } from '../deploy-output/target';
8
12
  import {
9
13
  emitFrameworkMicroVerticalReleaseEnvelope,
10
14
  emitNodeStagedReleaseEnvelope,
@@ -12,13 +16,8 @@ import {
12
16
  verifyNodeReleaseEnvelopeStaging,
13
17
  } from './framework-output';
14
18
 
15
- type ReleaseEnvelopeTarget = 'cloudflare' | 'node' | string;
16
-
17
19
  export interface ReleaseEnvelopeConfig {
18
- deploy?: {
19
- target?: string;
20
- node?: { publicAssets?: NodePublicAssetConfig[] };
21
- };
20
+ deploy?: { node?: { publicAssets?: NodePublicAssetConfig[] } };
22
21
  }
23
22
 
24
23
  export interface ReleaseEnvelopeAppContext {
@@ -26,6 +25,7 @@ export interface ReleaseEnvelopeAppContext {
26
25
  appDirectory: string;
27
26
  distDirectory: string;
28
27
  metaName: string;
28
+ deployTarget?: ResolvedDeployTarget;
29
29
  }
30
30
 
31
31
  export interface ReleaseEnvelopePluginApi<
@@ -47,33 +47,9 @@ export interface ReleaseEnvelopePlugin<
47
47
  setup(api: ReleaseEnvelopePluginApi<Config>): void;
48
48
  }
49
49
 
50
- export type ResolveDeployTarget<
51
- Config extends ReleaseEnvelopeConfig = ReleaseEnvelopeConfig,
52
- > = (config: Config) => ReleaseEnvelopeTarget;
53
-
54
- const resolveActiveDeployTarget = <Config extends ReleaseEnvelopeConfig>(
55
- api: ReleaseEnvelopePluginApi<Config>,
56
- resolveDeployTarget: ResolveDeployTarget<Config>,
57
- ) => {
58
- const { metaName } = api.getAppContext();
59
- const config = api.getNormalizedConfig();
60
- if (
61
- metaName !== 'modern-js' &&
62
- !config.deploy?.target &&
63
- !process.env.MODERNJS_DEPLOY
64
- ) {
65
- return undefined;
66
- }
67
- return resolveDeployTarget(config);
68
- };
69
-
70
50
  export const createUltramodernReleaseEnvelopePlugin = <
71
51
  Config extends ReleaseEnvelopeConfig,
72
- >({
73
- resolveDeployTarget,
74
- }: {
75
- resolveDeployTarget: ResolveDeployTarget<Config>;
76
- }): ReleaseEnvelopePlugin<Config> => {
52
+ >(): ReleaseEnvelopePlugin<Config> => {
77
53
  return {
78
54
  name: '@modern-js/ultramodern-release-envelope',
79
55
  pre: [
@@ -98,7 +74,7 @@ export const createUltramodernReleaseEnvelopePlugin = <
98
74
  };
99
75
 
100
76
  api.onAfterBuild(async () => {
101
- const configuredTarget = resolveDeployTarget(api.getNormalizedConfig());
77
+ const configuredTarget = api.getAppContext().deployTarget?.target;
102
78
  if (configuredTarget !== 'node' && configuredTarget !== 'cloudflare') {
103
79
  return;
104
80
  }
@@ -110,10 +86,7 @@ export const createUltramodernReleaseEnvelopePlugin = <
110
86
  });
111
87
 
112
88
  api.onBeforeDeploy(async () => {
113
- const configuredTarget = resolveActiveDeployTarget(
114
- api,
115
- resolveDeployTarget,
116
- );
89
+ const configuredTarget = getDeployingTarget(api.getAppContext());
117
90
  if (configuredTarget !== 'node') {
118
91
  return;
119
92
  }
@@ -123,10 +96,7 @@ export const createUltramodernReleaseEnvelopePlugin = <
123
96
  });
124
97
 
125
98
  api.onAfterDeploy(async () => {
126
- const configuredTarget = resolveActiveDeployTarget(
127
- api,
128
- resolveDeployTarget,
129
- );
99
+ const configuredTarget = getDeployingTarget(api.getAppContext());
130
100
  if (configuredTarget !== 'node') {
131
101
  return;
132
102
  }
@@ -1,14 +0,0 @@
1
- /**
2
- * Workerd cannot initialize Module Federation's Node-oriented SSR data-fetch
3
- * runtime. Cloudflare workers do not load native MF remotes, so retain the
4
- * runtime-plugin contract without installing its server data-fetch hooks.
5
- */
6
- export const injectDataFetchFunctionPlugin = () => ({
7
- name: '@module-federation/inject-data-fetch-function-plugin',
8
- setup() {},
9
- });
10
-
11
- export const mfSSRDevPlugin = () => ({
12
- name: '@module-federation/modern-js-v3',
13
- setup() {},
14
- });
@@ -1,14 +0,0 @@
1
- /**
2
- * Workerd cannot initialize Module Federation's Node-oriented SSR data-fetch
3
- * runtime. Cloudflare workers do not load native MF remotes, so retain the
4
- * runtime-plugin contract without installing its server data-fetch hooks.
5
- */
6
- export const injectDataFetchFunctionPlugin = () => ({
7
- name: '@module-federation/inject-data-fetch-function-plugin',
8
- setup() {},
9
- });
10
-
11
- export const mfSSRDevPlugin = () => ({
12
- name: '@module-federation/modern-js-v3',
13
- setup() {},
14
- });
@@ -1,14 +0,0 @@
1
- /**
2
- * Workerd cannot initialize Module Federation's Node-oriented SSR data-fetch
3
- * runtime. Cloudflare workers do not load native MF remotes, so retain the
4
- * runtime-plugin contract without installing its server data-fetch hooks.
5
- */
6
- export const injectDataFetchFunctionPlugin = () => ({
7
- name: '@module-federation/inject-data-fetch-function-plugin',
8
- setup() {},
9
- });
10
-
11
- export const mfSSRDevPlugin = () => ({
12
- name: '@module-federation/modern-js-v3',
13
- setup() {},
14
- });
@@ -1,13 +0,0 @@
1
- /**
2
- * Workerd cannot initialize Module Federation's Node-oriented SSR data-fetch
3
- * runtime. Cloudflare workers do not load native MF remotes, so retain the
4
- * runtime-plugin contract without installing its server data-fetch hooks.
5
- */
6
- export declare const injectDataFetchFunctionPlugin: () => {
7
- name: string;
8
- setup(): void;
9
- };
10
- export declare const mfSSRDevPlugin: () => {
11
- name: string;
12
- setup(): void;
13
- };
@@ -1,14 +0,0 @@
1
- /**
2
- * Workerd cannot initialize Module Federation's Node-oriented SSR data-fetch
3
- * runtime. Cloudflare workers do not load native MF remotes, so retain the
4
- * runtime-plugin contract without installing its server data-fetch hooks.
5
- */
6
- export const injectDataFetchFunctionPlugin = () => ({
7
- name: '@module-federation/inject-data-fetch-function-plugin',
8
- setup() {},
9
- });
10
-
11
- export const mfSSRDevPlugin = () => ({
12
- name: '@module-federation/modern-js-v3',
13
- setup() {},
14
- });