Stu*_*Man 6 javascript npm typescript
我在 Typescript 中创建了一个包并将其发布到 NPM 上。我已经开始declaration: true了tsconfig.json这个包项目。当我构建这个项目时,TS 编译器按预期生成 .d.ts 文件,但是,这些文件没有在 NPM 上发布,因此,当我在其他项目中安装这个包时,我收到错误,Cannot find module 'projectname-common' or its corresponding type declarations.
这是我的 package.json 文件:
\n{\n "name": "@Organisation/lib",\n "version": "1.0.0",\n "description": "",\n "main": "dist/index.js",\n "types": "dist/index.d.ts",\n "scripts": {\n "clean": "rm -rf ./dist",\n "build": "npm run clean && npx tsc",\n "pub": "git add . && git commit -m \\"Changes\\" && git push && npm version patch && npm run build && npm publish --access public"\n },\n "keywords": [],\n "author": "Author",\n "license": "ISC",\n "devDependencies": {\n //removed\n },\n "dependencies": {\n //removed\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n这是我的 tsconfig.json 文件:
\n{\n "compilerOptions": {\n "target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */\n "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */\n "declaration": true, /* Generates corresponding '.d.ts' file. */\n "sourceMap": true, /* Generates corresponding '.map' file. */\n "outDir": "dist", /* Redirect output structure to the directory. */\n "strict": true, /* Enable all strict type-checking options. */\n "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */\n "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */\n "baseUrl": "./", /* Base directory to resolve non-absolute module names. */\n "paths": {\n "*": ["*"]\n }, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */\n "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */\n "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */\n "skipLibCheck": true, /* Skip type checking of declaration files. */\n "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */\n },\n "include": [\n "src/**/*"\n ]\n }\nRun Code Online (Sandbox Code Playgroud)\n这是我的 .gitignore 文件:
\nlib-cov\n*.seed\n*.log\n*.csv\n*.dat\n*.out\n*.pid\n*.gz\n*.swp\n\npids\nlogs\nresults\ntmp\n\n# Build\npublic/css/main.css\n\n# Coverage reports\ncoverage\n\n# API keys and secrets\n.env\n\n# Dependency directory\nnode_modules\nbower_components\n\n# Editors\n.vscode\n.idea\n*.iml\n\n# OS metadata\n.DS_Store\nThumbs.db\n\n# Ignore built ts files\ndist/**/*\n\n# ignore yarn.lock\nyarn.lock\n\n**.log.**\nRun Code Online (Sandbox Code Playgroud)\n注意:即使dist包含在 .gitignore 中,该文件dist/index.js也会发布在 NPM 上,但没有任何.d.ts文件发布。
目录结构是这样的:
\nmy-package/\n\xe2\x94\x9c\xe2\x94\x80 node_modules/\n\xe2\x94\x9c\xe2\x94\x80 dist/\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80 models/\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80 index.d.ts\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80 index.js\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80 index.js.map\n\xe2\x94\x9c\xe2\x94\x80 src/\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80 models\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80 index.ts\n\xe2\x94\x9c\xe2\x94\x80 .gitignore\n\xe2\x94\x9c\xe2\x94\x80 package.json\n\xe2\x94\x9c\xe2\x94\x80 tsconfig.json\nRun Code Online (Sandbox Code Playgroud)\n
Iva*_* V. 13
您需要使用files key,这样只有files数组中的内容才会发布到 npm,并且您不需要.npmignore
例子:
"main": "dist/cjs/index.js",
"module": "dist/esm/untilted.esm.js",
"unpkg": "dist/unpkg/untilted.js",
"types": "dist/types/",
"files": [
"dist",
"src"
],
Run Code Online (Sandbox Code Playgroud)
在此示例中,dist目录保存所有已编译的文件,并src保存源代码(源映射所需)。这就是将要发布到 npm 的所有内容以及始终包含的一些文件(package.json、README、LICENCE 等...)
| 归档时间: |
|
| 查看次数: |
5465 次 |
| 最近记录: |