-
Notifications
You must be signed in to change notification settings - Fork 31
Create Plugin: Introduce experimental Rspack feature flag #1564
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
Open
jackw
wants to merge
11
commits into
main
Choose a base branch
from
jackw/feature-rspack
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9fdbd2a
feat(create-plugin): introduce rspack feature flag
jackw dc79ffc
feat(create-plugin): add rspack configs and use featureflag to write …
jackw 7838f78
feat(create-plugin): set npm scripts based on bundler choice
jackw af4c4b4
feat(create-plugin): provide a functioning rspack config file
jackw bcba63c
feat(create-plugin): depending on feature flag remove unused bundler …
jackw d83942b
feat(create-plugin): add basic rspack live reload plugin
jackw 3ff8334
feat(create-plugin): update rspack config, bump to 1.2.4 for sri plugin
jackw 83bae5f
refactor(create-plugin): move deletion of webpack directory if flag e…
jackw d4aaa0e
fix(create-plugin): make package.json template valid json
jackw be7d392
fix(create-plugin): fix rspack custom plugins and remove logging
jackw e8e9b29
fix(create-plugin): remove output.clean as incompatible with regex ri…
jackw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
packages/create-plugin/templates/common/.config/rspack/BuildModeRspackPlugin.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import * as webpack from 'webpack'; | ||
|
||
const PLUGIN_NAME = 'BuildModeRspackPlugin'; | ||
|
||
export class BuildModeRspackPlugin { | ||
apply(compiler: webpack.Compiler) { | ||
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => { | ||
compilation.hooks.processAssets.tap( | ||
{ | ||
name: PLUGIN_NAME, | ||
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS, | ||
}, | ||
(assets) => { | ||
const assetName = 'plugin.json'; | ||
const asset = assets[assetName]; | ||
if (!asset) { | ||
return; | ||
} | ||
|
||
const { RawSource } = compiler.webpack.sources; | ||
const pluginJsonContent = JSON.parse(asset.source().toString()); | ||
const pluginJsonWithBuildMode = JSON.stringify( | ||
{ | ||
...pluginJsonContent, | ||
buildMode: compilation.options.mode, | ||
}, | ||
null, | ||
4 | ||
); | ||
const source = new RawSource(pluginJsonWithBuildMode); | ||
compilation.updateAsset(assetName, source); | ||
} | ||
); | ||
}); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
packages/create-plugin/templates/common/.config/rspack/constants.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const SOURCE_DIR = 'src'; | ||
export const DIST_DIR = 'dist'; |
110 changes: 110 additions & 0 deletions
110
packages/create-plugin/templates/common/.config/rspack/liveReloadPlugin.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
const path = require('path'); | ||
const WebSocket = require('ws'); | ||
const http = require('http'); | ||
|
||
class RspackLiveReloadPlugin { | ||
constructor(options = {}) { | ||
this.options = Object.assign( | ||
{ | ||
port: 35729, | ||
delay: 0, | ||
appendScriptTag: true, | ||
protocol: 'http', | ||
}, | ||
options | ||
); | ||
} | ||
|
||
apply(compiler) { | ||
const isRspack = compiler.rspack !== undefined; | ||
if (!isRspack) { | ||
throw new Error('This plugin is designed to work with Rspack 1'); | ||
} | ||
|
||
compiler.hooks.afterEmit.tap('RspackLiveReloadPlugin', (compilation) => { | ||
this._startServer(); | ||
this._notifyClient(); | ||
}); | ||
|
||
compiler.hooks.done.tap('RspackLiveReloadPlugin', (stats) => { | ||
if (this.options.appendScriptTag) { | ||
this._injectLiveReloadScript(stats.compilation); | ||
} | ||
}); | ||
} | ||
|
||
_startServer() { | ||
if (this.server) { | ||
return; | ||
} | ||
|
||
const port = this.options.port; | ||
|
||
this.httpServer = http.createServer((req, res) => { | ||
if (req.url === '/livereload.js') { | ||
res.writeHead(200, { 'Content-Type': 'application/javascript' }); | ||
res.end(this._getLiveReloadScript()); | ||
} else { | ||
res.writeHead(404); | ||
res.end('Not Found'); | ||
} | ||
}); | ||
|
||
this.server = new WebSocket.Server({ server: this.httpServer }); | ||
this.httpServer.listen(port, () => { | ||
console.log(`LiveReload server started on http://localhost:${port}`); | ||
}); | ||
} | ||
|
||
_notifyClient() { | ||
if (!this.server) { | ||
return; | ||
} | ||
|
||
this.server.clients.forEach((client) => { | ||
if (client.readyState === WebSocket.OPEN) { | ||
client.send(JSON.stringify({ action: 'reload' })); | ||
} | ||
}); | ||
} | ||
|
||
_injectLiveReloadScript(compilation) { | ||
compilation.hooks.processAssets.tap( | ||
{ | ||
name: 'RspackLiveReloadPlugin', | ||
stage: compilation.PROCESS_ASSETS_STAGE_ADDITIONAL, | ||
}, | ||
(assets) => { | ||
Object.keys(assets).forEach((filename) => { | ||
if (path.extname(filename) === '.html') { | ||
const assetSource = compilation.getAsset(filename).source; | ||
const updatedSource = assetSource | ||
.source() | ||
.replace('</body>', `<script src="http://localhost:${this.options.port}/livereload.js"></script></body>`); | ||
compilation.updateAsset(filename, { | ||
source: () => updatedSource, | ||
size: () => updatedSource.length, | ||
}); | ||
} | ||
}); | ||
} | ||
); | ||
} | ||
|
||
_getLiveReloadScript() { | ||
return ` | ||
(function() { | ||
if (typeof WebSocket === 'undefined') return; | ||
const ws = new WebSocket('${this.options.protocol}://localhost:${this.options.port}'); | ||
ws.onmessage = function(event) { | ||
const data = JSON.parse(event.data); | ||
if (data.action === 'reload') { | ||
window.location.reload(); | ||
} | ||
}; | ||
})(); | ||
`; | ||
} | ||
} | ||
|
||
module.exports = RspackLiveReloadPlugin; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this be a random port?