-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added babel support based on the work of bazel-contrib/rules_nodejs#217
- Loading branch information
Paul WARD
committed
Nov 15, 2018
1 parent
2f15949
commit 7e62012
Showing
20 changed files
with
1,510 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
exports_files(["babel.rc.js"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.