Skip to content

Commit

Permalink
Merge pull request #51 from torusresearch/feat/esm
Browse files Browse the repository at this point in the history
move to es module syntax
  • Loading branch information
chaitanyapotti authored Feb 9, 2023
2 parents fe780c8 + 11459a7 commit 2560b40
Show file tree
Hide file tree
Showing 22 changed files with 216 additions and 521 deletions.
12 changes: 12 additions & 0 deletions packages/torus-scripts/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": ["eslint:recommended"],
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2022
},
"rules": {},
"env": {
"es2020": true,
"node": true
}
}
15 changes: 9 additions & 6 deletions packages/torus-scripts/bin/torus-scripts.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ process.on("unhandledRejection", (err) => {
throw err;
});

const parseArgs = require("yargs-parser");
const { helpText } = require("../helpers/constants");
import parseArgs from "yargs-parser";
import spawn from "cross-spawn";

import { readJSONFile, resolveFileUrl } from "../helpers/utils.js";
import { helpText } from "../helpers/constants.js";

const pkg = readJSONFile(new URL("../package.json", import.meta.url));

const aliases = {
h: "help",
Expand All @@ -35,19 +40,17 @@ if (parsedArgs.help) {
console.log(helpText);
process.exit(0);
} else if (parsedArgs.version) {
console.log(`v${require("../package.json").version}`);
console.log(`v${pkg.version}`);
process.exit(0);
}

const spawn = require("cross-spawn");

const scriptIndex = args.findIndex((x) => x === "build" || x === "start" || x === "release");

const script = scriptIndex === -1 ? args[0] : args[scriptIndex];
const nodeArgs = scriptIndex > 0 ? args.slice(0, scriptIndex) : [];

if (["build", "start", "release"].includes(script)) {
const result = spawn.sync(process.execPath, nodeArgs.concat(require.resolve("../scripts/" + script)).concat(args.slice(scriptIndex + 1)), {
const result = spawn.sync(process.execPath, nodeArgs.concat(resolveFileUrl("../scripts/" + script + ".js")).concat(args.slice(scriptIndex + 1)), {
stdio: "inherit",
});
if (result.signal) {
Expand Down
13 changes: 6 additions & 7 deletions packages/torus-scripts/config/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
// merges the user provided config with the default config
// and returns the merged config

const merge = require("babel-merge");
const fs = require("fs");
import merge from "babel-merge";

const paths = require("./paths");
import paths from "./paths.js";
import defaultConfig from "../defaults/babel.config.js";
import { readCjsFile } from "../helpers/utils.js";

const defaultConfig = require("../defaults/babel.config");
const userConfig = await readCjsFile(paths.appBabelConfig);

const userConfig = fs.existsSync(paths.appBabelConfig) ? require(paths.appBabelConfig) : {};

module.exports = merge(defaultConfig, userConfig);
export default merge(defaultConfig, userConfig.default || {});
13 changes: 6 additions & 7 deletions packages/torus-scripts/config/paths.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
"use strict";

const path = require("path");
const fs = require("fs");
import path from "path";
import fs from "fs";
import { resolveFileUrl } from "../helpers/utils.js";

// This works with lerna packages too
const appDirectory = fs.realpathSync(process.cwd());
const resolveApp = (relativePath) => path.resolve(appDirectory, relativePath);

const buildPath = process.env.BUILD_DIR || "dist";

const moduleFileExtensions = ["js", "ts", "json", "mjs", "jsx", "tsx"];
export const moduleFileExtensions = ["js", "ts", "json", "mjs", "jsx", "tsx"];

// Resolve file paths in the same order as webpack
const resolveModule = (resolveFn, filePath) => {
Expand All @@ -22,10 +23,10 @@ const resolveModule = (resolveFn, filePath) => {
return resolveFn(`${filePath}.js`);
};

const resolveOwn = (relativePath) => path.resolve(__dirname, "..", relativePath);
const resolveOwn = (relativePath) => path.resolve(resolveFileUrl(new URL("../" + relativePath, import.meta.url)));

// we're in ./node_modules/torus-scripts/config/
module.exports = {
export default {
dotenv: resolveApp(".env"),
appPath: resolveApp("."),
appBuild: resolveApp(buildPath),
Expand All @@ -47,5 +48,3 @@ module.exports = {
ownPath: resolveOwn("."),
ownNodeModules: resolveOwn("node_modules"), // This is empty on npm 3
};

module.exports.moduleFileExtensions = moduleFileExtensions;
28 changes: 15 additions & 13 deletions packages/torus-scripts/config/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@
// merges the user provided config with the default config
// and returns the merged config

const mergewith = require("lodash.mergewith");
const babel = require("@babel/core");
const typescript = require("@rollup/plugin-typescript");
const babelPlugin = require("@rollup/plugin-babel").default;
const path = require("path");
const fs = require("fs");
const requireFromString = require("require-from-string");
const tsconfigBuild = require("./tsconfig.build");
const babelConfig = require("./babel.config");
const torusConfig = require("./torus.config");
import mergewith from "lodash.mergewith";
import babel from "@babel/core";
import typescript from "@rollup/plugin-typescript";
import babelPlugin from "@rollup/plugin-babel";
import path from "path";
import fs from "fs";
import requireFromString from "require-from-string";

const paths = require("./paths");
const pkg = require(paths.appPackageJson);
import tsconfigBuild from "./tsconfig.build.js";
import torusConfig from "./torus.config.js";
import paths from "./paths.js";
import { readJSONFile } from "../helpers/utils.js";
import babelConfig from "./babel.config.js";

const pkg = readJSONFile(paths.appPackageJson);

const babelPluginOptions = {
extensions: [".ts", ".js", ".tsx", ".jsx", ".mjs"],
Expand Down Expand Up @@ -80,7 +82,7 @@ function customizer(objValue, srcValue, key) {

// We just return the user's array value

module.exports = (name) => {
export default (name) => {
const userConfig = fs.existsSync(paths.appRollupConfig)
? requireFromString(babel.transformFileSync(paths.appRollupConfig, { presets: ["@babel/env"] }).code).default
: {};
Expand Down
13 changes: 6 additions & 7 deletions packages/torus-scripts/config/torus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
// merges the user provided config with the default config
// and returns the merged config

const merge = require("lodash.mergewith");
const fs = require("fs");
import merge from "lodash.mergewith";

const paths = require("./paths");
import paths from "./paths.js";
import defaultConfig from "../defaults/torus.config.js";
import { readCjsFile } from "../helpers/utils.js";

const defaultConfig = require("../defaults/torus.config");
const userConfig = await readCjsFile(paths.appTorusConfig);

const userConfig = fs.existsSync(paths.appTorusConfig) ? require(paths.appTorusConfig) : {};

module.exports = merge(defaultConfig, userConfig);
export default merge(defaultConfig, userConfig.default || {});
11 changes: 6 additions & 5 deletions packages/torus-scripts/config/tsconfig.build.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
// merges the user provided config with the default config
// and returns the merged config

const paths = require("./paths");
const fs = require("fs");
const ts = require("typescript");
const merge = require("lodash.mergewith");
import fs from "fs";
import ts from "typescript";
import merge from "lodash.mergewith";

import paths from "./paths.js";

const defaultConfig = {
compilerOptions: {
Expand Down Expand Up @@ -39,4 +40,4 @@ function customizer(objValue, srcValue) {
}
}

module.exports = merge(defaultConfig, userConfig, customizer);
export default merge(defaultConfig, userConfig, customizer);
78 changes: 39 additions & 39 deletions packages/torus-scripts/config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@
// and returns the merged config

// By default this generates cjs, umd builds
const merge = require("lodash.mergewith");
const path = require("path");
const fs = require("fs");
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
const nodeExternals = require("webpack-node-externals");

const paths = require("./paths");
const babelConfig = require("./babel.config");
const ESLintPlugin = require("eslint-webpack-plugin");
const torusConfig = require("./torus.config");
const webpack = require("webpack");
import merge from "lodash.mergewith";
import path from "path";
import fs from "fs";
import { BundleAnalyzerPlugin } from "webpack-bundle-analyzer";
import nodeExternals from "webpack-node-externals";
import webpack from "webpack";

import paths, { moduleFileExtensions } from "./paths.js";
import babelConfig from "./babel.config.js";
import ESLintPlugin from "eslint-webpack-plugin";
import torusConfig from "./torus.config.js";
import { readCjsFile, readJSONFile } from "../helpers/utils.js";

const { appWebpackConfig, appBuild } = paths;
const { NODE_ENV = "production" } = process.env;

const pkg = require(paths.appPackageJson);
const { baseConfig: userBaseConfig, ...rest } = fs.existsSync(appWebpackConfig) ? require(appWebpackConfig) : { baseConfig: {} };
const pkg = readJSONFile(paths.appPackageJson);
const configImported = (await readCjsFile(appWebpackConfig)).default || { baseConfig: {} };

const babelLoaderOptions = {
...babelConfig,
Expand All @@ -35,7 +36,7 @@ if (fs.existsSync(paths.appBrowserslistConfig)) {
babelLoaderOptions.targets = torusConfig.browserslistrc;
}

const babelLoader = {
export const babelLoader = {
test: /\.(ts|js)x?$/,
exclude: /(node_modules|bower_components)/,
use: {
Expand All @@ -59,15 +60,23 @@ const polyfillPlugins = [
}),
];

export const resolveWebpackModule = (localPath) => {
if (fs.existsSync(path.resolve(paths.ownNodeModules, localPath))) return new URL(paths.ownNodeModules + "/" + localPath, import.meta.url).pathname;
else if (fs.existsSync(path.resolve(paths.appNodeModules, localPath)))
return new URL(paths.appNodeModules + "/" + localPath, import.meta.url).pathname;

throw new Error("Cannot resolve module: " + localPath + "Please install the dependency");
};

const polyfillFallback = {
http: require.resolve("stream-http"),
https: require.resolve("https-browserify"),
os: require.resolve("os-browserify/browser"),
crypto: require.resolve("crypto-browserify"),
assert: require.resolve("assert/"),
stream: require.resolve("stream-browserify"),
url: require.resolve("url/"),
buffer: require.resolve("buffer/"),
http: resolveWebpackModule("stream-http/index.js"),
https: resolveWebpackModule("https-browserify/index.js"),
os: resolveWebpackModule("os-browserify/browser.js"),
crypto: resolveWebpackModule("crypto-browserify/index.js"),
assert: resolveWebpackModule("assert/build/assert.js"),
stream: resolveWebpackModule("stream-browserify/index.js"),
url: resolveWebpackModule("url/url.js"),
buffer: resolveWebpackModule("buffer/index.js"),
fs: false,
path: false,
};
Expand All @@ -84,7 +93,8 @@ function customizer(objValue, srcValue, key) {
}
}

module.exports = (pkgName) => {
export default (pkgName) => {
const { baseConfig: userBaseConfig, ...rest } = configImported;
// create a copy of baseConfig every time so that loaders use new instances
const umdConfig = merge(
getDefaultUmdConfig(pkgName),
Expand Down Expand Up @@ -126,9 +136,7 @@ module.exports = (pkgName) => {
];
};

module.exports.babelLoader = babelLoader;

const getDefaultBaseConfig = () => {
export const getDefaultBaseConfig = () => {
return {
mode: NODE_ENV,
devtool: "source-map",
Expand All @@ -142,9 +150,9 @@ const getDefaultBaseConfig = () => {
},
},
resolve: {
extensions: paths.moduleFileExtensions.map((x) => `.${x}`),
extensions: moduleFileExtensions.map((x) => `.${x}`),
alias: {
"bn.js": require.resolve("bn.js"),
"bn.js": resolveWebpackModule("bn.js/lib/bn.js"),
},
},
plugins: [],
Expand All @@ -165,9 +173,7 @@ const getDefaultBaseConfig = () => {
};
};

module.exports.getDefaultBaseConfig = getDefaultBaseConfig;

const getDefaultUmdConfig = (pkgName) => {
export const getDefaultUmdConfig = (pkgName) => {
return {
output: {
filename: `${pkgName}.umd.min.js`,
Expand All @@ -193,9 +199,7 @@ const getDefaultUmdConfig = (pkgName) => {
};
};

module.exports.getDefaultUmdConfig = getDefaultUmdConfig;

const getDefaultCjsConfig = (pkgName) => {
export const getDefaultCjsConfig = (pkgName) => {
return {
...optimization,
output: {
Expand Down Expand Up @@ -223,9 +227,7 @@ const getDefaultCjsConfig = (pkgName) => {
};
};

module.exports.getDefaultCjsConfig = getDefaultCjsConfig;

const getDefaultCjsBundledConfig = (pkgName) => {
export const getDefaultCjsBundledConfig = (pkgName) => {
return {
...optimization,
output: {
Expand All @@ -241,5 +243,3 @@ const getDefaultCjsBundledConfig = (pkgName) => {
},
};
};

module.exports.getDefaultCjsBundledConfig = getDefaultCjsBundledConfig;
2 changes: 1 addition & 1 deletion packages/torus-scripts/defaults/babel.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
presets: ["@babel/env", "@babel/typescript"],
plugins: [
"@babel/plugin-syntax-bigint",
Expand Down
8 changes: 5 additions & 3 deletions packages/torus-scripts/defaults/torus.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const paths = require("../config/paths");
const pkg = require(paths.appPackageJson);
import paths from "../config/paths.js";
import { readJSONFile } from "../helpers/utils.js";

const pkg = readJSONFile(paths.appPackageJson);

function camelCase(input) {
return input.toLowerCase().replace(/-(.)/g, (_, group1) => group1.toUpperCase());
Expand All @@ -12,7 +14,7 @@ function generatePackageName(name) {
return usableName;
}

module.exports = {
export default {
name: generatePackageName(pkg.name),
esm: true,
cjs: true,
Expand Down
4 changes: 2 additions & 2 deletions packages/torus-scripts/defaults/tsconfig.build.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const paths = require("../config/paths");
import paths from "../config/paths.js";

module.exports = {
export default {
compilerOptions: {
rootDir: "src",
moduleResolution: "node",
Expand Down
Loading

0 comments on commit 2560b40

Please sign in to comment.