Skip to content

feat: support compilation.addRuntimeModule#7925

Merged
LingyuCoder merged 7 commits intomainfrom
feat/add-runtime-module
Sep 20, 2024
Merged

feat: support compilation.addRuntimeModule#7925
LingyuCoder merged 7 commits intomainfrom
feat/add-runtime-module

Conversation

@LingyuCoder
Copy link
Copy Markdown
Contributor

@LingyuCoder LingyuCoder commented Sep 19, 2024

Summary

Support compilation.addRuntimeModule to add custom runtime module:

const { RuntimeModule, RuntimeGlobals } = require("@rspack/core");

class CustomRuntimeModule extends RuntimeModule {
  constructor(chunk) {
    // set name and stage of this runtime module
    super("module-name", RuntimeModule.STAGE_NORMAL);
  }

  shouldIsolate() {
    // if false, will not be wrapped by IIFE
    return true;
  }

  generate(compilation) {
    return `
      // content of this runtime module
      __webpack_require__.xxx = xxx;
    `;
  }
}

module.exports = {
  // ...
  plugins: [
    compiler => {
      compiler.hooks.thisCompilation.tap("CustomRuntimePlugin", (compilation) => {
          compilation.hooks.runtimeRequirementInTree.tap("CustomRuntimePlugin", (chunk, set) => {
            // add dependent runtime globals
            set.add(RuntimeGlobals.publicPath);
            // add custom runtime module
            const m = new CustomRuntimeModule();
            // set fullHash or dependentHash to tell rspack this module is not cacheable
            m.fullHash = true;
            compilation.addRuntimeModule(chunk, m);
          })
        }
      );
    }
  ],
};

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).

@github-actions github-actions bot added release: feature release: feature related release(mr only) team The issue/pr is created by the member of Rspack. labels Sep 19, 2024
@LingyuCoder LingyuCoder force-pushed the feat/runtime-requirements-in-tree branch 2 times, most recently from 271d0f2 to 210af44 Compare September 19, 2024 10:51
Base automatically changed from feat/runtime-requirements-in-tree to main September 19, 2024 11:16
An error occurred while trying to automatically change base from feat/runtime-requirements-in-tree to main September 19, 2024 11:16
@LingyuCoder LingyuCoder force-pushed the feat/add-runtime-module branch from 1061b42 to 5ac50d2 Compare September 20, 2024 03:01
@netlify
Copy link
Copy Markdown

netlify bot commented Sep 20, 2024

Deploy Preview for rspack canceled.

Name Link
🔨 Latest commit 5cd5d63
🔍 Latest deploy log https://app.netlify.com/sites/rspack/deploys/66ed0f6243819600088964bd

@LingyuCoder LingyuCoder force-pushed the feat/add-runtime-module branch from d0921ee to 5cd5d63 Compare September 20, 2024 06:00
@LingyuCoder LingyuCoder marked this pull request as ready for review September 20, 2024 06:14
@LingyuCoder LingyuCoder merged commit ef9dc5b into main Sep 20, 2024
@LingyuCoder LingyuCoder deleted the feat/add-runtime-module branch September 20, 2024 06:30
JSerFeng pushed a commit that referenced this pull request Sep 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release: feature release: feature related release(mr only) team The issue/pr is created by the member of Rspack.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants