New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: webpack support #11346
Merged
Merged
feat: webpack support #11346
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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'); | ||
janvennemann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}).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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}); | ||
} | ||
}); | ||
}; | ||
|
||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
const EventEmitter = require('events'); | ||
|
||
/** | ||
* Creates a simplified promise-based client for Appcd | ||
* | ||
* @param {object} client A appcd-client instance | ||
* @return {object} Promisified wrapper around appcd-client | ||
*/ | ||
const appcd = (client) => ({ | ||
host: client.host, | ||
port: client.port, | ||
async connect(options) { | ||
return new Promise((resolve, reject) => { | ||
client.connect(options) | ||
.once('connected', resolve) | ||
.once('error', reject); | ||
}); | ||
}, | ||
disconnect() { | ||
client.disconnect(); | ||
}, | ||
async get(path) { | ||
return this.request({ path }); | ||
}, | ||
async post(path, data = {}) { | ||
return this.request({ path, data }); | ||
}, | ||
async request(options) { | ||
return new Promise((resolve, reject) => { | ||
client | ||
.request(options) | ||
.once('response', response => resolve(response)) | ||
.once('error', e => reject(e)); | ||
}); | ||
}, | ||
async subscribe(path) { | ||
return new Promise((resolve, reject) => { | ||
const subscription = new Subscription(path); | ||
client | ||
.request({ | ||
path, | ||
type: 'subscribe' | ||
}) | ||
.on('response', (data, response) => { | ||
if (typeof data === 'string' && data === 'Subscribed') { | ||
subscription.sid = response.sid; | ||
return resolve(subscription); | ||
} | ||
|
||
subscription.emit('message', data); | ||
}) | ||
.once('close', () => { | ||
subscription.emit('close'); | ||
}) | ||
.once('finish', () => { | ||
subscription.emit('close'); | ||
}) | ||
.once('error', e => { | ||
if (!subscription.sid) { | ||
reject(e); | ||
} else { | ||
subscription.emit('error', e); | ||
} | ||
}); | ||
}); | ||
} | ||
}); | ||
|
||
class Subscription extends EventEmitter { | ||
constructor(path) { | ||
super(); | ||
this.path = path; | ||
this.sid = null; | ||
} | ||
async unsubscribe() { | ||
if (!this.sid) { | ||
return; | ||
} | ||
|
||
await appcd.request({ | ||
path: this.path, | ||
sid: this.sid | ||
}); | ||
this.sid = null; | ||
} | ||
} | ||
|
||
module.exports = appcd; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems potentially problematic on windows...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
execSync
starts a new shell on windows and directly passes the command to that shell, so this shouldn't be an issue. I tested this on my Window 10 machine and works, but i'll ask QE to double check.