-
Notifications
You must be signed in to change notification settings - Fork 129
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
Support dynamic import() from ES6 module #180
Comments
This. |
I did it like this:I created a new "project" with a {
"name": "YOU_NAME_IT",
"version": "3.9.7",
"scripts": {
"build": "npx webpack"
},
"devDependencies": {
"jsmediatags": "^3.9.7",
"@babel/core": "^7.26.0",
"@babel/preset-env": "^7.26.0",
"babel-loader": "^9.2.1",
"webpack": "^5.95.0",
"webpack-cli": "^5.1.4"
}
} Than I created a module.exports = {
mode: 'production',
entry: './src/index.js',
output: {
filename: 'jsmediatags.mjs',
path: __dirname + '/dist',
libraryTarget: 'module'
},
experiments: {
outputModule: true
},
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
}
]
}
} and a {
"presets": ["@babel/preset-env"]
} I also create a folder const jsmediatags = require('../node_modules/jsmediatags/dist/jsmediatags.min.js')
export default jsmediatags
export class MediaTagReader {
static read(file, options) {
return new Promise((resolve, reject) => {
new jsmediatags.Reader(file).read({
onSuccess: resolve,
onError: reject
})
})
}
} after that, you simply run Usageimport { MediaTagReader } from '/PATH_TO_YOUR_PACKAGE_NAME/dist/jsmediatags.mjs'
try {
const { tags } = await MediaTagReader.read('http://path_to_your.mp3')
const picture = tags.picture
if (picture) {
const { data, format } = picture
const byteArray = new Uint8Array(data)
const blob = new Blob([byteArray], { type: format })
const url = URL.createObjectURL(blob)
console.log('Album cover found in this MP3 file:', url)
URL.revokeObjectURL(url)
} else {
console.log('No album cover found in this MP3 file.')
}
} catch (error) {
console.error('Error reading tags:', error.type, error.info)
} Hope that helps someone. :) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To reproduce:
Error:
Reason is that
this
isundefined
in ES6 modules:I understand we use
this
here to support Node. CangetGlobal()
returnwindow
if it is passedundefined
?The text was updated successfully, but these errors were encountered: