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

[Feature] Autodetect dist folder #41

Open
stalniy opened this issue Mar 10, 2020 · 4 comments
Open

[Feature] Autodetect dist folder #41

stalniy opened this issue Mar 10, 2020 · 4 comments

Comments

@stalniy
Copy link

stalniy commented Mar 10, 2020

Currently I need to prepend every dest field with dist/ folder, so I do duplicate where I plan to output files in several places (dest fields and output options of rollup).

Rollup has outputOptions hook which can be used to obtain directory from that options. So, I don't need to duplicate it.

There are 2 possible cases:

  1. when output.file is specified - dirname(ouput.file)
  2. when output.dir is specified - just use this dir as a root path.
@stalniy stalniy changed the title Autodetect dist folder [Feature] Autodetect dist folder Mar 10, 2020
@vladshcherbin
Copy link
Owner

@stalniy I had this in 1.0.0.

However, I removed it when hook option was added. Currently, buildEnd hook is used as a default one and it is executed before outputOptions hook.

@vladshcherbin
Copy link
Owner

I can add outputFolder option back so you can set global folder once, but you'll still need to set it (duplicate). Don't know a way to make autodetection since outputOptions can't be used with hook option.

What do you think?

@aminnairi
Copy link

aminnairi commented Aug 19, 2020

Hi everyone,

If I may, I think that something like a dest global option could be great.

import {resolve} from "path";

const src = (...paths) => resolve("src", ...paths);

export default {
    plugins: [
        copy({
            dest: resolve("dist"),
            targets: [
                src("index.html"),
                src("service-worker.js"),
                src("manifest.webmanifest"),
                {
                    src: resolve("something", "else.json"),
                    dest: resolve("somewhere", "else")
                }
            ]
        })
    ]
}

This would have the advantage of being really concise and reduce the code written for copying multiple files to a similar location. Plus (but I'm really unsure about that) we could maybe use this plugin multiple times for multiple shared folders (while keeping the actual behavior).

import {resolve} from "path";

const src = (...paths) => resolve("src", ...paths);
const dist = (...paths) => resolve("dist", ...paths);

export default {
    plugins: [
        copy({
            dest: dist("client"),
            targets: [
                src("foo"),
                src("bar"),
                src("baz")
            ]
        }),
        copy({
            dest: dist("server"),
            targets: [
                src("foo"),
                src("bar"),
                src("baz")
            ]
        })
    ]
}

As a workaround, I have found to be used this little function a lot in my Rollup projects. Here is the code snippet.

import {path} from "path";

const dist = (...paths) => resolve("path", "to", "dist", ...paths);

dist("client", "index.js"); // ./path/to/dist/client/index.js
dist("server", "main.js"); // ./path/to/dist/server/main.js

This also work independently on whether you are using GNU/Linux, Mac OS X or Windows since resolve will join the strings with the correct delimiter for the current operating system using path.sep.

I Hope it helps while a solution is found.

@artemjackson
Copy link

artemjackson commented Apr 14, 2021

@vladshcherbin Hey, I've worked around this allowing to specify function as dest:

import RollupPluginCopy from "rollup-plugin-copy"

/** @param {import("rollup-plugin-copy").CopyOptions} options */
function copy(options) {
  options.hook ??= "buildEnd"

  return {
    [options.hook](...args) {
      options.targets = options.targets.map(({ dest, ...target }) => ({
        ...target,
        dest: typeof dest === "function" ? dest(...args) : dest,
      }))

      const plugin = RollupPluginCopy(options)

      return plugin[options.hook](...args)
    },
  }
}

export default  {
  plugins: [copy({
    hook: "writeBundle",
    targets: [{
      src: "some/folder/files.*",
      dest: cfg => cfg.dir,
    })
  }]
}

Feel free to borrow the idea ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants