-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
兼容浏览器与nodejs的lib #293
Comments
支持双端引入使用使用rollup打包,输出两个模块。 安装 rollup $ yarn add rollup
yarn add v1.22.18
warning ../package.json: No license field
info No lockfile found.
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved lockfile.
success Saved 1 new dependency.
info Direct dependencies
└─ rollup@3.3.0
info All dependencies
└─ rollup@3.3.0
Done in 1.14s. 创建配置文件, 废弃的配置: const pkgName = 'index';
// 注意使用 js 文件时使用 module.exports
module.exports = {
input: 'index.js',
output: [
// 服务端使用
{
file: `/dist/${pkgName}.cjs.js`,
format: 'cjs'
},
// 浏览器使用
{
file: `/dist/${pkgName}.esm.js`,
format: 'es'
}
]
}; 添加开发时与生产时的构建脚本命令 "scripts": {
"dev": "rollup --environment NODE_ENV:development -c rollup.config.js --watch",
"build": "rollup --environment NODE_ENV:production -c rollup.config.js"
}, 最后使用的配置 const clear = require("rollup-plugin-clear");
const commonjs = require("@rollup/plugin-commonjs");
const nodeResolve = require("@rollup/plugin-node-resolve");
const babel = require('@rollup/plugin-babel');
const PACKAGE_NAME = 'index';
const emEnvType = {
PRODUCTION: 'production',
DEVELOPMENT: 'development'
};
const env = process.env.NODE_ENV || emEnvType.PRODUCTION;
const productionOutput = () => ({
file: `./dist/${PACKAGE_NAME}.umd.js`,
format: 'umd',
name: PACKAGE_NAME
});
const developmentOutput = () => ({
file: `./dist/${PACKAGE_NAME}.umd.js`,
format: 'umd',
name: PACKAGE_NAME
});
// config ref: https://www.rollupjs.com/guide/command-line-reference
module.exports = {
input: 'index.js',
output: [env === emEnvType.PRODUCTION ? productionOutput() : developmentOutput()],
plugins: [
clear({
targets: ['dist']
}),
commonjs(), nodeResolve(),
babel({ babelHelpers: 'bundled' })
]
}; 修改packagejson的入口说明 |
附录参考 |
生成types文件使用 typescript 的 tsc 命令生成声明文件 ( 安装 typescriptyarn add typescript --dev 配置 tsconfig.json{
// Change this to match your project
"include": ["index.js"],
"compilerOptions": {
// Tells TypeScript to read JS files, as
// normally they are ignored as source files
"allowJs": true,
// Generate d.ts files
"declaration": true,
// This compiler run should
// only output d.ts files
"emitDeclarationOnly": true,
// Types should go into this directory.
// Removing this would place the .d.ts files
// next to the .js files
"outDir": "dist/types",
// go to js file when using IDE functions like
// "Go to Definition" in VSCode
"declarationMap": true
}
} 更新packagejson描述 types 文件位置 {
"name": "@tencent/metaverse-ajv-schema",
"types": "dist/types/index.d.ts",
"scripts": {
// ...
"dts": "tsc"
},
"devDependencies": {
// ...
"typescript": "^4.8.4"
}
} 附录参考 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Table Of Contents
初始结构
使用的是es6+的js语法编写,注意不是ts。
在浏览器端使用 import引入没有问题,但是在服务端引入会跑错!
服务端当前使用的ts实现,当前的库也是缺少ts支持!
略微梳理一下,接下来的几个小目标:
The text was updated successfully, but these errors were encountered: