From 819559ba08e9db639a82f2e8db79dab4ade378f5 Mon Sep 17 00:00:00 2001 From: shirtiny Date: Thu, 9 Dec 2021 15:48:40 +0800 Subject: [PATCH] config/* convert to es module --- .sh.js | 6 +++--- config/builder.js | 40 ++++++++++++++++++++++++++-------------- config/logger.js | 10 ++++++---- config/server.js | 34 ++++++++++++++++++---------------- config/util.js | 10 ++++++---- config/var.js | 11 +++++------ package.json | 2 ++ yarn.lock | 34 +++++++++------------------------- 8 files changed, 75 insertions(+), 72 deletions(-) diff --git a/.sh.js b/.sh.js index 4a62d04..f97e69e 100644 --- a/.sh.js +++ b/.sh.js @@ -1,11 +1,11 @@ /* * @Author: Shirtiny * @Date: 2021-06-26 20:47:19 - * @LastEditTime: 2021-11-27 10:52:42 + * @LastEditTime: 2021-12-09 15:14:12 * @Description: */ -const camelCase = require("camelcase"); +import camelCase from "camelcase"; const { MY_ENV, HOST, PORT, NODE_ENV, npm_package_version, npm_package_name } = process.env; @@ -28,7 +28,7 @@ const globalName = console.log("APP globalName : ", globalName); -module.exports = { +export default { globalName, outputFileName: "main", devServer: { diff --git a/config/builder.js b/config/builder.js index e94d8c2..30052d3 100644 --- a/config/builder.js +++ b/config/builder.js @@ -1,21 +1,24 @@ /* * @Author: Shirtiny * @Date: 2021-06-26 17:41:22 - * @LastEditTime: 2021-12-09 14:54:59 + * @LastEditTime: 2021-12-09 15:47:31 * @Description: */ -const esbuild = require("esbuild"); -const childProcess = require("child_process"); -const path = require("path"); -const { sassPlugin } = require("esbuild-sass-plugin"); -const postcss = require("postcss"); -const autoprefixer = require("autoprefixer"); -const postcssPresetEnv = require("postcss-preset-env"); -const { config, isDev } = require("./var"); -const logger = require("./logger"); +import esbuild from "esbuild"; +import childProcess from "child_process"; +import path from "path"; +import { sassPlugin } from "esbuild-sass-plugin"; +import postcss from "postcss"; +import autoprefixer from "autoprefixer"; +import postcssPresetEnv from "postcss-preset-env"; +import timePlugin from "esbuild-plugin-time"; +import { config, isDev } from "./var.js"; +import logger from "./logger.js"; -const srcDirPath = "../src"; -const distDirPath = "../dist"; +const __dirname = process.cwd(); + +const srcDirPath = "./src"; +const distDirPath = "./dist"; const typesDirPath = path.resolve(__dirname, `${distDirPath}/types`); const fileName = config.outputFileName || "main"; @@ -27,6 +30,7 @@ const createFilePath = (dirPath, fileName) => { const buildList = [ { + name: "Bundle Browser", entryPoints: [createFilePath(srcDirPath, "browser.ts")], platform: "browser", outfile: createFilePath(distDirPath, fileName + ".browser.js"), @@ -46,6 +50,7 @@ const buildList = [ }, }, { + name: "Bundle Esm", entryPoints: [createFilePath(srcDirPath, "es.ts")], platform: "neutral", outfile: createFilePath(distDirPath, fileName + ".es.js"), @@ -65,6 +70,7 @@ const buildList = [ }, }, { + name: "Bundle Node", entryPoints: [createFilePath(srcDirPath, "cli.ts")], platform: "node", outfile: createFilePath(distDirPath, fileName + ".cli.js"), @@ -72,7 +78,13 @@ const buildList = [ }, ]; -const build = async ({ entryPoints = [], platform, outfile, plugins = [] }) => { +const build = async ({ + name, + entryPoints = [], + platform, + outfile, + plugins = [], +}) => { await logger.runTask({ title: `Building ${entryPoints.join("; ")}`, successTitle: `Build ${outfile} successfully`, @@ -88,7 +100,7 @@ const build = async ({ entryPoints = [], platform, outfile, plugins = [] }) => { "process.env": JSON.stringify(config.env || process.env), }, outfile, - plugins: [...plugins], + plugins: [...plugins, timePlugin(name)], jsxFactory: config.jsxFactory, jsxFragment: config.jsxFragment, }); diff --git a/config/logger.js b/config/logger.js index 66334dd..6e9791d 100644 --- a/config/logger.js +++ b/config/logger.js @@ -1,12 +1,12 @@ /* * @Author: Shirtiny * @Date: 2021-06-26 21:18:47 - * @LastEditTime: 2021-12-09 14:46:47 + * @LastEditTime: 2021-12-09 15:14:57 * @Description: */ -const chalk = require("chalk"); -const tasuku = require("tasuku"); +import chalk from "chalk"; +import tasuku from "tasuku"; const log = (...messages) => { console.log(chalk.hex("#00b7c3")(...messages)); @@ -48,9 +48,11 @@ const runTask = async ({ return t.result; }; -module.exports = { +const logger = { chan, server, log, runTask, }; + +export default logger; diff --git a/config/server.js b/config/server.js index 9f2a9ad..cb21d42 100644 --- a/config/server.js +++ b/config/server.js @@ -1,26 +1,28 @@ /* * @Author: Shirtiny * @Date: 2021-06-25 17:35:25 - * @LastEditTime: 2021-12-09 14:46:56 + * @LastEditTime: 2021-12-09 15:31:14 * @Description: */ "use strict"; -const esbuild = require("esbuild"); -const http = require("http"); -const path = require("path"); -const open = require("open"); -const { sassPlugin } = require("esbuild-sass-plugin"); -const postcss = require("postcss"); -const autoprefixer = require("autoprefixer"); -const postcssPresetEnv = require("postcss-preset-env"); -const util = require("./util"); -const { config } = require("./var"); -const logger = require("./logger"); - -const publicDirPath = path.resolve(__dirname, "../public"); -const srcDirPath = path.resolve(__dirname, "../src"); -const distDirPath = path.resolve(__dirname, "../dist"); +import esbuild from "esbuild"; +import http from "http"; +import path from "path"; +import open from "open"; +import { sassPlugin } from "esbuild-sass-plugin"; +import postcss from "postcss"; +import autoprefixer from "autoprefixer"; +import postcssPresetEnv from "postcss-preset-env"; +import { config } from "./var.js"; +import util from "./util.js"; +import logger from "./logger.js"; + +const __dirname = process.cwd(); + +const publicDirPath = path.resolve(__dirname, "./public"); +const srcDirPath = path.resolve(__dirname, "./src"); +const distDirPath = path.resolve(__dirname, "./dist"); const srcFileName = "index.ts"; const distFileName = "index.js"; diff --git a/config/util.js b/config/util.js index 01fdf48..1898409 100644 --- a/config/util.js +++ b/config/util.js @@ -1,12 +1,12 @@ /* * @Author: Shirtiny * @Date: 2021-06-26 18:51:15 - * @LastEditTime: 2021-12-09 14:47:01 + * @LastEditTime: 2021-12-09 15:18:21 * @Description: */ -const fs = require("fs"); -const shell = require("shelljs"); +import fs from "fs"; +import shell from "shelljs"; const isPathExisted = async (path) => { return new Promise((resolve) => { @@ -35,9 +35,11 @@ const cpAllDirChildsToDir = (dirPath, targetDirPath) => { shell.cp("-rf", `${dirPath}/*`, `${targetDirPath}/`); }; -module.exports = { +const util = { isPathExisted, mkdir, rm, cpAllDirChildsToDir, }; + +export default util; diff --git a/config/var.js b/config/var.js index d462afc..a4624fe 100644 --- a/config/var.js +++ b/config/var.js @@ -1,15 +1,14 @@ /* * @Author: Shirtiny * @Date: 2021-06-26 20:17:19 - * @LastEditTime: 2021-12-09 14:51:30 + * @LastEditTime: 2021-12-09 15:19:55 * @Description: */ -const config = require("../.sh"); +import config from "../.sh.js"; console.log("ENV: ", config.env); -module.exports = { - config, - isDev: process.env.NODE_ENV === "development", -}; +const isDev = process.env.NODE_ENV === "development"; + +export { config, isDev }; diff --git a/package.json b/package.json index fef1327..2999b3a 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,7 @@ "name": "@shirtiny/ts-lib-template", "version": "1.4.15", "description": "ts-lib-template desc", + "type": "module", "types": "./dist/types/main.d.ts", "main": "./dist/main.es.js", "browser": "./dist/main.es.js", @@ -60,6 +61,7 @@ "dotenv-cli": "^4.0.0", "esbuild": "0.12.5", "esbuild-plugin-inline-worker": "^0.1.1", + "esbuild-plugin-time": "^1.0.0", "esbuild-sass-plugin": "^1.4.8", "eslint": "^7.28.0", "eslint-config-prettier": "^8.3.0", diff --git a/yarn.lock b/yarn.lock index 5336936..c2af07b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1157,7 +1157,7 @@ chalk@^2.0.0: escape-string-regexp "^1.0.5" supports-color "^5.3.0" -chalk@^4.0.0: +chalk@^4.0.0, chalk@^4.1.2: version "4.1.2" resolved "https://registry.npmmirror.com/chalk/download/chalk-4.1.2.tgz#aac4e2b7734a740867aeb16bf02aad556a1e7a01" integrity sha1-qsTit3NKdAhnrrFr8CqtVWoeegE= @@ -1589,6 +1589,13 @@ esbuild-plugin-inline-worker@^0.1.1: esbuild latest find-cache-dir "^3.3.1" +esbuild-plugin-time@^1.0.0: + version "1.0.0" + resolved "https://registry.npmjs.org/esbuild-plugin-time/-/esbuild-plugin-time-1.0.0.tgz#0bac573ae8c0c7d012c39fb1a855519684d35ea0" + integrity sha512-I4Shhi0fpgXxSoc34djTHIuhbEzkcBbKKiNtb3LhZe2JEE1vSxXilW+KE0M/KZ4kqORLGgdGCKuJh9CNUL55yw== + dependencies: + chalk "^4.1.2" + esbuild-sass-plugin@^1.4.8: version "1.8.0" resolved "https://registry.npmmirror.com/esbuild-sass-plugin/download/esbuild-sass-plugin-1.8.0.tgz#36d00adb049735278ca2272bf2164f53a7b70e92" @@ -1624,30 +1631,7 @@ esbuild@0.12.5: resolved "https://registry.npmmirror.com/esbuild/download/esbuild-0.12.5.tgz#36076a6bc1966ba2741981d30512e95e8aaff495" integrity sha1-Ngdqa8GWa6J0GYHTBRLpXoqv9JU= -esbuild@^0.14.1: - version "0.14.2" - resolved "https://registry.npmmirror.com/esbuild/download/esbuild-0.14.2.tgz#9c1e1a652549cc33e44885eea42ea2cc6267edc2" - integrity sha512-l076A6o/PIgcyM24s0dWmDI/b8RQf41uWoJu9I0M71CtW/YSw5T5NUeXxs5lo2tFQD+O4CW4nBHJXx3OY5NpXg== - optionalDependencies: - esbuild-android-arm64 "0.14.2" - esbuild-darwin-64 "0.14.2" - esbuild-darwin-arm64 "0.14.2" - esbuild-freebsd-64 "0.14.2" - esbuild-freebsd-arm64 "0.14.2" - esbuild-linux-32 "0.14.2" - esbuild-linux-64 "0.14.2" - esbuild-linux-arm "0.14.2" - esbuild-linux-arm64 "0.14.2" - esbuild-linux-mips64le "0.14.2" - esbuild-linux-ppc64le "0.14.2" - esbuild-netbsd-64 "0.14.2" - esbuild-openbsd-64 "0.14.2" - esbuild-sunos-64 "0.14.2" - esbuild-windows-32 "0.14.2" - esbuild-windows-64 "0.14.2" - esbuild-windows-arm64 "0.14.2" - -esbuild@latest: +esbuild@^0.14.1, esbuild@latest: version "0.14.2" resolved "https://registry.npmjs.org/esbuild/-/esbuild-0.14.2.tgz#9c1e1a652549cc33e44885eea42ea2cc6267edc2" integrity sha512-l076A6o/PIgcyM24s0dWmDI/b8RQf41uWoJu9I0M71CtW/YSw5T5NUeXxs5lo2tFQD+O4CW4nBHJXx3OY5NpXg==