352 lines
9.5 KiB
JavaScript
352 lines
9.5 KiB
JavaScript
const { execSync } = require('child_process');
|
|
const path = require('path');
|
|
const fse = require('fs-extra');
|
|
|
|
const webpack = require('webpack');
|
|
const webpackMerge = require('webpack-merge');
|
|
const webpackNodeExternals = require('webpack-node-externals');
|
|
const webpackNodeTerser = require('terser-webpack-plugin');
|
|
|
|
async function buildForProduction(args) {
|
|
const rootPath = path.join(__dirname, '..');
|
|
|
|
const projectName = args[0];
|
|
|
|
const projectPath = path.join(rootPath, 'projects', projectName);
|
|
|
|
const packageJson = require(path.join(projectPath, 'package.json'));
|
|
const ucapPackageJson = require(path.join(projectPath, 'ucap-package.json'));
|
|
|
|
const distPath = path.join(projectPath, ucapPackageJson.dest);
|
|
const docPath = path.join(projectPath, ucapPackageJson.docDest);
|
|
|
|
const webpackConfig = (overrideConfig, compilerOptions) => {
|
|
const commonConfig = {
|
|
name: projectName,
|
|
target: 'node',
|
|
mode: 'production',
|
|
context: path.join(projectPath, 'src'),
|
|
entry: path.join(projectPath, ucapPackageJson.lib.entryFile),
|
|
output: {
|
|
path: path.join(distPath, 'bundles'),
|
|
filename: `${projectName}.umd.js`,
|
|
libraryTarget: 'umd',
|
|
library: projectName,
|
|
umdNamedDefine: true
|
|
},
|
|
resolve: {
|
|
extensions: ['.js', '.ts', '.jsx', '.tsx', '.json'],
|
|
modules: ['node_modules', 'src']
|
|
},
|
|
externals: [webpackNodeExternals()],
|
|
devtool: 'source-map',
|
|
module: {
|
|
rules: [
|
|
{
|
|
test: /\.tsx?$/,
|
|
use: [
|
|
{
|
|
loader: 'ts-loader',
|
|
options: {
|
|
configFile: path.join(projectPath, 'tsconfig.lib.prod.json'),
|
|
compilerOptions: !!compilerOptions
|
|
? compilerOptions
|
|
: {
|
|
declaration: false,
|
|
module: 'ES2015',
|
|
target: 'es5'
|
|
}
|
|
}
|
|
}
|
|
],
|
|
exclude: /node_modules/
|
|
}
|
|
]
|
|
}
|
|
};
|
|
|
|
return webpackMerge(commonConfig, overrideConfig);
|
|
};
|
|
|
|
const clean = () => {
|
|
return new Promise((resolve, reject) => {
|
|
const _distPath = path.join(distPath);
|
|
console.log(`${projectName}: cleaning started [${_distPath}]`);
|
|
execSync(`rimraf ${_distPath}`, {
|
|
stdio: 'inherit'
|
|
});
|
|
console.log(`${projectName}: cleaning complete`);
|
|
resolve();
|
|
});
|
|
};
|
|
|
|
const genDeclaration = () => {
|
|
return new Promise((resolve, reject) => {
|
|
console.log(`${projectName}: generating of declaration started`);
|
|
execSync(
|
|
`tsc --project ${path.join(
|
|
projectPath,
|
|
'tsconfig.lib.prod.json'
|
|
)} --module commonjs --target es5 --outDir ${path.join(
|
|
distPath,
|
|
'lib'
|
|
)} --emitDeclarationOnly true --declarationDir ${path.join(distPath)}`,
|
|
{ stdio: 'inherit' }
|
|
);
|
|
console.log(`${projectName}: generating of declaration complete`);
|
|
resolve();
|
|
});
|
|
};
|
|
|
|
const genEs5 = () => {
|
|
return new Promise((resolve, reject) => {
|
|
console.log(`${projectName}: generating of es5 started`);
|
|
execSync(
|
|
`tsc --project ${path.join(
|
|
projectPath,
|
|
'tsconfig.lib.prod.json'
|
|
)} --module es2015 --target es5 --outDir ${path.join(
|
|
distPath,
|
|
'es5'
|
|
)} --declaration false --sourceMap false --inlineSourceMap true`,
|
|
{ stdio: 'inherit' }
|
|
);
|
|
console.log(`${projectName}: generating of es5 complete`);
|
|
resolve();
|
|
});
|
|
};
|
|
|
|
const genEs2015 = () => {
|
|
return new Promise((resolve, reject) => {
|
|
console.log(`${projectName}: generating of es2015 started`);
|
|
execSync(
|
|
`tsc --project ${path.join(
|
|
projectPath,
|
|
'tsconfig.lib.prod.json'
|
|
)} --module es2015 --target es2015 --outDir ${path.join(
|
|
distPath,
|
|
'es2015'
|
|
)} --declaration false --sourceMap false --inlineSourceMap true`,
|
|
{ stdio: 'inherit' }
|
|
);
|
|
console.log(`${projectName}: generating of es2015 complete`);
|
|
resolve();
|
|
});
|
|
};
|
|
|
|
const genBundles = () => {
|
|
return new Promise(async (resolve, reject) => {
|
|
console.log(`${projectName}: generating of bundle[umd] started`);
|
|
await new Promise((resolve, reject) => {
|
|
webpack(
|
|
webpackConfig({ optimization: { minimize: false } }),
|
|
(err, status) => {
|
|
if (!!err) {
|
|
console.err(err);
|
|
reject(err);
|
|
return;
|
|
}
|
|
|
|
resolve();
|
|
}
|
|
);
|
|
}).catch(reason => {
|
|
reject(reason);
|
|
});
|
|
|
|
await new Promise((resolve, reject) => {
|
|
webpack(
|
|
webpackConfig({
|
|
output: {
|
|
filename: `${projectName}.umd.min.js`
|
|
},
|
|
optimization: { minimizer: [new webpackNodeTerser()] }
|
|
}),
|
|
(err, status) => {
|
|
if (!!err) {
|
|
console.err(err);
|
|
reject(err);
|
|
return;
|
|
}
|
|
|
|
resolve();
|
|
}
|
|
);
|
|
}).catch(reason => {
|
|
reject(reason);
|
|
});
|
|
|
|
console.log(`${projectName}: generating of bundle[umd] complete`);
|
|
resolve();
|
|
});
|
|
};
|
|
|
|
const genFes5 = () => {
|
|
return new Promise(async (resolve, reject) => {
|
|
console.log(`${projectName}: generating of fes5 started`);
|
|
await new Promise((resolve, reject) => {
|
|
webpack(
|
|
webpackConfig(
|
|
{
|
|
output: {
|
|
path: path.join(distPath, 'fes5'),
|
|
filename: `${projectName}.js`,
|
|
library: projectName
|
|
},
|
|
optimization: { minimize: false }
|
|
},
|
|
{
|
|
declaration: false,
|
|
module: 'es2015',
|
|
target: 'es5'
|
|
}
|
|
),
|
|
(err, status) => {
|
|
if (!!err) {
|
|
console.err(err);
|
|
reject(err);
|
|
return;
|
|
}
|
|
|
|
resolve();
|
|
}
|
|
);
|
|
}).catch(reason => {
|
|
reject(reason);
|
|
});
|
|
|
|
console.log(`${projectName}: generating of fes5 complete`);
|
|
resolve();
|
|
});
|
|
};
|
|
|
|
const genFes2015 = () => {
|
|
return new Promise(async (resolve, reject) => {
|
|
console.log(`${projectName}: generating of fes2015 started`);
|
|
await new Promise((resolve, reject) => {
|
|
webpack(
|
|
webpackConfig(
|
|
{
|
|
output: {
|
|
path: path.join(distPath, 'fes2015'),
|
|
filename: `${projectName}.js`,
|
|
library: projectName
|
|
},
|
|
optimization: { minimize: false }
|
|
},
|
|
{
|
|
declaration: false,
|
|
module: 'es2015',
|
|
target: 'es2015'
|
|
}
|
|
),
|
|
(err, status) => {
|
|
if (!!err) {
|
|
console.err(err);
|
|
reject(err);
|
|
return;
|
|
}
|
|
|
|
resolve();
|
|
}
|
|
);
|
|
}).catch(reason => {
|
|
reject(reason);
|
|
});
|
|
|
|
console.log(`${projectName}: generating of fes2015 complete`);
|
|
resolve();
|
|
});
|
|
};
|
|
|
|
const genPackageJson = () => {
|
|
return new Promise((resolve, reject) => {
|
|
console.log(`${projectName}: generating of package.json started`);
|
|
const projectpackageJson = {
|
|
...packageJson,
|
|
main: `bundles/${projectName}.umd.js`,
|
|
module: `fes5/${projectName}.js`,
|
|
es2015: `fes2015/${projectName}.js`,
|
|
esm5: `es5/public-api.js`,
|
|
esm2015: `es2016/public-api.js`,
|
|
fesm5: `fes5/${projectName}.js`,
|
|
fesm2015: `fes2015/${projectName}.js`,
|
|
typings: `public-api.d.ts`,
|
|
sideEffects: false
|
|
};
|
|
|
|
fse.writeFileSync(
|
|
path.join(distPath, 'package.json'),
|
|
JSON.stringify(projectpackageJson)
|
|
);
|
|
|
|
console.log(`${projectName}: generating of package.json complete`);
|
|
resolve();
|
|
});
|
|
};
|
|
|
|
const installPackage = () => {
|
|
return new Promise((resolve, reject) => {
|
|
console.log(`${projectName}: installation for local started`);
|
|
process.chdir(path.join(distPath));
|
|
execSync(`npm pack`, { stdio: 'inherit' });
|
|
process.chdir(path.join(rootPath));
|
|
const projectVersion = require(path.join(distPath, 'package.json'))
|
|
.version;
|
|
|
|
execSync(
|
|
`npm install -D ${path.join(
|
|
distPath,
|
|
`ucap-electron-${projectName}-${projectVersion}.tgz`
|
|
)}`,
|
|
{
|
|
stdio: 'inherit'
|
|
}
|
|
);
|
|
|
|
execSync(
|
|
`rimraf ${path.join(
|
|
distPath,
|
|
`ucap-electron-${projectName}-${projectVersion}.tgz`
|
|
)}`,
|
|
{
|
|
stdio: 'inherit'
|
|
}
|
|
);
|
|
|
|
console.log(`${projectName}: installation for local complete`);
|
|
resolve();
|
|
});
|
|
};
|
|
|
|
const genDoc = () => {
|
|
return new Promise((resolve, reject) => {
|
|
console.log(`${projectName}: generating of doc started`);
|
|
execSync(`rimraf ${path.join(docPath)}`, {
|
|
stdio: 'inherit'
|
|
});
|
|
|
|
execSync(
|
|
`typedoc --out ${path.join(docPath)} ${path.join(projectPath, 'src')}`,
|
|
{ stdio: 'inherit' }
|
|
);
|
|
|
|
console.log(`${projectName}: generating of doc complete`);
|
|
resolve();
|
|
});
|
|
};
|
|
|
|
await clean();
|
|
await genDeclaration();
|
|
await genEs5();
|
|
await genEs2015();
|
|
await genBundles();
|
|
await genFes5();
|
|
await genFes2015();
|
|
await genPackageJson();
|
|
await installPackage();
|
|
await genDoc();
|
|
}
|
|
|
|
buildForProduction(process.argv.slice(2));
|