Skip to content

Commit

Permalink
Added babel support based on the work of bazel-contrib/rules_nodejs#217
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul WARD committed Nov 15, 2018
1 parent 2f15949 commit 7e62012
Show file tree
Hide file tree
Showing 20 changed files with 1,510 additions and 122 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Bazel Toy

For now this only serves public files using ts_devserver.

The workspace config was taken mostly from [here](https://github.com/bazelbuild/rules_nodejs/pull/217)
Clone this repo, install docker-compose and run `dc up` from the repo root.
4 changes: 1 addition & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ services:
build:
context: .
dockerfile: ./Dockerfile
ports:
- 3000:3000
volumes:
- ./packages:/src
- bazel_cache:/root/.cache/bazel/
- bazel_repository_cache:/root/.cache/bazel_repository_cache/
- yarn_cache:/usr/local/share/.cache/yarn/
working_dir: /src
command: ["/usr/local/bin/ibazel", "run", "//devserver", "--", "--port=3000"]
command: ["/usr/local/bin/ibazel", "build", "//my-es6-lib"]
# command: ["/bin/bash", "-c", "echo \"sleeping indefinitely\"; while true; do sleep 100; done"]
4 changes: 2 additions & 2 deletions packages/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
### ---------------------------------
### As of 2018 1111, the link where this file came from incorrectly states that
### this file should be placed into the %workspace%/tools/bazel.rc folder.
### Instead the file should be placed in %workspace%/.bazel per issue #6319
### https://github.com/bazelbuild/bazel/issues/6319 the file should be
### Instead the file should be placed in %workspace%/.bazel per
### https://github.com/bazelbuild/bazel/issues/6319

###############################
# Directory structure #
Expand Down
69 changes: 11 additions & 58 deletions packages/WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,46 +1,15 @@
workspace(name = "my_workspace")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

http_archive(
name = "bazel_skylib",
url = "https://github.com/bazelbuild/bazel-skylib/archive/0.3.1.zip",
strip_prefix = "bazel-skylib-0.3.1",
sha256 = "95518adafc9a2b656667bbf517a952e54ce7f350779d0dd95133db4eb5c27fb1",
)

git_repository(
name = "build_bazel_rules_nodejs",
remote = "https://github.com/bazelbuild/rules_nodejs.git",
tag = "0.15.1",
)

http_archive(
name = "io_bazel_rules_webtesting",
url = "https://github.com/bazelbuild/rules_webtesting/archive/0.2.1.zip",
strip_prefix = "rules_webtesting-0.2.1",
sha256 = "7d490aadff9b5262e5251fa69427ab2ffd1548422467cb9f9e1d110e2c36f0fa",
)

http_archive(
name = "io_bazel_rules_go",
url = "https://github.com/bazelbuild/rules_go/releases/download/0.14.0/rules_go-0.14.0.tar.gz",
sha256 = "5756a4ad75b3703eb68249d50e23f5d64eaf1593e886b9aa931aa6e938c4e301",
)

http_archive(
name = "bazel_gazelle",
urls = ["https://github.com/bazelbuild/bazel-gazelle/releases/download/0.14.0/bazel-gazelle-0.14.0.tar.gz"],
sha256 = "c0a5739d12c6d05b6c1ad56f2200cb0b57c5a70e03ebd2f7b87ce88cabf09c7b",
)

http_archive(
name = "build_bazel_rules_typescript",
url = "https://github.com/bazelbuild/rules_typescript/archive/0.20.3.zip",
strip_prefix = "rules_typescript-0.20.3",
sha256 = "2a03b23c30c5109ab0863cfa60acce73ceb56337d41efc2dd67f8455a1c1d5f3",
)
load("@build_bazel_rules_nodejs//:package.bzl", "rules_nodejs_dependencies")
rules_nodejs_dependencies()

load("@build_bazel_rules_nodejs//:defs.bzl", "node_repositories")
node_repositories(
Expand All @@ -56,31 +25,15 @@ node_repositories(
},
node_urls = ["https://nodejs.org/dist/v{version}/{filename}"],
yarn_urls = ["https://github.com/yarnpkg/yarn/releases/download/v{version}/{filename}"],
package_json = ["//:package.json"]
)

load("@build_bazel_rules_nodejs//:defs.bzl", "yarn_install")
yarn_install(
name = "npm",
package_json = "//:package.json",
yarn_lock = "//:yarn.lock",
)

load("@io_bazel_rules_go//go:def.bzl", "go_rules_dependencies", "go_register_toolchains")
go_rules_dependencies()
go_register_toolchains()

load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
gazelle_dependencies()

load("@io_bazel_rules_webtesting//web:repositories.bzl", "browser_repositories", "web_test_repositories")
web_test_repositories()
browser_repositories(
chromium = True,
package_json = [
"//:package.json",
"//internal/babel_library:package.json",
"//my-es6-lib:package.json",
]
)

load("@build_bazel_rules_typescript//:defs.bzl", "ts_setup_workspace")
ts_setup_workspace()
load("//internal/babel_library:setup_workspace.bzl", "babel_library_setup_workspace")
babel_library_setup_workspace()

# load("//:setup_workspace.bzl", "devserver_example_setup_workspace")
# devserver_example_setup_workspace()
load("//my-es6-lib:setup_workspace.bzl", "my_es6_lib_setup_workspace")
my_es6_lib_setup_workspace()
36 changes: 0 additions & 36 deletions packages/devserver/BUILD.bazel

This file was deleted.

17 changes: 0 additions & 17 deletions packages/devserver/public/index.html

This file was deleted.

1 change: 1 addition & 0 deletions packages/internal/babel_library/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports_files(["babel.rc.js"])
28 changes: 28 additions & 0 deletions packages/internal/babel_library/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
## Example

```js
// test.js
const el = document.createElement('div');
el.innerText = 'Hello, JS';
el.className = 'js1';
document.body.appendChild(el);
```

```py
# BUILD.bazel
load("@build_bazel_rules_nodejs//:defs.bzl", "babel_library")

js_library(
name = "src",
srcs = ["test.js"],
)

load("@build_bazel_rules_typescript//:defs.bzl", "ts_devserver")

ts_devserver(
name = "devserver",
entry_module = "path/to/test",
serving_path = "/bundle.min.js",
deps = [":src"],
)
```
64 changes: 64 additions & 0 deletions packages/internal/babel_library/babel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// A simple wrapper around babel-core due to https://github.com/babel/babel/issues/8193

const babel = require("@babel/core");
const fs = require("fs");
const path = require("path");
const program = require("commander");
const { promisify } = require("util");

const mkdir = promisify(fs.mkdir);
const exists = promisify(fs.exists);
const writeFile = promisify(fs.writeFile);
const transformFile = promisify(babel.transformFile.bind(babel));

const mkdirp = async p => {
if (!(await exists(p))) {
await mkdirp(path.dirname(p));
await mkdir(p);
}
};

program
.version("1.0.0")
.usage("[options] <files...>")
.option("-o, --out-dir <value>", "Output directory for created files")
.option("-c, --config-file <value>", "babel.rc.js config file")
.parse(process.argv);

const outDir = program["outDir"] || "";

let babelConfig = {};
if (program["configFile"]) {
// Note: We do not want to use bazel resolve mechanisms, so use path resolve to get the absolute
// path and load that.
babelConfig = require(path.resolve(program["configFile"]));
}

const promises = [];
for (let i = 0; i < program.args.length; i += 2) {
const input = program.args[i];
const output = path.join(outDir, program.args[i + 1]);

promises.push(
(async () => {
let op
try {
op = `transforming file ${input}`;
const result = await transformFile(input, babelConfig);

const outputDir = path.dirname(output);
op = `creating folder ${outputDir}`;
await mkdirp(outputDir);

op = `writing file ${output}`;
await writeFile(output, result.code);
} catch (e) {
console.error("Problem", op, e.stack);
}
})()
);
}

Promise.all(promises)
.then(() => process.exit(0))
.catch(() => process.exit(-1));
26 changes: 26 additions & 0 deletions packages/internal/babel_library/babel.rc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var env_preset = require('@babel/preset-env')

const babelConfig = {
moduleIds: true,
getModuleId(name) {
const moduleName = 'TMPL_module_name';
if (moduleName) {
return moduleName;
}

const path = require('path');
const process = require('process');
const binDirPath = 'TMPL_bin_dir_path';
// TODO: Add workspace name to the id
moduleId = path.relative(process.cwd(), name);
if (moduleId.startsWith(binDirPath)) {
// For generated files we take out the bin dir path
return moduleId.slice(binDirPath.length + 1);
} else {
return moduleId
}
},
presets: [[env_preset, {modules: 'umd'}]],
};

module.exports = babelConfig;
Loading

0 comments on commit 7e62012

Please sign in to comment.