Browse Source

build: init

vian 5 years ago
commit
79a8ec9996
19 changed files with 2676 additions and 0 deletions
  1. 20 0
      .babelrc
  2. 1 0
      .eslintignore
  3. 28 0
      .eslintrc.js
  4. 21 0
      .gitignore
  5. 9 0
      README.md
  6. 19 0
      dist/index.cjs.js
  7. 3 0
      dist/index.d.ts
  8. 14 0
      dist/index.esm.js
  9. 1 0
      dist/index.min.js
  10. 1 0
      dist/module-a/index.d.ts
  11. 1 0
      dist/module-b/index.d.ts
  12. 2461 0
      package-lock.json
  13. 35 0
      package.json
  14. 6 0
      prettier.config.js
  15. 28 0
      rollup.config.js
  16. 7 0
      src/index.ts
  17. 3 0
      src/module-a/index.ts
  18. 3 0
      src/module-b/index.ts
  19. 15 0
      tsconfig.json

+ 20 - 0
.babelrc

@@ -0,0 +1,20 @@
+{
+  "presets": [
+      ["@babel/preset-env", {
+          "targets": {
+              "browsers": ["Android >= 4", "iOS >= 8"]
+          },
+          "modules": false,
+          "loose": true
+      }]
+  ],
+  "plugins": [
+      "@babel/plugin-external-helpers",
+      [
+          "@babel/plugin-transform-runtime",
+          {
+              "regenerator": true
+          }
+      ]
+  ]
+}

+ 1 - 0
.eslintignore

@@ -0,0 +1 @@
+/dist

+ 28 - 0
.eslintrc.js

@@ -0,0 +1,28 @@
+module.exports = {
+  env: {
+    browser: true,
+    es2021: true,
+    node: true,
+  },
+  extends: ['airbnb-base', 'plugin:@typescript-eslint/recommended', 'prettier'],
+  parser: '@typescript-eslint/parser',
+  parserOptions: {
+    ecmaVersion: 12,
+    sourceType: 'module',
+  },
+  plugins: ['@typescript-eslint', 'prettier'],
+  rules: {
+    'prettier/prettier': 'error',
+    'import/extensions': [
+      'error',
+      {
+        js: 'never',
+        jsx: 'never',
+        ts: 'never',
+        tsx: 'never',
+        json: 'always',
+      },
+    ],
+    'import/no-unresolved': 'off',
+  },
+};

+ 21 - 0
.gitignore

@@ -0,0 +1,21 @@
+.DS_Store
+node_modules
+
+# local env files
+.env.local
+.env.*.local
+
+# Log files
+npm-debug.log*
+yarn-debug.log*
+yarn-error.log*
+pnpm-debug.log*
+
+# Editor directories and files
+.idea
+.vscode
+*.suo
+*.ntvs*
+*.njsproj
+*.sln
+*.sw?

+ 9 - 0
README.md

@@ -0,0 +1,9 @@
+本项目是开发TypeScript npm包项目的模板,基于rollup打包,输出可被CommonJS、ESModule识别,并可直接被浏览器script引入的模块。
+### 初始化
+npm install
+### 构建
+npm run build
+### 代码风格
+- ESLint + Airbnb config
+- Prettier
+

+ 19 - 0
dist/index.cjs.js

@@ -0,0 +1,19 @@
+'use strict';
+
+Object.defineProperty(exports, '__esModule', { value: true });
+
+function testA() {
+    return 'testA';
+}
+
+function testB() {
+    return 'testB';
+}
+
+function justTest() {
+    return 'just a test';
+}
+
+exports.default = justTest;
+exports.testA = testA;
+exports.testB = testB;

+ 3 - 0
dist/index.d.ts

@@ -0,0 +1,3 @@
+export default function justTest(): string;
+export { default as testA } from './module-a/index';
+export { default as testB } from './module-b/index';

+ 14 - 0
dist/index.esm.js

@@ -0,0 +1,14 @@
+function testA() {
+    return 'testA';
+}
+
+function testB() {
+    return 'testB';
+}
+
+function justTest() {
+    return 'just a test';
+}
+
+export default justTest;
+export { testA, testB };

+ 1 - 0
dist/index.min.js

@@ -0,0 +1 @@
+!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).howLongUntilLunch={})}(this,(function(e){"use strict";e.default=function(){return"just a test"},e.testA=function(){return"testA"},e.testB=function(){return"testB"},Object.defineProperty(e,"__esModule",{value:!0})}));

