有一个打包好的文件umd.js文件,我希望能在html中直接使用他暴露出来的方法,出了将方法挂载到window上还有其他办法吗,这个是我暴露方式,打包是它通过vite打包的
const canvasEditorFun = {
initEditorByFile,
getHTML,
saveCEData,
initEditorByData
}
export default canvasEditorFunvite.config.ts
plugins: [
cssInjectedByJsPlugin({
styleId: `${name}-style`,
topExecutionPriority: true
}),
{
...typescript({
tsconfig: './tsconfig.json',
include: ['./src/**']
}),
apply: 'build',
declaration: true,
declarationDir: 'types/',
rootDir: '/'
}
],
build: {
lib: {
name,
fileName: name,
// entry: path.resolve(__dirname, 'src/editor/index.ts')
entry: path.resolve(__dirname, 'src/main.ts')
},
rollupOptions: {
output: {
sourcemap: true
}
}
}我希望可以直接在html中引用暴露出来的方法
只能全局引用(即挂载到
window上),然后通过合理的路径调用。不过你完全可以多打包几份,比如 umd、iife、esm 各一份,然后按照你的需要引用。