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

Support watching feature #42

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ module.exports = {
'object-curly-newline': ['error', { multiline: true, consistent: true }],
semi: ['error', 'never']
},
ignorePatterns: [
'index.d.ts'
],
env: {
jest: true
}
Expand Down
124 changes: 69 additions & 55 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,76 @@
import rollup from 'rollup';
import fs from 'fs-extra';
import globby from 'globby';

interface Target extends globby.GlobbyOptions {
/**
* Path or glob of what to copy.
*/
readonly src: string | readonly string[];

/**
* One or more destinations where to copy.
*/
readonly dest: string | readonly string[];

/**
* Change destination file or folder name.
*/
readonly rename?: string | Function;

/**
* Modify file contents.
*/
readonly transform?: Function;
import rollup from 'rollup'
import fs from 'fs-extra'
import globby from 'globby'


export interface Target extends globby.GlobbyOptions {
/**
* Path or glob of what to copy.
*/
readonly src: string | readonly string[]

/**
* One or more destinations where to copy.
*/
readonly dest: string | readonly string[]

/**
* Change destination file or folder name.
*/
readonly rename?: string | ((fileName: string, fileExt: string) => string)
/**
* Modify file contents.
*/
readonly transform?: (
content: string | ArrayBuffer,
srcPath: string,
destPath: string
) => string | ArrayBuffer
}

interface CopyOptions extends globby.GlobbyOptions, fs.CopyOptions {
/**
* Copy items once. Useful in watch mode.
* @default false
*/
readonly copyOnce?: boolean;

/**
* Remove the directory structure of copied files.
* @default true
*/
readonly flatten?: boolean;

/**
* Rollup hook the plugin should use.
* @default 'buildEnd'
*/
readonly hook?: string;

/**
* Array of targets to copy.
* @default []
*/
readonly targets?: readonly Target[];

/**
* Output copied items to console.
* @default false
*/
readonly verbose?: boolean;

export interface CopyOptions extends globby.GlobbyOptions, fs.CopyOptions {
/**
* Copy items once. Useful in watch mode.
* @default false
*/
readonly copyOnce?: boolean

/**
* Remove the directory structure of copied files.
* @default true
*/
readonly flatten?: boolean

/**
* Rollup hook the plugin should use.
* @default 'buildEnd'
*/
readonly hook?: string

/**
* Rollup hook the `this.addWatchFile` should call, only be used in hooks
* during the build phase, and must be processed earlier than hook
* @default 'buildStart'
* @see https://rollupjs.org/guide/en/#thisaddwatchfileid-string--void
*/
readonly watchStart?: 'buildStart' | 'load' | 'resolveId' | 'transform' | string

/**
* Array of targets to copy.
* @default []
*/
readonly targets?: readonly Target[]

/**
* Output copied items to console.
* @default false
*/
readonly verbose?: boolean
}


/**
* Copy files and folders using Rollup
*/
export default function copy(options?: CopyOptions): rollup.Plugin;
export default function copy(options?: CopyOptions): rollup.Plugin
22 changes: 21 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,12 @@ copy({
targets: [{
src: 'src/index.html',
dest: 'dist/public',
transform: (contents) => contents.toString().replace('__SCRIPT__', 'app.js')
transform: (contents, srcPath, destPath) => (
contents.toString()
.replace('__SCRIPT__', 'app.js')
.replace('__SOURCE_FILE_PATH__', srcPath)
.replace('__TARGET_FILE_NAME__', path.basename(srcPath))
)
}]
})
```
Expand Down Expand Up @@ -173,6 +178,21 @@ copy({
})
```

#### watchHook

Type: `string` | Default: `buildStart`

[Rollup hook](https://rollupjs.org/guide/en/#hooks) the [this.addWatchFile](https://rollupjs.org/guide/en/#thisaddwatchfileid-string--void) should call. By default, `addWatchFile` called on each rollup.rollup build.
Only be used in hooks during the build phase, i.e. in `buildStart`, `load`, `resolveId`, and `transform`.

```js
copy({
targets: [{ src: 'assets/*', dest: 'dist/public' }],
watchHook: 'resolveId'
})
```


#### copyOnce

Type: `boolean` | Default: `false`
Expand Down
Loading