Skip to content

Commit dad528a

Browse files
authored
improve webpack plugins
1 parent 8f9a4a0 commit dad528a

File tree

4 files changed

+70
-46
lines changed

4 files changed

+70
-46
lines changed

cli/helpers/lint-stream.mjs

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export async function run(options) {
2828
{
2929
cwd: process.env.INIT_CWD,
3030
ignore: [
31+
"**/*.git*/**",
3132
"**/*node_modules/**",
3233
"**/*build/**",
3334
"**/*dist/**",
@@ -36,6 +37,7 @@ export async function run(options) {
3637
"**/*.dll.js",
3738
"**/*-lock.json",
3839
"**/lint-final.json",
40+
"**/coverage-final.json",
3941
],
4042
},
4143
(error, files) => {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lastui/rocker",
3-
"version": "0.19.11",
3+
"version": "0.19.12",
44
"license": "Apache-2.0",
55
"author": "[email protected]",
66
"homepage": "https://github.com/lastui/rocker#readme",

webpack/plugins/ModuleLocalesPlugin.js

+56-38
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,32 @@ const path = require("path");
33

44
const settings = require("../settings");
55

6+
async function ensureFile(filename, reject, resolve) {
7+
fs.stat(filename, (stat_err, _stat) => {
8+
if (stat_err === null) {
9+
resolve(true);
10+
} else if (stat_err.code === "ENOENT") {
11+
fs.mkdir(path.dirname(filename), (mkdir_err) => {
12+
if (mkdir_err && mkdir_err.code !== "EEXIST") {
13+
reject(mkdir_err);
14+
} else {
15+
resolve(true);
16+
}
17+
});
18+
} else {
19+
reject(stat_err);
20+
}
21+
});
22+
}
23+
624
class ModuleLocalesPlugin {
7-
constructor(options) {
8-
this.from = options.from;
25+
constructor() {
926
this.paths_to_watch = [];
1027
for (const language of settings.SUPPORTED_LOCALES) {
1128
this.paths_to_watch.push(`${language}.json`);
1229
}
1330
}
1431

15-
async ensureAsset(filename) {
16-
try {
17-
return await fs.promises.readFile(filename, "utf8");
18-
} catch (_error) {
19-
await new Promise((resolve, reject) => {
20-
const parent = path.dirname(filename);
21-
fs.stat(parent, (stat_err, _stat) => {
22-
if (stat_err === null) {
23-
resolve(true);
24-
} else if (stat_err.code === "ENOENT") {
25-
fs.mkdir(parent, (mkdir_err) => {
26-
if (mkdir_err && mkdir_err.code !== "EEXIST") {
27-
reject(mkdir_err);
28-
} else {
29-
resolve(true);
30-
}
31-
});
32-
} else {
33-
reject(stat_err);
34-
}
35-
});
36-
});
37-
await fs.promises.writeFile(filename, "{}");
38-
return "{}";
39-
}
40-
}
41-
4232
apply(compiler) {
4333
compiler.hooks.thisCompilation.tap(ModuleLocalesPlugin.name, (compilation) => {
4434
compilation.hooks.processAssets.tapPromise(
@@ -49,6 +39,8 @@ class ModuleLocalesPlugin {
4939
async (_assets) => {
5040
const assetOutputDirectory = path.dirname(compilation.outputOptions.assetModuleFilename);
5141

42+
const assetsToWatch = [];
43+
5244
for (const entryPoint of compilation.entrypoints.values()) {
5345
let entryPointOrigin = null;
5446

@@ -65,14 +57,17 @@ class ModuleLocalesPlugin {
6557
if (origin.request.indexOf("webpack") !== -1) {
6658
continue;
6759
}
60+
6861
entryPointOrigin = path.extname(origin.request)
6962
? path.resolve(origin.request, "..", "..", "messages")
7063
: path.resolve(origin.request, "..", "messages");
7164
break;
7265
}
7366

67+
const hasRuntimeChunk = entryPoint.chunks.length > 1;
68+
7469
for (const chunk of entryPoint.chunks) {
75-
if (chunk.id === chunk.runtime) {
70+
if (hasRuntimeChunk && chunk.id === chunk.runtime) {
7671
continue;
7772
}
7873

@@ -81,18 +76,41 @@ class ModuleLocalesPlugin {
8176
if (compilation.getAsset(outputPath)) {
8277
continue;
8378
}
84-
8579
const inputPath = path.resolve(entryPointOrigin, asset);
86-
try {
87-
const content = await this.ensureAsset(inputPath);
88-
compilation.fileDependencies.add(inputPath);
89-
compilation.emitAsset(outputPath, new compiler.webpack.sources.RawSource(content));
90-
} catch (error) {
91-
console.error("Failed to process locale asset", inputPath, "with error", error);
92-
}
80+
assetsToWatch.push([inputPath, outputPath]);
9381
}
9482
}
9583
}
84+
85+
for (const [inputPath, outputPath] of assetsToWatch) {
86+
try {
87+
const content = await new Promise((resolve, reject) => {
88+
fs.readFile(inputPath, "utf8", (read_err, read_ok) => {
89+
if (!read_err) {
90+
resolve(read_ok);
91+
return;
92+
}
93+
ensureFile(inputPath, (ensure_err, _ensure_ok) => {
94+
if (ensure_err) {
95+
reject(ensure_err);
96+
return;
97+
}
98+
fs.writeFile(inputPath, "{}", (write_err, _write_ok) => {
99+
if (write_err) {
100+
reject(write_err);
101+
return;
102+
}
103+
resolve("{}");
104+
});
105+
});
106+
});
107+
});
108+
compilation.fileDependencies.add(inputPath);
109+
compilation.emitAsset(outputPath, new compiler.webpack.sources.RawSource(content));
110+
} catch (error) {
111+
console.error("Failed to process locale asset", inputPath, "with error", error);
112+
}
113+
}
96114
},
97115
);
98116
});

webpack/plugins/NormalizedModuleIdPlugin.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
class NormalizedModuleIdPlugin {
2+
constructor() {
3+
this.volatileDir = new RegExp(/\/(dist|esm?|cjs|lib|unpkg)\//);
4+
this.volatileExt = new RegExp(/\.(esm\.|cjs\.|m)js$/);
5+
}
6+
27
apply(compiler) {
38
compiler.hooks.compilation.tap(NormalizedModuleIdPlugin.name, (compilation) => {
49
compilation.hooks.beforeModuleIds.tap(NormalizedModuleIdPlugin.name, (modules) => {
510
for (const item of modules) {
611
if (!item.libIdent) {
712
continue;
813
}
14+
915
const id = item.libIdent({ context: compiler.options.context });
10-
if (!id) {
16+
if (!id || id.startsWith("dll-reference")) {
1117
continue;
1218
}
19+
1320
compilation.chunkGraph.setModuleId(
1421
item,
15-
id.startsWith("./node_modules/@lastui/rocker")
16-
? id.replace(/.\/node_modules\/@lastui\/rocker/g, "@rocker")
17-
: id
18-
.replace(/\/(dist|esm?|cjs|lib|unpkg)\//g, "/")
19-
.replace(/\.(esm|cjs)\.js$/g, ".js")
20-
.replace(/\.m?js$/g, ".js"),
22+
id.startsWith("./node_modules/@lastui/rocker/")
23+
? "@" + id.substr(23)
24+
: id.replace(this.volatileDir, "/").replace(this.volatileExt, ".js"),
2125
);
2226
}
2327
});

0 commit comments

Comments
 (0)