@rollup-extras/plugin-copy
TypeScript icon, indicating that this package has built-in type declarations

1.9.3 • Public • Published

Plugin Copy

Rollup plugin to copy assets during build.

Points:

  • Uses emitFile by default so all files goes through rollup asset pipeline
  • Minimal configuration
  • Runs once per file by default
  • Support hashes (uses assetFileNames from rollup)
  • Watch on files so when they changed that can be copied again (but only if timestamp is changed)
  • Minimal amount of logs by default
  • Supports globs (check 'glob' for syntax)
  • Can be run both as output or build plugin (build plugin by default for watch)

Uses @niceties/logger to log messages, can be configured through @niceties/logger API.

Changelog

Installation

Using npm:

npm install --save-dev @rollup-extras/plugin-copy

Using yarn:

yarn add --dev @rollup-extras/plugin-copy

Examples

Assuming you imported plugin using:

import copy from '@rollup-extras/plugin-copy';

Next examples are equivalent:

copy('assets/*')
copy(['assets/*'])
copy({ src: 'assets/*' })
copy({ src: ['assets/*'] })
copy([{ src: 'assets/*' }])
copy({ targets: 'assets/*' })
copy({ targets: ['assets/*'] })
copy({ targets: [{ src: 'assets/*' }] })

all of them will trigger a copy (through emitFile) of all files in assets in each output directory.

To copy files on every rebuild in watch mode use copyOnce = false:

copy({ src: 'assets/*', copyOnce: false })

// or

copy({ targets: ['assets/*'], copyOnce: false })

To stop triggering on changes in files use watch = false:

copy({ src: 'assets/*', watch: false })

// or

copy({ targets: ['assets/*'], watch: false })

To display more information in console use verbose = true:

copy({ src: 'assets/*', verbose: true })

// or

copy({ targets: ['assets/*'], verbose: true })

By default plugin uses glob-parent to preserve directory structure of assets (relative to glob parent path). To flatten files in assets directory use flatten = true:

copy({ src: 'assets/*', flatten: true })

// or

copy({ targets: ['assets/*'], flatten: true })

To add hashes to file names use exactFileNames = false, tweak assetFileNames option in rollup config if needed. Files with the same content will be deduplicated by rollup in this mode.

copy({ src: 'assets/*', exactFileNames: false })

// or

copy({ targets: ['assets/*'], exactFileNames: false })

To work as output plugin use outputPlugin = true option (watch mode will be disabled because of rollup limitations):

copy({ src: 'assets/*', outputPlugin: true })

// or

copy({ targets: ['assets/*'], outputPlugin: true })

To stop files being emitted through rollup pipeline use can use emitFiles = false. Please note that you need to specify dest and it will not be relative to the output directory, also the file will not be copied into each output directory.

copy({ src: 'assets/*', dest: 'public', emitFiles: false })

// or

copy({ targets: [{ src: 'assets/*', dest: 'public' }], emitFiles: false })

dest and exclude

Use the dest option to put assets into the subfolder in the assets directory. As an example if we have assets as a directory for assets and public as an output directory and we specify 'dest' = 'fonts' assets will be copied into public/assets/fonts preserving assets directory structure.

Use exclude option to filter out files in assets (passed to ignore option of glob options). For example *.json will filter out json files.

copy({ src: 'assets/*', dest: 'fonts', exclude: '*.json' })

// or

copy({ targets: [{ src: 'assets/*', dest: 'fonts', exclude: '*.json' }] })

Configuration

type SingleTargetDesc = {
    src: string | string[],
    exclude?: string | string[],
    dest?: string;
};

type MultipleTargetsDesc = string | string[] | SingleTargetDesc | SingleTargetDesc[];

type CopyPluginOptions = {
    targets?: MultipleTargetsDesc,
    pluginName?: string, // defaults to '@rollup-extras/plugin-copy'
    copyOnce?: boolean, // true by default
    watch?: boolean, // true by default
    verbose?: boolean | 'list-filenames', // false by default
    flattern?: boolean, // false by default
    exactFileNames?: boolean, // true by default
    outputPlugin?: boolean, // false by default
    emitFiles?: boolean // true by default
} | MultipleTargetsDesc;

Prior Art

License

MIT

Package Sidebar

Install

npm i @rollup-extras/plugin-copy

Weekly Downloads

101

Version

1.9.3

License

MIT

Unpacked Size

27.5 kB

Total Files

8

Last publish

Collaborators

  • kshutkin