From b324beeff5741629cd1c8fb1cefb886a10b35b61 Mon Sep 17 00:00:00 2001 From: "shuilan.cj" Date: Fri, 4 Nov 2022 09:50:04 +0800 Subject: [PATCH 1/8] fix: support request in data loader --- examples/with-plugin-request/ice.config.mts | 1 - packages/ice/src/webpack/DataLoaderPlugin.ts | 2 +- .../ice/templates/exports/data-loader.ts.ejs | 11 ++++++--- packages/runtime/src/dataLoader.ts | 24 ++++++++++++++++++- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/examples/with-plugin-request/ice.config.mts b/examples/with-plugin-request/ice.config.mts index 5685132f3..b0076d018 100644 --- a/examples/with-plugin-request/ice.config.mts +++ b/examples/with-plugin-request/ice.config.mts @@ -2,7 +2,6 @@ import { defineConfig } from '@ice/app'; import request from '@ice/plugin-request'; export default defineConfig({ - dataLoader: false, plugins: [ request(), ], diff --git a/packages/ice/src/webpack/DataLoaderPlugin.ts b/packages/ice/src/webpack/DataLoaderPlugin.ts index 2ade8ea3d..703c4aab1 100644 --- a/packages/ice/src/webpack/DataLoaderPlugin.ts +++ b/packages/ice/src/webpack/DataLoaderPlugin.ts @@ -45,7 +45,7 @@ export default class DataLoaderPlugin { }, { swc: { - keepExports: ['getData', 'getAppData'], + keepExports: ['getData', 'getAppData', 'request'], keepPlatform: 'web', getRoutePaths: () => { return getRoutePathsFromCache(this.dataCache); diff --git a/packages/ice/templates/exports/data-loader.ts.ejs b/packages/ice/templates/exports/data-loader.ts.ejs index e3554b974..b7a7659d0 100644 --- a/packages/ice/templates/exports/data-loader.ts.ejs +++ b/packages/ice/templates/exports/data-loader.ts.ejs @@ -1,7 +1,8 @@ import { dataLoader } from '@ice/runtime'; -<% if(hasExportAppData) {-%>import { getAppData } from '@/app';<% } -%> +<% if(hasExportAppData) {-%>import * as app from '@/app';<% } -%> <% if(dataLoaderImport.imports) {-%><%-dataLoaderImport.imports%><% } -%> +import { statics } from './runtimeModules'; <% if (loaders) {-%> <%- loaders %> @@ -9,7 +10,7 @@ import { dataLoader } from '@ice/runtime'; const loaders = {}; <% } -%> -<% if(hasExportAppData) {-%>loaders['__app'] = getAppData;<% } -%> +<% if(hasExportAppData) {-%>loaders['__app'] = app.getAppData;<% } -%> <% if(!dataLoaderImport.imports) {-%> let fetcher = (options) => { @@ -17,4 +18,8 @@ let fetcher = (options) => { } <% } -%> -dataLoader.init(loaders, fetcher); +dataLoader.init(loaders, { + fetcher, + runtimeModules: statics, + appExport: app, +}); diff --git a/packages/runtime/src/dataLoader.ts b/packages/runtime/src/dataLoader.ts index 8b2914f18..aee07cdea 100644 --- a/packages/runtime/src/dataLoader.ts +++ b/packages/runtime/src/dataLoader.ts @@ -110,11 +110,33 @@ function getLoaders(loadersConfig: LoadersConfig, fetcher: Function): Loaders { return loaders; } +interface Options { + fetcher: Function; + runtimeModules: RuntimeModules['static']; + appExport: AppExport; +} + /** * Load initial data and register global loader. * In order to load data, JavaScript modules, CSS and other assets in parallel. */ -function init(loadersConfig: LoadersConfig, fetcher: Function) { +async function init(loadersConfig: LoadersConfig, options: Options) { + const { + fetcher, + runtimeModules, + appExport, + } = options; + + const runtimeApi = { + appContext: { + appExport, + }, + }; + + if (runtimeModules) { + await Promise.all(runtimeModules.map(m => m(runtimeApi)).filter(Boolean)); + } + const loaders = getLoaders(loadersConfig, fetcher); try { From f89dacbb5b49af9ebaa3a85253f6d2483907c316 Mon Sep 17 00:00:00 2001 From: "shuilan.cj" Date: Fri, 4 Nov 2022 10:27:22 +0800 Subject: [PATCH 2/8] fix: type --- packages/runtime/src/dataLoader.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/runtime/src/dataLoader.ts b/packages/runtime/src/dataLoader.ts index aee07cdea..e27bc4831 100644 --- a/packages/runtime/src/dataLoader.ts +++ b/packages/runtime/src/dataLoader.ts @@ -1,4 +1,4 @@ -import type { GetData, GetDataConfig } from './types.js'; +import type { GetData, GetDataConfig, RuntimeModules, AppExport } from './types.js'; import getRequestContext from './requestContext.js'; interface Loaders { From 3c96d7d6d8b39bff401b0ea1c0582842704b4951 Mon Sep 17 00:00:00 2001 From: "shuilan.cj" Date: Fri, 4 Nov 2022 10:37:10 +0800 Subject: [PATCH 3/8] fix: type --- packages/runtime/src/dataLoader.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/runtime/src/dataLoader.ts b/packages/runtime/src/dataLoader.ts index e27bc4831..47a5d00d3 100644 --- a/packages/runtime/src/dataLoader.ts +++ b/packages/runtime/src/dataLoader.ts @@ -112,7 +112,7 @@ function getLoaders(loadersConfig: LoadersConfig, fetcher: Function): Loaders { interface Options { fetcher: Function; - runtimeModules: RuntimeModules['static']; + runtimeModules: RuntimeModules['statics']; appExport: AppExport; } From 46f5555e31577e4414d5247b6c70b2cf3810f743 Mon Sep 17 00:00:00 2001 From: "shuilan.cj" Date: Fri, 4 Nov 2022 11:32:46 +0800 Subject: [PATCH 4/8] fix: ts error --- packages/runtime/src/dataLoader.ts | 7 +++++-- packages/runtime/src/types.ts | 8 +++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/runtime/src/dataLoader.ts b/packages/runtime/src/dataLoader.ts index 47a5d00d3..b7e9bd8ce 100644 --- a/packages/runtime/src/dataLoader.ts +++ b/packages/runtime/src/dataLoader.ts @@ -1,4 +1,4 @@ -import type { GetData, GetDataConfig, RuntimeModules, AppExport } from './types.js'; +import type { GetData, GetDataConfig, RuntimeModules, AppExport, RuntimePlugin, CommonJsRuntime } from './types.js'; import getRequestContext from './requestContext.js'; interface Loaders { @@ -134,7 +134,10 @@ async function init(loadersConfig: LoadersConfig, options: Options) { }; if (runtimeModules) { - await Promise.all(runtimeModules.map(m => m(runtimeApi)).filter(Boolean)); + await Promise.all(runtimeModules.map(module => { + const runtimeModule = (module as CommonJsRuntime).default || module as RuntimePlugin; + return runtimeModule(runtimeApi); + }).filter(Boolean)); } const loaders = getLoaders(loadersConfig, fetcher); diff --git a/packages/runtime/src/types.ts b/packages/runtime/src/types.ts index c7145d4c1..60d3a7e97 100644 --- a/packages/runtime/src/types.ts +++ b/packages/runtime/src/types.ts @@ -171,9 +171,15 @@ export interface RuntimeAPI { useAppContext: UseAppContext; } +export interface StaticRuntimeAPI { + appContext: { + appExport: AppExport; + }; +} + export interface RuntimePlugin { ( - apis: RuntimeAPI, + apis: RuntimeAPI | StaticRuntimeAPI, runtimeOptions?: Record, ): Promise | void; } From c71bf2f692814c3520940c197a83e07e17cf86da Mon Sep 17 00:00:00 2001 From: "shuilan.cj" Date: Fri, 4 Nov 2022 11:49:41 +0800 Subject: [PATCH 5/8] fix: type --- packages/runtime/src/types.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/runtime/src/types.ts b/packages/runtime/src/types.ts index 60d3a7e97..3dad4633c 100644 --- a/packages/runtime/src/types.ts +++ b/packages/runtime/src/types.ts @@ -179,7 +179,14 @@ export interface StaticRuntimeAPI { export interface RuntimePlugin { ( - apis: RuntimeAPI | StaticRuntimeAPI, + apis: RuntimeAPI, + runtimeOptions?: Record, + ): Promise | void; +} + +export interface RuntimePlugin { + ( + apis: StaticRuntimeAPI, runtimeOptions?: Record, ): Promise | void; } From b3a4193c0e559a08bf5b31da935c8fb76b0c23ec Mon Sep 17 00:00:00 2001 From: "shuilan.cj" Date: Fri, 4 Nov 2022 15:44:42 +0800 Subject: [PATCH 6/8] feat: support config exports in plugin --- packages/ice/src/plugins/web/index.ts | 4 ++-- packages/ice/src/types/plugin.ts | 1 + packages/ice/src/webpack/DataLoaderPlugin.ts | 19 ++++++++++++++++--- packages/plugin-request/src/index.ts | 1 + 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/packages/ice/src/plugins/web/index.ts b/packages/ice/src/plugins/web/index.ts index c99971361..c099366d3 100644 --- a/packages/ice/src/plugins/web/index.ts +++ b/packages/ice/src/plugins/web/index.ts @@ -13,7 +13,7 @@ import getServerCompilerPlugin from '../../utils/getServerCompilerPlugin.js'; const plugin: Plugin = () => ({ name: 'plugin-web', - setup: ({ registerTask, onHook, context, generator, serverCompileTask, dataCache }) => { + setup: ({ registerTask, onHook, context, generator, serverCompileTask, dataCache, getAllPlugin }) => { const { rootDir, commandArgs, command, userConfig } = context; const { ssg } = userConfig; @@ -52,7 +52,7 @@ const plugin: Plugin = () => ({ serverOutfile = path.join(outputDir, SERVER_OUTPUT_DIR, `index${userConfig?.server?.format === 'esm' ? '.mjs' : '.cjs'}`); webpackConfigs[0].plugins.push( // Add webpack plugin of data-loader in web task - new DataLoaderPlugin({ serverCompiler, rootDir, dataCache }), + new DataLoaderPlugin({ serverCompiler, rootDir, dataCache, getAllPlugin }), // Add ServerCompilerPlugin getServerCompilerPlugin(serverCompiler, { rootDir, diff --git a/packages/ice/src/types/plugin.ts b/packages/ice/src/types/plugin.ts index 2b92e0b1f..10162a6a6 100644 --- a/packages/ice/src/types/plugin.ts +++ b/packages/ice/src/types/plugin.ts @@ -131,6 +131,7 @@ export interface OverwritePluginAPI extends ExtendsPluginAPI { export interface PluginData extends _Plugin { runtime?: string; staticRuntime?: boolean; + keepExports?: string[]; } export type Plugin = (options?: Options) => PluginData; diff --git a/packages/ice/src/webpack/DataLoaderPlugin.ts b/packages/ice/src/webpack/DataLoaderPlugin.ts index 703c4aab1..d83bc52ed 100644 --- a/packages/ice/src/webpack/DataLoaderPlugin.ts +++ b/packages/ice/src/webpack/DataLoaderPlugin.ts @@ -3,7 +3,8 @@ import fse from 'fs-extra'; import consola from 'consola'; import type { Compiler } from 'webpack'; import webpack from '@ice/bundles/compiled/webpack/index.js'; -import type { ServerCompiler } from '../types/plugin.js'; +import type { Context } from 'build-scripts'; +import type { ServerCompiler, PluginData } from '../types/plugin.js'; import { RUNTIME_TMP_DIR } from '../constant.js'; import { getRoutePathsFromCache } from '../utils/getRoutePaths.js'; @@ -14,19 +15,31 @@ export default class DataLoaderPlugin { private serverCompiler: ServerCompiler; private rootDir: string; private dataCache: Map; + private getAllPlugin: Context['getAllPlugin']; public constructor(options: { serverCompiler: ServerCompiler; rootDir: string; dataCache: Map; + getAllPlugin?: Context['getAllPlugin']; }) { - const { serverCompiler, rootDir, dataCache } = options; + const { serverCompiler, rootDir, dataCache, getAllPlugin } = options; this.serverCompiler = serverCompiler; this.rootDir = rootDir; this.dataCache = dataCache; + this.getAllPlugin = getAllPlugin; } public apply(compiler: Compiler) { + const plugins = this.getAllPlugin(['keepExports']) as PluginData[]; + + let keepExports = ['getData', 'getAppData']; + plugins.forEach(plugin => { + if (plugin.keepExports) { + keepExports = keepExports.concat(plugin.keepExports); + } + }); + compiler.hooks.thisCompilation.tap(pluginName, (compilation) => { compilation.hooks.processAssets.tapAsync({ name: pluginName, @@ -45,7 +58,7 @@ export default class DataLoaderPlugin { }, { swc: { - keepExports: ['getData', 'getAppData', 'request'], + keepExports, keepPlatform: 'web', getRoutePaths: () => { return getRoutePathsFromCache(this.dataCache); diff --git a/packages/plugin-request/src/index.ts b/packages/plugin-request/src/index.ts index a5e9aa1b4..6ac5163e6 100644 --- a/packages/plugin-request/src/index.ts +++ b/packages/plugin-request/src/index.ts @@ -26,6 +26,7 @@ const plugin: Plugin = () => ({ }, runtime: `${PLUGIN_NAME}/esm/runtime`, staticRuntime: true, + keepExports: ['request'], }); export type { From ae16444ac75eb60ca0d37af3f20aee49f02acefa Mon Sep 17 00:00:00 2001 From: "shuilan.cj" Date: Fri, 4 Nov 2022 16:19:13 +0800 Subject: [PATCH 7/8] fix: filter static modules --- .../ice/templates/exports/data-loader.ts.ejs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/ice/templates/exports/data-loader.ts.ejs b/packages/ice/templates/exports/data-loader.ts.ejs index b7a7659d0..c80ce22fb 100644 --- a/packages/ice/templates/exports/data-loader.ts.ejs +++ b/packages/ice/templates/exports/data-loader.ts.ejs @@ -1,8 +1,15 @@ import { dataLoader } from '@ice/runtime'; <% if(hasExportAppData) {-%>import * as app from '@/app';<% } -%> - <% if(dataLoaderImport.imports) {-%><%-dataLoaderImport.imports%><% } -%> -import { statics } from './runtimeModules'; +<% const staticModuleNames = []; -%> +<% if (runtimeModules.length) { -%> + <% runtimeModules.forEach((runtimeModule, index) => { -%> + <% if (runtimeModule.staticRuntime) { -%> + import module<%= index %> from '<%= runtimeModule.path %>'; + <% staticModuleNames.push('module' + index) -%> + <% } -%> + <% }) -%> +<% } -%> <% if (loaders) {-%> <%- loaders %> @@ -18,6 +25,12 @@ let fetcher = (options) => { } <% } -%> +const statics = [ +<% staticModuleNames.forEach((moduleName, index) => { -%> + <%= moduleName %>, +<% }) -%> +]; + dataLoader.init(loaders, { fetcher, runtimeModules: statics, From 979a4c9e8c97dbc53df85bdb2a427768b49ad7f6 Mon Sep 17 00:00:00 2001 From: "shuilan.cj" Date: Fri, 4 Nov 2022 17:53:21 +0800 Subject: [PATCH 8/8] refactor: rename --- packages/ice/templates/exports/data-loader.ts.ejs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/ice/templates/exports/data-loader.ts.ejs b/packages/ice/templates/exports/data-loader.ts.ejs index c80ce22fb..d891d3cc9 100644 --- a/packages/ice/templates/exports/data-loader.ts.ejs +++ b/packages/ice/templates/exports/data-loader.ts.ejs @@ -25,7 +25,8 @@ let fetcher = (options) => { } <% } -%> -const statics = [ +// Only init static runtime in data-loader. +const staticRuntimeModules = [ <% staticModuleNames.forEach((moduleName, index) => { -%> <%= moduleName %>, <% }) -%> @@ -33,6 +34,6 @@ const statics = [ dataLoader.init(loaders, { fetcher, - runtimeModules: statics, + runtimeModules: staticRuntimeModules, appExport: app, });