@enonic/react4xp
Version:
Build tools for React4xp
258 lines (251 loc) • 10.4 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/webpack.config.nashornPolyfills.ts
var webpack_config_nashornPolyfills_exports = {};
__export(webpack_config_nashornPolyfills_exports, {
default: () => webpack_config_nashornPolyfills_default
});
module.exports = __toCommonJS(webpack_config_nashornPolyfills_exports);
var import_fs = require("fs");
var import_path = require("path");
// src/constants.runtime.ts
var R4X_TARGETSUBDIR = "r4xAssets";
// src/constants.buildtime.ts
var DIR_PATH_RELATIVE_SRC_MAIN_RESOURCES = "src/main/resources";
var DIR_PATH_RELATIVE_SRC_R4X = `${DIR_PATH_RELATIVE_SRC_MAIN_RESOURCES}/react4xp`;
var DIR_PATH_RELATIVE_SRC_SITE = `${DIR_PATH_RELATIVE_SRC_MAIN_RESOURCES}/site`;
var DIR_PATH_RELATIVE_BUILD_RESOURCES_MAIN = "build/resources/main";
var DIR_PATH_RELATIVE_BUILD_LIB_R4X = `${DIR_PATH_RELATIVE_BUILD_RESOURCES_MAIN}/lib/enonic/react4xp`;
var DIR_PATH_RELATIVE_BUILD_ASSETS_R4X = `${DIR_PATH_RELATIVE_BUILD_RESOURCES_MAIN}/${R4X_TARGETSUBDIR}`;
var FILE_NAME_R4X_NASHORN_POLYFILLS = "react4xpNashornPolyfills.es6";
// src/util/makeVerboseLogger.ts
var import_q_i = require("q-i");
var makeVerboseLogger = (VERBOSE) => VERBOSE ? (item, label, stringify = false) => {
if (typeof item === "object") {
if (label) {
console.log(label + ":");
}
(0, import_q_i.print)(item, { maxItems: Infinity });
} else if (stringify) {
console.log((label ? label + " " : "") + "(" + typeof item + "): " + JSON.stringify(item));
} else {
console.log((label ? label + ": " : "") + item);
}
} : () => {
};
// src/util/webpackLogLevel.ts
function webpackLogLevel(r4xBuildLogLevel = "LIFECYCLE" /* LIFECYCLE */) {
switch (r4xBuildLogLevel) {
case "QUIET" /* QUIET */:
return "none" /* NONE */;
// disable logging
case "ERROR" /* ERROR */:
return "error" /* ERROR */;
// errors only
case "WARN" /* WARN */:
return "warn" /* WARN */;
// errors and warnings only
case "LIFECYCLE" /* LIFECYCLE */:
return "info" /* INFO */;
// errors, warnings, and info messages
case "INFO" /* INFO */:
return "log" /* LOG */;
// errors, warnings, info messages, log messages, groups, clears. Collapsed groups are displayed in a collapsed state
case "DEBUG" /* DEBUG */:
return "verbose" /* VERBOSE */;
// log everything except debug and trace
default:
console.warn(`Unknown log level '${r4xBuildLogLevel}' falling back to default log level 'info'`);
return "info" /* INFO */;
}
}
// src/webpack.config.nashornPolyfills.ts
var FILE_STEM_NASHORNPOLYFILLS_USERADDED = "nashornPolyfills.userAdded";
var webpack_config_nashornPolyfills_default = () => {
const R4X_DIR_PATH_ABSOLUTE_PROJECT = process.env.R4X_DIR_PATH_ABSOLUTE_PROJECT;
if (!(0, import_path.isAbsolute)(R4X_DIR_PATH_ABSOLUTE_PROJECT)) {
throw new Error(`System environment variable R4X_DIR_PATH_ABSOLUTE_PROJECT:${R4X_DIR_PATH_ABSOLUTE_PROJECT} not an absolute path!`);
}
const DIR_PATH_ABSOLUTE_BUILD_SYSTEM = (0, import_path.resolve)(__dirname, "..");
const DIR_PATH_ABSOLUTE_BUILD_LIB_R4X = (0, import_path.join)(R4X_DIR_PATH_ABSOLUTE_PROJECT, DIR_PATH_RELATIVE_BUILD_LIB_R4X);
const WEBPACK_MODE = process.env.NODE_ENV || "production";
const DEVMODE = WEBPACK_MODE !== "production";
const LOG_LEVEL = webpackLogLevel(process.env.R4X_BUILD_LOG_LEVEL);
const verboseLog = makeVerboseLogger([
"log" /* LOG */,
"verbose" /* VERBOSE */
].includes(LOG_LEVEL));
const filePathAbsoluteR4xNashornPolyfills = (0, import_path.join)(
R4X_DIR_PATH_ABSOLUTE_PROJECT,
DIR_PATH_RELATIVE_SRC_MAIN_RESOURCES,
FILE_NAME_R4X_NASHORN_POLYFILLS
);
const entry = {};
try {
const statsR4xNashornPolyfills = (0, import_fs.statSync)(filePathAbsoluteR4xNashornPolyfills);
if (statsR4xNashornPolyfills.isFile()) {
verboseLog(
`Adding custom nashorn polyfills: compiling ${filePathAbsoluteR4xNashornPolyfills} --> ${(0, import_path.join)(
DIR_PATH_ABSOLUTE_BUILD_LIB_R4X,
FILE_STEM_NASHORNPOLYFILLS_USERADDED
)}`
);
entry[FILE_STEM_NASHORNPOLYFILLS_USERADDED] = filePathAbsoluteR4xNashornPolyfills;
}
} catch (e) {
console.info(`${filePathAbsoluteR4xNashornPolyfills} not found, which is fine :)`);
}
const webpackConfigObjectNashornPolyfills = {
context: R4X_DIR_PATH_ABSOLUTE_PROJECT,
// Used as default for resolve.roots
entry,
mode: WEBPACK_MODE,
module: {
rules: [
{
test: /\.es6$/,
// I don't think we can exclude much, everything must be able to run:
// * server-side (Nashorn/Graal-JS) and
// * client-side (Browsers).
//exclude: /node_modules/,
// It takes time to transpile, if you know they don't need
// transpilation to run in Enonic XP (Nashorn/Graal-JS) you may list
// them here:
exclude: [
/node_modules[\\/]core-js/,
// will cause errors if they are transpiled by Babel
/node_modules[\\/]webpack[\\/]buildin/
// will cause errors if they are transpiled by Babel
],
loader: "builtin:swc-loader",
options: {
jsc: {
// parser: {
// syntax: 'typescript'
// },
// https://swc.rs/docs/configuration/compilation#jscexternalhelpers
// externalHelpers: true,
// `env` and `jsc.target` cannot be used together
target: "es2022",
// Tested on "Graal" in Enonic 7.14.4
transform: {
// https://swc.rs/docs/configuration/compilation#jsctransformreact
react: {
runtime: "automatic",
development: DEVMODE,
// $RefreshReg$ is not defined
// refresh: DEVMODE,
refresh: false
}
}
}
// env: { // `env` and `jsc.target` cannot be used together
// mode: 'usage',
// coreJs: '3.26.1',
// targets: 'Chrome >= 48',
// }
}
}
]
},
// module
optimization: {
minimize: !DEVMODE
},
output: {
path: DIR_PATH_ABSOLUTE_BUILD_LIB_R4X,
filename: "[name].js",
// No need for contenthash as this is serverside
environment: {
arrowFunction: false,
bigIntLiteral: false,
// Whether the environment supports const and let for variable declarations.
const: false,
// Not enough to transpile 'const' and 'let' to 'var'
destructuring: false,
dynamicImport: false,
forOf: false,
module: false,
optionalChaining: true,
templateLiteral: false
},
// In order for 'const' and 'let' to be transpiled to 'var',
// library or libraryTarget must be set!
//libraryTarget: 'commonjs' // 7963B
//libraryTarget: 'commonjs-module' // 7870B // ReferenceError: 'module' is not defined
libraryTarget: "commonjs-static"
// 7907B
//libraryTarget: 'commonjs2' // 7870B // ReferenceError: 'module' is not defined
/*library: { // 7893B Trying out what is used in buildComponents, also transpiles 'const' and 'let' to 'var'
name: '[name]',
type: 'var',
}*/
// To make UMD build available on both browsers and Node.js, set output.globalObject option to 'this'.
/*libraryTarget: 'umd', // 8111B
globalObject: 'this'*/
/*libraryTarget: 'umd2', // 8111B
globalObject: 'this'*/
},
// output
resolve: {
alias: {
// Graalvm works with server-side version only!
"html-dom-parser": [
(0, import_path.resolve)(R4X_DIR_PATH_ABSOLUTE_PROJECT, "node_modules/html-dom-parser/lib/server/html-to-dom.js"),
(0, import_path.resolve)(DIR_PATH_ABSOLUTE_BUILD_SYSTEM, "node_modules/html-dom-parser/lib/server/html-to-dom.js")
]
},
extensions: [".ts", ".tsx", ".es6", ".es", ".jsx", ".js", ".json"],
modules: [
// Tell webpack what directories should be searched when resolving
// modules.
// Absolute and relative paths can both be used, but be aware that they
// will behave a bit differently.
// A relative path will be scanned similarly to how Node scans for
// node_modules, by looking through the current directory as well as its
// ancestors (i.e. ./node_modules, ../node_modules, and on).
// With an absolute path, it will only search in the given directory.
// To resolve node_modules installed under the app
(0, import_path.resolve)(R4X_DIR_PATH_ABSOLUTE_PROJECT, "node_modules"),
// To resolve node_modules installed under the build system
(0, import_path.resolve)(DIR_PATH_ABSOLUTE_BUILD_SYSTEM, "node_modules")
//'node_modules'
]
/*roots: [ // Works, but maybe modules is more specific
// A list of directories where requests of server-relative URLs
// (starting with '/') are resolved, defaults to context configuration
// option. On non-Windows systems these requests are resolved as an
// absolute path first.
R4X_DIR_PATH_ABSOLUTE_PROJECT, // same as context
DIR_PATH_ABSOLUTE_BUILD_SYSTEM
],*/
},
// resolve
stats: {
colors: true,
hash: false,
logging: LOG_LEVEL,
modules: false,
moduleTrace: false,
timings: false,
version: false
}
// stats
};
return webpackConfigObjectNashornPolyfills;
};