-
Notifications
You must be signed in to change notification settings - Fork 75
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
Run script before MDX Bundler runs #74
Comments
Looking at it this seems like something an esbuild plugin could do. You could then pass the plugin into mdx-bundler so it can generate the file at the same time. |
@Arcath just to be clear, do I have to write an I already converted the code: import fs from 'fs'
import path from 'path'
import sharp from 'sharp'
import { Plugin } from 'esbuild'
const ROOT_PATH = process.cwd()
const POSTS_PATH = path.join(ROOT_PATH, 'public')
function* walkSync(dir: fs.PathLike): any {
const files = fs.readdirSync(dir, { withFileTypes: true })
for (let i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
yield* walkSync(path.join(dir as string, files[i].name))
} else {
yield path.join(dir as string, files[i].name)
}
}
}
const gifToPng = async () => {
try {
for (let [i, file] of [...walkSync(POSTS_PATH)].entries()) {
const extname = path.extname(file)
if (extname === '.gif') {
console.log(file)
const dirname = path.dirname(file)
const png = path.resolve(dirname, path.basename(file).replace('.gif', '.png'))
await sharp(file).png().toFile(png)
}
}
} catch (e) {
console.error('Error thrown:', e)
}
}
export const gifToPngPlugin = (): Plugin => ({
name: 'gifToPng',
setup(build) {
build.onLoad({ filter: /\.gif$/ }, async (args) => {
const fileName = path.basename(args.path, '.gif')
let contents = await fs.promises.readFile(args.path, 'utf8')
console.log({ fileName, contents })
return {
contents,
loader: 'file',
}
})
},
}) And I'm trying to write the plugin but getting an error:
Not sure if ESBuild can run extensions with |
it would need to be a esbuild plugin that has a filter of |
@Arcath i've already read it. that's where I got this issue from. the issue isn't see |
oops, found this :) |
mdx-bundler
version: ^5.1.0node
version: v16.3.0npm
version: 7.15.1Relevant code or config
I have a
gif-to-png.js
script that converts GIFs to PNG indevelopment
in thesrc/_posts
directory. As to why I use it, I have written about it here.TL;DR I'm creating a GIF Player that shows static PNG images when someone pauses a GIF & play button to play the GIF because I hate to see GIF loop while reading text.
gif-to-png.js
What you did:
This makes a copy of PNG in
src/_posts
folder appropriately by converting the GIF file.What happened:
It doesn't copy the PNG file to the appropriate location as
mdx-bundler
probably runs earlier than this.Reproduction repository:
https://github.com/deadcoder0904/mdx-bundler-gif-png-error
Problem description:
I just want those PNG files in
public/
directory. Is there any way to run the scripts beforemdx-bundler
?Suggested solution:
No idea. I did try running the script in
public
directory instead ofsrc/_posts
so it renames there but it didn't work.The text was updated successfully, but these errors were encountered: