forked from tidev/titanium-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.js
177 lines (159 loc) · 5.35 KB
/
webpack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// eslint-disable-next-line security/detect-child-process
const { execSync } = require('child_process');
const fs = require('fs-extra');
const path = require('path');
const semver = require('semver');
const util = require('util');
require('colors');
const appcd = require('../lib/webpack/appcd');
const WebpackService = require('../lib/webpack/service');
const MIN_APPCD_VERSION = '3.2.0';
const createWebpackLogger = (logger) => {
const webpackLogger = {
formatWithBadge(args) {
return `${' WEBPACK '.bgCyan.black} ${util.format(...args)}`;
}
};
[ 'info', 'debug', 'warn', 'error' ].forEach(level => {
webpackLogger[level] = (...args) => logger[level](webpackLogger.formatWithBadge(args));
});
return webpackLogger;
};
exports.id = 'ti.webpack';
exports.init = (logger, config, cli) => {
const isAppcCli = typeof process.env.APPC_ENV !== 'undefined';
let commandName;
let isWebpackEnabled = false;
let projectType;
let client;
cli.on('cli:command-loaded', (hookData) => {
const command = hookData.command;
commandName = command.name;
});
cli.on('cli:post-validate', () => {
projectType = getWebpackProjectType(cli.argv['project-dir']);
if (projectType === null) {
return;
}
isWebpackEnabled = true;
process.env.TI_USE_WEBPACK = true;
if (commandName !== 'build') {
return;
}
let appcdRootPath;
if (isAppcCli) {
// we were invoked by appc-cli, load bundled daemon client.
appcdRootPath = resolveModuleRoot('appcd');
if (!fs.existsSync(appcdRootPath)) {
throw new Error('Unable to find Appcelerator Daemon inside the appc-cli. Please make sure to use a recent appc-cli version. You can install/select it with "appc use latest".');
}
} else {
// plain ti-cli, load daemon from global modules
const globalModulesPath = execSync('npm root -g').toString().replace(/\s+$/, '');
appcdRootPath = resolveModuleRoot('appcd', [ globalModulesPath ]);
if (!fs.existsSync(appcdRootPath)) {
throw new Error('Unable to find global Appcelerator Daemon install. You can install it with "npm i appcd -g".');
}
}
ensureAppcdVersion(appcdRootPath, MIN_APPCD_VERSION, isAppcCli);
const AppcdClient = resolveAppcdClient(appcdRootPath);
const baseClient = new AppcdClient();
if (isAppcCli) {
baseClient.appcd = path.resolve(appcdRootPath, '..', '.bin', 'appcd');
if (process.platform === 'win32') {
baseClient.appcd += '.cmd';
}
}
client = appcd(baseClient);
});
cli.on('build.pre.compile', {
priority: 800,
post(builder, callback) {
if (!isWebpackEnabled) {
return callback();
}
builder.useWebpack = true;
const badgedLogger = createWebpackLogger(logger);
const webpackService = new WebpackService({
client,
logger: badgedLogger,
cli,
builder,
projectType
});
Promise.resolve().then(async () => {
await webpackService.build();
return callback();
}).catch(e => {
if (e.status === 404) {
badgedLogger.info('Daemon was unable to find the Webpack plugin. To continue you need to:');
badgedLogger.info('');
const installCommand = 'npm i -g @appcd/webpack-plugin';
badgedLogger.info(`- Install it with ${installCommand.cyan}`);
const restartCommand = `${isAppcCli ? 'appc ' : ''}appcd restart`;
badgedLogger.info(`- Restart the daemon with ${restartCommand.cyan}`);
badgedLogger.info('');
}
if (cli.argv.platform === 'ios') {
// The iOS build shows the error message only, log the stack manually
// to make sure it get printed as well
logger.error(e.stack);
}
callback(e);
});
}
});
};
function getWebpackProjectType(projectDir) {
if (typeof projectDir !== 'string') {
return null;
}
const pkgPath = path.join(projectDir, 'package.json');
if (!fs.existsSync(pkgPath)) {
return null;
}
const tiPlugins = [
'@titanium-sdk/webpack-plugin-classic',
'@titanium-sdk/webpack-plugin-alloy',
'@titanium-sdk/webpack-plugin-vue',
'@titanium-sdk/webpack-plugin-angular'
];
// eslint-disable-next-line security/detect-non-literal-require
const pkg = require(pkgPath);
const allDeps = Object.keys(pkg.devDependencies || {})
.concat(Object.keys(pkg.dependencies || {}));
const pluginId = tiPlugins.find(id => allDeps.includes(id));
if (!pluginId) {
return null;
}
return pluginId.substring(pluginId.lastIndexOf('-') + 1);
}
function resolveModuleRoot(name, paths) {
try {
let resolvedPath;
if (paths) {
resolvedPath = require.resolve(name, { paths });
} else {
resolvedPath = require.resolve(name);
}
return resolvedPath.substr(0, resolvedPath.indexOf(`${path.sep}appcd${path.sep}`) + 7);
} catch (e) {
return null;
}
}
function ensureAppcdVersion(appcdRootPath, version, isAppcCli = false) {
// eslint-disable-next-line security/detect-non-literal-require
const pkg = require(path.join(appcdRootPath, 'package.json'));
if (!semver.gte(pkg.version, version)) {
throw new Error(`The Webpack build system requires Appcelerator Daemon v${MIN_APPCD_VERSION}+ (installed: ${pkg.version}). Please update your ${isAppcCli ? 'appc-cli with "appc use latest"' : 'global daemon install with "npm i appcd -g"'}.`);
}
}
function resolveAppcdClient(appcdPackagePath) {
const appcdClientPath = require.resolve('appcd-client', {
paths: [
path.join(appcdPackagePath, 'node_modules')
]
});
// eslint-disable-next-line security/detect-non-literal-require
return require(appcdClientPath).default;
}