Skip to content

Commit

Permalink
Minify @dataform/core and bundle all dependendencies
Browse files Browse the repository at this point in the history
  • Loading branch information
lewish committed Oct 18, 2023
1 parent 2ce51b4 commit 3b9e561
Show file tree
Hide file tree
Showing 6 changed files with 1,352 additions and 70 deletions.
17 changes: 13 additions & 4 deletions core/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ import { Session } from "df/core/session";
import * as utils from "df/core/utils";
import { dataform } from "df/protos/ts";

declare var __webpack_require__: any;
declare var __non_webpack_require__: any;

// If this code is bundled with webpack, we need to side-step the webpack require re-writing and use the real require method in here.
const nativeRequire =
typeof __webpack_require__ === "function"
? __non_webpack_require__
: require;

/**
* This is the main entry point into the user space code that should be invoked by the compilation wrapper sandbox.
*
Expand All @@ -16,7 +25,7 @@ export function main(encodedCoreExecutionRequest: string): string {
const compileRequest = request.compile;

// Read the project config from the root of the project.
const originalProjectConfig = require("dataform.json");
const originalProjectConfig = nativeRequire("dataform.json");

const projectConfigOverride = compileRequest.compileConfig.projectConfigOverride ?? {};

Expand All @@ -36,7 +45,7 @@ export function main(encodedCoreExecutionRequest: string): string {
};

// Initialize the compilation session.
const session = require("@dataform/core").session as Session;
const session = nativeRequire("@dataform/core").session as Session;

session.init(compileRequest.compileConfig.projectDir, projectConfig, originalProjectConfig);

Expand All @@ -54,7 +63,7 @@ export function main(encodedCoreExecutionRequest: string): string {
.forEach(includePath => {
try {
// tslint:disable-next-line: tsr-detect-non-literal-require
topLevelIncludes[utils.baseFilename(includePath)] = require(includePath);
topLevelIncludes[utils.baseFilename(includePath)] = nativeRequire(includePath);
} catch (e) {
session.compileError(e, includePath);
}
Expand All @@ -75,7 +84,7 @@ export function main(encodedCoreExecutionRequest: string): string {
.forEach(definitionPath => {
try {
// tslint:disable-next-line: tsr-detect-non-literal-require
require(definitionPath);
nativeRequire(definitionPath);
} catch (e) {
session.compileError(e, definitionPath);
}
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@
"vscode-languageclient": "^6.1.3",
"vscode-languageserver": "^6.1.1",
"vscode-languageserver-textdocument": "^1.0.1",
"webpack": "^4.20.0",
"webpack-cli": "^3.3.10",
"yargs": "^16.2.0"
},
"resolutions": {
Expand Down
58 changes: 30 additions & 28 deletions packages/@dataform/core/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,49 @@ ts_library(
],
)

externals = [
"protobufjs",
"tarjan-graph",
"semver",
"moo",
"long",
]
load("@npm//webpack:index.bzl", "webpack")

webpack(
name = "bundler",
data = [
":webpack.config.js",
":core",
"@npm//webpack-cli",
],
)

load("@build_bazel_rules_nodejs//:index.bzl", "npm_package_bin")

npm_package_bin(
name = "bundle",
outs = [
"bundle.js",
],
args = [
"--config=$(location webpack.config.js)",
"--output=$(location bundle.js)",
"--mode=production"
],
data = [
":webpack.config.js",
],
tool = ":bundler",
)

pkg_json(
name = "json",
package_name = "@dataform/core",
description = "Dataform core API.",
external_deps = externals,
external_deps = [],
layers = [
"//:package.json",
"//packages/@dataform:package.layer.json",
],
main = "bundle.js",
types = "bundle.d.ts",
# types = "bundle.d.ts",
version = DF_VERSION,
)

pkg_bundle(
name = "bundle",
entry_point = "index.ts",
externals = externals,
deps = [
":core",
],
)

pkg_bundle_dts(
name = "bundle.d",
entry_point = "index.ts",
externals = externals,
deps = [
":core",
],
)

copy_file(
name = "core_proto",
src = "//protos:core.proto",
Expand All @@ -63,7 +66,6 @@ pkg_npm_tar(
name = "package",
deps = [
":bundle",
":bundle.d",
":package.json",
":core.proto",
],
Expand Down
3 changes: 2 additions & 1 deletion packages/@dataform/core/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "df/core";
export { compiler, main, session, indexFileGenerator, adapters, compileStandaloneSqlxQuery } from "df/core";
export { version } from "df/core/version";
40 changes: 40 additions & 0 deletions packages/@dataform/core/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const path = require("path");
const webpack = require("webpack");

module.exports = (env, argv) => {
const config = {
mode: argv.mode || "development",

target: 'node',
entry: [path.resolve(process.env.RUNFILES, "df/packages/@dataform/core/index")],
output: {
path: path.dirname(path.resolve(argv.output)),
filename: path.basename(argv.output),
libraryTarget: "commonjs-module",
},
optimization: {
minimize: true
},
stats: {
warnings: true
},
node: {
// fs: "empty",
// child_process: "empty"
},
resolve: {
extensions: [".ts", ".js", ".json"],
alias: {
df: path.resolve(process.env.RUNFILES, "df")
}
},

plugins: [
new webpack.optimize.LimitChunkCountPlugin({
maxChunks: 1
})
],

};
return config;
};
Loading

0 comments on commit 3b9e561

Please sign in to comment.