From 6e47f9d715934611f214627c716430e6d3bb3a72 Mon Sep 17 00:00:00 2001 From: hardfist Date: Tue, 24 Sep 2024 18:03:36 +0800 Subject: [PATCH] feat: add afterResolve hook usage (#143) --- pnpm-lock.yaml | 9 ++++++++ rspack/hooks/after-resolve/index.html | 12 +++++++++++ rspack/hooks/after-resolve/package.json | 16 ++++++++++++++ rspack/hooks/after-resolve/rspack.config.js | 24 +++++++++++++++++++++ rspack/hooks/after-resolve/src/answer.js | 1 + rspack/hooks/after-resolve/src/index.js | 5 +++++ 6 files changed, 67 insertions(+) create mode 100644 rspack/hooks/after-resolve/index.html create mode 100644 rspack/hooks/after-resolve/package.json create mode 100644 rspack/hooks/after-resolve/rspack.config.js create mode 100644 rspack/hooks/after-resolve/src/answer.js create mode 100644 rspack/hooks/after-resolve/src/index.js diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 627abf38..dec3e58c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1417,6 +1417,15 @@ importers: specifier: ^2.6.0 version: 2.6.0 + rspack/hooks/after-resolve: + devDependencies: + '@rspack/cli': + specifier: ^1.0.3 + version: 1.0.3(@rspack/core@1.0.3(@swc/helpers@0.5.12))(@types/express@4.17.21)(webpack@5.94.0) + '@rspack/core': + specifier: ^1.0.3 + version: 1.0.3(@swc/helpers@0.5.12) + rspack/html-webpack-plugin: devDependencies: '@rspack/cli': diff --git a/rspack/hooks/after-resolve/index.html b/rspack/hooks/after-resolve/index.html new file mode 100644 index 00000000..7bac4f56 --- /dev/null +++ b/rspack/hooks/after-resolve/index.html @@ -0,0 +1,12 @@ + + + + + + + Document + + +
+ + diff --git a/rspack/hooks/after-resolve/package.json b/rspack/hooks/after-resolve/package.json new file mode 100644 index 00000000..e3320696 --- /dev/null +++ b/rspack/hooks/after-resolve/package.json @@ -0,0 +1,16 @@ +{ + "name": "example-hooks-after-resolve", + "version": "1.0.0", + "private": true, + "license": "MIT", + "sideEffects": ["**/*.css", "**/*.less", "**/*.scss"], + "main": "index.js", + "scripts": { + "build": "rspack build", + "dev": "rspack serve" + }, + "devDependencies": { + "@rspack/cli": "^1.0.3", + "@rspack/core": "^1.0.3" + } +} diff --git a/rspack/hooks/after-resolve/rspack.config.js b/rspack/hooks/after-resolve/rspack.config.js new file mode 100644 index 00000000..45f1091d --- /dev/null +++ b/rspack/hooks/after-resolve/rspack.config.js @@ -0,0 +1,24 @@ +const rspack = require('@rspack/core'); +/** @type {import('@rspack/cli').Configuration} */ +const config = { + entry: './src/index.js', + plugins: [ + new rspack.HtmlRspackPlugin({ + template: './index.html', + }), + { + /** + * + * @param {import('@rspack/core').Compiler} compiler + */ + apply(compiler) { + compiler.hooks.normalModuleFactory.tap('nmf', (nmf) => { + nmf.hooks.afterResolve.tap('afterResolver', (data) => { + console.log('data:', data.createData?.resource); + }); + }); + }, + }, + ], +}; +module.exports = config; diff --git a/rspack/hooks/after-resolve/src/answer.js b/rspack/hooks/after-resolve/src/answer.js new file mode 100644 index 00000000..64a32fd2 --- /dev/null +++ b/rspack/hooks/after-resolve/src/answer.js @@ -0,0 +1 @@ +export const answer = 42; diff --git a/rspack/hooks/after-resolve/src/index.js b/rspack/hooks/after-resolve/src/index.js new file mode 100644 index 00000000..cfbdbfb8 --- /dev/null +++ b/rspack/hooks/after-resolve/src/index.js @@ -0,0 +1,5 @@ +import { answer } from './answer'; +function render() { + document.getElementById('root').innerHTML = `the answer to the universe is ${answer}`; +} +render();