Bazel + Angular Prod-Server não funciona ("Uncaught SyntaxError: Unexpected token '<'")

Flo

Usei este aplicativo de exemplo do rules_nodejsrepositório como um modelo para um aplicativo Angular que pode ser construído / servido com o Bazel .

Iniciar o ts_devserver( arquivo BUILD ) funciona perfeitamente bem. Mas servir o history_server( arquivo BUILD ) tem um problema. Começa:

INFO: Build completed successfully, 1 total action
history-server serving all apps in /home/flolu/.cache/bazel/_bazel_flolu/2d92189468588702b7bac6d93cc5343a/execroot/cents_ideas/bazel-out/k8-fastbuild/bin/services/client/src/prodapp
  /_prodapp/src/example => ../../prodapp/_prodapp/src/example
  /                     => ../../prodapp
history-server listening on port 8080; Ctrl+C to stop

Questão

Mas quando abro http: // localhost: 8080, vejo uma página em branco e há um erro no console:

Uncaught SyntaxError: Unexpected token '<'

erro de console

Estou relativamente certo de que o erro deve estar em alguma configuração errada neste arquivo BUILD (porque servir o prodapp funciona bem no aplicativo de exemplo de rules_nodejs):

load("@build_bazel_rules_nodejs//:index.bzl", "pkg_web")
load("@io_bazel_rules_docker//container:container.bzl", "container_image")
load("@io_bazel_rules_docker//nodejs:image.bzl", "nodejs_image")
load("@npm//@babel/cli:index.bzl", "babel")
load("@npm//history-server:index.bzl", "history_server")
load("@npm//html-insert-assets:index.bzl", "html_insert_assets")
load("@npm_angular_bazel//:index.bzl", "ng_module")
load("@npm_bazel_rollup//:index.bzl", "rollup_bundle")
load("@npm_bazel_terser//:index.bzl", "terser_minified")
load("@npm_bazel_typescript//:index.bzl", "ts_config", "ts_devserver", "ts_library")

package(default_visibility = ["//:__subpackages__"])

exports_files([
    "tsconfig.json",
    "tsconfig.spec.json",
])

ts_library(
    name = "initialize_testbed",
    testonly = 1,
    srcs = [
        "initialize_testbed.ts",
    ],
    deps = [
        "@npm//@angular/core",
        "@npm//@angular/platform-browser-dynamic",
        "@npm//@types",
    ],
)

ng_module(
    name = "src",
    srcs = [
        "main.dev.ts",
        "main.prod.ts",
    ],
    tsconfig = "//services/client:tsconfig.json",
    deps = [
        "//services/client/src/app",

        "@npm//@angular/core",
        "@npm//@angular/platform-browser",
        "@npm//@angular/router",
        #"@npm//@ngrx/store",
    ],
)

filegroup(
    name = "rxjs_umd_modules",
    srcs = [
        ":rxjs_shims.js",
        "@npm//:node_modules/rxjs/bundles/rxjs.umd.js",
    ],
)

_ASSETS = [
    ":styles.css",
    "@npm//:node_modules/zone.js/dist/zone.min.js",
]

html_insert_assets(
    name = "inject_scripts_for_dev",
    outs = ["index.html"],
    args = [
        "--html=$(execpath //services/client/src:example/index.html)",
        "[email protected]",
        "--roots=. $(RULEDIR)",
        "--assets",
    ] + ["$(execpath %s)" % s for s in _ASSETS] + [
        "./_/ts_scripts.js",
    ],
    data = ["//services/client/src:example/index.html"] + _ASSETS,
)

ts_devserver(
    name = "devserver",
    additional_root_paths = ["src/example"],
    entry_module = "cents_ideas/services/client/src/main.dev",
    scripts = [
        "@npm//:node_modules/tslib/tslib.js",
        ":rxjs_umd_modules",
        "@npm//date-fns:date-fns.umd.js",
    ],
    static_files = _ASSETS + [
        ":inject_scripts_for_dev",
        "//services/client/src/assets",
    ],
    deps = ["//services/client/src"],
)

rollup_bundle(
    name = "bundle-es2015",
    config_file = "rollup.config.js",
    entry_points = {
        ":main.prod.ts": "index",
    },
    output_dir = True,
    deps = [
        "//services/client/src",
        "@npm//rollup-plugin-commonjs",
        "@npm//rollup-plugin-node-resolve",
    ],
)

