@modern-js/app-tools 2.3.1-alpha.2 → 2.5.0-alpha.0
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.
- package/CHANGELOG.md +115 -0
- package/README.md +14 -18
- package/dist/cjs/analyze/constants.js +100 -0
- package/dist/cjs/analyze/generateCode.js +251 -0
- package/dist/cjs/analyze/getBundleEntry.js +101 -0
- package/dist/cjs/analyze/getClientRoutes/getRoutes.js +225 -0
- package/dist/cjs/analyze/getClientRoutes/getRoutesLegacy.js +221 -0
- package/dist/cjs/analyze/getClientRoutes/index.js +30 -0
- package/dist/cjs/analyze/getClientRoutes/utils.js +58 -0
- package/dist/cjs/analyze/getFileSystemEntry.js +131 -0
- package/dist/cjs/analyze/getHtmlTemplate.js +124 -0
- package/dist/cjs/analyze/getServerRoutes.js +174 -0
- package/dist/cjs/analyze/index.js +294 -0
- package/dist/cjs/analyze/isDefaultExportFunction.js +71 -0
- package/dist/cjs/analyze/makeLegalIdentifier.js +36 -0
- package/dist/cjs/analyze/nestedRoutes.js +179 -0
- package/dist/cjs/analyze/templates.js +330 -0
- package/dist/cjs/analyze/utils.js +152 -0
- package/dist/cjs/builder/builder-rspack/index.js +31 -0
- package/dist/cjs/builder/builder-webpack/builderPlugins/compatModern.js +64 -0
- package/dist/cjs/builder/builder-webpack/index.js +78 -0
- package/dist/cjs/builder/builder-webpack/webpackPlugins/RouterPlugin.js +120 -0
- package/dist/cjs/builder/builder-webpack/webpackPlugins/index.js +17 -0
- package/dist/cjs/builder/generator/createBuilderOptions.js +47 -0
- package/dist/cjs/builder/generator/createBuilderProviderConfig.js +49 -0
- package/dist/cjs/builder/generator/getBuilderTargets.js +39 -0
- package/dist/cjs/builder/generator/index.js +58 -0
- package/dist/cjs/builder/index.js +46 -0
- package/dist/cjs/builder/shared/builderPlugins/adapterModern.js +230 -0
- package/dist/{js/node/builder/webpackPlugins/htmlAsyncChunkPlugin.js → cjs/builder/shared/bundlerPlugins/HtmlAsyncChunkPlugin.js} +17 -20
- package/dist/{js/node/builder/webpackPlugins/htmlBottomTemplate.js → cjs/builder/shared/bundlerPlugins/HtmlBottomTemplate.js} +21 -24
- package/dist/{js/node/builder/share.js → cjs/builder/shared/createCopyPattern.js} +3 -3
- package/dist/cjs/builder/shared/index.js +19 -0
- package/dist/{js/node/builder → cjs/builder/shared}/loaders/serverModuleLoader.js +0 -0
- package/dist/cjs/builder/shared/types.js +15 -0
- package/dist/cjs/commands/build.js +75 -0
- package/dist/cjs/commands/deploy.js +31 -0
- package/dist/cjs/commands/dev.js +91 -0
- package/dist/cjs/commands/index.js +19 -0
- package/dist/cjs/commands/inspect.js +41 -0
- package/dist/cjs/commands/serve.js +63 -0
- package/dist/cjs/config/default.js +186 -0
- package/dist/cjs/config/index.js +19 -0
- package/dist/{js/node/config/initial → cjs/config/initialize}/index.js +10 -16
- package/dist/cjs/config/initialize/inits.js +161 -0
- package/dist/{js/node/config/initial → cjs/config/legacy}/createHtmlConfig.js +0 -0
- package/dist/{js/node/config/initial → cjs/config/legacy}/createOutputConfig.js +0 -0
- package/dist/{js/node/config/initial → cjs/config/legacy}/createSourceConfig.js +0 -0
- package/dist/{js/node/config/initial → cjs/config/legacy}/createToolsConfig.js +0 -0
- package/dist/{js/node/config/initial/transformNormalizedConfig.js → cjs/config/legacy/index.js} +8 -3
- package/dist/cjs/defineConfig.js +33 -0
- package/dist/cjs/exports/server.js +27 -0
- package/dist/cjs/hooks.js +54 -0
- package/dist/cjs/index.js +198 -0
- package/dist/cjs/initialize/index.js +102 -0
- package/dist/cjs/locale/en.js +59 -0
- package/dist/cjs/locale/index.js +33 -0
- package/dist/cjs/locale/zh.js +59 -0
- package/dist/cjs/schema/Schema.js +63 -0
- package/dist/cjs/schema/index.js +118 -0
- package/dist/cjs/schema/legacy.js +169 -0
- package/dist/cjs/types/config/deploy.js +15 -0
- package/dist/cjs/types/config/dev.js +15 -0
- package/dist/cjs/types/config/experiments.js +15 -0
- package/dist/cjs/types/config/html.js +15 -0
- package/dist/cjs/types/config/index.js +17 -0
- package/dist/cjs/types/config/output.js +15 -0
- package/dist/cjs/types/config/performance.js +15 -0
- package/dist/cjs/types/config/security.js +15 -0
- package/dist/cjs/types/config/source.js +15 -0
- package/dist/cjs/types/config/tools.js +15 -0
- package/dist/cjs/types/hooks.js +15 -0
- package/dist/cjs/types/index.js +19 -0
- package/dist/cjs/types/legacyConfig/deploy.js +15 -0
- package/dist/cjs/types/legacyConfig/dev.js +15 -0
- package/dist/cjs/types/legacyConfig/index.js +15 -0
- package/dist/cjs/types/legacyConfig/output.js +15 -0
- package/dist/cjs/types/legacyConfig/source.js +15 -0
- package/dist/cjs/types/legacyConfig/tools.js +15 -0
- package/dist/cjs/types/utils.js +15 -0
- package/dist/cjs/utils/config.js +115 -0
- package/dist/cjs/utils/createServer.js +70 -0
- package/dist/cjs/utils/env.js +38 -0
- package/dist/cjs/utils/generateWatchFiles.js +63 -0
- package/dist/cjs/utils/getSelectedEntries.js +61 -0
- package/dist/cjs/utils/getServerInternalPlugins.js +41 -0
- package/dist/cjs/utils/language.js +31 -0
- package/dist/cjs/utils/printInstructions.js +34 -0
- package/dist/cjs/utils/restart.js +45 -0
- package/dist/cjs/utils/routes.js +39 -0
- package/dist/cjs/utils/types.js +15 -0
- package/dist/esm/analyze/constants.js +42 -0
- package/dist/esm/analyze/generateCode.js +486 -0
- package/dist/esm/analyze/getBundleEntry.js +64 -0
- package/dist/esm/analyze/getClientRoutes/getRoutes.js +233 -0
- package/dist/esm/analyze/getClientRoutes/getRoutesLegacy.js +231 -0
- package/dist/esm/analyze/getClientRoutes/index.js +3 -0
- package/dist/esm/analyze/getClientRoutes/utils.js +23 -0
- package/dist/esm/analyze/getFileSystemEntry.js +107 -0
- package/dist/esm/analyze/getHtmlTemplate.js +302 -0
- package/dist/esm/analyze/getServerRoutes.js +224 -0
- package/dist/esm/analyze/index.js +703 -0
- package/dist/esm/analyze/isDefaultExportFunction.js +47 -0
- package/dist/esm/analyze/makeLegalIdentifier.js +13 -0
- package/dist/esm/analyze/nestedRoutes.js +409 -0
- package/dist/esm/analyze/templates.js +466 -0
- package/dist/esm/analyze/utils.js +374 -0
- package/dist/esm/builder/builder-rspack/index.js +6 -0
- package/dist/esm/builder/builder-webpack/builderPlugins/compatModern.js +64 -0
- package/dist/esm/builder/builder-webpack/index.js +301 -0
- package/dist/esm/builder/builder-webpack/webpackPlugins/RouterPlugin.js +346 -0
- package/dist/esm/builder/builder-webpack/webpackPlugins/index.js +1 -0
- package/dist/esm/builder/generator/createBuilderOptions.js +41 -0
- package/dist/esm/builder/generator/createBuilderProviderConfig.js +70 -0
- package/dist/esm/builder/generator/getBuilderTargets.js +16 -0
- package/dist/esm/builder/generator/index.js +199 -0
- package/dist/esm/builder/index.js +175 -0
- package/dist/esm/builder/shared/builderPlugins/adapterModern.js +300 -0
- package/dist/{js/treeshaking/builder/webpackPlugins/htmlAsyncChunkPlugin.js → esm/builder/shared/bundlerPlugins/HtmlAsyncChunkPlugin.js} +0 -0
- package/dist/esm/builder/shared/bundlerPlugins/HtmlBottomTemplate.js +72 -0
- package/dist/{js/treeshaking/builder/share.js → esm/builder/shared/createCopyPattern.js} +0 -0
- package/dist/esm/builder/shared/index.js +3 -0
- package/dist/{js/treeshaking/builder → esm/builder/shared}/loaders/serverModuleLoader.js +0 -0
- package/dist/esm/builder/shared/types.js +1 -0
- package/dist/esm/commands/build.js +291 -0
- package/dist/esm/commands/deploy.js +154 -0
- package/dist/esm/commands/dev.js +301 -0
- package/dist/esm/commands/index.js +3 -0
- package/dist/esm/commands/inspect.js +149 -0
- package/dist/esm/commands/serve.js +199 -0
- package/dist/esm/config/default.js +210 -0
- package/dist/esm/config/index.js +3 -0
- package/dist/esm/config/initialize/index.js +10 -0
- package/dist/esm/config/initialize/inits.js +211 -0
- package/dist/esm/config/legacy/createHtmlConfig.js +19 -0
- package/dist/esm/config/legacy/createOutputConfig.js +41 -0
- package/dist/esm/config/legacy/createSourceConfig.js +42 -0
- package/dist/esm/config/legacy/createToolsConfig.js +23 -0
- package/dist/{js/treeshaking/config/initial/transformNormalizedConfig.js → esm/config/legacy/index.js} +4 -1
- package/dist/esm/defineConfig.js +61 -0
- package/dist/esm/exports/server.js +2 -0
- package/dist/esm/hooks.js +29 -0
- package/dist/esm/index.js +713 -0
- package/dist/esm/initialize/index.js +284 -0
- package/dist/esm/locale/en.js +38 -0
- package/dist/esm/locale/index.js +9 -0
- package/dist/esm/locale/zh.js +38 -0
- package/dist/esm/schema/Schema.js +273 -0
- package/dist/esm/schema/index.js +181 -0
- package/dist/esm/schema/legacy.js +337 -0
- package/dist/esm/types/config/deploy.js +1 -0
- package/dist/esm/types/config/dev.js +1 -0
- package/dist/esm/types/config/experiments.js +1 -0
- package/dist/esm/types/config/html.js +1 -0
- package/dist/esm/types/config/index.js +1 -0
- package/dist/esm/types/config/output.js +1 -0
- package/dist/esm/types/config/performance.js +1 -0
- package/dist/esm/types/config/security.js +1 -0
- package/dist/esm/types/config/source.js +1 -0
- package/dist/esm/types/config/tools.js +1 -0
- package/dist/esm/types/hooks.js +1 -0
- package/dist/esm/types/index.js +3 -0
- package/dist/esm/types/legacyConfig/deploy.js +1 -0
- package/dist/esm/types/legacyConfig/dev.js +1 -0
- package/dist/esm/types/legacyConfig/index.js +1 -0
- package/dist/esm/types/legacyConfig/output.js +1 -0
- package/dist/esm/types/legacyConfig/source.js +1 -0
- package/dist/esm/types/legacyConfig/tools.js +1 -0
- package/dist/esm/types/utils.js +1 -0
- package/dist/esm/utils/config.js +302 -0
- package/dist/esm/utils/createServer.js +258 -0
- package/dist/esm/utils/env.js +13 -0
- package/dist/esm/utils/generateWatchFiles.js +214 -0
- package/dist/esm/utils/getSelectedEntries.js +186 -0
- package/dist/esm/utils/getServerInternalPlugins.js +210 -0
- package/dist/esm/utils/language.js +6 -0
- package/dist/esm/utils/printInstructions.js +152 -0
- package/dist/esm/utils/restart.js +187 -0
- package/dist/esm/utils/routes.js +153 -0
- package/dist/esm/utils/types.js +1 -0
- package/dist/esm-node/analyze/constants.js +56 -0
- package/dist/esm-node/analyze/generateCode.js +227 -0
- package/dist/esm-node/analyze/getBundleEntry.js +77 -0
- package/dist/esm-node/analyze/getClientRoutes/getRoutes.js +201 -0
- package/dist/esm-node/analyze/getClientRoutes/getRoutesLegacy.js +197 -0
- package/dist/esm-node/analyze/getClientRoutes/index.js +6 -0
- package/dist/esm-node/analyze/getClientRoutes/utils.js +31 -0
- package/dist/esm-node/analyze/getFileSystemEntry.js +109 -0
- package/dist/esm-node/analyze/getHtmlTemplate.js +95 -0
- package/dist/esm-node/analyze/getServerRoutes.js +154 -0
- package/dist/esm-node/analyze/index.js +285 -0
- package/dist/esm-node/analyze/isDefaultExportFunction.js +42 -0
- package/dist/esm-node/analyze/makeLegalIdentifier.js +13 -0
- package/dist/esm-node/analyze/nestedRoutes.js +150 -0
- package/dist/esm-node/analyze/templates.js +297 -0
- package/dist/esm-node/analyze/utils.js +121 -0
- package/dist/esm-node/builder/builder-rspack/index.js +8 -0
- package/dist/esm-node/builder/builder-webpack/builderPlugins/compatModern.js +41 -0
- package/dist/esm-node/builder/builder-webpack/index.js +51 -0
- package/dist/esm-node/builder/builder-webpack/webpackPlugins/RouterPlugin.js +96 -0
- package/dist/esm-node/builder/builder-webpack/webpackPlugins/index.js +1 -0
- package/dist/esm-node/builder/generator/createBuilderOptions.js +24 -0
- package/dist/esm-node/builder/generator/createBuilderProviderConfig.js +26 -0
- package/dist/esm-node/builder/generator/getBuilderTargets.js +21 -0
- package/dist/esm-node/builder/generator/index.js +29 -0
- package/dist/esm-node/builder/index.js +17 -0
- package/dist/esm-node/builder/shared/builderPlugins/adapterModern.js +202 -0
- package/dist/esm-node/builder/shared/bundlerPlugins/HtmlAsyncChunkPlugin.js +27 -0
- package/dist/esm-node/builder/shared/bundlerPlugins/HtmlBottomTemplate.js +34 -0
- package/dist/{js/modern/builder/share.js → esm-node/builder/shared/createCopyPattern.js} +0 -0
- package/dist/esm-node/builder/shared/index.js +3 -0
- package/dist/{js/modern/builder → esm-node/builder/shared}/loaders/serverModuleLoader.js +0 -0
- package/dist/esm-node/builder/shared/types.js +0 -0
- package/dist/esm-node/commands/build.js +52 -0
- package/dist/esm-node/commands/deploy.js +8 -0
- package/dist/esm-node/commands/dev.js +72 -0
- package/dist/esm-node/commands/index.js +3 -0
- package/dist/esm-node/commands/inspect.js +18 -0
- package/dist/esm-node/commands/serve.js +34 -0
- package/dist/esm-node/config/default.js +162 -0
- package/dist/esm-node/config/index.js +3 -0
- package/dist/esm-node/config/initialize/index.js +12 -0
- package/dist/esm-node/config/initialize/inits.js +136 -0
- package/dist/{js/modern/config/initial → esm-node/config/legacy}/createHtmlConfig.js +0 -0
- package/dist/{js/modern/config/initial → esm-node/config/legacy}/createOutputConfig.js +0 -0
- package/dist/{js/modern/config/initial → esm-node/config/legacy}/createSourceConfig.js +0 -0
- package/dist/{js/modern/config/initial → esm-node/config/legacy}/createToolsConfig.js +0 -0
- package/dist/{js/modern/config/initial/transformNormalizedConfig.js → esm-node/config/legacy/index.js} +4 -0
- package/dist/esm-node/defineConfig.js +9 -0
- package/dist/esm-node/exports/server.js +4 -0
- package/dist/esm-node/hooks.js +35 -0
- package/dist/esm-node/index.js +172 -0
- package/dist/esm-node/initialize/index.js +91 -0
- package/dist/esm-node/locale/en.js +36 -0
- package/dist/esm-node/locale/index.js +9 -0
- package/dist/esm-node/locale/zh.js +36 -0
- package/dist/esm-node/schema/Schema.js +40 -0
- package/dist/esm-node/schema/index.js +88 -0
- package/dist/esm-node/schema/legacy.js +148 -0
- package/dist/esm-node/types/config/deploy.js +0 -0
- package/dist/esm-node/types/config/dev.js +0 -0
- package/dist/esm-node/types/config/experiments.js +0 -0
- package/dist/esm-node/types/config/html.js +0 -0
- package/dist/esm-node/types/config/index.js +1 -0
- package/dist/esm-node/types/config/output.js +0 -0
- package/dist/esm-node/types/config/performance.js +0 -0
- package/dist/esm-node/types/config/security.js +0 -0
- package/dist/esm-node/types/config/source.js +0 -0
- package/dist/esm-node/types/config/tools.js +0 -0
- package/dist/esm-node/types/hooks.js +0 -0
- package/dist/esm-node/types/index.js +3 -0
- package/dist/esm-node/types/legacyConfig/deploy.js +0 -0
- package/dist/esm-node/types/legacyConfig/dev.js +0 -0
- package/dist/esm-node/types/legacyConfig/index.js +0 -0
- package/dist/esm-node/types/legacyConfig/output.js +0 -0
- package/dist/esm-node/types/legacyConfig/source.js +0 -0
- package/dist/esm-node/types/legacyConfig/tools.js +0 -0
- package/dist/esm-node/types/utils.js +0 -0
- package/dist/esm-node/utils/config.js +88 -0
- package/dist/esm-node/utils/createServer.js +37 -0
- package/dist/esm-node/utils/env.js +15 -0
- package/dist/esm-node/utils/generateWatchFiles.js +33 -0
- package/dist/esm-node/utils/getSelectedEntries.js +38 -0
- package/dist/esm-node/utils/getServerInternalPlugins.js +18 -0
- package/dist/esm-node/utils/language.js +8 -0
- package/dist/esm-node/utils/printInstructions.js +11 -0
- package/dist/esm-node/utils/restart.js +22 -0
- package/dist/esm-node/utils/routes.js +10 -0
- package/dist/esm-node/utils/types.js +0 -0
- package/dist/js/modern/analyze/index.js +45 -48
- package/dist/js/modern/analyze/nestedRoutes.js +1 -10
- package/dist/js/modern/builder/builder-rspack/index.js +8 -0
- package/dist/js/modern/builder/builder-webpack/builderPlugins/compatModern.js +41 -0
- package/dist/js/modern/builder/builder-webpack/index.js +93 -0
- package/dist/js/modern/builder/{webpackPlugins/routerPlugin.js → builder-webpack/webpackPlugins/RouterPlugin.js} +1 -1
- package/dist/js/modern/builder/builder-webpack/webpackPlugins/index.js +1 -0
- package/dist/js/modern/builder/generator/createBuilderOptions.js +24 -0
- package/dist/js/modern/builder/generator/createBuilderProviderConfig.js +39 -0
- package/dist/js/modern/builder/generator/getBuilderTargets.js +12 -0
- package/dist/js/modern/builder/generator/index.js +53 -0
- package/dist/js/modern/builder/index.js +13 -133
- package/dist/js/modern/builder/{builderPlugins/compatModern.js → shared/builderPlugins/adapterModern.js} +81 -111
- package/dist/js/modern/builder/shared/bundlerPlugins/HtmlAsyncChunkPlugin.js +27 -0
- package/dist/js/modern/builder/shared/bundlerPlugins/HtmlBottomTemplate.js +34 -0
- package/dist/js/modern/builder/shared/createCopyPattern.js +46 -0
- package/dist/js/modern/builder/shared/index.js +3 -0
- package/dist/js/modern/builder/shared/loaders/serverModuleLoader.js +7 -0
- package/dist/js/modern/builder/shared/types.js +0 -0
- package/dist/js/modern/config/default.js +2 -2
- package/dist/js/modern/config/index.js +3 -8
- package/dist/js/modern/config/initialize/index.js +12 -0
- package/dist/js/modern/config/{initial → initialize}/inits.js +4 -2
- package/dist/js/modern/config/legacy/createHtmlConfig.js +35 -0
- package/dist/js/modern/config/legacy/createOutputConfig.js +68 -0
- package/dist/js/modern/config/legacy/createSourceConfig.js +39 -0
- package/dist/js/modern/config/legacy/createToolsConfig.js +42 -0
- package/dist/js/modern/config/legacy/index.js +51 -0
- package/dist/js/modern/index.js +18 -8
- package/dist/js/modern/initialize/index.js +36 -28
- package/dist/js/modern/locale/zh.js +1 -1
- package/dist/js/modern/types/utils.js +0 -0
- package/dist/js/node/analyze/index.js +46 -49
- package/dist/js/node/analyze/nestedRoutes.js +1 -10
- package/dist/js/node/builder/builder-rspack/index.js +31 -0
- package/dist/js/node/builder/builder-webpack/builderPlugins/compatModern.js +64 -0
- package/dist/js/node/builder/builder-webpack/index.js +118 -0
- package/dist/js/node/builder/{webpackPlugins/routerPlugin.js → builder-webpack/webpackPlugins/RouterPlugin.js} +7 -5
- package/dist/js/node/builder/builder-webpack/webpackPlugins/index.js +17 -0
- package/dist/js/node/builder/generator/createBuilderOptions.js +47 -0
- package/dist/js/node/builder/generator/createBuilderProviderConfig.js +60 -0
- package/dist/js/node/builder/generator/getBuilderTargets.js +35 -0
- package/dist/js/node/builder/generator/index.js +82 -0
- package/dist/js/node/builder/index.js +14 -123
- package/dist/js/node/builder/{builderPlugins/compatModern.js → shared/builderPlugins/adapterModern.js} +86 -115
- package/dist/js/node/builder/shared/bundlerPlugins/HtmlAsyncChunkPlugin.js +50 -0
- package/dist/js/node/builder/shared/bundlerPlugins/HtmlBottomTemplate.js +57 -0
- package/dist/js/node/builder/shared/createCopyPattern.js +75 -0
- package/dist/js/node/builder/shared/index.js +19 -0
- package/dist/js/node/builder/shared/loaders/serverModuleLoader.js +28 -0
- package/dist/js/node/builder/shared/types.js +15 -0
- package/dist/js/node/config/default.js +2 -2
- package/dist/js/node/config/index.js +4 -19
- package/dist/js/node/config/initialize/index.js +35 -0
- package/dist/js/node/config/{initial → initialize}/inits.js +4 -2
- package/dist/js/node/config/legacy/createHtmlConfig.js +58 -0
- package/dist/js/node/config/legacy/createOutputConfig.js +91 -0
- package/dist/js/node/config/legacy/createSourceConfig.js +62 -0
- package/dist/js/node/config/legacy/createToolsConfig.js +65 -0
- package/dist/js/node/config/legacy/index.js +75 -0
- package/dist/js/node/index.js +18 -8
- package/dist/js/node/initialize/index.js +35 -28
- package/dist/js/node/locale/zh.js +1 -1
- package/dist/js/node/types/utils.js +15 -0
- package/dist/js/treeshaking/analyze/index.js +135 -130
- package/dist/js/treeshaking/analyze/nestedRoutes.js +1 -10
- package/dist/js/treeshaking/builder/builder-rspack/index.js +6 -0
- package/dist/js/treeshaking/builder/builder-webpack/builderPlugins/compatModern.js +64 -0
- package/dist/js/treeshaking/builder/builder-webpack/index.js +301 -0
- package/dist/js/treeshaking/builder/{webpackPlugins/routerPlugin.js → builder-webpack/webpackPlugins/RouterPlugin.js} +1 -1
- package/dist/js/treeshaking/builder/builder-webpack/webpackPlugins/index.js +1 -0
- package/dist/js/treeshaking/builder/generator/createBuilderOptions.js +41 -0
- package/dist/js/treeshaking/builder/generator/createBuilderProviderConfig.js +70 -0
- package/dist/js/treeshaking/builder/generator/getBuilderTargets.js +12 -0
- package/dist/js/treeshaking/builder/generator/index.js +199 -0
- package/dist/js/treeshaking/builder/index.js +29 -228
- package/dist/js/treeshaking/builder/{builderPlugins/compatModern.js → shared/builderPlugins/adapterModern.js} +103 -156
- package/dist/js/treeshaking/builder/shared/bundlerPlugins/HtmlAsyncChunkPlugin.js +110 -0
- package/dist/js/treeshaking/builder/{webpackPlugins/htmlBottomTemplate.js → shared/bundlerPlugins/HtmlBottomTemplate.js} +0 -0
- package/dist/js/treeshaking/builder/shared/createCopyPattern.js +40 -0
- package/dist/js/treeshaking/builder/shared/index.js +3 -0
- package/dist/js/treeshaking/builder/shared/loaders/serverModuleLoader.js +5 -0
- package/dist/js/treeshaking/builder/shared/types.js +1 -0
- package/dist/js/treeshaking/config/default.js +2 -2
- package/dist/js/treeshaking/config/index.js +3 -3
- package/dist/js/treeshaking/config/initialize/index.js +10 -0
- package/dist/js/treeshaking/config/{initial → initialize}/inits.js +4 -2
- package/dist/js/treeshaking/config/{initial → legacy}/createHtmlConfig.js +0 -0
- package/dist/js/treeshaking/config/{initial → legacy}/createOutputConfig.js +0 -0
- package/dist/js/treeshaking/config/{initial → legacy}/createSourceConfig.js +0 -0
- package/dist/js/treeshaking/config/{initial → legacy}/createToolsConfig.js +0 -0
- package/dist/js/treeshaking/config/legacy/index.js +38 -0
- package/dist/js/treeshaking/index.js +19 -12
- package/dist/js/treeshaking/initialize/index.js +34 -26
- package/dist/js/treeshaking/locale/zh.js +1 -1
- package/dist/js/treeshaking/types/utils.js +1 -0
- package/dist/types/analyze/generateCode.d.ts +1 -1
- package/dist/types/analyze/getBundleEntry.d.ts +1 -1
- package/dist/types/analyze/getFileSystemEntry.d.ts +1 -1
- package/dist/types/analyze/getHtmlTemplate.d.ts +2 -2
- package/dist/types/analyze/getServerRoutes.d.ts +1 -1
- package/dist/types/analyze/index.d.ts +5 -1
- package/dist/types/analyze/templates.d.ts +3 -1
- package/dist/types/builder/builder-rspack/index.d.ts +2 -0
- package/dist/types/builder/{builderPlugins → builder-webpack/builderPlugins}/compatModern.d.ts +2 -2
- package/dist/types/builder/builder-webpack/index.d.ts +4 -0
- package/dist/types/builder/{webpackPlugins/routerPlugin.d.ts → builder-webpack/webpackPlugins/RouterPlugin.d.ts} +1 -1
- package/dist/types/builder/builder-webpack/webpackPlugins/index.d.ts +1 -0
- package/dist/types/builder/generator/createBuilderOptions.d.ts +3 -0
- package/dist/types/builder/generator/createBuilderProviderConfig.d.ts +2 -0
- package/dist/types/builder/generator/getBuilderTargets.d.ts +3 -0
- package/dist/types/builder/generator/index.d.ts +17 -0
- package/dist/types/builder/index.d.ts +1 -18
- package/dist/types/builder/shared/builderPlugins/adapterModern.d.ts +13 -0
- package/dist/types/builder/{webpackPlugins/htmlAsyncChunkPlugin.d.ts → shared/bundlerPlugins/HtmlAsyncChunkPlugin.d.ts} +2 -1
- package/dist/types/builder/{webpackPlugins/htmlBottomTemplate.d.ts → shared/bundlerPlugins/HtmlBottomTemplate.d.ts} +2 -1
- package/dist/types/builder/{share.d.ts → shared/createCopyPattern.d.ts} +1 -1
- package/dist/types/builder/shared/index.d.ts +3 -0
- package/dist/types/builder/shared/loaders/serverModuleLoader.d.ts +4 -0
- package/dist/types/builder/shared/types.d.ts +16 -0
- package/dist/types/commands/build.d.ts +1 -1
- package/dist/types/commands/deploy.d.ts +1 -1
- package/dist/types/commands/dev.d.ts +1 -1
- package/dist/types/commands/inspect.d.ts +1 -1
- package/dist/types/commands/serve.d.ts +1 -1
- package/dist/types/config/default.d.ts +1 -1
- package/dist/types/config/index.d.ts +3 -2
- package/dist/types/config/initialize/index.d.ts +2 -0
- package/dist/types/config/{initial → initialize}/inits.d.ts +3 -3
- package/dist/types/config/{initial → legacy}/createHtmlConfig.d.ts +1 -1
- package/dist/types/config/{initial → legacy}/createOutputConfig.d.ts +1 -1
- package/dist/types/config/{initial → legacy}/createSourceConfig.d.ts +1 -1
- package/dist/types/config/{initial → legacy}/createToolsConfig.d.ts +1 -1
- package/dist/types/config/legacy/index.d.ts +3 -0
- package/dist/types/defineConfig.d.ts +2 -2
- package/dist/types/index.d.ts +7 -3
- package/dist/types/initialize/index.d.ts +6 -2
- package/dist/types/types/config/deploy.d.ts +1 -2
- package/dist/types/types/config/dev.d.ts +2 -3
- package/dist/types/types/config/experiments.d.ts +1 -2
- package/dist/types/types/config/html.d.ts +5 -3
- package/dist/types/types/config/index.d.ts +41 -48
- package/dist/types/types/config/output.d.ts +8 -3
- package/dist/types/types/config/performance.d.ts +6 -3
- package/dist/types/types/config/security.d.ts +3 -3
- package/dist/types/types/config/source.d.ts +7 -3
- package/dist/types/types/config/tools.d.ts +10 -4
- package/dist/types/types/hooks.d.ts +6 -4
- package/dist/types/types/index.d.ts +19 -7
- package/dist/types/types/utils.d.ts +7 -0
- package/dist/types/utils/config.d.ts +1 -1
- package/dist/types/utils/getServerInternalPlugins.d.ts +1 -1
- package/dist/types/utils/printInstructions.d.ts +1 -1
- package/package.json +40 -32
- package/dist/js/modern/builder/webpackPlugins/htmlAsyncChunkPlugin.js +0 -30
- package/dist/js/modern/builder/webpackPlugins/htmlBottomTemplate.js +0 -37
- package/dist/js/modern/config/initial/index.js +0 -16
- package/dist/js/treeshaking/config/initial/index.js +0 -12
- package/dist/types/builder/loaders/serverModuleLoader.d.ts +0 -3
- package/dist/types/config/initial/index.d.ts +0 -4
- package/dist/types/config/initial/transformNormalizedConfig.d.ts +0 -2
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { fs, getEntryOptions, logger } from "@modern-js/utils";
|
|
3
|
+
import {
|
|
4
|
+
useResolvedConfigContext
|
|
5
|
+
} from "@modern-js/core";
|
|
6
|
+
import * as templates from "./templates";
|
|
7
|
+
import { getClientRoutes, getClientRoutesLegacy } from "./getClientRoutes";
|
|
8
|
+
import {
|
|
9
|
+
FILE_SYSTEM_ROUTES_FILE_NAME,
|
|
10
|
+
ENTRY_POINT_FILE_NAME,
|
|
11
|
+
ENTRY_BOOTSTRAP_FILE_NAME
|
|
12
|
+
} from "./constants";
|
|
13
|
+
import { getDefaultImports, getServerLoadersFile } from "./utils";
|
|
14
|
+
import { walk } from "./nestedRoutes";
|
|
15
|
+
const createImportSpecifier = (specifiers) => {
|
|
16
|
+
let defaults = "";
|
|
17
|
+
const named = [];
|
|
18
|
+
for (const { local, imported } of specifiers) {
|
|
19
|
+
if (local && imported) {
|
|
20
|
+
named.push(`${imported} as ${local}`);
|
|
21
|
+
} else if (local) {
|
|
22
|
+
defaults = local;
|
|
23
|
+
} else {
|
|
24
|
+
named.push(imported);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
if (defaults && named.length) {
|
|
28
|
+
return `${defaults}, { ${named.join(", ")} }`;
|
|
29
|
+
} else if (defaults) {
|
|
30
|
+
return defaults;
|
|
31
|
+
} else {
|
|
32
|
+
return `{ ${named.join(", ")} }`;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
const createImportStatements = (statements) => {
|
|
36
|
+
var _a, _b;
|
|
37
|
+
const deDuplicated = [];
|
|
38
|
+
const seen = /* @__PURE__ */ new Map();
|
|
39
|
+
for (const { value, specifiers, initialize } of statements) {
|
|
40
|
+
if (!seen.has(value)) {
|
|
41
|
+
deDuplicated.push({
|
|
42
|
+
value,
|
|
43
|
+
specifiers,
|
|
44
|
+
initialize
|
|
45
|
+
});
|
|
46
|
+
seen.set(value, specifiers);
|
|
47
|
+
} else {
|
|
48
|
+
seen.get(value).push(...specifiers);
|
|
49
|
+
const modifyIndex = deDuplicated.findIndex((v) => v.value === value);
|
|
50
|
+
const originInitialize = (_b = (_a = deDuplicated[modifyIndex]) == null ? void 0 : _a.initialize) != null ? _b : "";
|
|
51
|
+
deDuplicated[modifyIndex].initialize = originInitialize.concat(
|
|
52
|
+
`
|
|
53
|
+
${initialize || ""}`
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return deDuplicated.map(
|
|
58
|
+
({ value, specifiers, initialize }) => `import ${createImportSpecifier(specifiers)} from '${value}';
|
|
59
|
+
${initialize || ""}`
|
|
60
|
+
).join("\n");
|
|
61
|
+
};
|
|
62
|
+
const generateCode = async (appContext, config, entrypoints, api) => {
|
|
63
|
+
var _a, _b, _c;
|
|
64
|
+
const {
|
|
65
|
+
internalDirectory,
|
|
66
|
+
srcDirectory,
|
|
67
|
+
internalDirAlias,
|
|
68
|
+
internalSrcAlias,
|
|
69
|
+
packageName,
|
|
70
|
+
metaName
|
|
71
|
+
} = appContext;
|
|
72
|
+
const hookRunners = api.useHookRunners();
|
|
73
|
+
const isV5 = typeof ((_a = config.runtime) == null ? void 0 : _a.router) !== "boolean" && ((_c = (_b = config == null ? void 0 : config.runtime) == null ? void 0 : _b.router) == null ? void 0 : _c.mode) === "react-router-5";
|
|
74
|
+
const { mountId } = config.html;
|
|
75
|
+
const getRoutes = isV5 ? getClientRoutesLegacy : getClientRoutes;
|
|
76
|
+
await Promise.all(entrypoints.map(generateEntryCode));
|
|
77
|
+
async function generateEntryCode(entrypoint) {
|
|
78
|
+
const { entryName, isAutoMount, customBootstrap, fileSystemRoutes } = entrypoint;
|
|
79
|
+
if (isAutoMount) {
|
|
80
|
+
if (fileSystemRoutes) {
|
|
81
|
+
let initialRoutes = [];
|
|
82
|
+
let nestedRoute = null;
|
|
83
|
+
if (entrypoint.entry) {
|
|
84
|
+
initialRoutes = getRoutes({
|
|
85
|
+
entrypoint,
|
|
86
|
+
srcDirectory,
|
|
87
|
+
srcAlias: internalSrcAlias,
|
|
88
|
+
internalDirectory,
|
|
89
|
+
internalDirAlias
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
if (entrypoint.nestedRoutesEntry) {
|
|
93
|
+
if (!isV5) {
|
|
94
|
+
nestedRoute = await walk(
|
|
95
|
+
entrypoint.nestedRoutesEntry,
|
|
96
|
+
entrypoint.nestedRoutesEntry,
|
|
97
|
+
{
|
|
98
|
+
name: internalSrcAlias,
|
|
99
|
+
basename: srcDirectory
|
|
100
|
+
},
|
|
101
|
+
entrypoint.entryName
|
|
102
|
+
);
|
|
103
|
+
if (nestedRoute) {
|
|
104
|
+
initialRoutes.unshift(nestedRoute);
|
|
105
|
+
}
|
|
106
|
+
} else {
|
|
107
|
+
logger.error("Nested routes is not supported in legacy mode.");
|
|
108
|
+
process.exit(1);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
const { routes } = await hookRunners.modifyFileSystemRoutes({
|
|
112
|
+
entrypoint,
|
|
113
|
+
routes: initialRoutes
|
|
114
|
+
});
|
|
115
|
+
const config2 = useResolvedConfigContext();
|
|
116
|
+
const ssr = getEntryOptions(
|
|
117
|
+
entryName,
|
|
118
|
+
config2.server.ssr,
|
|
119
|
+
config2.server.ssrByEntries,
|
|
120
|
+
packageName
|
|
121
|
+
);
|
|
122
|
+
let mode;
|
|
123
|
+
if (ssr) {
|
|
124
|
+
mode = typeof ssr === "object" ? ssr.mode || "string" : "string";
|
|
125
|
+
} else {
|
|
126
|
+
mode = false;
|
|
127
|
+
}
|
|
128
|
+
if (mode === "stream") {
|
|
129
|
+
const hasPageRoute = routes.some(
|
|
130
|
+
(route) => "type" in route && route.type === "page"
|
|
131
|
+
);
|
|
132
|
+
if (hasPageRoute) {
|
|
133
|
+
logger.error(
|
|
134
|
+
"Streaming ssr is not supported when pages dir exists"
|
|
135
|
+
);
|
|
136
|
+
process.exit(1);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
const { code: code2 } = await hookRunners.beforeGenerateRoutes({
|
|
140
|
+
entrypoint,
|
|
141
|
+
code: await templates.fileSystemRoutes({
|
|
142
|
+
routes,
|
|
143
|
+
ssrMode: mode,
|
|
144
|
+
nestedRoutesEntry: entrypoint.nestedRoutesEntry,
|
|
145
|
+
entryName: entrypoint.entryName,
|
|
146
|
+
internalDirectory
|
|
147
|
+
})
|
|
148
|
+
});
|
|
149
|
+
if (entrypoint.nestedRoutesEntry && mode) {
|
|
150
|
+
const routesServerFile = getServerLoadersFile(
|
|
151
|
+
internalDirectory,
|
|
152
|
+
entryName
|
|
153
|
+
);
|
|
154
|
+
const code3 = templates.routesForServer({
|
|
155
|
+
routes,
|
|
156
|
+
metaName
|
|
157
|
+
});
|
|
158
|
+
await fs.ensureFile(routesServerFile);
|
|
159
|
+
await fs.writeFile(routesServerFile, code3);
|
|
160
|
+
}
|
|
161
|
+
fs.outputFileSync(
|
|
162
|
+
path.resolve(
|
|
163
|
+
internalDirectory,
|
|
164
|
+
`./${entryName}/${FILE_SYSTEM_ROUTES_FILE_NAME}`
|
|
165
|
+
),
|
|
166
|
+
code2,
|
|
167
|
+
"utf8"
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
const { imports: importStatements } = await hookRunners.modifyEntryImports({
|
|
171
|
+
entrypoint,
|
|
172
|
+
imports: getDefaultImports({
|
|
173
|
+
entrypoint,
|
|
174
|
+
srcDirectory,
|
|
175
|
+
internalSrcAlias,
|
|
176
|
+
internalDirAlias,
|
|
177
|
+
internalDirectory
|
|
178
|
+
})
|
|
179
|
+
});
|
|
180
|
+
const { plugins } = await hookRunners.modifyEntryRuntimePlugins({
|
|
181
|
+
entrypoint,
|
|
182
|
+
plugins: []
|
|
183
|
+
});
|
|
184
|
+
const { code: renderFunction } = await hookRunners.modifyEntryRenderFunction({
|
|
185
|
+
entrypoint,
|
|
186
|
+
code: templates.renderFunction({
|
|
187
|
+
plugins,
|
|
188
|
+
customBootstrap,
|
|
189
|
+
fileSystemRoutes
|
|
190
|
+
})
|
|
191
|
+
});
|
|
192
|
+
const { exportStatement } = await hookRunners.modifyEntryExport({
|
|
193
|
+
entrypoint,
|
|
194
|
+
exportStatement: "export default AppWrapper;"
|
|
195
|
+
});
|
|
196
|
+
const code = templates.index({
|
|
197
|
+
mountId,
|
|
198
|
+
imports: createImportStatements(importStatements),
|
|
199
|
+
renderFunction,
|
|
200
|
+
exportStatement
|
|
201
|
+
});
|
|
202
|
+
const entryFile = path.resolve(
|
|
203
|
+
internalDirectory,
|
|
204
|
+
`./${entryName}/${ENTRY_POINT_FILE_NAME}`
|
|
205
|
+
);
|
|
206
|
+
entrypoint.entry = entryFile;
|
|
207
|
+
if (config.source.enableAsyncEntry) {
|
|
208
|
+
const { code: asyncEntryCode } = await hookRunners.modifyAsyncEntry({
|
|
209
|
+
entrypoint,
|
|
210
|
+
code: `import('./${ENTRY_BOOTSTRAP_FILE_NAME}');`
|
|
211
|
+
});
|
|
212
|
+
fs.outputFileSync(entryFile, asyncEntryCode, "utf8");
|
|
213
|
+
const bootstrapFile = path.resolve(
|
|
214
|
+
internalDirectory,
|
|
215
|
+
`./${entryName}/${ENTRY_BOOTSTRAP_FILE_NAME}`
|
|
216
|
+
);
|
|
217
|
+
fs.outputFileSync(bootstrapFile, code, "utf8");
|
|
218
|
+
} else {
|
|
219
|
+
fs.outputFileSync(entryFile, code, "utf8");
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
export {
|
|
225
|
+
createImportStatements,
|
|
226
|
+
generateCode
|
|
227
|
+
};
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import {
|
|
3
|
+
ensureAbsolutePath,
|
|
4
|
+
fs,
|
|
5
|
+
findExists,
|
|
6
|
+
MAIN_ENTRY_NAME
|
|
7
|
+
} from "@modern-js/utils";
|
|
8
|
+
import { getFileSystemEntry } from "./getFileSystemEntry";
|
|
9
|
+
import { JS_EXTENSIONS } from "./constants";
|
|
10
|
+
const ensureExtensions = (file) => {
|
|
11
|
+
if (!path.extname(file)) {
|
|
12
|
+
return findExists(JS_EXTENSIONS.map((ext) => `${file}${ext}`)) || file;
|
|
13
|
+
}
|
|
14
|
+
return file;
|
|
15
|
+
};
|
|
16
|
+
const ifAlreadyExists = (entrypoints, checked) => entrypoints.some((entrypoint) => {
|
|
17
|
+
if (ensureExtensions(entrypoint.entry) === ensureExtensions(checked.entry)) {
|
|
18
|
+
checked.entryName = entrypoint.entryName;
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
if (entrypoint.entry.startsWith(checked.entry) || checked.entry.startsWith(entrypoint.entry)) {
|
|
22
|
+
throw new Error(
|
|
23
|
+
`Entry configuration conflicts
|
|
24
|
+
Your configuration: ${checked.entry}.
|
|
25
|
+
Default entrypoint: ${entrypoint.entry}
|
|
26
|
+
Please reset source.entries or set source.disableDefaultEntries to disable the default entry rules.`
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
return false;
|
|
30
|
+
});
|
|
31
|
+
const getBundleEntry = (appContext, config) => {
|
|
32
|
+
const { appDirectory, packageName } = appContext;
|
|
33
|
+
const {
|
|
34
|
+
source: { disableDefaultEntries, entries, entriesDir }
|
|
35
|
+
} = config;
|
|
36
|
+
const defaults = disableDefaultEntries ? [] : getFileSystemEntry(appContext, config);
|
|
37
|
+
if (entries) {
|
|
38
|
+
Object.keys(entries).forEach((name) => {
|
|
39
|
+
const value = entries[name];
|
|
40
|
+
const entrypoint = typeof value === "string" ? {
|
|
41
|
+
entryName: name,
|
|
42
|
+
entry: ensureAbsolutePath(appDirectory, value),
|
|
43
|
+
absoluteEntryDir: path.dirname(
|
|
44
|
+
ensureAbsolutePath(appDirectory, value)
|
|
45
|
+
),
|
|
46
|
+
isAutoMount: true,
|
|
47
|
+
fileSystemRoutes: fs.statSync(ensureAbsolutePath(appDirectory, value)).isDirectory() ? {} : void 0
|
|
48
|
+
} : {
|
|
49
|
+
entryName: name,
|
|
50
|
+
entry: ensureAbsolutePath(appDirectory, value.entry),
|
|
51
|
+
absoluteEntryDir: path.dirname(
|
|
52
|
+
ensureAbsolutePath(appDirectory, value.entry)
|
|
53
|
+
),
|
|
54
|
+
isAutoMount: !value.disableMount,
|
|
55
|
+
customBootstrap: value.customBootstrap && ensureAbsolutePath(appDirectory, value.customBootstrap),
|
|
56
|
+
fileSystemRoutes: fs.statSync(ensureAbsolutePath(appDirectory, value.entry)).isDirectory() ? {} : void 0
|
|
57
|
+
};
|
|
58
|
+
if (entrypoint.fileSystemRoutes) {
|
|
59
|
+
entrypoint.nestedRoutesEntry = entrypoint.entry;
|
|
60
|
+
}
|
|
61
|
+
if (!ifAlreadyExists(defaults, entrypoint)) {
|
|
62
|
+
defaults.push(entrypoint);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
if (!disableDefaultEntries) {
|
|
67
|
+
const entriesDirAbs = ensureAbsolutePath(appDirectory, entriesDir || "");
|
|
68
|
+
const found = defaults.find(
|
|
69
|
+
({ entryName, entry, nestedRoutesEntry = "" }) => entryName === packageName || path.dirname(entry) === entriesDirAbs || path.dirname(nestedRoutesEntry) === entriesDirAbs
|
|
70
|
+
);
|
|
71
|
+
found && (found.entryName = MAIN_ENTRY_NAME);
|
|
72
|
+
}
|
|
73
|
+
return defaults;
|
|
74
|
+
};
|
|
75
|
+
export {
|
|
76
|
+
getBundleEntry
|
|
77
|
+
};
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { fs } from "@modern-js/utils";
|
|
3
|
+
import { makeLegalIdentifier } from "../makeLegalIdentifier";
|
|
4
|
+
import {
|
|
5
|
+
FILE_SYSTEM_ROUTES_COMPONENTS_DIR,
|
|
6
|
+
FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP,
|
|
7
|
+
FILE_SYSTEM_ROUTES_INDEX,
|
|
8
|
+
FILE_SYSTEM_ROUTES_LAYOUT
|
|
9
|
+
} from "../constants";
|
|
10
|
+
import { replaceWithAlias } from "../utils";
|
|
11
|
+
import { debug, findLayout, shouldSkip, getRouteWeight } from "./utils";
|
|
12
|
+
const compName = (srcDirectory, filePath) => {
|
|
13
|
+
const legalCompName = makeLegalIdentifier(
|
|
14
|
+
path.relative(srcDirectory, filePath)
|
|
15
|
+
);
|
|
16
|
+
return `Comp_${legalCompName}`;
|
|
17
|
+
};
|
|
18
|
+
const layoutNameAbbr = (filePath) => {
|
|
19
|
+
const prefix = "L_";
|
|
20
|
+
const dirName = path.dirname(filePath).split("/").pop() || "";
|
|
21
|
+
return `${prefix}${makeLegalIdentifier(dirName)}`;
|
|
22
|
+
};
|
|
23
|
+
const parents = [];
|
|
24
|
+
const recursiveReadDir = ({
|
|
25
|
+
dir,
|
|
26
|
+
routes,
|
|
27
|
+
basePath = "/",
|
|
28
|
+
srcDirectory,
|
|
29
|
+
srcAlias
|
|
30
|
+
}) => {
|
|
31
|
+
let hasDynamicRoute = false;
|
|
32
|
+
let resetParent = false;
|
|
33
|
+
let parent = parents[parents.length - 1];
|
|
34
|
+
const layout = findLayout(dir);
|
|
35
|
+
if (layout) {
|
|
36
|
+
if (basePath === "/") {
|
|
37
|
+
throw new Error(`should use _app instead of _layout in ${dir}`);
|
|
38
|
+
} else {
|
|
39
|
+
const alias = replaceWithAlias(srcDirectory, layout, srcAlias);
|
|
40
|
+
const componentName = compName(srcDirectory, layout);
|
|
41
|
+
const route = {
|
|
42
|
+
path: `${basePath.substring(0, basePath.length - 1)}`,
|
|
43
|
+
children: [],
|
|
44
|
+
_component: alias,
|
|
45
|
+
component: componentName,
|
|
46
|
+
parent,
|
|
47
|
+
type: "page"
|
|
48
|
+
};
|
|
49
|
+
parent = route;
|
|
50
|
+
resetParent = true;
|
|
51
|
+
routes.push(route);
|
|
52
|
+
parents.push(route);
|
|
53
|
+
routes = route.children;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
for (const relative of fs.readdirSync(dir)) {
|
|
57
|
+
const filePath = path.join(dir, relative);
|
|
58
|
+
if (!shouldSkip(filePath)) {
|
|
59
|
+
const filename = path.basename(filePath, path.extname(filePath));
|
|
60
|
+
const alias = replaceWithAlias(srcDirectory, filePath, srcAlias);
|
|
61
|
+
const componentName = compName(srcDirectory, filePath);
|
|
62
|
+
const dynamicRouteMatched = FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP.exec(filename);
|
|
63
|
+
if (dynamicRouteMatched) {
|
|
64
|
+
if (hasDynamicRoute) {
|
|
65
|
+
throw new Error(
|
|
66
|
+
`Can't set two dynamic route in one directory: ${dir}`
|
|
67
|
+
);
|
|
68
|
+
} else {
|
|
69
|
+
hasDynamicRoute = true;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const route = {
|
|
73
|
+
path: `${basePath}${dynamicRouteMatched ? `:${dynamicRouteMatched[1]}${dynamicRouteMatched[2]}` : filename}`,
|
|
74
|
+
_component: alias,
|
|
75
|
+
component: componentName,
|
|
76
|
+
parent,
|
|
77
|
+
type: "page"
|
|
78
|
+
};
|
|
79
|
+
if (fs.statSync(filePath).isDirectory()) {
|
|
80
|
+
recursiveReadDir({
|
|
81
|
+
dir: filePath,
|
|
82
|
+
routes,
|
|
83
|
+
basePath: `${route.path}/`,
|
|
84
|
+
srcDirectory,
|
|
85
|
+
srcAlias
|
|
86
|
+
});
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (filename === FILE_SYSTEM_ROUTES_LAYOUT) {
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
if (filename === FILE_SYSTEM_ROUTES_INDEX) {
|
|
93
|
+
route.path = basePath === "/" ? basePath : `${basePath.substring(0, basePath.length - 1)}`;
|
|
94
|
+
}
|
|
95
|
+
if (filename === "404" && basePath === "/") {
|
|
96
|
+
route.path = "*";
|
|
97
|
+
}
|
|
98
|
+
routes.push(route);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (resetParent) {
|
|
102
|
+
parents.pop();
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
const normalizeNestedRoutes = (nested, internalComponentsDir, internalDirectory, internalDirAlias) => {
|
|
106
|
+
const flat = (routes) => routes.reduce(
|
|
107
|
+
(memo, route) => memo.concat(
|
|
108
|
+
Array.isArray(route.children) ? flat(route.children) : [route]
|
|
109
|
+
),
|
|
110
|
+
[]
|
|
111
|
+
);
|
|
112
|
+
const generate = (route) => {
|
|
113
|
+
const codes = [];
|
|
114
|
+
let lastComponent = route.component;
|
|
115
|
+
const imports = [
|
|
116
|
+
`import React from 'react';`,
|
|
117
|
+
`import ${lastComponent} from '${route._component}'`
|
|
118
|
+
];
|
|
119
|
+
while (route = route.parent) {
|
|
120
|
+
const layoutComponent = route.component;
|
|
121
|
+
const layoutComponentAbbr = layoutNameAbbr(route._component);
|
|
122
|
+
imports.push(`import ${layoutComponent} from '${route._component}';`);
|
|
123
|
+
const currentComponent = `${layoutComponentAbbr}_${lastComponent}`;
|
|
124
|
+
codes.push(
|
|
125
|
+
`const ${currentComponent} = props => <${layoutComponent} Component={${lastComponent}} {...props} />;`
|
|
126
|
+
);
|
|
127
|
+
lastComponent = currentComponent;
|
|
128
|
+
}
|
|
129
|
+
const file = path.resolve(internalComponentsDir, `${lastComponent}.jsx`);
|
|
130
|
+
fs.outputFileSync(
|
|
131
|
+
file,
|
|
132
|
+
`${imports.join("\n")}
|
|
133
|
+
${codes.join(
|
|
134
|
+
"\n"
|
|
135
|
+
)}
|
|
136
|
+
export default ${lastComponent}`
|
|
137
|
+
);
|
|
138
|
+
return {
|
|
139
|
+
component: lastComponent,
|
|
140
|
+
_component: replaceWithAlias(internalDirectory, file, internalDirAlias)
|
|
141
|
+
};
|
|
142
|
+
};
|
|
143
|
+
const normalized = flat(nested).map(
|
|
144
|
+
(route) => route.parent ? { ...route, ...generate(route), parent: void 0 } : { ...route, parent: void 0 }
|
|
145
|
+
);
|
|
146
|
+
return normalized;
|
|
147
|
+
};
|
|
148
|
+
const getClientRoutes = ({
|
|
149
|
+
entrypoint,
|
|
150
|
+
srcDirectory,
|
|
151
|
+
srcAlias,
|
|
152
|
+
internalDirectory,
|
|
153
|
+
internalDirAlias
|
|
154
|
+
}) => {
|
|
155
|
+
const { entryName, pageRoutesEntry } = entrypoint;
|
|
156
|
+
if (!pageRoutesEntry) {
|
|
157
|
+
return [];
|
|
158
|
+
}
|
|
159
|
+
if (!fs.existsSync(pageRoutesEntry)) {
|
|
160
|
+
throw new Error(
|
|
161
|
+
`generate file system routes error, ${pageRoutesEntry} directory not found.`
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
if (!(fs.existsSync(pageRoutesEntry) && fs.statSync(pageRoutesEntry).isDirectory())) {
|
|
165
|
+
throw new Error(
|
|
166
|
+
`generate file system routes error, ${pageRoutesEntry} should be directory.`
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
let routes = [];
|
|
170
|
+
recursiveReadDir({
|
|
171
|
+
dir: pageRoutesEntry,
|
|
172
|
+
routes,
|
|
173
|
+
basePath: "/",
|
|
174
|
+
srcDirectory,
|
|
175
|
+
srcAlias
|
|
176
|
+
});
|
|
177
|
+
const internalComponentsDir = path.resolve(
|
|
178
|
+
internalDirectory,
|
|
179
|
+
`${entryName}/${FILE_SYSTEM_ROUTES_COMPONENTS_DIR}`
|
|
180
|
+
);
|
|
181
|
+
fs.emptyDirSync(internalComponentsDir);
|
|
182
|
+
routes = normalizeNestedRoutes(
|
|
183
|
+
routes,
|
|
184
|
+
internalComponentsDir,
|
|
185
|
+
internalDirectory,
|
|
186
|
+
internalDirAlias
|
|
187
|
+
);
|
|
188
|
+
parents.length = 0;
|
|
189
|
+
routes.sort((a, b) => {
|
|
190
|
+
const delta = getRouteWeight(a.path) - getRouteWeight(b.path);
|
|
191
|
+
if (delta === 0) {
|
|
192
|
+
return a.path.length - b.path.length;
|
|
193
|
+
}
|
|
194
|
+
return delta;
|
|
195
|
+
});
|
|
196
|
+
debug(`fileSystem routes: %o`, routes);
|
|
197
|
+
return routes;
|
|
198
|
+
};
|
|
199
|
+
export {
|
|
200
|
+
getClientRoutes
|
|
201
|
+
};
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { fs } from "@modern-js/utils";
|
|
3
|
+
import { makeLegalIdentifier } from "../makeLegalIdentifier";
|
|
4
|
+
import {
|
|
5
|
+
FILE_SYSTEM_ROUTES_COMPONENTS_DIR,
|
|
6
|
+
FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP,
|
|
7
|
+
FILE_SYSTEM_ROUTES_INDEX,
|
|
8
|
+
FILE_SYSTEM_ROUTES_LAYOUT
|
|
9
|
+
} from "../constants";
|
|
10
|
+
import { replaceWithAlias } from "../utils";
|
|
11
|
+
import { debug, findLayout, shouldSkip, getRouteWeight } from "./utils";
|
|
12
|
+
const compName = (srcDirectory, filePath) => {
|
|
13
|
+
const legalCompName = makeLegalIdentifier(
|
|
14
|
+
path.relative(srcDirectory, filePath)
|
|
15
|
+
);
|
|
16
|
+
return `Comp_${legalCompName}`;
|
|
17
|
+
};
|
|
18
|
+
const layoutNameAbbr = (filePath) => {
|
|
19
|
+
const prefix = "L_";
|
|
20
|
+
const dirName = path.dirname(filePath).split("/").pop() || "";
|
|
21
|
+
return `${prefix}${makeLegalIdentifier(dirName)}`;
|
|
22
|
+
};
|
|
23
|
+
const parents = [];
|
|
24
|
+
const recursiveReadDirLegacy = ({
|
|
25
|
+
dir,
|
|
26
|
+
routes,
|
|
27
|
+
basePath = "/",
|
|
28
|
+
srcDirectory,
|
|
29
|
+
srcAlias
|
|
30
|
+
}) => {
|
|
31
|
+
let hasDynamicRoute = false;
|
|
32
|
+
let resetParent = false;
|
|
33
|
+
let parent = parents[parents.length - 1];
|
|
34
|
+
const layout = findLayout(dir);
|
|
35
|
+
if (layout) {
|
|
36
|
+
if (basePath === "/") {
|
|
37
|
+
throw new Error(`should use _app instead of _layout in ${dir}`);
|
|
38
|
+
} else {
|
|
39
|
+
const alias = replaceWithAlias(srcDirectory, layout, srcAlias);
|
|
40
|
+
const componentName = compName(srcDirectory, layout);
|
|
41
|
+
const route = {
|
|
42
|
+
path: `${basePath.substring(0, basePath.length - 1)}`,
|
|
43
|
+
exact: false,
|
|
44
|
+
routes: [],
|
|
45
|
+
_component: alias,
|
|
46
|
+
component: componentName,
|
|
47
|
+
parent
|
|
48
|
+
};
|
|
49
|
+
parent = route;
|
|
50
|
+
resetParent = true;
|
|
51
|
+
routes.push(route);
|
|
52
|
+
parents.push(route);
|
|
53
|
+
routes = route.routes;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
for (const relative of fs.readdirSync(dir)) {
|
|
57
|
+
const filePath = path.join(dir, relative);
|
|
58
|
+
if (!shouldSkip(filePath)) {
|
|
59
|
+
const filename = path.basename(filePath, path.extname(filePath));
|
|
60
|
+
const alias = replaceWithAlias(srcDirectory, filePath, srcAlias);
|
|
61
|
+
const componentName = compName(srcDirectory, filePath);
|
|
62
|
+
const dynamicRouteMatched = FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP.exec(filename);
|
|
63
|
+
if (dynamicRouteMatched) {
|
|
64
|
+
if (hasDynamicRoute) {
|
|
65
|
+
throw new Error(
|
|
66
|
+
`Can't set two dynamic route in one directory: ${dir}`
|
|
67
|
+
);
|
|
68
|
+
} else {
|
|
69
|
+
hasDynamicRoute = true;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const route = {
|
|
73
|
+
path: `${basePath}${dynamicRouteMatched ? `:${dynamicRouteMatched[1]}${dynamicRouteMatched[2]}` : filename}`,
|
|
74
|
+
_component: alias,
|
|
75
|
+
component: componentName,
|
|
76
|
+
exact: true,
|
|
77
|
+
parent
|
|
78
|
+
};
|
|
79
|
+
if (fs.statSync(filePath).isDirectory()) {
|
|
80
|
+
recursiveReadDirLegacy({
|
|
81
|
+
dir: filePath,
|
|
82
|
+
routes,
|
|
83
|
+
basePath: `${route.path}/`,
|
|
84
|
+
srcDirectory,
|
|
85
|
+
srcAlias
|
|
86
|
+
});
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (filename === FILE_SYSTEM_ROUTES_LAYOUT) {
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
if (filename === FILE_SYSTEM_ROUTES_INDEX) {
|
|
93
|
+
route.path = basePath === "/" ? basePath : `${basePath.substring(0, basePath.length - 1)}`;
|
|
94
|
+
}
|
|
95
|
+
if (filename === "404" && basePath === "/") {
|
|
96
|
+
route.path = "*";
|
|
97
|
+
route.exact = false;
|
|
98
|
+
}
|
|
99
|
+
routes.push(route);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (resetParent) {
|
|
103
|
+
parents.pop();
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
const normalizeNestedRoutes = (nested, internalComponentsDir, internalDirectory, internalDirAlias) => {
|
|
107
|
+
const flat = (routes) => routes.reduce(
|
|
108
|
+
(memo, route) => memo.concat(Array.isArray(route.routes) ? flat(route.routes) : [route]),
|
|
109
|
+
[]
|
|
110
|
+
);
|
|
111
|
+
const generate = (route) => {
|
|
112
|
+
const codes = [];
|
|
113
|
+
let lastComponent = route.component;
|
|
114
|
+
const imports = [
|
|
115
|
+
`import React from 'react';`,
|
|
116
|
+
`import ${lastComponent} from '${route._component}'`
|
|
117
|
+
];
|
|
118
|
+
while (route = route.parent) {
|
|
119
|
+
const layoutComponent = route.component;
|
|
120
|
+
const layoutComponentAbbr = layoutNameAbbr(route._component);
|
|
121
|
+
imports.push(`import ${layoutComponent} from '${route._component}';`);
|
|
122
|
+
const currentComponent = `${layoutComponentAbbr}_${lastComponent}`;
|
|
123
|
+
codes.push(
|
|
124
|
+
`const ${currentComponent} = props => <${layoutComponent} Component={${lastComponent}} {...props} />;`
|
|
125
|
+
);
|
|
126
|
+
lastComponent = currentComponent;
|
|
127
|
+
}
|
|
128
|
+
const file = path.resolve(internalComponentsDir, `${lastComponent}.jsx`);
|
|
129
|
+
fs.outputFileSync(
|
|
130
|
+
file,
|
|
131
|
+
`${imports.join("\n")}
|
|
132
|
+
${codes.join(
|
|
133
|
+
"\n"
|
|
134
|
+
)}
|
|
135
|
+
export default ${lastComponent}`
|
|
136
|
+
);
|
|
137
|
+
return {
|
|
138
|
+
component: lastComponent,
|
|
139
|
+
_component: replaceWithAlias(internalDirectory, file, internalDirAlias)
|
|
140
|
+
};
|
|
141
|
+
};
|
|
142
|
+
const normalized = flat(nested).map(
|
|
143
|
+
(route) => route.parent ? { ...route, ...generate(route), parent: void 0 } : { ...route, parent: void 0 }
|
|
144
|
+
);
|
|
145
|
+
return normalized;
|
|
146
|
+
};
|
|
147
|
+
const getClientRoutes = ({
|
|
148
|
+
entrypoint,
|
|
149
|
+
srcDirectory,
|
|
150
|
+
srcAlias,
|
|
151
|
+
internalDirectory,
|
|
152
|
+
internalDirAlias
|
|
153
|
+
}) => {
|
|
154
|
+
const { entry, entryName } = entrypoint;
|
|
155
|
+
if (!fs.existsSync(entry)) {
|
|
156
|
+
throw new Error(
|
|
157
|
+
`generate file system routes error, ${entry} directory not found.`
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
if (!(fs.existsSync(entry) && fs.statSync(entry).isDirectory())) {
|
|
161
|
+
throw new Error(
|
|
162
|
+
`generate file system routes error, ${entry} should be directory.`
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
let routes = [];
|
|
166
|
+
recursiveReadDirLegacy({
|
|
167
|
+
dir: entry,
|
|
168
|
+
routes,
|
|
169
|
+
basePath: "/",
|
|
170
|
+
srcDirectory,
|
|
171
|
+
srcAlias
|
|
172
|
+
});
|
|
173
|
+
const internalComponentsDir = path.resolve(
|
|
174
|
+
internalDirectory,
|
|
175
|
+
`${entryName}/${FILE_SYSTEM_ROUTES_COMPONENTS_DIR}`
|
|
176
|
+
);
|
|
177
|
+
fs.emptyDirSync(internalComponentsDir);
|
|
178
|
+
routes = normalizeNestedRoutes(
|
|
179
|
+
routes,
|
|
180
|
+
internalComponentsDir,
|
|
181
|
+
internalDirectory,
|
|
182
|
+
internalDirAlias
|
|
183
|
+
);
|
|
184
|
+
parents.length = 0;
|
|
185
|
+
routes.sort((a, b) => {
|
|
186
|
+
const delta = getRouteWeight(a.path) - getRouteWeight(b.path);
|
|
187
|
+
if (delta === 0) {
|
|
188
|
+
return a.path.length - b.path.length;
|
|
189
|
+
}
|
|
190
|
+
return delta;
|
|
191
|
+
});
|
|
192
|
+
debug(`fileSystem routes: %o`, routes);
|
|
193
|
+
return routes;
|
|
194
|
+
};
|
|
195
|
+
export {
|
|
196
|
+
getClientRoutes
|
|
197
|
+
};
|