Skip to content

Commit

Permalink
fix(cli-plugin-service): clear asset dir
Browse files Browse the repository at this point in the history
  • Loading branch information
front-end-captain committed Apr 27, 2021
1 parent 63018a6 commit 5979ee7
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 35 deletions.
28 changes: 13 additions & 15 deletions packages/@luban/cli-plugin-service/src/commands/build.ts
Expand Up @@ -5,7 +5,7 @@ import chalk from "chalk";

import { formatStats, logStatsErrorsAndWarnings } from "../utils/formatStats";
import { delay } from "../utils/serverRender";
import { cleanDest } from "../utils/clean";
import { cleanDest } from "../utils/cleanDest";
import { buildServerSideDeployFIle } from "../utils/buildServerSideDeployFile";

import { CommandPluginAPI } from "../lib/PluginAPI";
Expand Down Expand Up @@ -52,14 +52,16 @@ class Build {
webpack(webpackConfig, (err, stats) => {
// Fatal webpack errors (wrong configuration, etc)
if (err) {
return reject(err);
reject(err);
return;
}

logStatsErrorsAndWarnings(stats);

// Compilation errors (missing modules, syntax errors, eslint-errors. etc)
if (stats.hasErrors()) {
return reject("Build failed with some Compilation errors occurred.");
reject("Build failed with some Compilation errors occurred.");
return;
}

const targetDirShort = path.relative(this.api.getContext(), this.outputDir);
Expand Down Expand Up @@ -88,20 +90,18 @@ class Build {
}

webpack(webpackConfig, (err, stats) => {
if (err || stats.hasErrors()) {
reject();
if (err) {
reject(err);
return;
}

const targetDirShort = path.relative(this.api.getContext(), this.outputDir);
if (stats.hasErrors()) {
reject("Build failed with some Compilation errors occurred.");
return;
}

log(formatStats(stats, targetDirShort, this.api));
console.log();
done(
`Server Build complete. The file ${chalk.cyan(
`${targetDirShort}/server.js`,
)} is ready to be deployed.`,
);
done("Server side Build complete");
console.log();

resolve();
Expand All @@ -112,8 +112,6 @@ class Build {
public async start() {
const ctx = this.api.getContext();

info(`clean dest files...`);

await cleanDest(ctx, this.outputDir);

await delay(1000);
Expand All @@ -135,7 +133,7 @@ class Build {
}

console.log();
info("Done 🎉");
done("Build Done 🎉");

["SIGINT", "SIGTERM"].forEach((signal) => {
process.on(signal, () => {
Expand Down
4 changes: 1 addition & 3 deletions packages/@luban/cli-plugin-service/src/commands/serve.ts
Expand Up @@ -36,7 +36,7 @@ import {
getTemplate,
generateInjectedTag,
} from "../utils/serverRender";
import { cleanDest } from "../utils/clean";
import { cleanDest } from "../utils/cleanDest";
import { getCertificate } from "../utils/getCertificate";

type ServerSideHttpsOptions = { key?: Buffer; cert?: Buffer; spdy: { protocols: string[] } };
Expand Down Expand Up @@ -477,8 +477,6 @@ class Serve {
public async start() {
const context = this.pluginApi.getContext();

info(`clean dest files...`);

await cleanDest(context, this.pluginApi.resolve(this.projectConfig.outputDir));

await this.init();
Expand Down
3 changes: 2 additions & 1 deletion packages/@luban/cli-plugin-service/src/config/module.ts
Expand Up @@ -6,6 +6,7 @@ import {
UrlLoaderOptions,
WebpackConfigName,
} from "../definitions";
import { cleanAssetPath } from "../utils/cleanAssetPath";

class Module implements ConfigPluginInstance {
apply(args: ConfigPluginApplyCallbackArgs) {
Expand All @@ -26,7 +27,7 @@ class Module implements ConfigPluginInstance {
loader: "file-loader",
options: {
publicPath: projectConfig.publicPath,
name: `${dir}/[name].[hash:8].[ext]`,
name: cleanAssetPath(`${dir}/[name].[hash:8].[ext]`),
context: api.getContext(),
},
},
Expand Down
20 changes: 8 additions & 12 deletions packages/@luban/cli-plugin-service/src/config/output.ts
@@ -1,27 +1,23 @@
import { ConfigPluginInstance, ConfigPluginApplyCallbackArgs } from "../definitions";

function getScriptsDir(dir: string = ""): string {
const adaptedDir = dir.replace(/^\/|\/$|\s+/g, "");
if (adaptedDir === "") {
return "";
}

return `${adaptedDir}/`;
}
import { cleanAssetPath } from "../utils/cleanAssetPath";

class Output implements ConfigPluginInstance {
apply(args: ConfigPluginApplyCallbackArgs) {
const { api, commandName, projectConfig } = args;

const outputDir = api.resolve(projectConfig.outputDir);

const scriptsDir = getScriptsDir(projectConfig.assetsDir.scripts);
const scriptsDir = projectConfig.assetsDir.scripts;

const baseFilename = "[name]-[hash:8].js";
const filename = commandName === "build" ? `${scriptsDir}${baseFilename}` : baseFilename;
const filename =
commandName === "build" ? cleanAssetPath(`${scriptsDir}/${baseFilename}`) : baseFilename;

const baseChunkFilename = "[name]-[hash:8].chunk.js";
const chunkFilename =
commandName === "build" ? `${scriptsDir}${baseChunkFilename}` : baseChunkFilename;
commandName === "build"
? cleanAssetPath(`${scriptsDir}/${baseChunkFilename}`)
: baseChunkFilename;

api.chainWebpack("server", (webpackConfig) => {
webpackConfig.output
Expand Down
21 changes: 21 additions & 0 deletions packages/@luban/cli-plugin-service/src/config/performance.ts
@@ -0,0 +1,21 @@
import { ConfigPluginInstance, ConfigPluginApplyCallbackArgs } from "../definitions";

class Performance implements ConfigPluginInstance {
apply(args: ConfigPluginApplyCallbackArgs) {
const { api } = args;

const isProduction = process.env.NODE_ENV === "production";

api.chainWebpack("client", (webpackConfig) => {
webpackConfig.performance
.hints(isProduction ? "warning" : false)
// max entry point size 1M
.maxEntrypointSize(1048576)
// max asset size 256kb
.maxAssetSize(262144)
.end();
});
}
}

export default Performance;
5 changes: 3 additions & 2 deletions packages/@luban/cli-plugin-service/src/config/plugin.ts
Expand Up @@ -21,6 +21,7 @@ import {
BuildCliArgs,
ParsedArgs,
} from "../definitions";
import { cleanAssetPath } from "../utils/cleanAssetPath";

class Plugin implements ConfigPluginInstance {
apply(params: ConfigPluginApplyCallbackArgs) {
Expand All @@ -37,8 +38,8 @@ class Plugin implements ConfigPluginInstance {
}.css`;

const extractOptions = {
filename,
chunkFilename,
filename: cleanAssetPath(filename),
chunkFilename: cleanAssetPath(chunkFilename),
};

const outputDir = api.resolve(projectConfig.outputDir);
Expand Down
Expand Up @@ -36,7 +36,7 @@ function buildDeployFile(outputDir: string) {
}

export async function buildServerSideDeployFIle(outputDir: string) {
info("generate server side deploy file");
info("Building server side deploy file...");

const template = fs.readFileSync(outputDir + "/server.ejs", { encoding: "utf-8" });

Expand Down
@@ -0,0 +1,7 @@
export function cleanAssetPath(path: string) {
return path.replace(/(\/{2,})/g, "/").replace(/^\/|\/$/g, "");
}

export function removeSlash(path: string) {
return path.replace(/^\/|\/$/g, "");
}
@@ -1,8 +1,10 @@
import fs from "fs-extra";
import path from "path";
import { error } from "@luban-cli/cli-shared-utils";
import { error, info } from "@luban-cli/cli-shared-utils";

export async function cleanDest(context: string, outputDir: string) {
info(`clean dest files...`);

const _path = path.resolve(context, outputDir);

try {
Expand Down

0 comments on commit 5979ee7

Please sign in to comment.