Not sure if this is an issue with babel or bazel or a combination, but I am just trying to write a js_library that uses babel to downlevel es6 to amd. I am running into issues with babel trying to load its plugins. I started out just using the cli and passing it a .babelrc - but now also tried using the node api for easier debugging.
So quite simple, for testing now I have:
nodejs_binary(
name = "babel",
entry_point = "__main__/common/bazel/rules/js/babel.js",
data = [
"babel.js",
"@js_library_deps//:node_modules",
],
node_modules = "@js_library_deps//:node_modules",
visibility = ["//visibility:public"],
)
// babel.js
const babel = require("@babel/core");
// This require does work.
// const transform_plugin = require("@babel/plugin-transform-modules-amd");
console.log(babel.transformFileSync("./babel.js", {
plugins: ["@babel/plugin-transform-modules-amd"]
}).code);
Then, when executing this via bazel run I get:
failed to load main Error: Cannot find module '@babel/plugin-transform-modules-amd' from '/home/markus/.cache/bazel/_bazel_markus/c83dd5dc66625baf386a59b750f50a10/execroot/__main__/bazel-out/k8-fastbuild/bin/common/bazel/rules/js/babel.runfiles/__main__'
at Function.module.exports [as sync] (/home/markus/.cache/bazel/_bazel_markus/c83dd5dc66625baf386a59b750f50a10/execroot/__main__/bazel-out/k8-fastbuild/bin/common/bazel/rules/js/babel.runfiles/js_library_deps/node_modules/resolve/lib/sync.js:42:15)
at resolveStandardizedName (/home/markus/.cache/bazel/_bazel_markus/c83dd5dc66625baf386a59b750f50a10/execroot/__main__/bazel-out/k8-fastbuild/bin/common/bazel/rules/js/babel.runfiles/js_library_deps/node_modules/@babel/core/lib/config/files/plugins.js:100:31)
at resolvePlugin (/home/markus/.cache/bazel/_bazel_markus/c83dd5dc66625baf386a59b750f50a10/execroot/__main__/bazel-out/k8-fastbuild/bin/common/bazel/rules/js/babel.runfiles/js_library_deps/node_modules/@babel/core/lib/config/files/plugins.js:53:10)
at loadPlugin (/home/markus/.cache/bazel/_bazel_markus/c83dd5dc66625baf386a59b750f50a10/execroot/__main__/bazel-out/k8-fastbuild/bin/common/bazel/rules/js/babel.runfiles/js_library_deps/node_modules/@babel/core/lib/config/files/plugins.js:61:20)
at createDescriptor (/home/markus/.cache/bazel/_bazel_markus/c83dd5dc66625baf386a59b750f50a10/execroot/__main__/bazel-out/k8-fastbuild/bin/common/bazel/rules/js/babel.runfiles/js_library_deps/node_modules/@babel/core/lib/config/config-descriptors.js:135:21)
at items.map (/home/markus/.cache/bazel/_bazel_markus/c83dd5dc66625baf386a59b750f50a10/execroot/__main__/bazel-out/k8-fastbuild/bin/common/bazel/rules/js/babel.runfiles/js_library_deps/node_modules/@babel/core/lib/config/config-descriptors.js:87:50)
at Array.map (<anonymous>)
at createDescriptors (/home/markus/.cache/bazel/_bazel_markus/c83dd5dc66625baf386a59b750f50a10/execroot/__main__/bazel-out/k8-fastbuild/bin/common/bazel/rules/js/babel.runfiles/js_library_deps/node_modules/@babel/core/lib/config/config-descriptors.js:87:29)
at createPluginDescriptors (/home/markus/.cache/bazel/_bazel_markus/c83dd5dc66625baf386a59b750f50a10/execroot/__main__/bazel-out/k8-fastbuild/bin/common/bazel/rules/js/babel.runfiles/js_library_deps/node_modules/@babel/core/lib/config/config-descriptors.js:83:10)
at alias (/home/markus/.cache/bazel/_bazel_markus/c83dd5dc66625baf386a59b750f50a10/execroot/__main__/bazel-out/k8-fastbuild/bin/common/bazel/rules/js/babel.runfiles/js_library_deps/node_modules/@babel/core/lib/config/config-descriptors.js:75:49)
Executing it with node directly works as expected.
It seems like something is missing from the pasted error - it's just a stack with no message text?
If the error is in module resolution, I usually make local edits to enable extra debugging - in node_launcher.sh you can print the filesystem contents in the sandbox where the build is running, and in node_loader.js you can add more println debugging to see what is being required - usually it's a trivial matter of some paths being relative to the wrong thing, once you see what node is trying to resolve and what .js files are present for it to read.
DM me if you need more help?
Just posting this here for completeness sake, I did indeed manage to cut some of the output and here is the full output based on what is in the PR:
node_loader: try to resolve build_bazel_rules_nodejs_js_library_deps/node_modules/resolve
node_loader: resolved directory /home/markus/.cache/bazel/_bazel_markus/8a660bd637164ed93766281cfe17450f/sandbox/linux-sandbox/1/execroot/devserver_example/bazel-out/host/bin/external/build_bazel_rules_nodejs/internal/js_library/v2/babel.runfiles/build_bazel_rules_nodejs_js_library_deps/node_modules/resolve/index.js
{ Error: Cannot find module '@babel/plugin-transform-modules-amd' from '/home/markus/.cache/bazel/_bazel_markus/8a660bd637164ed93766281cfe17450f/sandbox/linux-sandbox/1/execroot/devserver_example/external/build_bazel_rules_nodejs/internal/js_library/v2'
at Function.module.exports [as sync] (/home/markus/.cache/bazel/_bazel_markus/8a660bd637164ed93766281cfe17450f/external/build_bazel_rules_nodejs_js_library_deps/node_modules/resolve/lib/sync.js:42:15)
at resolveStandardizedName (/home/markus/.cache/bazel/_bazel_markus/8a660bd637164ed93766281cfe17450f/external/build_bazel_rules_nodejs_js_library_deps/node_modules/@babel/core/lib/config/files/plugins.js:100:31)
at resolvePlugin (/home/markus/.cache/bazel/_bazel_markus/8a660bd637164ed93766281cfe17450f/external/build_bazel_rules_nodejs_js_library_deps/node_modules/@babel/core/lib/config/files/plugins.js:53:10)
at loadPlugin (/home/markus/.cache/bazel/_bazel_markus/8a660bd637164ed93766281cfe17450f/external/build_bazel_rules_nodejs_js_library_deps/node_modules/@babel/core/lib/config/files/plugins.js:61:20)
at createDescriptor (/home/markus/.cache/bazel/_bazel_markus/8a660bd637164ed93766281cfe17450f/external/build_bazel_rules_nodejs_js_library_deps/node_modules/@babel/core/lib/config/config-descriptors.js:114:9)
at items.map (/home/markus/.cache/bazel/_bazel_markus/8a660bd637164ed93766281cfe17450f/external/build_bazel_rules_nodejs_js_library_deps/node_modules/@babel/core/lib/config/config-descriptors.js:69:50)
at Array.map (<anonymous>)
at createDescriptors (/home/markus/.cache/bazel/_bazel_markus/8a660bd637164ed93766281cfe17450f/external/build_bazel_rules_nodejs_js_library_deps/node_modules/@babel/core/lib/config/config-descriptors.js:69:29)
at createPluginDescriptors (/home/markus/.cache/bazel/_bazel_markus/8a660bd637164ed93766281cfe17450f/external/build_bazel_rules_nodejs_js_library_deps/node_modules/@babel/core/lib/config/config-descriptors.js:65:10)
at plugins (/home/markus/.cache/bazel/_bazel_markus/8a660bd637164ed93766281cfe17450f/external/build_bazel_rules_nodejs_js_library_deps/node_modules/@babel/core/lib/config/config-descriptors.js:36:19) code: 'MODULE_NOT_FOUND' }
Target //src:test failed to build
Looking further into it just now
Edit:
Aahh, I think the source of the issue is babel using https://github.com/browserify/resolve to load the plugins which then I guess does not use the patched require() function.
The issue was indeed due to the resolve library and we fixed this now in a pairing session with @alexeagle by simply requiring the plugin first in the babel.rc.js.
@Globegitter Any chance you could post some example code of using babel with this issue fixed? Trying to get this working myself!
@mattmoor sorry just seeing this now, here an example of one of our babel.config.js files:
const babelPresetEnv = require('@babel/preset-env');
const isTest = process.env.NODE_ENV === 'test';
const targets = isTest ? { node: 'current' } : {
browsers: 'last 2 versions',
safari: '9',
ie: '11',
ios: '8',
};
const modules = isTest ? 'commonjs' : 'auto';
const presets = [
[babelPresetEnv, { targets, modules }],
];
module.exports = { presets };
Most helpful comment
@mattmoor sorry just seeing this now, here an example of one of our
babel.config.jsfiles: