Skip to content

Commit

Permalink
fix: handle nodejs builtins (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattem authored Apr 27, 2020
1 parent d0d97f5 commit 183baf5
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 3 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Changelog

#### 0.0.0-PLACEHOLDER

* **feat**: Support generating `nodejs_binary` target. The `data` attr will be set to `entry_points` generating rule [#36](https://github.com/Evertz/bzlgen/pull/36)
* **feat**: Use the buildozer API via the NodeJS bindings rather than invoking directly in a shell on a text file [#35](https://github.com/Evertz/bzlgen/pull/35)
* **fix**: Handle NodeJS builtins, adding `@types/node` as a dep
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@
"dependencies": {
"@bazel/buildozer": "2.2.1",
"@phenomnomnominal/tsquery": "3.0.0",
"builtins": "3.0.1",
"gonzales-pe": "4.2.4",
"lodash.kebabcase": "4.1.1",
"minimatch": "3.0.4",
"patch-package": "6.2.2",
"shelljs": "0.8.3",
"signale": "1.4.0",
"typescript": "3.8.3",
"tsconfig-paths": "3.9.0",
"typescript": "3.8.3",
"yargs": "14.2.0"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions src/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ RUNTIME_NPM_DEPS = [
"@npm//typescript",
"@npm//minimatch",
"@npm//tsconfig-paths",
"@npm//builtins",
]

ts_library(
Expand Down
11 changes: 10 additions & 1 deletion src/generators/ts/ts.generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import {
sys
} from 'typescript';
import { createMatchPath, MatchPath } from 'tsconfig-paths';
import * as Builtins from 'builtins';

import { GeneratorType } from '../../flags';
import { Label } from '../../label';
import { fatal, log } from '../../logger';
import { log } from '../../logger';
import { Workspace } from '../../workspace';
import { BuildFileGenerator } from '../generator';

Expand All @@ -23,10 +24,12 @@ const EXPORTS_QUERY = `ExportDeclaration:has(StringLiteral)`;

export class TsGenerator extends BuildFileGenerator {
protected readonly tsPathsMatcher: MatchPath;
protected readonly builtins: string[] = [];

constructor(workspace: Workspace) {
super(workspace);

this.builtins = Builtins();
this.tsPathsMatcher = this.createPathMatcherForTsPaths();
}

Expand Down Expand Up @@ -114,6 +117,12 @@ export class TsGenerator extends BuildFileGenerator {
let label = this.workspace.tryResolveLabelFromStaticMapping(imp, undefined, '.');
if (label) { return label; }

// if the module is a builtin, return @types/node
// if the import had been overridden it should have happened above
if (this.builtins.includes(imp)) {
return Label.parseAbsolute(`@${npmWorkspace}//@types/node`);
}

const relative = this.workspace.isWorkspaceRelative(imp) || imp.startsWith('.');

if (relative) {
Expand Down
22 changes: 21 additions & 1 deletion test/generators/ts.generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { TsGenerator } from '../../src/generators/ts/ts.generator';
import { Workspace } from '../../src/workspace';
import { TsGeneratorFlags } from '../../src/generators/ts/ts.generator.flags';

describe('ng generator', () => {
describe('ts generator', () => {
const TS_ONE =
`import { component } from '@angular/core';
import { Foo } from '../other/foo';
Expand Down Expand Up @@ -116,6 +116,26 @@ export class Some {}
new ts_library some|//src/some:__pkg__
add srcs one.ts|//src/some:some
add deps @npm//package:package @npm//@scope/package:package|//src/some:some
set tsconfig "//:tsconfig"|//src/some:some`;

expect(commands.join('\n')).toEqual(expected);
});

it('converts builtin imports for node types', async () => {
mockfs({
'/home/workspace/src/some': {
'one.ts': `import * as fs from 'fs';`
},
},);

await gen.generate();

const commands = workspace.getBuildozer().toCommands();

const expected =`new_load @npm_bazel_typescript//:index.bzl ts_library|//src/some:__pkg__
new ts_library some|//src/some:__pkg__
add srcs one.ts|//src/some:some
add deps @npm//@types/node:node|//src/some:some
set tsconfig "//:tsconfig"|//src/some:some`;

expect(commands.join('\n')).toEqual(expected);
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,13 @@ buffer-from@^1.0.0:
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef"
integrity sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==

builtins@3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/builtins/-/builtins-3.0.1.tgz#38c55022f40a752f61de255eabd41892278032ea"
integrity sha512-JxlnRUmQpQReAt5mUshMNQpHe7MEZuy1qH+Eck31mc4GBBM9AmGX9oBrzQH/zAOIRM/CAqNA0J2692a/yJiGQQ==
dependencies:
semver "^7.0.0"

cache-base@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/cache-base/-/cache-base-1.0.1.tgz#0a7f46416831c8b662ee36fe4e7c59d76f666ab2"
Expand Down Expand Up @@ -1470,6 +1477,11 @@ semver@5.6.0:
resolved "https://registry.yarnpkg.com/semver/-/semver-5.6.0.tgz#7e74256fbaa49c75aa7c7a205cc22799cac80004"
integrity sha512-RS9R6R35NYgQn++fkDWaOmqGoj4Ek9gGs+DPxNUZKuwE183xjJroKvyo1IzVFeXvUrvmALy6FWD5xrdJT25gMg==

semver@^7.0.0:
version "7.3.2"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.2.tgz#604962b052b81ed0786aae84389ffba70ffd3938"
integrity sha512-OrOb32TeeambH6UrhtShmF7CRDqhL6/5XpPNp2DuRH6+9QLw/orhp72j87v8Qa1ScDkvrrBNpZcDejAirJmfXQ==

set-blocking@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/set-blocking/-/set-blocking-2.0.0.tgz#045f9782d011ae9a6803ddd382b24392b3d890f7"
Expand Down

0 comments on commit 183baf5

Please sign in to comment.