babel(
    name = "bundle-es5",
    args = [
        "$(execpath :bundle-es2015)",
        "--no-babelrc",
        "--source-maps",
        "[email protected]/preset-env",
        "--out-dir",
        "$(@D)",
    ],
    data = [
        ":bundle-es2015",
        "@npm//@babel/preset-env",
    ],
    output_dir = True,
)

terser_minified(
    name = "bundle-es2015.min",
    src = ":bundle-es2015",
)

terser_minified(
    name = "bundle-es5.min",
    src = ":bundle-es5",
)

html_insert_assets(
    name = "inject_scripts_for_prod",
    outs = ["_prodapp/src/example/index.html"],
    args = [
        "--html=$(execpath //services/client/src:example/index.prod.html)",
        "[email protected]",
        "--roots=. $(RULEDIR)",
        "--assets",
    ] + ["$(execpath %s)" % s for s in _ASSETS],
    data = ["//services/client/src:example/index.prod.html"] + _ASSETS,
)

pkg_web(
    name = "prodapp",
    srcs = _ASSETS + [
        ":bundle-es2015.min",
        ":bundle-es5.min",
        ":inject_scripts_for_prod",
        "//services/client/src/assets",
        "@npm//:node_modules/systemjs/dist/system.js",
        "@npm//:node_modules/core-js/client/core.min.js",
        "index.html",
    ],
    additional_root_paths = [
        "npm/node_modules/core-js/client",
        "npm/node_modules/systemjs/dist",
    ],
)

history_server(
    name = "prodserver",
    data = [":prodapp"],
    templated_args = [
        "-a",
        "$(rlocation cents_ideas/services/client/src/prodapp)",
    ],
)

nodejs_image(
    name = "nodejs_image",
    data = [":prodapp"],
    entry_point = "@npm//:node_modules/history-server/modules/cli.js",
    node_modules = "@npm//:node_modules",
    tags = ["local"],
    templated_args = ["services/client/src/prodapp"],
)

container_image(
    name = "image",
    base = ":nodejs_image",
    tags = ["local"],
    workdir = "/app/src/nodejs_image.binary.runfiles/cents_ideas/services/client",
)

informação adicional

Aqui está a árvore de arquivos de dentro do contêiner do docker do prodapp (dentro /app/services/client/src/nodejs_image.binary.runfiles/cents_ideas)

  |-services
  |  |-client
  |  |  |-src
  |  |  |  |-prodapp
  |  |  |  |  |-npm
  |  |  |  |  |  |-node_modules
  |  |  |  |  |  |  |-zone.js
  |  |  |  |  |  |  |  |-dist
  |  |  |  |  |-_prodapp
  |  |  |  |  |  |-src
  |  |  |  |  |  |  |-example
  |  |  |  |  |-bundle-es2015.min
  |  |  |  |  |-bundle-es5.min

Reproduza o erro

Você pode reproduzir o erro seguindo estas etapas:

  1. clone este repositório: https://github.com/flolu/cents-ideas/tree/473381537a30d71e9d76d7a37c555908ab8db970
  2. instale dependências npm executando yarnounpm install
  3. execute o prod-server via yarn client:serve-prod
  4. abra o navegador em http: // localhost: 8080
Flo

Aqui está o arquivo BUILD de trabalho. Os créditos vão para Ray com esta solicitação de pull.

load("@build_bazel_rules_nodejs//:index.bzl", "pkg_web")
load("@io_bazel_rules_docker//container:container.bzl", "container_image")
load("@io_bazel_rules_docker//nodejs:image.bzl", "nodejs_image")
load("@npm//@babel/cli:index.bzl", "babel")
load("@npm//history-server:index.bzl", "history_server")
load("@npm//html-insert-assets:index.bzl", "html_insert_assets")
load("@npm_angular_bazel//:index.bzl", "ng_module")
load("@npm_bazel_rollup//:index.bzl", "rollup_bundle")
load("@npm_bazel_terser//:index.bzl", "terser_minified")
load("@npm_bazel_typescript//:index.bzl", "ts_config", "ts_devserver", "ts_library")

package(default_visibility = ["//:__subpackages__"])

exports_files([
    "tsconfig.json",
    "tsconfig.spec.json",
])

ts_library(
    name = "initialize_testbed",
    testonly = 1,
    srcs = [
        "initialize_testbed.ts",
    ],
    deps = [
        "@npm//@angular/core",
        "@npm//@angular/platform-browser-dynamic",
        "@npm//@types",
    ],
)

ng_module(
    name = "src",
    srcs = [
        "main.dev.ts",
        "main.prod.ts",
    ],
    tsconfig = "//services/client:tsconfig.json",
    deps = [
        "//services/client/src/app",

        "@npm//@angular/core",
        "@npm//@angular/platform-browser",
        "@npm//@angular/router",
        #"@npm//@ngrx/store",
    ],
)

