Skip to content

Commit 3194286

Browse files
committed
feat: added dynamic imports
1 parent 4f69e5b commit 3194286

File tree

7 files changed

+54
-51
lines changed

7 files changed

+54
-51
lines changed

src/build/build-distfiles.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export async function buildDistFiles(basePath: string): Promise<boolean> {
3535

3636
const jsonIndent = 2;
3737

38-
fs.writeFileSync(libPackage, `${JSON.stringify(libObj, undefined, jsonIndent)}\n`, "utf8");
38+
await fs.writeFile(libPackage, `${JSON.stringify(libObj, undefined, jsonIndent)}\n`, "utf8");
3939

4040
console.log(`package.dist.json updated successfully to version ${pkgInfo.version}`);
4141

@@ -52,19 +52,19 @@ export async function buildDistFiles(basePath: string): Promise<boolean> {
5252
const src = path.join(basePath, typeof file === "string" ? file : file.source),
5353
dest = path.join(distPath, typeof file === "string" ? file : file.destination);
5454

55-
fs.copyFileSync(src, dest);
55+
await fs.copyFile(src, dest);
5656
}
5757

5858
const scriptsPath = path.join(basePath, "scripts"),
5959
distScriptsPath = path.join(distPath, "scripts");
6060

61-
if (fs.existsSync(scriptsPath) && !fs.existsSync(distScriptsPath)) {
62-
fs.mkdirSync(distScriptsPath);
61+
if ((await fs.exists(scriptsPath)) && !(await fs.exists(distScriptsPath))) {
62+
await fs.mkdir(distScriptsPath);
6363

6464
const installPath = path.join(scriptsPath, "install.js");
6565

66-
if (fs.existsSync(installPath)) {
67-
fs.copyFileSync(installPath, path.join(distScriptsPath, "install.js"));
66+
if (await fs.exists(installPath)) {
67+
await fs.copyFile(installPath, path.join(distScriptsPath, "install.js"));
6868
}
6969
}
7070

src/build/build-diststats.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import fs from "fs-extra";
2-
import path from "path";
32

43
export interface IDistStats {
54
bundleSize: number;
@@ -25,7 +24,8 @@ async function getFolderStats(folderPath: string, bundlePath?: string): Promise<
2524
return stats;
2625
}
2726

28-
const dir = await fs.promises.opendir(folderPath);
27+
const dir = await fs.promises.opendir(folderPath),
28+
path = await import("path");
2929

3030
for await (const dirent of dir) {
3131
const increment = 1;
@@ -57,7 +57,8 @@ async function getFolderStats(folderPath: string, bundlePath?: string): Promise<
5757
* @returns the stats for the dist folder
5858
*/
5959
export async function getDistStats(basePath: string): Promise<IDistStats> {
60-
const distFolder = path.join(basePath, "dist"),
60+
const path = await import("path"),
61+
distFolder = path.join(basePath, "dist"),
6162
pkgInfo = (await fs.exists(path.join(distFolder, "package.json")))
6263
? ((await import(path.join(distFolder, "package.json"))) as { jsdelivr?: string })
6364
: {},

src/build/build-tsc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import fs from "fs-extra";
22
import path from "path";
3-
import ts from "typescript";
43

54
const enum ExitCodes {
65
OK = 0,
@@ -123,7 +122,8 @@ async function compile(basePath: string, type: "browser" | "cjs" | "esm" | "type
123122
return ExitCodes.NoOptions;
124123
}
125124

126-
const parsed = ts.parseJsonConfigFileContent(options, ts.sys, basePath);
125+
const ts = await import("typescript"),
126+
parsed = ts.parseJsonConfigFileContent(options, ts.sys, basePath);
127127

128128
if (!parsed) {
129129
return ExitCodes.ParseError;

src/build/build.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
import { prettifyPackageDistJson, prettifyPackageJson, prettifyReadme, prettifySrc } from "./build-prettier";
21
import { Command } from "commander";
3-
import { buildDistFiles } from "./build-distfiles";
4-
import { buildTS } from "./build-tsc";
5-
import { bundle } from "./build-bundle";
6-
import { clearDist } from "./build-clear";
7-
import fs from "fs-extra";
8-
import { getDistStats } from "./build-diststats";
9-
import { lint } from "./build-eslint";
10-
import path from "path";
112

123
const buildCommand = new Command("build");
134

@@ -42,13 +33,18 @@ buildCommand.action(async (argPath: string) => {
4233
tsc = all || !!opts.tsc;
4334

4435
const basePath = process.cwd(),
36+
{ getDistStats } = await import("./build-diststats.js"),
4537
oldStats = await getDistStats(basePath);
4638

4739
if (clean) {
40+
const { clearDist } = await import("./build-clear.js");
41+
4842
await clearDist(basePath);
4943
}
5044

51-
const srcPath = path.join(basePath, argPath);
45+
const path = await import("path"),
46+
srcPath = path.join(basePath, argPath),
47+
fs = await import("fs-extra");
5248

5349
if (!(await fs.pathExists(srcPath))) {
5450
throw new Error("Provided path does not exist");
@@ -57,28 +53,40 @@ buildCommand.action(async (argPath: string) => {
5753
let canContinue = true;
5854

5955
if (canContinue && prettier) {
56+
const { prettifySrc } = await import("./build-prettier.js");
57+
6058
canContinue = await prettifySrc(basePath, srcPath, ci);
6159
}
6260

6361
if (canContinue && doLint) {
62+
const { lint } = await import("./build-eslint.js");
63+
6464
canContinue = await lint(ci);
6565
}
6666

6767
if (canContinue && tsc) {
68+
const { buildTS } = await import("./build-tsc.js");
69+
6870
canContinue = await buildTS(basePath);
6971
}
7072

7173
if (canContinue && doBundle) {
74+
const { bundle } = await import("./build-bundle.js");
75+
7276
canContinue = await bundle(basePath);
7377
}
7478

7579
if (canContinue && prettier) {
80+
const { prettifyReadme, prettifyPackageJson, prettifyPackageDistJson } = await import("./build-prettier");
81+
7682
canContinue = await prettifyReadme(basePath, ci);
7783
canContinue = await prettifyPackageJson(basePath, ci);
7884
canContinue = await prettifyPackageDistJson(basePath, ci);
7985
}
8086

8187
if (canContinue && distfiles) {
88+
const { buildDistFiles } = await import("./build-distfiles.js");
89+
8290
canContinue = await buildDistFiles(basePath);
8391
}
8492

src/create/plugin/plugin.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ pluginCommand.description("Create a new tsParticles plugin");
1111
pluginCommand.argument("<destination>", "Destination folder");
1212
pluginCommand.action(async (destination: string) => {
1313
const destPath = await getDestinationDir(destination),
14-
repoUrl = await getRepositoryUrl();
15-
16-
const initialName = destPath.split(path.sep).pop(),
14+
repoUrl = await getRepositoryUrl(),
15+
initialName = destPath.split(path.sep).pop(),
1716
questions: PromptObject[] = [
1817
{
1918
type: "text",
@@ -35,13 +34,12 @@ pluginCommand.action(async (destination: string) => {
3534
message: "What is the repository URL? (optional)",
3635
initial: repoUrl.trim(),
3736
},
38-
];
39-
40-
const { name, description, repositoryUrl } = (await prompts(questions)) as {
41-
description: string;
42-
name: string;
43-
repositoryUrl: string;
44-
};
37+
],
38+
{ name, description, repositoryUrl } = (await prompts(questions)) as {
39+
description: string;
40+
name: string;
41+
repositoryUrl: string;
42+
};
4543

4644
await createPluginTemplate(name.trim(), description.trim(), repositoryUrl.trim(), destPath);
4745
});

src/create/preset/preset.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ presetCommand.description("Create a new tsParticles preset");
1111
presetCommand.argument("<destination>", "Destination folder");
1212
presetCommand.action(async (destination: string) => {
1313
const destPath = await getDestinationDir(destination),
14-
repoUrl = await getRepositoryUrl();
15-
16-
const initialName = destPath.split(path.sep).pop(),
14+
repoUrl = await getRepositoryUrl(),
15+
initialName = destPath.split(path.sep).pop(),
1716
questions: PromptObject[] = [
1817
{
1918
type: "text",
@@ -35,13 +34,12 @@ presetCommand.action(async (destination: string) => {
3534
message: "What is the repository URL? (optional)",
3635
initial: repoUrl.trim(),
3736
},
38-
];
39-
40-
const { name, description, repositoryUrl } = (await prompts(questions)) as {
41-
description: string;
42-
name: string;
43-
repositoryUrl: string;
44-
};
37+
],
38+
{ name, description, repositoryUrl } = (await prompts(questions)) as {
39+
description: string;
40+
name: string;
41+
repositoryUrl: string;
42+
};
4543

4644
await createPresetTemplate(name.trim(), description.trim(), repositoryUrl.trim(), destPath);
4745
});

src/create/shape/shape.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ shapeCommand.description("Create a new tsParticles shape");
1111
shapeCommand.argument("<destination>", "Destination folder");
1212
shapeCommand.action(async (destination: string) => {
1313
const destPath = await getDestinationDir(destination),
14-
repoUrl = await getRepositoryUrl();
15-
16-
const initialName = destPath.split(path.sep).pop(),
14+
repoUrl = await getRepositoryUrl(),
15+
initialName = destPath.split(path.sep).pop(),
1716
questions: PromptObject[] = [
1817
{
1918
type: "text",
@@ -35,13 +34,12 @@ shapeCommand.action(async (destination: string) => {
3534
message: "What is the repository URL? (optional)",
3635
initial: repoUrl.trim(),
3736
},
38-
];
39-
40-
const { name, description, repositoryUrl } = (await prompts(questions)) as {
41-
description: string;
42-
name: string;
43-
repositoryUrl: string;
44-
};
37+
],
38+
{ name, description, repositoryUrl } = (await prompts(questions)) as {
39+
description: string;
40+
name: string;
41+
repositoryUrl: string;
42+
};
4543

4644
await createShapeTemplate(name.trim(), description.trim(), repositoryUrl.trim(), destPath);
4745
});

0 commit comments

Comments
 (0)