apply()

in fusion-cli/build/plugins/instrumented-import-dependency-template-plugin.js [109:148]


  apply(dep /*: any */, source /*: any */, runtime /*: any */) {
    const depBlock = dep.block;
    let chunkIds = [];
    let translationKeys = [];
    if (dep instanceof InstrumentedImportDependency) {
      const instrumentation = dep.getInstrumentation();
      chunkIds = instrumentation.chunkIds;
      translationKeys = instrumentation.translationKeys;
    } else if (this.clientChunkIndex) {
      // Template invoked without InstrumentedImportDependency
      // server-side, use values from client bundle
      const ids = this.clientChunkIndex.get(getModuleResource(dep.module));
      chunkIds = ids ? Array.from(ids) : [];
    } else {
      // Prevent future developers from creating a broken webpack state
      throw new Error(
        'Dependency is not Instrumented and lacks a clientChunkIndex'
      );
    }
    const content = runtime.moduleNamespacePromise({
      block: dep.block,
      module: dep.module,
      request: dep.request,
      strict: dep.originModule.buildMeta.strictHarmonyModule,
      message: 'import()',
    });
    // Add the following properties to the promise returned by import()
    // - `__CHUNK_IDS`: the webpack chunk ids for the dynamic import
    // - `__MODULE_ID`: the webpack module id of the dynamically imported module. Equivalent to require.resolveWeak(path)
    // - `__I18N_KEYS`: the translation keys used in the client chunk group for this import()
    const customContent = chunkIds
      ? `Object.defineProperties(${content}, {
        "__CHUNK_IDS": {value:${JSON.stringify(chunkIds)}},
        "__MODULE_ID": {value:${JSON.stringify(dep.module.id)}},
        "__I18N_KEYS": {value:${JSON.stringify(translationKeys)}}
        })`
      : content;
    // replace with `customContent` instead of `content`
    source.replace(depBlock.range[0], depBlock.range[1] - 1, customContent);
  }