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

92 lines
2.5 KiB
TypeScript
Raw Permalink Normal View History

2017-12-28 20:28:44 +08:00
import * as fs from 'fs-extra';
import * as path from 'path';
import * as uglifyJsPlugin from 'uglifyjs-webpack-plugin';
import * as unminifiedPlugin from 'unminified-webpack-plugin';
2018-03-23 17:57:54 +08:00
import * as webpack from 'webpack';
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
const DIST = path.resolve(ROOT, 'dist');
const INDEX_PATH = path.resolve(DIST, 'index.js');
2020-05-16 20:40:49 +08:00
const INJECTABLE_CLASSES = fs.readJSONSync(EMIT_PATH).map((item: InjectableClassEntry) => {
item.file =
'./' +
item.file
.split(/[\/\\]+/)
.slice(-4, -1)
.join('/');
return item;
});
2017-12-28 20:28:44 +08:00
const webpackConfig: webpack.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,
2020-05-16 20:40:49 +08:00
filename: 'ionic-native.min.js',
2017-12-28 20:28:44 +08:00
},
resolve: {
modules: ['node_modules'],
extensions: ['.js'],
alias: {
2020-05-16 20:40:49 +08:00
'@ionic-native/core': path.resolve(DIST, '@ionic-native/core/index.js'),
},
2017-12-28 20:28:44 +08:00
},
module: {
2018-03-23 17:57:54 +08:00
rules: [
{
test: /\.js$/,
2020-05-16 20:40:49 +08:00
use: path.resolve(ROOT, 'scripts/build/remove-tslib-helpers.js'),
},
],
2017-12-28 20:28:44 +08:00
},
plugins: [
new webpack.ProvidePlugin({
2020-05-16 20:40:49 +08:00
__extends: ['tslib', '__extends'],
2017-12-28 20:28:44 +08:00
}),
new webpack.optimize.OccurrenceOrderPlugin(true),
new webpack.DefinePlugin({
2020-05-16 20:40:49 +08:00
'process.env.NODE_ENV': JSON.stringify('production'),
2017-12-28 20:28:44 +08:00
}),
new uglifyJsPlugin({
2020-05-16 20:40:49 +08:00
sourceMap: true,
2017-12-29 11:12:48 +08:00
}),
2020-05-16 20:40:49 +08:00
new unminifiedPlugin(),
],
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`;
fileContent += INJECTABLE_CLASSES.map(e => e.className).join(',\n');
fileContent += '\n};\n';
2017-12-28 20:40:57 +08:00
fileContent += `require('./@ionic-native/core/bootstrap').checkReady();\n`;
fileContent += `require('./@ionic-native/core/ng1').initAngular1(window.IonicNative);`;
2017-12-28 20:28:44 +08:00
fs.writeFileSync(INDEX_PATH, fileContent, { encoding: 'utf-8' });
}
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();