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 all commits
Commits
File filter

Filter by extension

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);
}));
}
}
}
42 changes: 33 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,11 @@ exports.runWebpackCompiler = function runWebpackCompiler(config, $projectData, $
env["sourceMap"] = true;
}

// Currently externals param is passed only from before-preview-sync hook. This hook is triggered only when `tns preview --bundle` command is executed
if (hookArgs && hookArgs.externals) {
env.externals = hookArgs.externals;
}

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

Expand Down Expand Up @@ -77,22 +82,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 +116,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 +154,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
6 changes: 5 additions & 1 deletion templates/webpack.angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ module.exports = env => {
uglify, // --env.uglify
report, // --env.report
sourceMap, // --env.sourceMap
hmr, // --env.hmr
hmr, // --env.hmr,
} = env;
const externals = (env.externals || []).map((e) => { // --env.externals
return new RegExp(e + ".*");
});

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

const appFullPath = resolve(projectRoot, appPath);
const appResourcesFullPath = resolve(projectRoot, appResourcesPath);
Expand All @@ -52,6 +55,7 @@ module.exports = env => {
const config = {
mode: uglify ? "production" : "development",
context: appFullPath,
externals,
watchOptions: {
ignored: [
appResourcesFullPath,
Expand Down
6 changes: 5 additions & 1 deletion templates/webpack.typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ module.exports = env => {
uglify, // --env.uglify
report, // --env.report
sourceMap, // --env.sourceMap
hmr, // --env.hmr
hmr, // --env.hmr,
} = env;
const externals = (env.externals || []).map((e) => { // --env.externals
return new RegExp(e + ".*");
});

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