Skip to content

Instantly share code, notes, and snippets.

@monad-one
Forked from hallettj/Makefile
Created March 1, 2018 03:11
Show Gist options
  • Save monad-one/064fc6819e6410b7a13a9e6bb9fdc70b to your computer and use it in GitHub Desktop.
Save monad-one/064fc6819e6410b7a13a9e6bb9fdc70b to your computer and use it in GitHub Desktop.

Revisions

  1. @hallettj hallettj revised this gist Feb 23, 2018. 1 changed file with 5 additions and 7 deletions.
    12 changes: 5 additions & 7 deletions Makefile
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    # Makefile for transpiling with Babel in a Node app, or in a client- or
    # server-side shared library.

    .PHONY: all build clean
    .PHONY: all clean

    # Install `babel-cli` in a project to get the transpiler.
    babel := node_modules/.bin/babel
    @@ -28,14 +28,12 @@ transpiled_files := $(patsubst src/%,lib/%,$(src_files))
    # `transpiled_files` list with a `.js.flow` extension.
    flow_files := $(patsubst %.js,%.js.flow,$(transpiled_files))

    all: build

    # Ask `make` to build all of the transpiled `.js` and `.js.flow` files that we
    # want to end up with in `lib/`.
    #
    # This target also depends on the `node_modules/` directory, so that `make`
    # automatically runs `yarn install` if `package.json` has changed.
    build: node_modules $(flow_files) $(transpiled_files)
    all: node_modules $(flow_files) $(transpiled_files)

    # This rule tells `make` how to transpile a source file using `babel`.
    # Transpiled files will be written to `lib/`
    @@ -58,7 +56,7 @@ clean:
    rm -rf lib

    # This rule informs `make` that the `node_modules/` directory is out-of-date
    # after changes to `package.json`, and instructs `make` on how to install
    # modules to get back up-to-date.
    node_modules: package.json
    # after changes to `package.json` or `yarn.lock`, and instructs `make` on how to
    # install modules to get back up-to-date.
    node_modules: package.json yarn.lock
    yarn install
  2. @hallettj hallettj revised this gist Feb 23, 2017. 1 changed file with 35 additions and 24 deletions.
    59 changes: 35 additions & 24 deletions Makefile
    Original file line number Diff line number Diff line change
    @@ -6,48 +6,59 @@
    # Install `babel-cli` in a project to get the transpiler.
    babel := node_modules/.bin/babel

    # Identify modules to be transpiled by finding files ending in `.js.flow`.
    src_files := $(shell find . -name '*.js.flow' -not -path './node_modules/*')

    # Build list of expected output files by changing `.js.flow` to `.js` in
    # `src_files` list. Generated files are put in the same directory as the source
    # file. Because we generate the list in this way, we can selectively remove
    # generated files in the `clean` target. Modules that should be excluded from
    # transpiling (`.js` files with no corresponding `.js.flow` file) are excluded
    # from this list, so they are not overwritten when running `make`, and are not
    # removed when running `make clean`.
    out_files := $(patsubst %.js.flow,%.js,$(src_files))
    # Identify modules to be transpiled by recursively searching the `src/`
    # directory.
    src_files := $(shell find src/ -name '*.js')

    # Building will involve copying every `.js` file from `src/` to a corresponding
    # file in `lib/` with a `.js.flow` extension. Then we will run `babel` to
    # transpile copied files, where the transpiled file will get a `.js` extension.
    # This assignment computes the list of transpiled `.js` that we expect to end up;
    # and we will work backward from there to figure out how to build them.
    transpiled_files := $(patsubst src/%,lib/%,$(src_files))

    # Putting each generated file in the same directory with its corresponding
    # source file is important when working with Flow: during type-checking Flow
    # will look in npm packages for `.js.flow` files to find type definitions. So
    # putting `.js` and `.js.flow` files side-by-side is how you export type
    # definitions from a shared library.

    # We also want to build a list of `.js.map` output files, so that we can remove
    # them in the `clean` target.
    map_files := $(patsubst %.js.flow,%.js.map,$(src_files))
    # Compute the list of type-definition source files that we want to end up with.
    # This is done by replacing the `.js` extension from every value in the
    # `transpiled_files` list with a `.js.flow` extension.
    flow_files := $(patsubst %.js,%.js.flow,$(transpiled_files))

    all: build

    # Ask `make` to build all of the output `.js` files that we want to produce.
    # Ask `make` to build all of the transpiled `.js` and `.js.flow` files that we
    # want to end up with in `lib/`.
    #
    # This target also depends on the `node_modules/` directory, so that `make`
    # automatically runs `npm install` if `package.json` has changed.
    build: node_modules $(out_files)
    # automatically runs `yarn install` if `package.json` has changed.
    build: node_modules $(flow_files) $(transpiled_files)

    # This rule tells `make` how to transpile a source file using `babel`.
    # Transpiled files will be written to `lib/`
    lib/%: src/%
    mkdir -p $(dir $@)
    $(babel) $< --out-file $@ --source-maps

    # This rule tells `make` how to turn a `.js.flow` file into a `.js` file.
    # Transpiling one file at a time makes incremental compilation faster:
    # Transpiling one file at a time makes incremental transpilation faster:
    # `make` will only transpile source files that have changed since the last
    # invocation.
    %.js: %.js.flow
    $(babel) $< --out-file $@ --source-maps

    # When cleaning, remove files generated by the `build` target, and nothing else.
    # This rule tells `make` how to produce a `.js.flow` file. It is just a copy of
    # a source file - the rule copies a file from `src/` to `lib/` and changes the
    # extension.
    lib/%.js.flow: src/%.js
    mkdir -p $(dir $@)
    cp $< $@

    clean:
    rm -f $(out_files) $(map_files)
    rm -rf lib

    # This rule informs `make` that the `node_modules/` directory is out-of-date
    # after changes to `package.json`, and instructs `make` on how to install
    # modules to get back up-to-date.
    node_modules: package.json
    npm install
    yarn install
  3. @hallettj hallettj revised this gist Nov 3, 2016. No changes.
  4. @hallettj hallettj created this gist Nov 3, 2016.
    53 changes: 53 additions & 0 deletions Makefile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    # Makefile for transpiling with Babel in a Node app, or in a client- or
    # server-side shared library.

    .PHONY: all build clean

    # Install `babel-cli` in a project to get the transpiler.
    babel := node_modules/.bin/babel

    # Identify modules to be transpiled by finding files ending in `.js.flow`.
    src_files := $(shell find . -name '*.js.flow' -not -path './node_modules/*')

    # Build list of expected output files by changing `.js.flow` to `.js` in
    # `src_files` list. Generated files are put in the same directory as the source
    # file. Because we generate the list in this way, we can selectively remove
    # generated files in the `clean` target. Modules that should be excluded from
    # transpiling (`.js` files with no corresponding `.js.flow` file) are excluded
    # from this list, so they are not overwritten when running `make`, and are not
    # removed when running `make clean`.
    out_files := $(patsubst %.js.flow,%.js,$(src_files))

    # Putting each generated file in the same directory with its corresponding
    # source file is important when working with Flow: during type-checking Flow
    # will look in npm packages for `.js.flow` files to find type definitions. So
    # putting `.js` and `.js.flow` files side-by-side is how you export type
    # definitions from a shared library.

    # We also want to build a list of `.js.map` output files, so that we can remove
    # them in the `clean` target.
    map_files := $(patsubst %.js.flow,%.js.map,$(src_files))

    all: build

    # Ask `make` to build all of the output `.js` files that we want to produce.
    # This target also depends on the `node_modules/` directory, so that `make`
    # automatically runs `npm install` if `package.json` has changed.
    build: node_modules $(out_files)

    # This rule tells `make` how to turn a `.js.flow` file into a `.js` file.
    # Transpiling one file at a time makes incremental compilation faster:
    # `make` will only transpile source files that have changed since the last
    # invocation.
    %.js: %.js.flow
    $(babel) $< --out-file $@ --source-maps

    # When cleaning, remove files generated by the `build` target, and nothing else.
    clean:
    rm -f $(out_files) $(map_files)

    # This rule informs `make` that the `node_modules/` directory is out-of-date
    # after changes to `package.json`, and instructs `make` on how to install
    # modules to get back up-to-date.
    node_modules: package.json
    npm install