awesome-cordova-plugins/scripts/tasks/build-es5.ts

92 lines
2.5 KiB
TypeScript
Raw Normal View History

import { readJSONSync, writeFileSync } from 'fs-extra';
2021-09-28 02:32:20 +08:00
import { resolve } from 'path';
import * as TerserPlugin from 'terser-webpack-plugin';
2017-12-28 20:28:44 +08:00
import * as unminifiedPlugin from 'unminified-webpack-plugin';
import { Configuration, DefinePlugin, ProvidePlugin, webpack } from 'webpack';
2018-03-23 17:57:54 +08:00
2017-12-28 20:28:44 +08:00
import { ROOT } from '../build/helpers';
2020-05-16 20:40:49 +08:00
import { cleanEmittedData, EMIT_PATH, InjectableClassEntry } from '../build/transformers/extract-injectables';
2017-12-29 12:15:34 +08:00
import { Logger } from '../logger';
2017-12-28 20:28:44 +08:00
2021-09-28 02:32:20 +08:00
const DIST = resolve(ROOT, 'dist');
const INDEX_PATH = resolve(DIST, 'index.js');
const INJECTABLE_CLASSES = readJSONSync(EMIT_PATH).map((item: InjectableClassEntry) => {
2020-05-16 20:40:49 +08:00
item.file =
'./' +
item.file
.split(/[\/\\]+/)
.slice(-4, -1)
.join('/');
return item;
});
2017-12-28 20:28:44 +08:00
const webpackConfig: Configuration = {
2018-03-19 20:13:55 +08:00
mode: 'production',
2017-12-28 20:28:44 +08:00
entry: INDEX_PATH,
devtool: 'source-map',
target: 'web',
output: {
path: DIST,
filename: 'awesome-cordova-plugins.min.js',
2017-12-28 20:28:44 +08:00
},
resolve: {
modules: ['node_modules'],
extensions: ['.js'],
alias: {
2021-09-28 02:32:20 +08:00
'@awesome-cordova-plugins/core': resolve(DIST, '@awesome-cordova-plugins/core/index.js'),
2020-05-16 20:40:49 +08:00
},
2017-12-28 20:28:44 +08:00
},
module: {
2018-03-23 17:57:54 +08:00
rules: [
{
test: /\.js$/,
2021-09-28 02:32:20 +08:00
use: resolve(ROOT, 'scripts/build/remove-tslib-helpers.js'),
2020-05-16 20:40:49 +08:00
},
],
2017-12-28 20:28:44 +08:00
},
plugins: [
new ProvidePlugin({
2020-05-16 20:40:49 +08:00
__extends: ['tslib', '__extends'],
2017-12-28 20:28:44 +08:00
}),
new DefinePlugin({
2020-05-16 20:40:49 +08:00
'process.env.NODE_ENV': JSON.stringify('production'),
2017-12-28 20:28:44 +08:00
}),
2020-05-16 20:40:49 +08:00
new unminifiedPlugin(),
],
optimization: {
minimize: true,
minimizer: [new TerserPlugin()],
},
2017-12-28 20:28:44 +08:00
};
function getPluginImport(entry: InjectableClassEntry) {
2018-03-23 17:57:54 +08:00
return `import { ${entry.className} } from '${entry.file}';`;
2017-12-28 20:28:44 +08:00
}
function createIndexFile() {
let fileContent = '';
fileContent += INJECTABLE_CLASSES.map(getPluginImport).join('\n');
fileContent += `\nwindow.IonicNative = {\n`;
2021-09-28 04:09:05 +08:00
fileContent += INJECTABLE_CLASSES.map((e) => e.className).join(',\n');
2017-12-28 20:28:44 +08:00
fileContent += '\n};\n';
fileContent += `require('./@awesome-cordova-plugins/core/bootstrap').checkReady();\n`;
fileContent += `require('./@awesome-cordova-plugins/core/ng1').initAngular1(window.IonicNative);`;
2017-12-28 20:28:44 +08:00
writeFileSync(INDEX_PATH, fileContent, { encoding: 'utf-8' });
2017-12-28 20:28:44 +08:00
}
function compile() {
2017-12-29 12:15:34 +08:00
Logger.profile('build-es5');
2017-12-28 20:28:44 +08:00
webpack(webpackConfig, (err, stats) => {
2018-06-23 00:24:09 +08:00
Logger.profile('build-es5');
2017-12-29 12:15:34 +08:00
if (err) Logger.error('Error occurred while compiling with Webpack', err);
else {
Logger.info('Compiled ES5 file with Webpack successfully.');
}
2017-12-28 20:40:57 +08:00
cleanEmittedData();
2017-12-28 20:28:44 +08:00
});
}
createIndexFile();
compile();