Skip to content

Commit

Permalink
Add babel and create cjs build (#585)
Browse files Browse the repository at this point in the history
* make build and publish types

* add babel and create cjs build

* remove yarn.lock from root directory
  • Loading branch information
salman01zp committed Apr 28, 2023
1 parent 58d5251 commit ed68aeb
Show file tree
Hide file tree
Showing 9 changed files with 2,010 additions and 97 deletions.
10 changes: 10 additions & 0 deletions types/babel-config-cjs.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const polkadotBabelConfig = require('./polkadot-dev-configs/babel-config-cjs.cjs');

module.exports = {
plugins: [
...polkadotBabelConfig.plugins,
],
presets: [
...polkadotBabelConfig.presets
]
};
41 changes: 32 additions & 9 deletions types/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@webb-tools/dkg-substrate-types",
"version": "0.0.4",
"version": "0.0.5",
"description": "Polkadot.js type definitions required for interacting with Webb's DKG protocol",
"main": "./build/index.js",
"author": "Webb Developers <drew@webb.tools>",
Expand All @@ -16,21 +16,44 @@
},
"homepage": "https://github.com/webb-tools/dkg-substrate",
"dependencies": {
"@babel/cli": "^7.20.7",
"@babel/core": "^7.20.12",
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.18.6",
"@babel/plugin-proposal-numeric-separator": "^7.18.6",
"@babel/plugin-proposal-optional-chaining": "^7.20.7",
"@babel/plugin-syntax-bigint": "^7.8.3",
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
"@babel/plugin-syntax-import-assertions": "^7.20.0",
"@babel/plugin-syntax-import-meta": "^7.10.4",
"@babel/plugin-syntax-top-level-await": "^7.14.5",
"@babel/plugin-transform-regenerator": "^7.20.5",
"@babel/plugin-transform-runtime": "^7.19.6",
"@babel/preset-env": "^7.20.2",
"@babel/preset-react": "^7.18.6",
"@babel/preset-typescript": "^7.18.6",
"@babel/register": "^7.18.9",
"@babel/runtime": "^7.20.13",
"babel-jest": "^29.4.1",
"babel-plugin-module-extension-resolver": "^1.0.0",
"babel-plugin-module-resolver": "^5.0.0",
"babel-plugin-styled-components": "^2.0.7",
"@open-web3/orml-types": "^1.1.3",
"@polkadot/api-derive": "10.3.2",
"@polkadot/dev": "^0.72.42",
"@polkadot/typegen": "10.3.2",
"@polkadot/types": "10.3.2"
"@polkadot/types": "10.3.2",
"fs-extra": "^11.1.1",
"glob2base": "^0.0.12",
"minimatch": "^7.4.2",
"mkdirp": "^2.1.5"
},
"devDependencies": {
"@types/websocket": "^1.0.0",
"rimraf": "3.0.2",
"ts-node": "10.8.0",
"tsconfig-paths": "^3.14.1",
"typescript": "4.7.2",
"@polkadot/dev": "^0.72.34",
"websocket": "^1.0.31",
"mkdirp": "^2.1.5",
"glob2base": "^0.0.12"
"ts-node": "10.9.1",
"tsconfig-paths": "^4.2.0",
"typescript": "5.0.4",
"websocket": "^1.0.31"
},
"scripts": {
"build": "node ./scripts/build.js",
Expand Down
12 changes: 12 additions & 0 deletions types/polkadot-dev-configs/babel-config-cjs.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2017-2023 @polkadot/dev authors & contributors
// SPDX-License-Identifier: Apache-2.0

const general = require('./babel-general.cjs');
const plugins = require('./babel-plugins.cjs');
const presets = require('./babel-presets.cjs');

module.exports = {
...general,
plugins: plugins(false),
presets: presets(false)
};
15 changes: 15 additions & 0 deletions types/polkadot-dev-configs/babel-general.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2017-2023 @polkadot/dev authors & contributors
// SPDX-License-Identifier: Apache-2.0

module.exports = {
assumptions: {
// ensure that ?. & ?? uses !=/== null checks (not also undefined)
noDocumentAll: true,
// no extra defineProperty for private class fields
privateFieldsAsProperties: true,
// no extra defineProperty for public class fields
setPublicClassFields: true
}
// Really want to switch this on, but not as of yet...
// comments: false
};
21 changes: 21 additions & 0 deletions types/polkadot-dev-configs/babel-plugins.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2017-2023 @polkadot/dev authors & contributors
// SPDX-License-Identifier: Apache-2.0

const resolver = require('./babel-resolver.cjs');

module.exports = function (isEsm) {
return resolver([
'@babel/plugin-proposal-nullish-coalescing-operator',
'@babel/plugin-proposal-numeric-separator',
'@babel/plugin-proposal-optional-chaining',
['@babel/plugin-transform-runtime', {
useESModules: isEsm
}],
'@babel/plugin-syntax-bigint',
'@babel/plugin-syntax-dynamic-import',
'@babel/plugin-syntax-import-assertions',
'@babel/plugin-syntax-import-meta',
'@babel/plugin-syntax-top-level-await',
'babel-plugin-styled-components'
]);
};
33 changes: 33 additions & 0 deletions types/polkadot-dev-configs/babel-presets.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2017-2023 @polkadot/dev authors & contributors
// SPDX-License-Identifier: Apache-2.0

const resolver = require('./babel-resolver.cjs');

module.exports = function (isEsm) {
return resolver([
'@babel/preset-typescript',
['@babel/preset-react', {
development: false,
runtime: 'automatic'
}],
['@babel/preset-env', {
exclude: [
// we don't want 2n ** 128n to Math.pow(2n, 128n)
'@babel/plugin-transform-exponentiation-operator',
// we don't want await import(...) to Promise.resolve(require(...))
'proposal-dynamic-import'
],
modules: isEsm
? false
: 'commonjs',
targets: isEsm
? {
node: '14'
}
: {
browsers: '>0.25% and last 2 versions and not ie 11 and not OperaMini all',
node: '14'
}
}]
]);
};
14 changes: 14 additions & 0 deletions types/polkadot-dev-configs/babel-resolver.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2017-2023 @polkadot/dev authors & contributors
// SPDX-License-Identifier: Apache-2.0

module.exports = function resolver (input) {
return Array.isArray(input)
? input
.filter((plugin) => !!plugin)
.map((plugin) =>
Array.isArray(plugin)
? [require.resolve(plugin[0]), plugin[1]]
: require.resolve(plugin)
)
: require.resolve(input);
};
35 changes: 33 additions & 2 deletions types/scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright 2017-2020 @polkadot/dev authors & contributors
// SPDX-License-Identifier: Apache-2.0

const babel = require('@babel/cli/lib/babel/dir').default;
const path = require('path');
const mkdirp = require('mkdirp');
const { execSync } = require('child_process');
Expand Down Expand Up @@ -75,22 +76,52 @@ function copyMiscFiles(dir) {
fs.writeFileSync('./build/package.json', newPkgString);
}

// @param module - the module system to use cjs or esm.
async function buildBabel(dir, module = 'esm') {
console.log('build babel for: ', module);

// babel configuratiom
const configFileName = `babel-config-cjs.cjs`;

// Prefer to use local config over the root one.
const conf = path.join(process.cwd(), configFileName);

// Commonjs builds will exist in a '/cjs' directory for the package.
await babel({
babelOptions: {
configFile: conf
},
cliOptions: {
extensions: ['.ts'],
filenames: ['src'],
ignore: '**/*.d.ts',
outDir: path.join(process.cwd(), 'build'),
outFileExtension: '.js'
}
});

copyMiscFiles(dir, module);
}


async function buildJs(dir) {
if (!fs.existsSync(path.join(process.cwd(), '.skip-build'))) {
const { name, version } = require(path.join(process.cwd(), './package.json'));

console.log(`*** ${name} ${version}`);

mkdirp.sync('build');
copyMiscFiles();

await buildBabel(dir, 'cjs');

// copyMiscFiles();
}
}

async function buildMonorepo() {
executeSync('yarn clean');
executeSync('tsc --emitDeclarationOnly --outdir ./ts-types');
await buildJs('types');
copyMiscFiles();
}

async function main() {
Expand Down
Loading

0 comments on commit ed68aeb

Please sign in to comment.