请问 写一个npm包的正确姿势是什么,官方能给一个helloworld吗。我搞的包creator中总是加载不到。
我的配置文件如下
rollup.config.js
import typescript from "rollup-plugin-typescript";
export default {
input: "./index.ts",
externals: ["cc", "cc/env"],
plugins: [
typescript({
exclude: "node_modules/**",
typescript: require("typescript")
}),
],
output: [
{
format: "esm",
file: "dist/index.js"
}
]
};
package.json
{
"name": "gamelib",
"version": "1.0.0",
"description": "",
"type": "module",
"main": "./dist/index.js",
"scripts": {
"build": "rollup -c"
},
"author": "",
"license": "ISC",
"devDependencies": {
"rollup": "^2.75.7",
"rollup-plugin-typescript": "^1.0.1",
"tslib": "^2.4.0",
"typescript": "^4.7.4"
}
}
tsconfig.json
{
"compilerOptions": {
"module": "esnext",
"target": "esnext",
"experimentalDecorators": true,
"noEmitHelpers": false,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"exclude": [
"node_modules"
]
}
没人吗???
可能去node js 的相关社区提问关心的人会多点,毕竟这里是游戏引擎论坛搞node的人没那么多
你是写插件么,插件官方有demo的。
这样打包出来有什么吗?
output那里,输出成.mjs后缀吧
如果只是封装的话,可以不用NPM,建议看下这个
没事了。改成mjs,并重启了电脑可以了。
下次不用重启电脑,试下 [开发者]/[清除缓存]/[代码缓存]
目前能在npm包里写 Component 或者存 资源吗?
不能,因为 Component 需要生成 UUID。现在只有项目 assets 下面会生成 UUID。
大佬再看看我另外一个贴的问题呗
哪个?你连接贴出来
抱歉,我对这类问题不了解,转发给别人了
2.4版本, 打包库的js format设置为umd可以用
rollup.config.js
import filesize from 'rollup-plugin-filesize'
import babel from '@rollup/plugin-babel'
import resolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import typescript from '@rollup/plugin-typescript'
import { terser } from 'rollup-plugin-terser'
const config = [{
input: 'src/index.ts',
output: [
{
format: 'umd',
file: 'dist/index.js',
sourcemap: true,
name: 'lib-name',
},
],
plugins: [
typescript({ tsconfig: './tsconfig.type.json' }),
resolve(),
commonjs(),
terser({
compress: {
drop_console: true,
},
}),
filesize(),
babel({ babelHelpers: 'runtime', exclude: ['node_modules/**'] }),
],
},]
tsconfig.type.json
{
"compilerOptions": {
"module": "ES2015",
"lib": [
"es2015",
"es2017",
"dom"
],
"target": "ES2015",
"experimentalDecorators": true,
"skipLibCheck": true,
"declaration": true,
"outDir": "dist"
},
"exclude": [
"node_modules"
]
}