// Copyright 2024 The Lynx Authors. All rights reserved. // Licensed under the Apache License Version 2.0 that can be found in the // LICENSE file in the root directory of this source tree. import type { BannerPluginArgument, Chunk, Compiler } from '@rspack/core'; import { RuntimeGlobals } from '@lynx-js/webpack-runtime-globals'; /** * The options for RuntimeWrapperWebpackPluginOptions * * @public */ interface RuntimeWrapperWebpackPluginOptions { /** * {@inheritDoc @lynx-js/react-rsbuild-plugin#PluginReactLynxOptions.targetSdkVersion} */ targetSdkVersion: string; /** * Wrap the emitted JS assets whose filenames match this condition. * Assets marked as main thread are never wrapped. * * @defaultValue `/\.js$/` * * @public */ test: Extract['test']; /** * There are two types of banners: script(for app-service) and bundle(for js-render). You can decide to use which banner by the filename. * * @defaultValue `() => 'script'` * * @public */ bannerType: (filename: string) => 'script' | 'bundle'; /** * A function that receives the default variable list and returns the list to inject. */ injectVars?: ((vars: string[]) => string[]) | string[]; /** * {@inheritDoc @lynx-js/react-rsbuild-plugin#PluginReactLynxOptions.experimental_isLazyBundle} */ experimental_isLazyBundle?: boolean; /** * {@inheritDoc @lynx-js/react-rsbuild-plugin#PluginReactLynxOptions.experimental_reEvalJSOnReload} * * @defaultValue false */ experimental_reEvalJSOnReload?: boolean; } const defaultInjectVars = [ 'Card', 'setTimeout', 'setInterval', 'clearInterval', 'clearTimeout', 'NativeModules', 'tt', 'console', 'Component', 'ReactLynx', 'nativeAppId', 'Behavior', 'LynxJSBI', 'lynx', // BOM API 'window', 'document', 'frames', 'self', 'location', 'navigator', 'localStorage', 'history', 'Caches', 'screen', 'alert', 'confirm', 'prompt', 'fetch', 'XMLHttpRequest', '__WebSocket__', // We would provide `WebSocket` using `ProvidePlugin` 'webkit', 'Reporter', 'print', 'global', // Lynx API 'requestAnimationFrame', 'cancelAnimationFrame', ]; /** * RuntimeWrapperWebpackPlugin adds runtime wrappers to JavaScript and allow to be loaded by Lynx. * * @public */ class RuntimeWrapperWebpackPlugin { constructor( private readonly options: Partial = {}, ) {} /** * `defaultOptions` is the default options that the {@link RuntimeWrapperWebpackPlugin} uses. * * @public */ static defaultOptions: Readonly< Required > = Object.freeze>({ targetSdkVersion: '3.2', test: /\.js$/, bannerType: () => 'script', injectVars: defaultInjectVars, experimental_isLazyBundle: false, experimental_reEvalJSOnReload: false, }); /** * The entry point of a webpack plugin. * @param compiler - the webpack compiler */ apply(compiler: Compiler): void { new RuntimeWrapperWebpackPluginImpl( compiler, Object.assign( {}, RuntimeWrapperWebpackPlugin.defaultOptions, this.options, ), ); } } export { RuntimeWrapperWebpackPlugin }; export type { RuntimeWrapperWebpackPluginOptions }; const defaultInjectStr = defaultInjectVars.join(','); class RuntimeWrapperWebpackPluginImpl { name = 'RuntimeWrapperWebpackPlugin'; constructor( public compiler: Compiler, public options: RuntimeWrapperWebpackPluginOptions, ) { const { targetSdkVersion, test, experimental_isLazyBundle } = options; const isDev = process.env['NODE_ENV'] === 'development' || compiler.options.mode === 'development'; let injectStr = defaultInjectStr; if (typeof options.injectVars === 'function') { injectStr = options.injectVars(defaultInjectVars).join(','); } const iife = compiler.options.output.iife ?? true; const wrapperHeader = ( { chunk, filename, supportsReEval }: { chunk: Chunk; filename: string; supportsReEval: boolean; }, ) => { const banner = this.#getBannerType(filename) === 'script' ? loadScriptBanner() : loadBundleBanner(); return banner + amdBanner({ // TODO: config injectStr, overrideRuntimePromise: true, moduleId: '[name].js', targetSdkVersion, iife, supportsReEval, }) // In standalone lazy bundle mode, the lazy bundle will // also has chunk.id "main", it will be conflict with the // consumer project. // We disable it for standalone lazy bundle since we do not // support HMR for standalone lazy bundle now. + (isDev && !experimental_isLazyBundle ? lynxChunkEntries(JSON.stringify(chunk.id)) : ''); }; const wrapperFooter = (filename: string) => { const footer = this.#getBannerType(filename) === 'script' ? loadScriptFooter : loadBundleFooter; return amdFooter('[name].js', iife) + footer; }; compiler.hooks.thisCompilation.tap(this.name, (compilation) => { const { ModuleFilenameHelpers, sources: { ConcatSource } } = compiler.webpack; compilation.hooks.processAssets.tap( { name: this.name, // Wrap at PROCESS_ASSETS_STAGE_NONE (0), after the release BannerPlugins // the legacy source-map plugin + debug-metadata, at/around ADDITIONS // (-100) so the wrapper stays outermost and their `_SetSourceMapRelease` // runtimes land INSIDE it, yet still before DEV_TOOLING so the source map // accounts for it. stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_NONE, }, () => { for (const chunk of compilation.chunks) { for (const filename of chunk.files) { if ( !ModuleFilenameHelpers.matchObject({ test: test! }, filename) ) { continue; } if (compilation.getAsset(filename)?.info['lynx:main-thread']) { continue; } const data = { chunk, filename, supportsReEval: Boolean( options.experimental_reEvalJSOnReload && !experimental_isLazyBundle, ), }; const header = compilation.getPath(wrapperHeader(data), data); const footer = compilation.getPath(wrapperFooter(filename), data); compilation.updateAsset( filename, (source) => new ConcatSource(header, '\n', source, '\n', footer), ); } } }, ); }); } #getBannerType(filename: string) { const bannerType = this.options.bannerType(filename); if (bannerType !== 'bundle' && bannerType !== 'script') { throw new Error( `The return value of the bannerType expects the 'bundle' or 'script', but received '${bannerType as string}'.`, ); } return bannerType; } } function lynxChunkEntries(chunkId: string) { return `// lynx chunks entries if (!${RuntimeGlobals.lynxChunkEntries}) { // Initialize once ${RuntimeGlobals.lynxChunkEntries} = {}; } if (!${RuntimeGlobals.lynxChunkEntries}[${chunkId}]) { ${RuntimeGlobals.lynxChunkEntries}[${chunkId}] = globDynamicComponentEntry; } else { globDynamicComponentEntry = ${RuntimeGlobals.lynxChunkEntries}[${chunkId}]; } `; } // This is Lynx js runtime code. It will wrap user code (e. app-service.js, chunk.js) // with two function wrapper, the outer function provide some utility functions, such as // * `define` objects that the frontend framework implementor can use to provide some global-looking variables // that are actually specific to the module in the inner function wrapper, such as // * The `module` and `exports` are reserved objects that used to export values from the module in cjs, Lynx // use `requireModule`/`requireModuleAsync` & return object to do `require` / `export` staffs rather than them. // * common JS API `setTimeout`, `setInterval`, `clearInterval`, `clearTimeout`, `NativeModules`, `console`, // * `nativeAppId`, `lynx`, `LynxJSBI` // Besides these, runtime code also contains some injected code to provide certain features. // For example, the Promise object is replaced with a call to "getPromise" when `overrideRuntimePromise` is true. const loadScriptBanner = (strictMode = true) => `(function(){ ${strictMode ? '\'use strict\';' : ';'} var g = globalThis; function __init_card_bundle__(lynxCoreInject) { g.__bundle__holder = undefined; var globDynamicComponentEntry = g.globDynamicComponentEntry || '__Card__'; var tt = lynxCoreInject.tt;`; const loadBundleBanner = (strictMode = true) => `(function(){ ${strictMode ? '\'use strict\';' : ';'} var g = globalThis; function initBundle(lynxCoreInject) { var tt = lynxCoreInject.tt; `; const amdBanner = ({ injectStr, moduleId, overrideRuntimePromise, targetSdkVersion, iife, supportsReEval, }: { injectStr: string; moduleId: string; overrideRuntimePromise: boolean; targetSdkVersion: string; iife: boolean; supportsReEval: boolean; }) => { const iifeWrapper = iife ? '' : ` // This needs to be wrapped in an IIFE because it needs to be isolated against Lynx injected variables. (() => {`; return ( ` tt.define("${moduleId}", function(require, module, exports, ${injectStr}) { lynx = lynx || {}; lynx.targetSdkVersion=lynx.targetSdkVersion||${ JSON.stringify(targetSdkVersion) }; ${ supportsReEval ? `lynx[Symbol.for('__LYNX_ENTRY_SUPPORTS_RE_EVAL__')] = true;\n` : '' }${overrideRuntimePromise ? `var Promise = lynx.Promise;` : ''} fetch = fetch || lynx.fetch; requestAnimationFrame = requestAnimationFrame || lynx.requestAnimationFrame; cancelAnimationFrame = cancelAnimationFrame || lynx.cancelAnimationFrame; ${iifeWrapper} ` ); }; const amdFooter = (moduleId: string, iife: boolean) => ` ${iife ? '' : '})();'} }); return tt.require("${moduleId}");`; // footer for app-service.js chunk const loadScriptFooter = ` };` // FYI: bundleModuleMode // The loadScript behavior of app-service.js is determined by bundleModuleMode // bundleModuleMode: 'EvalRequire' (default, unable to get the correct error stack.) // ------------ // lynx_core.js // const jsContent = nativeApp.readScript('app-service.js'); // eval(jsContent); // ------------- // bundleModuleMode: 'ReturnByFunction' (most used since 2 yrs ago, 2020-11) // ------------ // lynx_core.js // const bundleInitReturnObj: BundleInitReturnObj = nativeApp.loadScript(appServiceName); // if (bundleInitReturnObj && bundleInitReturnObj.init) { // bundleInitReturnObj.init({ tt }); // } // ------------- + ` if (g && g.bundleSupportLoadScript){ var res = {init: __init_card_bundle__}; g.__bundle__holder = res; return res; } else { __init_card_bundle__({"tt": tt}); }; })(); `; const loadBundleFooter = `} g.initBundle = initBundle; })()`;