Skip to content
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

Feat: exports for Base Plugin #3495

Merged
merged 9 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@
"types": "./dist/plugins/*.d.ts",
"require": "./dist/plugins/*.cjs"
},
"./dist/base-plugin.js": {
"import": "./dist/base-plugin.esm.js",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might backfire because the main bundle will have the same imports but a different copy thereof. So you would basically ship duplicate code, plus the class/type identity will differ. Wavesurfer expects plugins to be subclassesof that specific BasePlugin import, not another copy. Hope this makes sense, it barely does to me :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup I think this PR still has some issues beyond just that. Demoted it to a draft again and will look at it some more here in a bit. But I think I might just end up closing it for now.

More to come soon!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it's possible to ignore named exports for CommonJS modules via a rollup plugin.

Another alternative, just to acommodate the CommonJS module, would be to expose BasePlugin as a static property of the WaveSurfer class instead of exporting it as a named export.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might backfire because the main bundle will have the same imports but a different copy thereof. So you would basically ship duplicate code, plus the class/type identity will differ. Wavesurfer expects plugins to be subclassesof that specific BasePlugin import, not another copy. Hope this makes sense, it barely does to me :)

Just to clarify, this is already an existing problem. The current exports do not use the same BasePlugin class instance. Let me give an example.

import RegionsPlugin from 'wavesurfer.js/dist/plugins/regions.esm.js';
import TimelinePlugin from 'wavesurfer.js/dist/plugins/timeline.esm.js';

Both of these imports are distinct compilations of the source code with their own cjs/esm/min bundles. They contain the code for BasePlugin individually and are not referencing the same instance of the class.

This can be illustrated by running the following code:

console.log('same proto?', RegionsPlugin.__proto__ === TImelinePlugin.__proto__);

Or using instanceof:

const regions = new RegionsPlugin();
const timeline = new TimelinePlugin();
console.log('instanceof RegionsPlugin.__proto__?', regions instanceof RegionsPlugin.__proto__);
console.log('instanceof TimelinePlugin.__proto__?', regions instanceof TimelinePlugin.__proto__);

The way to fix this would be to modify the library to return a single bundle.

import WaveSurfer, { BasePlugin, TimelinePlugin, RegionsPlugin } from 'wavesurfer.js'

// Now TimelinePlugin.__proto__ === RegionsPlugin.__proto__ === BasePlugin

You can get this to work pretty easily by having a root index.js that contains the exports. Use rollup to build one single bundle and export it all. It lends itself to tree shaking this way, too.

But it would be a breaking change to people so this would require a major version bump.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, and thanks for Index.ts pointer.

"types": "./dist/base-plugin.d.ts",
"require": "./dist/base-plugin.cjs"
},
"./dist/dom.js": {
"import": "./dist/dom.esm.js",
"types": "./dist/dom.d.ts",
"require": "./dist/dom.cjs"
},
"./dist/*": {
"import": "./dist/*"
}
Expand Down
62 changes: 62 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,66 @@ export default [
},
])
.flat(),

// ES module
{
input: 'src/base-plugin.ts',
output: {
file: 'dist/base-plugin.esm.js',
format: 'esm',
},
plugins,
},
// CommonJS module (Node.js)
{
input: 'src/base-plugin.ts',
output: {
file: 'dist/base-plugin.cjs',
format: 'cjs',
exports: 'default',
},
plugins,
},
// UMD (browser script tag)
{
input: 'src/base-plugin.ts',
output: {
name: 'BasePlugin',
file: 'dist/base-plugin.min.js',
format: 'umd',
exports: 'default',
},
plugins,
},

