Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support compilation.addRuntimeModule #7925

Merged
merged 7 commits into from
Sep 20, 2024
Merged

Conversation

LingyuCoder
Copy link
Collaborator

@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
Copy link

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 merged commit ef9dc5b into main Sep 20, 2024
31 checks passed
@LingyuCoder LingyuCoder deleted the feat/add-runtime-module branch September 20, 2024 06:30
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