Keen Yee Liau 012a2b55e1 build(language-service): use 'export =' syntax for default export (#41165)
Tsserver expects `@angular/language-service` to provide a factory function
as the default export (commonjs-style) of the package.

The current implementation side steps TypeScript's import syntax by using
`module.exports = factory`.
This allows the code to incorrectly re-export other symbols:

```ts
export * from './api';
```

which transpiles to:

```js
var tslib_1 = require("tslib");
tslib_1.__exportStar(require("@angular/language-service/api"), exports);
```

Doing this meant that the package now has a runtime dependency on `tslib`,
which is totally unnecessary.

With the proper `export =` syntax, `tslib` is removed, and no other exports
are allowed.

Output:
```js
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define("@angular/language-service", ["require", "exports"], factory);
    }
})(function (require, exports) {
    "use strict";
    return function factory(tsModule) {
        var plugin;
        return {
            create: function (info) {
                var config = info.config;
                var bundleName = config.ivy ? 'ivy.js' : 'language-service.js';
                plugin = require("./bundles/" + bundleName)(tsModule);
                return plugin.create(info);
            },
            getExternalFiles: function (project) {
                var _a, _b;
                return (_b = (_a = plugin === null || plugin === void 0 ? void 0 : plugin.getExternalFiles) === null || _a === void 0 ? void 0 : _a.call(plugin, project)) !== null && _b !== void 0 ? _b : [];
            },
            onConfigurationChanged: function (config) {
                var _a;
                (_a = plugin === null || plugin === void 0 ? void 0 : plugin.onConfigurationChanged) === null || _a === void 0 ? void 0 : _a.call(plugin, config);
            },
        };
    };
});
```

PR Close #41165
2021-03-11 14:48:33 -08:00

66 lines
1.3 KiB
Python

load("//tools:defaults.bzl", "pkg_npm", "ts_library")
package(default_visibility = ["//visibility:public"])
ts_library(
name = "api",
srcs = [
"api.ts",
"index.ts",
],
prodmode_module = "commonjs",
deps = [
"@npm//@types/node",
"@npm//typescript",
],
)
ts_library(
name = "language-service",
srcs = glob(
[
"src/**/*.ts",
],
exclude = [
"src/ts_utils.ts",
],
),
deps = [
":api",
":ts_utils",
"//packages:types",
"//packages/compiler",
"//packages/compiler-cli",
"//packages/core",
"@npm//@types/node",
"@npm//typescript",
],
)
ts_library(
name = "ts_utils",
srcs = ["src/ts_utils.ts"],
deps = [
"@npm//typescript",
],
)
pkg_npm(
name = "npm_package",
srcs = ["package.json"],
tags = [
"release-with-framework",
],
# Do not add more to this list.
# Dependencies on the full npm_package cause long re-builds.
visibility = [
"//integration:__pkg__",
],
deps = [
":api",
# min bundle is not used at the moment; omit from package to speed up build
"//packages/language-service/bundles:language-service.js",
"//packages/language-service/bundles:ivy.js",
],
)