Skip to content

Commit

Permalink
chore: remove ssr build when production
Browse files Browse the repository at this point in the history
  • Loading branch information
ZLY201 committed Feb 8, 2024
1 parent 6dbb173 commit fed5c9e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"clean": "rm -rf dist",
"dev": "rspack serve --config=tools/rspack.config.ts --watch",
"build:ssr": "rspack build --config=tools/rspack.ssr.config.ts",
"build": "yarn clean && rspack build --config=tools/rspack.config.ts && rm -rf dist/ssr",
"build": "yarn clean && yarn build:ssr && rspack build --config=tools/rspack.config.ts && rm -rf dist/ssr",
"lint": "eslint --fix --color --cache --quiet .",
"prepare": "husky install"
},
Expand Down
45 changes: 27 additions & 18 deletions tools/RspackSSRPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,21 @@ class RspackSSRPlugin implements RspackPluginInstance {
? compiler.hooks.watchRun
: compiler.hooks.beforeRun;
tabFunc.tapAsync(pluginName, (compiler, callback) => {
this.replaceTemplateFile(compiler, callback);
this.runSSRBuild(compiler, callback);
});
tabFunc.tap(pluginName, compiler => {
this.replaceTemplateFile(compiler);
});
compiler.hooks.done.tap(pluginName, () =>
this.recoverTemplateFile(compiler),
);
}
replaceTemplateFile(compiler: Compiler, callback: () => void) {
const token = this.options.token;
const context = compiler.context;
runSSRBuild(compiler: Compiler, callback: () => void) {
const mode = compiler.options.mode;
const templateContent = this.templateContent;
if (mode !== 'development') {
callback();
return;
}
const buildProcess = childProcess.spawn('yarn', [
'build:ssr',
`--mode=${mode}`,
Expand All @@ -43,25 +47,30 @@ class RspackSSRPlugin implements RspackPluginInstance {
buildProcess.stderr.on('data', process.stderr.write);
buildProcess.on('close', function (code) {
if (code === 0) {
const ssrFilePath = path.resolve(context, './dist/ssr/ssr.bundle.js');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const getSSRContent = require(ssrFilePath).default;
delete require.cache[require.resolve(ssrFilePath)];
const ssrContent = getSSRContent();
const newTemplateContent = templateContent.replace(token, ssrContent);
writeFileSync(
path.resolve(context, './html/index.html'),
newTemplateContent,
{
encoding: 'utf-8',
},
);
callback();
} else {
throw new Error(`Generate SSR content failed with code ${code}`);
}
});
}
replaceTemplateFile(compiler: Compiler) {
const token = this.options.token;
const context = compiler.context;
const templateContent = this.templateContent;
const ssrFilePath = path.resolve(context, './dist/ssr/ssr.bundle.js');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const getSSRContent = require(ssrFilePath).default;
delete require.cache[require.resolve(ssrFilePath)];
const ssrContent = getSSRContent();
const newTemplateContent = templateContent.replace(token, ssrContent);
writeFileSync(
path.resolve(context, './html/index.html'),
newTemplateContent,
{
encoding: 'utf-8',
},
);
}
recoverTemplateFile(compiler: Compiler) {
const context = compiler.context;
const templateContent = this.templateContent;
Expand Down

0 comments on commit fed5c9e

Please sign in to comment.