Skip to content

Consider a simpler API #85

@Rich-Harris

Description

@Rich-Harris

It feels like we can make the API simpler. Why isn't the community template just this, for example?

import { defineIntegration } from '@sveltejs/cli/integration';

export default defineIntegration({
  description: 'An adder template demo',
  environments: { kit: true, svelte: true },
  options: {
    demo: {
      question: 'Do you want to use a demo?',
      type: 'boolean',
      default: false
    }
  },
  files: ({ options }) => [
    {
      name: 'adder-template-demo.txt',
      content: (file) => {
        if (options.demo) {
          return 'This is a text file made by the integration template demo!'
        }

        return file.content;
      }
    },
    {
      name: 'src/DemoComponent.svelte',
      content: (file) => {
        file.addImport('../adder-template-demo.txt?raw', 'default', 'Demo');
      }
    {
  ]
});

Things to note:

  • it's a single file. To me this is the difference between 'oh hey I can build one of these!' and 'ugh. strap in, I guess'. It probably doesn't even need a src directory
  • id and name have been removed. They don't need to be separate concepts — it's less confusing if we just use an id consistently, instead of leaving people who see 'Foo Bar' listed somewhere to wonder whether it's foobar or foo-bar or whatever. And for stuff on npm, the package name is the id, I'd argue we don't need another (that could potentially be something different than the package name, which is another source of confusion). Maybe there's an argument for mandating an id for programmatic usage but I think it's probably unnecessary
  • description and environments moved up to the top level instead of being in metadata
  • no packages array. Surely package.json can be just like any other JSON file? The smaller the API surface area the better
  • files is a function that returns an array, instead of an array of objects. That way it's easier to put logic in one place (e.g. you can do const ext = options.typescript ? 'ts' : 'js' once instead of in each name method, and easier to return a different set of files entirely under certain conditions
  • contentType is inferred from file extension (I don't know if this is possible to do reliably from a types perspective, just an idea)
  • utilities like addImport are passed in rather than imported. The big downside here is that it slightly discourages you from extracting the logic out into separate functions, but I'd argue a) this is a context where less indirection is better anyway, and b) it's outweighed by the benefits of discoverability and not having to deal with the exposed guts of things like jsAst unless you absolutely have to

Related note: What's the technical reason for the prohibition on dependencies? Seems very restrictive

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions