Skip to content

Commit 7e62012

Browse files
author
Paul WARD
committed
Added babel support based on the work of bazel-contrib/rules_nodejs#217
1 parent 2f15949 commit 7e62012

20 files changed

+1510
-122
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
# Bazel Toy
22

3-
For now this only serves public files using ts_devserver.
4-
5-
The workspace config was taken mostly from [here](https://github.com/bazelbuild/rules_nodejs/pull/217)
3+
Clone this repo, install docker-compose and run `dc up` from the repo root.

docker-compose.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ services:
1010
build:
1111
context: .
1212
dockerfile: ./Dockerfile
13-
ports:
14-
- 3000:3000
1513
volumes:
1614
- ./packages:/src
1715
- bazel_cache:/root/.cache/bazel/
1816
- bazel_repository_cache:/root/.cache/bazel_repository_cache/
1917
- yarn_cache:/usr/local/share/.cache/yarn/
2018
working_dir: /src
21-
command: ["/usr/local/bin/ibazel", "run", "//devserver", "--", "--port=3000"]
19+
command: ["/usr/local/bin/ibazel", "build", "//my-es6-lib"]
2220
# command: ["/bin/bash", "-c", "echo \"sleeping indefinitely\"; while true; do sleep 100; done"]

packages/.bazelrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
### ---------------------------------
77
### As of 2018 1111, the link where this file came from incorrectly states that
88
### this file should be placed into the %workspace%/tools/bazel.rc folder.
9-
### Instead the file should be placed in %workspace%/.bazel per issue #6319
10-
### https://github.com/bazelbuild/bazel/issues/6319 the file should be
9+
### Instead the file should be placed in %workspace%/.bazel per
10+
### https://github.com/bazelbuild/bazel/issues/6319
1111

1212
###############################
1313
# Directory structure #

packages/WORKSPACE

Lines changed: 11 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,15 @@
11
workspace(name = "my_workspace")
22

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

6-
http_archive(
7-
name = "bazel_skylib",
8-
url = "https://github.com/bazelbuild/bazel-skylib/archive/0.3.1.zip",
9-
strip_prefix = "bazel-skylib-0.3.1",
10-
sha256 = "95518adafc9a2b656667bbf517a952e54ce7f350779d0dd95133db4eb5c27fb1",
11-
)
12-
135
git_repository(
146
name = "build_bazel_rules_nodejs",
157
remote = "https://github.com/bazelbuild/rules_nodejs.git",
168
tag = "0.15.1",
179
)
1810

19-
http_archive(
20-
name = "io_bazel_rules_webtesting",
21-
url = "https://github.com/bazelbuild/rules_webtesting/archive/0.2.1.zip",
22-
strip_prefix = "rules_webtesting-0.2.1",
23-
sha256 = "7d490aadff9b5262e5251fa69427ab2ffd1548422467cb9f9e1d110e2c36f0fa",
24-
)
25-
26-
http_archive(
27-
name = "io_bazel_rules_go",
28-
url = "https://github.com/bazelbuild/rules_go/releases/download/0.14.0/rules_go-0.14.0.tar.gz",
29-
sha256 = "5756a4ad75b3703eb68249d50e23f5d64eaf1593e886b9aa931aa6e938c4e301",
30-
)
31-
32-
http_archive(
33-
name = "bazel_gazelle",
34-
urls = ["https://github.com/bazelbuild/bazel-gazelle/releases/download/0.14.0/bazel-gazelle-0.14.0.tar.gz"],
35-
sha256 = "c0a5739d12c6d05b6c1ad56f2200cb0b57c5a70e03ebd2f7b87ce88cabf09c7b",
36-
)
37-
38-
http_archive(
39-
name = "build_bazel_rules_typescript",
40-
url = "https://github.com/bazelbuild/rules_typescript/archive/0.20.3.zip",
41-
strip_prefix = "rules_typescript-0.20.3",
42-
sha256 = "2a03b23c30c5109ab0863cfa60acce73ceb56337d41efc2dd67f8455a1c1d5f3",
43-
)
11+
load("@build_bazel_rules_nodejs//:package.bzl", "rules_nodejs_dependencies")
12+
rules_nodejs_dependencies()
4413

4514
load("@build_bazel_rules_nodejs//:defs.bzl", "node_repositories")
4615
node_repositories(
@@ -56,31 +25,15 @@ node_repositories(
5625
},
5726
node_urls = ["https://nodejs.org/dist/v{version}/{filename}"],
5827
yarn_urls = ["https://github.com/yarnpkg/yarn/releases/download/v{version}/{filename}"],
59-
package_json = ["//:package.json"]
60-
)
61-
62-
load("@build_bazel_rules_nodejs//:defs.bzl", "yarn_install")
63-
yarn_install(
64-
name = "npm",
65-
package_json = "//:package.json",
66-
yarn_lock = "//:yarn.lock",
67-
)
68-
69-
load("@io_bazel_rules_go//go:def.bzl", "go_rules_dependencies", "go_register_toolchains")
70-
go_rules_dependencies()
71-
go_register_toolchains()
72-
73-
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
74-
gazelle_dependencies()
75-
76-
load("@io_bazel_rules_webtesting//web:repositories.bzl", "browser_repositories", "web_test_repositories")
77-
web_test_repositories()
78-
browser_repositories(
79-
chromium = True,
28+
package_json = [
29+
"//:package.json",
30+
"//internal/babel_library:package.json",
31+
"//my-es6-lib:package.json",
32+
]
8033
)
8134

82-
load("@build_bazel_rules_typescript//:defs.bzl", "ts_setup_workspace")
83-
ts_setup_workspace()
35+
load("//internal/babel_library:setup_workspace.bzl", "babel_library_setup_workspace")
36+
babel_library_setup_workspace()
8437

85-
# load("//:setup_workspace.bzl", "devserver_example_setup_workspace")
86-
# devserver_example_setup_workspace()
38+
load("//my-es6-lib:setup_workspace.bzl", "my_es6_lib_setup_workspace")
39+
my_es6_lib_setup_workspace()

packages/devserver/BUILD.bazel

Lines changed: 0 additions & 36 deletions
This file was deleted.

packages/devserver/public/index.html

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports_files(["babel.rc.js"])
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## Example
2+
3+
```js
4+
// test.js
5+
const el = document.createElement('div');
6+
el.innerText = 'Hello, JS';
7+
el.className = 'js1';
8+
document.body.appendChild(el);
9+
```
10+
11+
```py
12+
# BUILD.bazel
13+
load("@build_bazel_rules_nodejs//:defs.bzl", "babel_library")
14+
15+
js_library(
16+
name = "src",
17+
srcs = ["test.js"],
18+
)
19+
20+
load("@build_bazel_rules_typescript//:defs.bzl", "ts_devserver")
21+
22+
ts_devserver(
23+
name = "devserver",
24+
entry_module = "path/to/test",
25+
serving_path = "/bundle.min.js",
26+
deps = [":src"],
27+
)
28+
```
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// A simple wrapper around babel-core due to https://github.com/babel/babel/issues/8193
2+
3+
const babel = require("@babel/core");
4+
const fs = require("fs");
5+
const path = require("path");
6+
const program = require("commander");
7+
const { promisify } = require("util");
8+
9+
const mkdir = promisify(fs.mkdir);
10+
const exists = promisify(fs.exists);
11+
const writeFile = promisify(fs.writeFile);
12+
const transformFile = promisify(babel.transformFile.bind(babel));
13+
14+
const mkdirp = async p => {
15+
if (!(await exists(p))) {
16+
await mkdirp(path.dirname(p));
17+
await mkdir(p);
18+
}
19+
};
20+
21+
program
22+
.version("1.0.0")
23+
.usage("[options] <files...>")
24+
.option("-o, --out-dir <value>", "Output directory for created files")
25+
.option("-c, --config-file <value>", "babel.rc.js config file")
26+
.parse(process.argv);
27+
28+
const outDir = program["outDir"] || "";
29+
30+
let babelConfig = {};
31+
if (program["configFile"]) {
32+
// Note: We do not want to use bazel resolve mechanisms, so use path resolve to get the absolute
33+
// path and load that.
34+
babelConfig = require(path.resolve(program["configFile"]));
35+
}
36+
37+
const promises = [];
38+
for (let i = 0; i < program.args.length; i += 2) {
39+
const input = program.args[i];
40+
const output = path.join(outDir, program.args[i + 1]);
41+
42+
promises.push(
43+
(async () => {
44+
let op
45+
try {
46+
op = `transforming file ${input}`;
47+
const result = await transformFile(input, babelConfig);
48+
49+
const outputDir = path.dirname(output);
50+
op = `creating folder ${outputDir}`;
51+
await mkdirp(outputDir);
52+
53+
op = `writing file ${output}`;
54+
await writeFile(output, result.code);
55+
} catch (e) {
56+
console.error("Problem", op, e.stack);
57+
}
58+
})()
59+
);
60+
}
61+
62+
Promise.all(promises)
63+
.then(() => process.exit(0))
64+
.catch(() => process.exit(-1));
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var env_preset = require('@babel/preset-env')
2+
3+
const babelConfig = {
4+
moduleIds: true,
5+
getModuleId(name) {
6+
const moduleName = 'TMPL_module_name';
7+
if (moduleName) {
8+
return moduleName;
9+
}
10+
11+
const path = require('path');
12+
const process = require('process');
13+
const binDirPath = 'TMPL_bin_dir_path';
14+
// TODO: Add workspace name to the id
15+
moduleId = path.relative(process.cwd(), name);
16+
if (moduleId.startsWith(binDirPath)) {
17+
// For generated files we take out the bin dir path
18+
return moduleId.slice(binDirPath.length + 1);
19+
} else {
20+
return moduleId
21+
}
22+
},
23+
presets: [[env_preset, {modules: 'umd'}]],
24+
};
25+
26+
module.exports = babelConfig;

0 commit comments

Comments
 (0)