Skip to content
akameco edited this page Nov 8, 2017 · 2 revisions

Configの設定

s2s.config.jsをプロジェクトのルートに配置します。 基本的に必要となる項目は、watchpluginstemplatesの3つです。

module.exports = {
  watch: './**/*.js', // string
  plugins: [],
  templates: [],
}

watch

対象となるファイルをGlobパターンで指定します。

plugins

オブジェクトの配列を指定します。 必須項目は、testpluginです。 testは、対象とするファイルを正規表現で指定します。 plugin.babelrcの指定方法と同様にプラグイン名、オプションが必要な場合は、配列で渡すことが可能です。 handlerについては、後述します。

type EventType = 'add' | 'change' | 'unlink'
type Only = EventType[]
type PluginOpts = PluginName | [PluginName, Object]

export type Plugin = {|
  test: RegExp,
  plugin: PluginOpts,
  handler?: HanlderFunc,
  only?: Only,
  input?: string,
  output?: string,
|}

templates

テンプレートからファイルをコピーします。 必要項目は、testinputです。 testは、pluginsと同様にファイルパスを正規表現で指定します。 inputはtemplatesディレクトリにあるファイルを指定します。

type Template = {|
  test: RegExp,
  input: Path,
  output?: Path,
|}

handler

ハンドラーとは何かというと、プラグインを処理するパーサを指定することが出来ます。 webpackのloaderを思い浮かべていただくとわかりやすいでしょう。 デフォルトでは、Babelで処理しますが、代わりにtypescriptを使うことが出来ます。
詳しくは、Exampleを参照してください。

Example typescript

handlerはCodeを受け取ってCodeを返すHandlerFunc型を取ります。

type Code = string
type Path = string

type HandlerOpts = {
  eventPath: Path,
  filename: Path,
  plugin: Plugin,
}

type HanlderFunc = (code: Code, opts: HandlerOpts) => Code
Clone this wiki locally