// ES module
{
input: 'src/dom.ts',
output: {
file: 'dist/dom.esm.js',
format: 'esm',
},
plugins,
},
// CommonJS module (Node.js)
{
input: 'src/dom.ts',
output: {
file: 'dist/dom.cjs',
format: 'cjs',
exports: 'default',
},
plugins,
},
// UMD (browser script tag)
{
input: 'src/dom.ts',
output: {
name: 'createElement',
file: 'dist/dom.min.js',
format: 'umd',
exports: 'default',
},
plugins,
},
]
2 changes: 1 addition & 1 deletion src/base-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type BasePluginEvents = {
export type GenericPlugin = BasePlugin<BasePluginEvents, unknown>

/** Base class for wavesurfer plugins */
export class BasePlugin<EventTypes extends BasePluginEvents, Options> extends EventEmitter<EventTypes> {
class BasePlugin<EventTypes extends BasePluginEvents, Options> extends EventEmitter<EventTypes> {
protected wavesurfer?: WaveSurfer
protected subscriptions: (() => void)[] = []
protected options: Options
Expand Down
8 changes: 4 additions & 4 deletions src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ function renderNode(tagName: string, content: TreeNode): HTMLElement | SVGElemen
return element
}

function render(tagName: string, content: TreeNode & { xmlns: string }, container?: Node): SVGElement
function render(tagName: string, content?: TreeNode, container?: Node): HTMLElement
function render(tagName: string, content?: TreeNode, container?: Node): HTMLElement | SVGElement {
function createElement(tagName: string, content: TreeNode & { xmlns: string }, container?: Node): SVGElement
function createElement(tagName: string, content?: TreeNode, container?: Node): HTMLElement
function createElement(tagName: string, content?: TreeNode, container?: Node): HTMLElement | SVGElement {
const el = renderNode(tagName, content || {})
container?.appendChild(el)
return el
}

export default render
export default createElement
8 changes: 4 additions & 4 deletions src/plugins/envelope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import BasePlugin, { type BasePluginEvents } from '../base-plugin.js'
import { makeDraggable } from '../draggable.js'
import EventEmitter from '../event-emitter.js'
import render from '../dom.js'
import createElement from '../dom.js'

export type EnvelopePoint = {
id?: string
Expand Down Expand Up @@ -66,7 +66,7 @@ class Polyline extends EventEmitter<{
const height = wrapper.clientHeight

// SVG element
const svg = render(
const svg = createElement(
'svg',
{
xmlns: 'http://www.w3.org/2000/svg',
Expand All @@ -88,7 +88,7 @@ class Polyline extends EventEmitter<{
this.svg = svg

// A polyline representing the envelope
const polyline = render(
const polyline = createElement(
'polyline',
{
xmlns: 'http://www.w3.org/2000/svg',
Expand Down Expand Up @@ -173,7 +173,7 @@ class Polyline extends EventEmitter<{
private createCircle(x: number, y: number) {
const size = this.options.dragPointSize
const radius = size / 2
return render(
return createElement(
'ellipse',
{
xmlns: 'http://www.w3.org/2000/svg',
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/hover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

import BasePlugin, { type BasePluginEvents } from '../base-plugin.js'
import render from '../dom.js'
import createElement from '../dom.js'

export type HoverPluginOptions = {
lineColor?: string
Expand Down Expand Up @@ -33,8 +33,8 @@ class HoverPlugin extends BasePlugin<HoverPluginEvents, HoverPluginOptions> {
this.options = Object.assign({}, defaultOptions, options)

// Create the plugin elements
this.wrapper = render('div', { part: 'hover' })
this.label = render('span', { part: 'hover-label' }, this.wrapper)
this.wrapper = createElement('div', { part: 'hover' })
this.label = createElement('span', { part: 'hover-label' }, this.wrapper)
}

public static create(options?: HoverPluginOptions) {
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/minimap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import BasePlugin, { type BasePluginEvents } from '../base-plugin.js'
import WaveSurfer, { type WaveSurferOptions } from '../wavesurfer.js'
import render from '../dom.js'
import createElement from '../dom.js'

export type MinimapPluginOptions = {
overlayColor?: string
Expand Down Expand Up @@ -63,7 +63,7 @@ class MinimapPlugin extends BasePlugin<MinimapPluginEvents, MinimapPluginOptions
}

private initMinimapWrapper(): HTMLElement {
return render('div', {
return createElement('div', {
part: 'minimap',
style: {
position: 'relative',
Expand All @@ -72,7 +72,7 @@ class MinimapPlugin extends BasePlugin<MinimapPluginEvents, MinimapPluginOptions
}

private initOverlay(): HTMLElement {
return render(
return createElement(
'div',
{
part: 'minimap-overlay',
Expand Down
12 changes: 6 additions & 6 deletions src/plugins/regions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import BasePlugin, { type BasePluginEvents } from '../base-plugin.js'
import { makeDraggable } from '../draggable.js'
import EventEmitter from '../event-emitter.js'
import render from '../dom.js'
import createElement from '../dom.js'

export type RegionsPluginOptions = undefined

Expand Down Expand Up @@ -120,7 +120,7 @@ class SingleRegion extends EventEmitter<RegionEvents> {
wordBreak: 'keep-all',
}

const leftHandle = render(
const leftHandle = createElement(
'div',
{
part: 'region-handle region-handle-left',
Expand All @@ -134,7 +134,7 @@ class SingleRegion extends EventEmitter<RegionEvents> {
element,
)

const rightHandle = render(
const rightHandle = createElement(
'div',
{
part: 'region-handle region-handle-right',
Expand Down Expand Up @@ -188,7 +188,7 @@ class SingleRegion extends EventEmitter<RegionEvents> {
elementTop = elementHeight * this.channelIdx
}

const element = render('div', {
const element = createElement('div', {
style: {
position: 'absolute',
top: `${elementTop}%`,
Expand Down Expand Up @@ -319,7 +319,7 @@ class SingleRegion extends EventEmitter<RegionEvents> {
}
if (typeof content === 'string') {
const isMarker = this.start === this.end
this.content = render('div', {
this.content = createElement('div', {
style: {
padding: `0.2em ${isMarker ? 0.2 : 0.4}em`,
display: 'inline-block',
Expand Down Expand Up @@ -439,7 +439,7 @@ class RegionsPlugin extends BasePlugin<RegionsPluginEvents, RegionsPluginOptions
}

private initRegionsContainer(): HTMLElement {
return render('div', {
return createElement('div', {
style: {
position: 'absolute',
top: '0',
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/spectrogram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ function FFT(bufferSize: number, sampleRate: number, windowFunc: string, alpha:
* Spectrogram plugin for wavesurfer.
*/
import BasePlugin, { type BasePluginEvents } from '../base-plugin.js'
import render from '../dom.js'
import createElement from '../dom.js'

export type SpectrogramPluginOptions = {
/** Selector of element or element in which to render */
Expand Down Expand Up @@ -347,7 +347,7 @@ class SpectrogramPlugin extends BasePlugin<SpectrogramPluginEvents, SpectrogramP
}

private createWrapper() {
this.wrapper = render('div', {
this.wrapper = createElement('div', {
style: {
display: 'block',
position: 'relative',
Expand All @@ -357,7 +357,7 @@ class SpectrogramPlugin extends BasePlugin<SpectrogramPluginEvents, SpectrogramP

// if labels are active
if (this.options.labels) {
this.labelsEl = render(
this.labelsEl = createElement(
'canvas',
{
part: 'spec-labels',
Expand All @@ -376,7 +376,7 @@ class SpectrogramPlugin extends BasePlugin<SpectrogramPluginEvents, SpectrogramP
}

private createCanvas() {
this.canvas = render(
this.canvas = createElement(
'canvas',
{
style: {
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

import BasePlugin, { type BasePluginEvents } from '../base-plugin.js'
import render from '../dom.js'
import createElement from '../dom.js'

export type TimelinePluginOptions = {
/** The height of the timeline in pixels, defaults to 20 */
Expand Down Expand Up @@ -104,7 +104,7 @@ class TimelinePlugin extends BasePlugin<TimelinePluginEvents, TimelinePluginOpti
}

private initTimelineWrapper(): HTMLElement {
return render('div', { part: 'timeline-wrapper', style: { pointerEvents: 'none' } })
return createElement('div', { part: 'timeline-wrapper', style: { pointerEvents: 'none' } })
}

// Return how many seconds should be between each notch
Expand Down Expand Up @@ -153,7 +153,7 @@ class TimelinePlugin extends BasePlugin<TimelinePluginEvents, TimelinePluginOpti
const secondaryLabelSpacing = this.options.secondaryLabelSpacing
const isTop = this.options.insertPosition === 'beforebegin'

const timeline = render('div', {
const timeline = createElement('div', {
style: {
height: `${this.options.height}px`,
overflow: 'hidden',
Expand All @@ -179,7 +179,7 @@ class TimelinePlugin extends BasePlugin<TimelinePluginEvents, TimelinePluginOpti
Object.assign(timeline.style, this.options.style)
}

const notchEl = render('div', {
const notchEl = createElement('div', {
style: {
width: '0',
height: '50%',
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/zoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* });
*/

import { BasePlugin, BasePluginEvents } from '../base-plugin.js'
import BasePlugin, { type BasePluginEvents } from '../base-plugin.js'

export type ZoomPluginOptions = {
/**
Expand Down