Coder Social home page Coder Social logo

Comments (4)

webketje avatar webketje commented on May 28, 2024 1

Ok in that case, what I can do to resolve this issue is:

  • 1. make sure the core plugins use the match(pattern, files) signature
  • 2. write a note in the API docs about the potential pitfalls when using branch/watch

I feel this shouldn't be a core feature of metalsmith, cf:

metalsmith itself has no notion of a "temporarily altered filelist" (and I'm keen to keep it out)

MS takes a source and a destination and that's it. Another way to solve it would be running multiple ms builds using ignore and clean(false) or different source folders.

But feel free to use my snippet as a starting point for a new branch plugin.
I'll mark this issue closed when I've gotten to the 2 points above

from default-values.

emmercm avatar emmercm commented on May 28, 2024

The most recent plugin update here is causing my function parameter to be undefined for files that are matched with metalsmith.match() but aren't in my branch:

https://github.com/emmercm/www/blob/e285f05d09eaf33d32520181a1b1a5bfe607a3ad/index.js#L491

from default-values.

webketje avatar webketje commented on May 28, 2024

A similar issue has already been reported at metalsmith/layouts#183; there I expressed some elaborate points of view concerning plugins like branch/watch. Not sure why you reported on this plugin rather than metalsmith itself?

TBH I'm not sure how to "resolve" this as it is intended behavior. metalsmith.match works as documented at https://metalsmith.io/api/#Metalsmith+match and metalsmith itself has no notion of a "temporarily altered filelist" (and I'm keen to keep it out). If plugins author wish, they can go this route (but rather by using metalsmith methods like I did for a local plugin here: https://github.com/metalsmith/js-bundle/blob/main/test/index.cjs#L22, than by doing 'unorthodox things' like requiring MS private dependencies).

The broader issue title is "how to run a plugin sequence on a subset of files?", to which I would say specific plugins should provide a pattern option or target only relevant files (eg permalinks does .html only)

The metalsmith-branch plugin has several issues:

  1. it is not CLI-compatible
  2. it reconstructs ms internal logic using ware
  3. it is incompatible with any plugin making changes to the Files object keys (=file paths)

Below would be a more "compliant" branch plugin (untested, no support for nested branches).
Note that when using metalsmith.run instead of ware.run, metalsmith.match shorthand will target only the relevant files, as the _files cache is reset within this method

function configureBranch(pattern, ...plugins) {
  return function branch(files, ms, done) {
    const subset = ms.match(pattern, Object.keys(files))
      .reduce((subset, key) => {
      subset[key] = files[key]
      subset[key].__originalPath = key
      return subset
    }, {})

    ms.run(subset, plugins)
      .then(subset => {
        Object.entries(subset).forEach(([path, file]) => {
          files[path] = file
          if (file.__originalPath !== path) {
            delete files[file.__originalPath]
          }
          delete file.__originalPath
        })
        done()
      })
      .catch(done)
  }
}

// usage
ms.use(branch('**/*.md',
   layouts(), // these plugins can safely use metalsmith.match(pattern) shorthand
   markdown()
))

from default-values.

emmercm avatar emmercm commented on May 28, 2024

I opened an issue on this plugin specifically because it's the one that is causing the problem for me. But as I admitted, the "fix" I've been putting in my plugins is required for every plugin in order to work around metalsmith-branch. I didn't realize there was an issue with the manipulation of the files object.

I think it's somewhat unrealistic to expect every plugin to have a pattern option, even if that would be ideal. We can probably count on one hand the number of active plugin developers, or ones that even respond on GitHub. You can see how many issues I've opened on abandoned plugins over the last 3 years: https://github.com/issues?q=is%3Aopen+is%3Aissue+author%3Aemmercm+archived%3Afalse+metalsmith

Your issue looks fairly elegant, is that something one of us should create, or how do you feel about it being a core feature of Metalsmith?

from default-values.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.