filegroup(
    name = "rxjs_umd_modules",
    srcs = [
        ":rxjs_shims.js",
        "@npm//:node_modules/rxjs/bundles/rxjs.umd.js",
    ],
)

_ASSETS = [
    "@npm//:node_modules/zone.js/dist/zone.min.js",
]

html_insert_assets(
    name = "inject_scripts_for_dev",
    outs = ["index.html"],
    args = [
        "--html=$(execpath //services/client/src:_index.html)",
        "[email protected]",
        "--roots=. $(RULEDIR)",
        "--assets",
    ] + ["$(execpath %s)" % s for s in _ASSETS] + [
        "./_/ts_scripts.js",
    ],
    data = [":_index.html"] + _ASSETS,
)

ts_devserver(
    name = "devserver",
    additional_root_paths = ["src/example"],
    entry_module = "cents_ideas/services/client/src/main.dev",
    scripts = [
        "@npm//:node_modules/tslib/tslib.js",
        ":rxjs_umd_modules",
        "@npm//date-fns:date-fns.umd.js",
    ],
    static_files = _ASSETS + [
        ":inject_scripts_for_dev",
        ":styles.css",
        "//services/client/src/assets",
    ],
    deps = ["//services/client/src"],
)

rollup_bundle(
    name = "bundle-es2015",
    config_file = "rollup.config.js",
    entry_points = {
        ":main.prod.ts": "index",
    },
    output_dir = True,
    deps = [
        "//services/client/src",
        "@npm//rollup-plugin-commonjs",
        "@npm//rollup-plugin-node-resolve",
    ],
)

babel(
    name = "bundle-es5",
    args = [
        "$(execpath :bundle-es2015)",
        "--no-babelrc",
        "--source-maps",
        "[email protected]/preset-env",
        "--out-dir",
        "$(@D)",
    ],
    data = [
        ":bundle-es2015",
        "@npm//@babel/preset-env",
    ],
    output_dir = True,
)

terser_minified(
    name = "bundle-es2015.min",
    src = ":bundle-es2015",
)

terser_minified(
    name = "bundle-es5.min",
    src = ":bundle-es5",
)

_PROD_ASSETS = [
    "@npm//:node_modules/systemjs/dist/system.js",
    "@npm//:node_modules/core-js/client/core.min.js",
]

_BUNDLE_ASSETS = [
    "//services/client/src:bundle-es2015.min",
    "//services/client/src:bundle-es5.min",
]

html_insert_assets(
    name = "inject_scripts_for_prod",
    outs = ["_prodapp/services/client/src/index.html"],
    args = [
        "--html=$(execpath //services/client/src:_index.html)",
        "[email protected]",
        "--roots=. $(RULEDIR) npm/node_modules/core-js/client npm/node_modules/systemjs/dist",
        "--assets",
    ] + ["$(execpath %s)" % s for s in _ASSETS + _PROD_ASSETS]
      + ["$(execpath %s)/index.js" % s for s in _BUNDLE_ASSETS],
    data = [":_index.html"] + _ASSETS + _PROD_ASSETS + _BUNDLE_ASSETS,
)

pkg_web(
    name = "prodapp",
    srcs = _ASSETS + _PROD_ASSETS + _BUNDLE_ASSETS + [
        ":inject_scripts_for_prod",
        "styles.css",
        "//services/client/src/assets",
    ],
    additional_root_paths = [
        "npm/node_modules/core-js/client",
        "npm/node_modules/systemjs/dist",
    ],
)

history_server(
    name = "prodserver",
    data = [":prodapp"],
    templated_args = [
        "-a",
        "$(rlocation cents_ideas/services/client/src/prodapp)",
    ],
)

nodejs_image(
    name = "nodejs_image",
    data = [":prodapp"],
    entry_point = "@npm//:node_modules/history-server/modules/cli.js",
    node_modules = "@npm//:node_modules",
    tags = ["local"],
    templated_args = ["services/client/src/prodapp"],
)

container_image(
    name = "image",
    base = ":nodejs_image",
    tags = ["local"],
    workdir = "/app/src/nodejs_image.binary.runfiles/cents_ideas/services/client",
)

Este artigo é coletado da Internet.

Se houver alguma infração, entre em [email protected] Delete.

editar em
0

deixe-me dizer algumas palavras

0comentários
loginDepois de participar da revisão

Artigos relacionados

TOP lista

quentelabel

Arquivo