+ 1 - 0
dist/module-a/index.d.ts

@@ -0,0 +1 @@
+export default function testA(): string;

+ 1 - 0
dist/module-b/index.d.ts

@@ -0,0 +1 @@
+export default function testB(): string;

File diff suppressed because it is too large
+ 2461 - 0
package-lock.json


+ 35 - 0
package.json

@@ -0,0 +1,35 @@
+{
+  "name": "@sc/npm-template",
+  "version": "1.0.0",
+  "description": "a template for npm package coding",
+  "main": "./dist/index.cjs.js",
+  "module": "./dist/index.esm.js",
+  "browser": "./dist/index.min.js",
+  "types": "./dist/index.d.ts",
+  "scripts": {
+    "test": "echo \"Error: no test specnpified\" && exit 1",
+    "build": "rollup -c"
+  },
+  "keywords": [],
+  "author": "smartcost",
+  "license": "ISC",
+  "devDependencies": {
+    "@types/ms": "^0.7.31",
+    "@typescript-eslint/eslint-plugin": "^4.4.1",
+    "@typescript-eslint/parser": "^4.4.1",
+    "eslint": "^7.11.0",
+    "eslint-config-airbnb-base": "^14.2.0",
+    "eslint-config-prettier": "^6.12.0",
+    "eslint-plugin-import": "^2.22.1",
+    "eslint-plugin-prettier": "^3.1.4",
+    "prettier": "^2.1.2",
+    "rollup": "^2.30.0",
+    "rollup-plugin-commonjs": "^10.1.0",
+    "rollup-plugin-node-resolve": "^5.2.0",
+    "rollup-plugin-terser": "^7.0.2",
+    "rollup-plugin-typescript2": "^0.27.3",
+    "ts-node": "^9.0.0",
+    "tslib": "^2.0.3",
+    "typescript": "^4.0.3"
+  },
+  "dependencies": {}

+ 6 - 0
prettier.config.js

@@ -0,0 +1,6 @@
+module.exports = {
+  singleQuote: true, // 单引号
+  trailingComma: 'es5', // 对象末尾以逗号结束
+  arrowParens: 'avoid', // 箭头函数只有一个参数的时候,不使用()
+  endOfLine: 'auto', // CRLF,LF都可以
+};

+ 28 - 0
rollup.config.js

@@ -0,0 +1,28 @@
+import resolve from 'rollup-plugin-node-resolve';
+import commonjs from 'rollup-plugin-commonjs';
+import typescript from 'rollup-plugin-typescript2'; // 一定要是typescript2,如果使用typescript,没法自动生成.d.ts文件
+import { terser } from 'rollup-plugin-terser';
+import pkg from './package.json';
+
+export default [
+  // UMD for browser
+  {
+    input: 'src/index.ts',
+    output: {
+      name: 'howLongUntilLunch',
+      file: pkg.browser,
+      format: 'umd',
+    },
+    plugins: [resolve(), commonjs(), typescript(), terser()], // 浏览器使用的代码文件进行简化
+  },
+  // CommonJS for Node and ES module for bundlers build
+  {
+    input: 'src/index.ts',
+    external: ['ms'],
+    plugins: [typescript()],
+    output: [
+      { file: pkg.main, format: 'cjs' },
+      { file: pkg.module, format: 'es' },
+    ],
+  },
+];

+ 7 - 0
src/index.ts

@@ -0,0 +1,7 @@
+export default function justTest(): string {
+  return 'just a test';
+}
+
+export { default as testA } from './module-a/index';
+
+export { default as testB } from './module-b/index';

+ 3 - 0
src/module-a/index.ts

@@ -0,0 +1,3 @@
+export default function testA(): string {
+  return 'testA';
+}

+ 3 - 0
src/module-b/index.ts

@@ -0,0 +1,3 @@
+export default function testB(): string {
+  return 'testB';
+}

+ 15 - 0
tsconfig.json

@@ -0,0 +1,15 @@
+{
+  "compilerOptions": {
+    "target": "es5",
+    "module": "es6",
+    "declaration": true,
+    "outDir": "./",
+    "strict": true
+  },
+  "include": [
+    "src/**/*.ts",
+  ],
+  "exclude": [
+    "node_modules"
+  ]
+}