简体   繁体   English

带有 ESM 模块的 Tone.js

[英]Tone.js with ESM modules

I am trying to use tone.js on ESM modules.我正在尝试在 ESM 模块上使用tone.js。 (I could use it without problems in “commonjs” with a bundler) (我可以使用捆绑器在“commonjs”中毫无问题地使用它)

In the html I have在我的html中

<script src="tests.js" type="module"></script>

and tests.js:和tests.js:

import * as Tone from "./Tone.js"  

gives -> Tone.Gain is not a constructor给出-> Tone.Gain is not a constructor

If I try:如果我尝试:

import * as Tone from "./node_modules/tone/build/esm/index.js";

then Chrome shows status 404 Global not found, and the same for classes, version, ToneAudioBuffer, AudioContext, ToneAudioBuffers andToneBufferSource然后 Chrome 显示状态 404 Global not found, classes, version, ToneAudioBuffer, AudioContext, ToneAudioBuffers andToneBufferSource

(Maybe I am wrong, just starting with ESM modules, but digging into that esm/index.js the imports are like import { ToneAudioBuffer } from "./core/context/ToneAudioBuffer"; (without .js extension, shouldn't have any ESM module explicitly add the extension?) (也许我错了,只是从 ESM 模块开始,但是深入研究 esm/index.js 导入就像 import { ToneAudioBuffer } from "./core/context/ToneAudioBuffer"; (没有 .js 扩展名,不应该有任何 ESM 模块显式添加扩展?)

I've lost track of other combinations I have tried without success and I can not find a working example of a such project.我已经忘记了我尝试过的其他组合但没有成功,我找不到此类项目的工作示例。 What would be the right way - if possible- to run Tone.js on js modules?如果可能的话,在 js 模块上运行 Tone.js 的正确方法是什么?

Without bundling, serving a HTML with a module script, try import "./node_modules/tone/build/esm/index.js";不捆绑,使用模块脚本提供 HTML,尝试import "./node_modules/tone/build/esm/index.js"; . .

Or use a build of some kind, then use the recommended import import * as Tone from "tone";或者使用某种构建,然后使用推荐的 import import * as Tone from "tone"; . .

Optionally, use the CDN, with the plain global <script src="https://unpkg.com/tone@14.7.77/build/Tone.js"></script> or the module syntax above.或者,使用 CDN,使用普通的全局<script src="https://unpkg.com/tone@14.7.77/build/Tone.js"></script>或上面的模块语法。

 <script type="module"> import "https://unpkg.com/tone@14.7.77/build/Tone.js"; const btn = document.querySelector("button"); btn.addEventListener("click", async () => { await Tone.start(); const synth = new Tone.Synth().toDestination(); const now = Tone.now(); synth.triggerAttack("C4", now); synth.triggerRelease(now + 50); }); </script> <button>Play</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM