Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions lib/after-watch.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const compiler = require('./compiler');
const { stopWebpackCompiler } = require('./compiler');

module.exports = function($logger) {
const webpackProcess = compiler.getWebpackProcess();
if (webpackProcess) {
$logger.info("Stopping webpack watch");
webpackProcess.kill("SIGINT");
}
$logger.info("Stopping webpack watch");
stopWebpackCompiler();
}
9 changes: 5 additions & 4 deletions lib/before-cleanApp.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const { cleanSnapshotArtefacts } = require("../snapshot/android/project-snapshot-generator");
const { isAndroid } = require("../projectHelpers");
const { getWebpackProcess } = require("./compiler");
const { getWebpackProcesses } = require("./compiler");

module.exports = function (hookArgs) {
return (args, originalMethod) => {
const webpackProcess = getWebpackProcess();
const promise = webpackProcess ? Promise.resolve() : originalMethod(...args);
const platform = hookArgs.platformInfo.platform;
const webpackProcesses = getWebpackProcesses();
const promise = webpackProcesses[platform] ? Promise.resolve() : originalMethod(...args);
return promise.then(() => {
if (isAndroid(hookArgs.platformInfo.platform)) {
if (isAndroid(platform)) {
cleanSnapshotArtefacts(hookArgs.platformInfo.projectData.projectDir);
}
});
Expand Down
21 changes: 21 additions & 0 deletions lib/before-preview-sync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { runWebpackCompiler } = require("./compiler");

module.exports = function($logger, $liveSyncService, hookArgs) {
const { config } = hookArgs;
const bundle = config && config.appFilesUpdaterOptions && config.appFilesUpdaterOptions.bundle;
if (bundle) {
const env = config.env || {};
const platform = config.platform;
const release = config && config.appFilesUpdaterOptions && config.appFilesUpdaterOptions.release;
const compilerConfig = {
env,
platform,
bundle,
release,
watch: true
};

return runWebpackCompiler(compilerConfig, hookArgs.projectData, $logger, $liveSyncService, hookArgs);
}
}

50 changes: 30 additions & 20 deletions lib/before-watch.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
const { runWebpackCompiler } = require("./compiler");
const { getWebpackProcesses, runWebpackCompiler, stopWebpackCompiler } = require("./compiler");

module.exports = function ($logger, $liveSyncService, $options, hookArgs) {
if (hookArgs.config) {
const appFilesUpdaterOptions = hookArgs.config.appFilesUpdaterOptions;
if (appFilesUpdaterOptions.bundle) {
const platforms = hookArgs.config.platforms;
return Promise.all(platforms.map(platform => {
const env = hookArgs.config.env || {};
env.hmr = !!$options.hmr;
const config = {
env,
platform,
bundle: appFilesUpdaterOptions.bundle,
release: appFilesUpdaterOptions.release,
watch: true
};
module.exports = function ($logger, $liveSyncService, $options, $devicesService, hookArgs) {
if (hookArgs.config) {
const appFilesUpdaterOptions = hookArgs.config.appFilesUpdaterOptions;
if (appFilesUpdaterOptions.bundle) {
$liveSyncService.on("liveSyncStopped", data => {
const webpackProcesses = getWebpackProcesses();
Object.keys(webpackProcesses).forEach(platform => {
const devices = $devicesService.getDevicesForPlatform(platform);
if (!devices || !devices.length) {
stopWebpackCompiler(platform);
}
});
});

return runWebpackCompiler(config, hookArgs.projectData, $logger, $liveSyncService, hookArgs);
}));
}
}
const platforms = hookArgs.config.platforms;
return Promise.all(platforms.map(platform => {
const env = hookArgs.config.env || {};
env.hmr = !!$options.hmr;
const config = {
env,
platform,
bundle: appFilesUpdaterOptions.bundle,
release: appFilesUpdaterOptions.release,
watch: true
};

return runWebpackCompiler(config, hookArgs.projectData, $logger, $liveSyncService, hookArgs);
}));
}
}
}
41 changes: 32 additions & 9 deletions lib/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ const { buildEnvData, debuggingEnabled } = require("./utils");

let hasBeenInvoked = false;

let webpackProcess = null;
let webpackProcesses = {};
let hasLoggedSnapshotWarningMessage = false;

exports.getWebpackProcess = function getWebpackProcess() {
return webpackProcess;
exports.getWebpackProcesses = function getWebpackProcess() {
return webpackProcesses;
}

exports.runWebpackCompiler = function runWebpackCompiler(config, $projectData, $logger, $liveSyncService, hookArgs) {
if (config.bundle) {
return new Promise(function (resolveBase, rejectBase) {
if (webpackProcess) {
if (webpackProcesses[config.platform]) {
return resolveBase();
}

Expand All @@ -43,6 +43,10 @@ exports.runWebpackCompiler = function runWebpackCompiler(config, $projectData, $
env["sourceMap"] = true;
}

if (hookArgs && hookArgs.externals) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it's a good idea to add a comment here that the externals are passed when we use the tns preview command only?

env.externals = hookArgs.externals;
}

const envData = buildEnvData($projectData, platform, env);
const envParams = buildEnvCommandLineParams(config, envData, $logger);

Expand Down Expand Up @@ -77,22 +81,27 @@ exports.runWebpackCompiler = function runWebpackCompiler(config, $projectData, $

if (hookArgs.filesToSync && hookArgs.startSyncFilesTimeout) {
hookArgs.filesToSync.push(...message.emittedFiles);
hookArgs.startSyncFilesTimeout();
hookArgs.startSyncFilesTimeout(platform);
}

if (hookArgs.filesToSyncMap && hookArgs.startSyncFilesTimeout) {
hookArgs.filesToSyncMap[platform] = message.emittedFiles;
hookArgs.startSyncFilesTimeout(platform);
}
}
}

if (config.watch) {
childProcess.on("message", resolveOnWebpackCompilationComplete);
if (webpackProcess) {
if (webpackProcesses[platform]) {
throw new Error("Webpack process already spawned.");
}
webpackProcess = childProcess;
webpackProcesses[platform] = childProcess;
}

childProcess.on("close", code => {
if (webpackProcess == childProcess) {
webpackProcess = null;
if (webpackProcesses[platform] === childProcess) {
delete webpackProcesses[platform];
}
if (code === 0) {
resolve();
Expand All @@ -106,6 +115,14 @@ exports.runWebpackCompiler = function runWebpackCompiler(config, $projectData, $
}
}

exports.stopWebpackCompiler = function stopWebpackCompiler(platform) {
if (platform) {
stopWebpackForPlatform(platform);
} else {
Object.keys(webpackProcesses).forEach(platform => stopWebpackForPlatform(platform));
}
}

function buildEnvCommandLineParams(config, envData, $logger) {
const envFlagNames = Object.keys(envData);
const snapshotEnvIndex = envFlagNames.indexOf("snapshot");
Expand Down Expand Up @@ -136,3 +153,9 @@ function logSnapshotWarningMessage($logger) {
}
}

function stopWebpackForPlatform(platform) {
const webpackProcess = webpackProcesses[platform];
webpackProcess.kill("SIGINT");
delete webpackProcesses[platform];
}

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
"type": "after-prepare",
"script": "lib/after-prepare.js",
"inject": true
},
{
"type": "before-preview-sync",
"script": "lib/before-preview-sync",
"inject": true
}
]
},
Expand Down
4 changes: 3 additions & 1 deletion templates/webpack.angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ module.exports = env => {
uglify, // --env.uglify
report, // --env.report
sourceMap, // --env.sourceMap
hmr, // --env.hmr
hmr, // --env.hmr,
externals
} = env;

const appFullPath = resolve(projectRoot, appPath);
Expand All @@ -63,6 +64,7 @@ module.exports = env => {
const config = {
mode: uglify ? "production" : "development",
context: appFullPath,
externals,
watchOptions: {
ignored: [
appResourcesFullPath,
Expand Down
4 changes: 3 additions & 1 deletion templates/webpack.javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ module.exports = env => {
uglify, // --env.uglify
report, // --env.report
sourceMap, // --env.sourceMap
hmr, // --env.hmr
hmr, // --env.hmr,
externals
} = env;

const appFullPath = resolve(projectRoot, appPath);
Expand All @@ -52,6 +53,7 @@ module.exports = env => {
const config = {
mode: uglify ? "production" : "development",
context: appFullPath,
externals,
watchOptions: {
ignored: [
appResourcesFullPath,
Expand Down
2 changes: 2 additions & 0 deletions templates/webpack.typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ module.exports = env => {
report, // --env.report
sourceMap, // --env.sourceMap
hmr, // --env.hmr
externals
} = env;

const appFullPath = resolve(projectRoot, appPath);
Expand All @@ -52,6 +53,7 @@ module.exports = env => {
const config = {
mode: uglify ? "production" : "development",
context: appFullPath,
externals,
watchOptions: {
ignored: [
appResourcesFullPath,
Expand Down