commit 8a10db103742ebf53f18eb31bcfff2e89cb61a6e Author: Geequlim Date: Sun May 17 14:36:52 2020 +0800 Initial commit diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..8b96428 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,8 @@ +root = true + +[*] +indent_style = tab +indent_size = 4 +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = false diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..498b0b2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +dist/ +node_modules/ +yarn.lock \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..ea5b488 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,14 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "启动程序", + "skipFiles": [ + "/**" + ], + "program": "${workspaceFolder}/dist/binary.js" + } + ] +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..570372d --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# NodeJS 起始项目 + +搭建好 TypeScript NodeJS 的空项目,提供编译、调试流程,内置 tiny 使用代码库。 \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..ad6c144 --- /dev/null +++ b/package.json @@ -0,0 +1,18 @@ +{ + "name": "tiny-nodejs-starter-kit", + "version": "1.0.0", + "main": "index.js", + "license": "MIT", + "scripts": { + "dev": "node_modules/.bin/webpack --config webpack.config.js --watch", + "compile": "node_modules/.bin/webpack --config webpack.config.js --env.production" + }, + "devDependencies": { + "@types/node": "^14.0.1", + "ts-loader": "^7.0.4", + "tsconfig-paths-webpack-plugin": "^3.2.0", + "typescript": "^3.9.2", + "webpack": "^4.43.0", + "webpack-cli": "^3.3.11" + } +} diff --git a/src/main.ts b/src/main.ts new file mode 100644 index 0000000..a6dd14d --- /dev/null +++ b/src/main.ts @@ -0,0 +1,4 @@ +import { get_startup_arguments } from "./tiny/env"; +(async function main(argv: string[]) { + console.log(argv); +})(get_startup_arguments()); diff --git a/src/tiny/env.ts b/src/tiny/env.ts new file mode 100644 index 0000000..a9a95ee --- /dev/null +++ b/src/tiny/env.ts @@ -0,0 +1,36 @@ +/** + * JavaScript 运行环境 + */ +export enum JavaScriptRuntime { + /** 未知 */ + Unknown, + /** NodeJS 运行时 */ + NodeJS, + /** 浏览器 */ + Browser, +} + +/** + * 获取当前的运行环境 + */ +export function get_runtime(): JavaScriptRuntime { + if (typeof window == 'object' && typeof document == 'object') { + return JavaScriptRuntime.Browser; + } else if (typeof process == 'object' && process.release.name == 'node') { + return JavaScriptRuntime.NodeJS; + } + return JavaScriptRuntime.Unknown; +} + +/** + * 获取启动参数 + */ +export function get_startup_arguments(): string[] { + switch (get_runtime()) { + case JavaScriptRuntime.NodeJS: + return process.argv; + default: + return []; + } +} + diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..b7bd28d --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "target": "ES2018", + "module": "commonjs", + "lib": [ "ESNext", "DOM" ], + "declaration": false, + "allowJs": true, + "checkJs": true, + "sourceMap": true, + "outDir": "dist", + "moduleResolution": "node", + "baseUrl": "src", + "experimentalDecorators": true, + "emitDecoratorMetadata": true + }, + "include": [ + "src/**/*" + ], + "exclude": [ + "node_modules/**/*" + ] +} diff --git a/webpack.config.js b/webpack.config.js new file mode 100644 index 0000000..761ba4e --- /dev/null +++ b/webpack.config.js @@ -0,0 +1,38 @@ +const path = require('path'); +const webpack = require('webpack'); +const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin'); +const workSpaceDir = path.resolve(__dirname); + +module.exports = (env) => { + if (!env) { env = {production: false};} + console.log("Compile environment:", env); + return ({ + target: 'node', + entry: path.join(workSpaceDir, 'src/main.ts'), + output: { + path: path.join(workSpaceDir, 'dist'), + filename: 'binary.js' + }, + module: { + rules: [ + { test: /\.tsx?$/, use: 'ts-loader', exclude: /node_modules/ }, + { test:/\.ya?ml$/, use: [ "json-loader", "yaml-loader" ] }, + { test:/\.(html|txt|md)$/, use: "raw-loader" }, + ] + }, + resolve: { + extensions: [ + '.tsx', '.ts', '.js', + ".yaml", ".html", ".md", ".txt" + ], + plugins: [ + new TsconfigPathsPlugin({configFile: path.join(workSpaceDir, 'tsconfig.json')}) + ] + }, + externals: {}, + plugins: [ + ], + devtool: env.production ? "" : "source-map", + mode: "development", + }); +}; \ No newline at end of file