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

83 lines
2.5 KiB
TypeScript
Raw Normal View History

2017-12-28 20:28:44 +08:00
import * as fs from 'fs-extra';
import * as path from 'path';
import * as webpack from 'webpack';
import * as uglifyJsPlugin from 'uglifyjs-webpack-plugin';
import * as unminifiedPlugin from 'unminified-webpack-plugin';
import { cleanEmittedData, EMIT_PATH, InjectableClassEntry } from '../build/transformers/extract-injectables';
import { ROOT } from '../build/helpers';
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');
const INJECTABLE_CLASSES = fs.readJSONSync(EMIT_PATH).map((item: InjectableClassEntry) => {
2017-12-28 20:40:57 +08:00
item.file = './' + item.file.split(/[\/\\]+/).slice(-4, -1).join('/');
2017-12-28 20:28:44 +08:00
return item;
});
const webpackConfig: webpack.Configuration = {
entry: INDEX_PATH,
devtool: 'source-map',
target: 'web',
output: {
path: DIST,
filename: 'ionic-native.min.js'
},
resolve: {
modules: ['node_modules'],
extensions: ['.js'],
alias: {
2017-12-29 11:12:48 +08:00
'@ionic-native/core': path.resolve(DIST, '@ionic-native/core/index.js')
2017-12-28 20:28:44 +08:00
}
},
module: {
rules: [{
test: /\.js$/,
use: path.resolve(ROOT, 'scripts/build/remove-tslib-helpers.js')
}]
},
plugins: [
new webpack.ProvidePlugin({
'__extends': ['tslib', '__extends']
}),
new webpack.optimize.OccurrenceOrderPlugin(true),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production')
}),
new uglifyJsPlugin({
sourceMap: true
2017-12-29 11:12:48 +08:00
}),
2017-12-29 12:15:34 +08:00
new unminifiedPlugin()
2017-12-28 20:28:44 +08:00
]
};
function getPluginImport(entry: InjectableClassEntry) {
return `import { ${ entry.className } } from '${ entry.file }';`;
}
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) => {
2017-12-29 12:43:29 +08:00
Logger.profile('build-es5', { level: 'verbose' });
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();