Skip to content

Commit

Permalink
feat: automatically externalize titanium modules
Browse files Browse the repository at this point in the history
  • Loading branch information
janvennemann committed Oct 23, 2019
1 parent b0cd689 commit d4ccb82
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
9 changes: 6 additions & 3 deletions src/WebpackJob.js
Expand Up @@ -77,7 +77,7 @@ export default class WebpackJob extends EventEmitter {
}

setOptions(newOptions) {
this.options = Object.assign({}, { env: {} }, newOptions);
this.options = Object.assign({}, { modules: [] }, newOptions);

const { platform, projectPath, type } = this.options;
this.name = projectPath.split('/').pop();
Expand All @@ -95,9 +95,12 @@ export default class WebpackJob extends EventEmitter {

const args = [
path.resolve(__dirname, './tasks/build.js'),
'--project', this.options.projectPath,
'--platform', this.options.platform
'--project', this.projectPath,
'--platform', this.platform,
];
for (const module of this.options.modules) {
args.push('-m', module.id);
}
if (this.options.watch) {
args.push('--watch');
}
Expand Down
23 changes: 11 additions & 12 deletions src/config/base.js
Expand Up @@ -4,7 +4,12 @@ const FriendlyErrorsPlugin = require('@soda/friendly-errors-webpack-plugin');
const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const webpack = require('webpack');
const { GenerateAppJsPlugin, PlatformAwareFileSystemPlugin, titaniumTarget } = require('webpack-target-titanium');
const {
GenerateAppJsPlugin,
PlatformAwareFileSystemPlugin,
titaniumTarget,
TitaniumExternalsPlugins
} = require('webpack-target-titanium');

const { generateTranspileDepRegex } = require('../utils');
const { ApiTrackerPlugin, StateNotifierPlugin } = require('../webpack');
Expand Down Expand Up @@ -33,17 +38,7 @@ module.exports = function (api, options) {
.filename('[name].js')
.libraryTarget('commonjs2')
.globalObject('global')
.end()
.externals(
(context, request, callback) => {
// TODO: automatically externalize used native modules
if (request === './app' || request === 'com.appcelerator.aca') {
return callback(null, `commonjs ${request}`);
}

callback();
}
);
.end();

// babel-loader ------------------------------------------------------------

Expand Down Expand Up @@ -159,6 +154,10 @@ module.exports = function (api, options) {
'process.env.TARGET_PLATFORM': JSON.stringify(platformName)
}
]);
config.plugin('titanium-externals')
.use(TitaniumExternalsPlugins, [
options.modules
]);
config.plugin('app.js')
.use(GenerateAppJsPlugin, [
[ 'main' ]
Expand Down
20 changes: 20 additions & 0 deletions src/webpack/plugins/TitaniumExternalsPlugin.js
@@ -0,0 +1,20 @@
'use strict';

const ExternalsPlugin = require('webpack/lib/ExternalsPlugin');

class TitaniumExternalsPlugin {
constructor(modules) {
this.modules = modules;
}

apply(compiler) {
const externals = [
'./app.js',
'com.appcelerator.aca',
...this.modules
];
new ExternalsPlugin('commonjs', externals).apply(compiler);
}
}

module.exports = TitaniumExternalsPlugin;

0 comments on commit d4ccb82

Please sign in to comment.