Skip to content

Commit

Permalink
add babel and create cjs build (#324)
Browse files Browse the repository at this point in the history
  • Loading branch information
salman01zp authored Apr 28, 2023
1 parent b98fb70 commit a38d860
Show file tree
Hide file tree
Showing 10 changed files with 2,069 additions and 406 deletions.
2 changes: 1 addition & 1 deletion types/babel-config-cjs.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const polkadotBabelConfig = require('@polkadot/dev/config/babel-config-cjs.cjs');
const polkadotBabelConfig = require('./polkadot-dev-configs/babel-config-cjs.cjs');

module.exports = {
plugins: [
Expand Down
40 changes: 29 additions & 11 deletions types/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@webb-tools/protocol-substrate-types",
"version": "0.0.8",
"version": "0.0.10",
"description": "Polkadot.js type definitions required for interacting with Webb's substrate protocol",
"main": "./build/index.js",
"author": "Webb Developers <drew@webb.tools>",
Expand All @@ -16,14 +16,32 @@
},
"homepage": "https://github.com/webb-tools/protocol-substrate",
"dependencies": {
"@babel/cli": "^7.21.0",
"@babel/core": "^7.21.3",
"@babel/runtime": "^7.19.0",
"@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.1.4",
"@polkadot/dev": "^0.72.34",
"@polkadot/typegen": "10.1.4",
"@polkadot/types": "10.1.4",
"@polkadot/api-derive": "10.3.2",
"@polkadot/dev": "^0.72.42",
"@polkadot/typegen": "10.3.2",
"@polkadot/types": "10.3.2",
"fs-extra": "^11.1.1",
"glob2base": "^0.0.12",
"minimatch": "^7.4.2",
Expand All @@ -32,9 +50,9 @@
"devDependencies": {
"@types/websocket": "^1.0.0",
"rimraf": "3.0.2",
"ts-node": "10.8.0",
"tsconfig-paths": "^3.14.1",
"typescript": "4.7.2",
"ts-node": "10.9.1",
"tsconfig-paths": "^4.2.0",
"typescript": "5.0.4",
"websocket": "^1.0.31"
},
"scripts": {
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);
};
32 changes: 31 additions & 1 deletion types/scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,45 @@ 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');

await buildBabel(dir, 'cjs');

copyMiscFiles();
// copyMiscFiles();
}
}

Expand Down
1 change: 1 addition & 0 deletions types/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"scripts",
"node_modules/**/*",
"**/node_modules/**/*",
"polkadot-dev-configs"
],
"ts-node": {
"files": true
Expand Down
Loading

0 comments on commit a38d860

Please sign in to comment.