feat(webpack):transfer webpack.config.js and bundle script to typescript

dev
neeboo 5 years ago
parent 6daa1120e0
commit 6d416b260a
  1. 10
      package.json
  2. 32
      scripts/babel/babel.browser.config.js
  3. 31
      scripts/babel/babel.server.config.js
  4. 18
      scripts/babel/babel.src.config.js
  5. 19
      scripts/babel/babel.test.config.js
  6. 9
      scripts/packages.js
  7. 9
      scripts/packagesList.js
  8. 166
      scripts/rollup/bundleBrowser.js
  9. 49
      scripts/rollup/bundleServer.js
  10. 13
      scripts/rollup/getDependencies.js
  11. 24
      scripts/webpack.ts
  12. 22
      scripts/webpack/packagesForWP.ts
  13. 29
      scripts/webpack/webpack.config.ts

@ -6,14 +6,15 @@
],
"scripts": {
"bootstrap": "lerna bootstrap && yarn build:ts",
"watch":"yarn build:ts -w",
"watch": "yarn build:ts -w",
"build": "yarn packages:clean && yarn build:ts",
"build:ts": "tsc -b tsconfig.json",
"build:test":"tsc -b tsconfig.test.json",
"build:e2e":"tsc -b tsconfig.e2e.json",
"build:test": "tsc -b tsconfig.test.json",
"build:e2e": "tsc -b tsconfig.e2e.json",
"bundle": "ts-node -P scripts/tsconfig.json scripts/bundle.ts umd,esm",
"bundle:webpack": "rm -rf dist && cross-env NODE_ENV=production ts-node -P scripts/tsconfig.json scripts/webpack.ts",
"clean": "lerna clean --yes && lerna run clean && rimraf includes && yarn packages:clean",
"dist": "yarn packages:bundler && rm -rf dist && cross-env NODE_ENV=production webpack --config webpack.config.js",
"dist": "yarn packages:bundler && yarn bundle:webpack",
"packages:clean": "gulp cleanServer && gulp cleanUnexpected",
"packages:bundler": "yarn packages:clean && yarn build:ts && ts-node -P scripts/tsconfig.json scripts/bundle.ts",
"schema": "ts-node -P scripts/tsconfig.json scripts/typings/schema.ts core",
@ -86,6 +87,7 @@
"jest-fetch-mock": "^1.6.6",
"jest-json-schema": "^2.0.1",
"jest-watch-typeahead": "^0.2.0",
"jsdoc": "^3.6.3",
"lerna": "^3.2.1",
"mitt": "^1.1.3",
"mkdirp": "^0.5.1",

@ -1,32 +0,0 @@
export default {
babelrc: false,
runtimeHelpers: true,
presets: [
[
'@babel/env',
{
modules: false,
targets: {
browsers: ['>0.25%'],
},
useBuiltIns: 'usage',
},
],
'@babel/typescript',
],
plugins: [
['@babel/transform-runtime'],
'@babel/proposal-object-rest-spread',
'@babel/proposal-export-default-from',
'@babel/proposal-export-namespace-from',
[
'@babel/plugin-proposal-decorators',
{
legacy: true,
},
],
'@babel/proposal-class-properties',
'add-module-exports',
],
exclude: 'packages/**/node_modules/**',
};

@ -1,31 +0,0 @@
export default {
babelrc: false,
// runtimeHelpers: true,
presets: [
[
'@babel/env',
{
modules: false,
targets: {
node: 'current'
}
}
]
],
plugins: [
// ['@babel/transform-runtime'],
'@babel/proposal-object-rest-spread',
'@babel/proposal-export-default-from',
'@babel/proposal-export-namespace-from',
[
'@babel/plugin-proposal-decorators',
{
legacy: true
}
],
'@babel/proposal-class-properties',
'add-module-exports'
],
exclude: 'packages/**/node_modules/**'
}

@ -1,18 +0,0 @@
module.exports = {
babelrc: false,
presets: ['@babel/env'],
plugins: [
['@babel/transform-runtime'],
'@babel/proposal-object-rest-spread',
'@babel/proposal-export-default-from',
'@babel/proposal-export-namespace-from',
[
'@babel/plugin-proposal-decorators',
{
legacy: true
}
],
'@babel/proposal-class-properties',
'add-module-exports'
]
}

@ -1,19 +0,0 @@
module.exports = {
babelrc: false,
presets: ['@babel/env'],
plugins: [
['@babel/transform-runtime'],
'@babel/proposal-object-rest-spread',
'@babel/proposal-export-default-from',
'@babel/proposal-export-namespace-from',
[
'@babel/plugin-proposal-decorators',
{
legacy: true
}
],
'@babel/proposal-class-properties',
'add-module-exports'
],
sourceMaps: 'inline'
}

@ -1,9 +0,0 @@
export default [
'harmony-core',
'harmony-utils',
'harmony-crypto',
'harmony-account',
'harmony-contract',
'harmony-network',
'harmony-transaction',
];

@ -1,9 +0,0 @@
module.exports = [
{ name: 'HarmonyJs', dest: 'harmony-core' },
{ name: 'HarmonyNetwork', dest: 'harmony-network' },
{ name: 'HarmonyUtils', dest: 'harmony-utils' },
{ name: 'HarmonyCrypto', dest: 'harmony-crypto' },
{ name: 'HarmonyContract', dest: 'harmony-contract' },
{ name: 'HarmonyAccount', dest: 'harmony-account' },
{ name: 'HarmonyTransaction', dest: 'harmony-transaction' },
];

@ -1,166 +0,0 @@
import * as path from 'path';
import * as fs from 'fs';
import * as rollup from 'rollup';
import alias from 'rollup-plugin-alias';
import babel from 'rollup-plugin-babel';
import commonjs from 'rollup-plugin-commonjs';
import license from 'rollup-plugin-license';
import json from 'rollup-plugin-json';
import globals from 'rollup-plugin-node-globals';
import resolve from 'rollup-plugin-node-resolve';
import typescript2 from 'rollup-plugin-typescript2';
import camelCase from 'camelcase';
import ts from 'typescript';
import packages from '../packages';
import browserConfig from '../babel/babel.browser.config.js';
import { getKeys } from './getDependencies';
import {
rootPath,
includesPath,
packagesPath,
projects,
preProcessFunc,
preProcessProjects,
} from '../projects';
async function bundles() {
await preProcessFunc(preProcessProjects);
for (const pkg of projects) {
const externals = projects
.filter((p) => p.name !== pkg.name)
.map((p) => p.scopedName);
const base = {
input: path.join(pkg.src, 'index.ts'),
plugins: [
alias({
elliptic: path.resolve(
__dirname,
'../',
'includes/elliptic/elliptic.js',
),
}),
resolve({
browser: true,
jsnext: true,
preferBuiltins: true,
}),
// babel(browserConfig),
globals(),
commonjs(),
json(),
typescript2({
typescript: ts, // ensure we're using the same typescript (3.x) for rollup as for regular builds etc
tsconfig: `${packagesPath}/${p.name}/tsconfig.json`,
tsconfigOverride: {
module: 'esnext',
stripInternal: true,
emitDeclarationOnly: false,
composite: false,
declaration: false,
declarationMap: false,
sourceMap: true,
},
}),
license({
banner: `Test Banner`,
}),
],
external: projects
.filter((p) => p.name !== pkg.name)
.map((p) => p.scopedName)
.concat(['cross-fetch']),
};
const pkgBundler = await rollup.rollup(base);
await pkgBundler.write({
file: pkg.esm,
name: pkg.globalName,
format: 'esm',
sourcemap: true,
});
await bundle.write({
file: pkg.umd,
exports: 'named',
name: pkg.globalName,
globals: {
...projects.reduce((g, pkg) => {
g[pkg.scopedName] = pkg.globalName;
return g;
}, {}),
tslib: 'tslib',
},
format: 'umd',
sourcemap: true,
});
}
// return projects.map((p) => {
// const external = getKeys(p.name).filter(
// (pkg) => !pkg.startsWith('@harmony'),
// );
// const externalSetting = getKeys(p.name).length > 0 ? { external } : {};
// const externalObject = external.reduce((g, pkg) => {
// g[`${pkg}`] = !pkg.startsWith('@harmony')
// ? pkg
// : `@harmony/${p.name.replace('harmony-', '')}`;
// return g;
// }, {});
// const normal = {
// input: `packages/${p.name}/src/index.ts`,
// plugins: [
// alias({
// elliptic: path.resolve(
// __dirname,
// '../',
// 'includes/elliptic/elliptic.js',
// ),
// }),
// resolve({
// browser: true,
// jsnext: true,
// preferBuiltins: true,
// }),
// // babel(browserConfig),
// globals(),
// commonjs(),
// json(),
// typescript2({
// typescript: ts, // ensure we're using the same typescript (3.x) for rollup as for regular builds etc
// tsconfig: `${packagesPath}/${p.name}/tsconfig.json`,
// tsconfigOverride: {
// module: 'esnext',
// stripInternal: true,
// emitDeclarationOnly: false,
// composite: false,
// declaration: false,
// declarationMap: false,
// sourceMap: true,
// },
// }),
// license({
// banner: `Test Banner`,
// }),
// ],
// output: {
// file: `packages/${p.name}/dist/index.js`,
// exports: 'named',
// format: 'umd',
// sourcemap: true,
// name: camelCase(p.name),
// globals: {
// ...externalObject,
// tslib: 'tslib',
// },
// },
// };
// // return normal;
// return Object.assign(normal, externalSetting);
// });
}
export default bundles();

@ -1,49 +0,0 @@
import babel from 'rollup-plugin-babel'
import json from 'rollup-plugin-json'
import license from 'rollup-plugin-license'
import vuedoc from 'vuepress-jsdoc/cmds/index'
import packages from '../packages'
import serverConfig from '../babel/babel.server.config.js'
import { getKeys } from './getDependencies'
function bundles() {
return packages.map(p => {
const external = getKeys(p)
const externalSetting = getKeys(p).length > 0 ? { external } : {}
vuedoc.generate({
source: `packages/${p}/src`,
dist: `packages/${p}`,
folder: 'doc',
readme: `packages/${p}/README.md`
})
const normal = {
input: `packages/${p}/src/index.js`,
output: {
file: `packages/${p}/node/index.js`,
format: 'umd',
name: 'Laksa'
},
plugins: [
babel(serverConfig),
json(),
license({
banner: `This source code is being disclosed to you solely for the purpose of your participation in
testing Zilliqa and Laksa. You may view, compile and run the code for that purpose and pursuant to
the protocols and algorithms that are programmed into, and intended by, the code. You may
not do anything else with the code without express permission from Zilliqa Research Pte. Ltd.,
including modifying or publishing the code (or any part of it), and developing or forming
another public or private blockchain network. This source code is provided as is and no
warranties are given as to title or non-infringement, merchantability or fitness for purpose
and, to the extent permitted by law, all liability for your use of the code is disclaimed.
Some programs in this code are governed by the GNU General Public License v3.0 (available at
https://www.gnu.org/licenses/gpl-3.0.en.html) (‘GPLv3’). The programs that are governed by
GPLv3.0 are those programs that are located in the folders src/depends and tests/depends
and which include a reference to GPLv3 in their program files.`
})
]
}
return Object.assign(normal, externalSetting)
})
}
export default bundles()

@ -1,13 +0,0 @@
import fs from 'fs';
export function getKeys(p) {
const packageJsonFile = `${process.cwd()}/packages/${p}/package.json`;
const data = fs.readFileSync(packageJsonFile, 'utf-8');
const { dependencies } = JSON.parse(data);
const keys = dependencies
? Object.keys(dependencies) //.filter((d) => !/harmony/.test(d))
: [];
return keys;
}

@ -0,0 +1,24 @@
// tslint:disable-next-line: no-implicit-dependencies
import webpack from 'webpack';
// tslint:disable-next-line: no-implicit-dependencies
import batch from './webpack/webpack.config';
webpack(batch).run((err, stats) => {
if (err) {
console.error(err);
return;
}
console.log(
stats.toString({
all: false,
modules: false,
errors: true,
warnings: false,
moduleTrace: true,
errorDetails: true,
chunks: true,
colors: true,
}),
);
});

@ -0,0 +1,22 @@
// tslint:disable-next-line: no-implicit-dependencies
import camelCase from 'camelcase';
import {packages} from '../packagesTs';
export interface PackageItem {
name: string;
dest: string;
}
function generatePackageList(rawArray: string[]): PackageItem[] {
const resultArray: PackageItem[] = [];
for (const item of rawArray) {
const name = camelCase(item)
.replace('har', 'Har')
.replace('Core', 'Js');
const dest = item;
resultArray.push({name, dest});
}
return resultArray;
}
export const packageList = generatePackageList(packages);

@ -1,9 +1,16 @@
const path = require('path');
const UglifyJs = require('uglifyjs-webpack-plugin');
const packagesSettings = require('./scripts/packagesList');
import path from 'path';
// tslint:disable-next-line: no-implicit-dependencies
import UglifyJs from 'uglifyjs-webpack-plugin';
// tslint:disable-next-line: no-implicit-dependencies
import {Configuration} from 'webpack';
function createBatchConfig(list) {
return list.map((l) => {
// // tslint:disable-next-line: no-implicit-dependencies
// import {createVariants} from 'parallel-webpack';
import {packageList, PackageItem} from './packagesForWP';
function createBatchConfig(list: PackageItem[]) {
return list.map((l: PackageItem) => {
const entryBase = {};
entryBase[l.name] = [`./packages/${l.dest}/dist/index.js`];
@ -49,8 +56,9 @@ function createBatchConfig(list) {
output: {
libraryTarget: 'umd',
library: `${l.name}`,
filename: '[name].browser.js',
path: path.join(__dirname, 'dist'),
filename: `${l.name}.browser.js`,
// filename: '[name].browser.js',
path: path.join(__dirname, '../../', 'dist'),
},
};
@ -58,10 +66,11 @@ function createBatchConfig(list) {
});
}
function reduceDimension(arr) {
function reduceDimension(arr: any[]) {
return Array.prototype.concat.apply([], arr);
}
const batch = reduceDimension(createBatchConfig(packagesSettings));
const batch: Configuration = reduceDimension(createBatchConfig(packageList));
module.exports = batch;
// tslint:disable-next-line: no-default-export
export default batch;
Loading…
Cancel
Save