Skip to content
This repository has been archived by the owner on Jun 5, 2021. It is now read-only.

Commit

Permalink
Custom resolvers (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
schoren authored and fwouts committed Mar 28, 2019
1 parent fc5dbe5 commit 8133bf9
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
1 change: 1 addition & 0 deletions internal/js_script_and_test/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export NODE_PATH=${path.relative(
destinationDirShort,
installedNpmPackagesDirShort
)}/node_modules
export GENDIR=${process.env.GENDIR}
export LIB_DIR=${path.dirname(libBuildfilePath)}
${yarnShellCommand(destinationDirShort, "start")}
`,
Expand Down
1 change: 1 addition & 0 deletions internal/js_script_and_test/rule.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def _js_script_impl(ctx):
executable = ctx.file._internal_nodejs,
env = {
"NODE_PATH": ctx.attr._internal_packages[NpmPackagesInfo].installed_dir.path + "/node_modules",
"GENDIR": ctx.var["GENDIR"],
},
arguments = [
# Run `node js_script/compile.js`.
Expand Down
24 changes: 22 additions & 2 deletions internal/web_bundle/create_webpack_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,23 @@ const [
optionalLibrary,
splitChunksStr,
publicPath,
webpackConfigPath
webpackConfigPath,
joinedExternals
] = process.argv;

const externals = joinedExternals
.split("|")
.map(extTuple => extTuple.split(":", 2))
.reduce((exts, [k, v]) => {
if (k == "") {
return exts;
}

exts.push(`"${k}": "${v}"`);
return exts;
}, [])
.join(",");

const [libraryName, libraryTarget] = optionalLibrary.split("/");
const splitChunks = splitChunksStr === "1";

Expand Down Expand Up @@ -52,6 +66,9 @@ module.exports = (
const OptimizeCSSAssetsPlugin = require(path.resolve(\`\${loadersNpmPackagesDir}/node_modules/optimize-css-assets-webpack-plugin\`));
const UglifyJsPlugin = require(path.resolve(\`\${loadersNpmPackagesDir}/node_modules/uglifyjs-webpack-plugin\`));
const base = path.join(process.cwd(), sourceDir)
const gendir = path.join(base, process.env["GENDIR"], '..', 'bin')
return {
entry: (sourceDir.startsWith("/") ? "" : "./") + path.join(
sourceDir,
Expand All @@ -70,6 +87,7 @@ module.exports = (
: ""
}
},
externals: {${externals}},
mode: "${mode}",
bail: ${mode === "production" ? "true" : "false"},
target: "web",
Expand Down Expand Up @@ -211,7 +229,9 @@ module.exports = (
modules: [
path.join(installedNpmPackagesDir, "node_modules"),
// Necessary for webpack-hot-client with the dev server.
path.join(loadersNpmPackagesDir, "node_modules")
path.join(loadersNpmPackagesDir, "node_modules"),
base,
gendir,
]
},
resolveLoader: {
Expand Down
18 changes: 12 additions & 6 deletions internal/web_bundle/rule.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def _web_bundle_impl(ctx):
executable = ctx.file._internal_nodejs,
env = {
"NODE_PATH": ctx.attr._internal_packages[NpmPackagesInfo].installed_dir.path + "/node_modules",
"GENDIR": ctx.var["GENDIR"],
},
arguments = [
# Run `node web_bundle/compile.js`.
Expand Down Expand Up @@ -207,6 +208,9 @@ def _strip_buildfile(path):
def _create_webpack_config(ctx):
webpack_config = ctx.actions.declare_file(ctx.label.name + ".webpack.config.js")

mode_arg = ctx.attr.mode
mode_arg = ctx.expand_make_variables("mode", mode_arg, {})

# Create the Webpack config file.
ctx.actions.run(
inputs = [
Expand All @@ -221,6 +225,7 @@ def _create_webpack_config(ctx):
executable = ctx.file._internal_nodejs,
env = {
"NODE_PATH": ctx.attr._internal_packages[NpmPackagesInfo].installed_dir.path + "/node_modules",
"GENDIR": ctx.var["GENDIR"],
},
arguments = [
# Run `node web_bundle/create_webpack_config.js`.
Expand All @@ -232,7 +237,7 @@ def _create_webpack_config(ctx):
# Output file name (e.g. "bundle.js").
ctx.attr.output,
# Mode for Webpack.
ctx.attr.mode,
mode_arg,
# Library for Webpack (optional).
ctx.attr.library_name + "/" + ctx.attr.library_target if ctx.attr.library_name else "",
# Enable split chunks or not.
Expand All @@ -241,6 +246,11 @@ def _create_webpack_config(ctx):
ctx.attr.public_path,
# Path where to create the Webpack config.
webpack_config.path,
# Externals definitions
("|".join([
key + ":" + value
for key, value in ctx.attr.externals.items()
])),
],
)

Expand All @@ -256,18 +266,14 @@ _ATTRS = {
providers = [JsModuleInfo],
),
"env": attr.string_dict(),
"externals": attr.string_dict(),
"entry": attr.string(
mandatory = True,
),
"output": attr.string(
default = "bundle.js",
),
"mode": attr.string(
values = [
"none",
"development",
"production",
],
default = "none",
),
"split_chunks": attr.bool(
Expand Down

0 comments on commit 8133bf9

Please sign in to comment.