Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

fix: tell the {N} CLI to ignore the source dir when watching for changes #586

Merged
merged 3 commits into from
Jun 25, 2018
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
25 changes: 4 additions & 21 deletions lib/before-watchPatterns.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,19 @@
const { basename } = require("path");
const {
buildEnvData,
getCompilationContext,
} = require("./utils");
const { getAppPathFromProjectData } = require("../projectHelpers");

module.exports = function (hookArgs) {
const { liveSyncData } = hookArgs;
if (!liveSyncData || !liveSyncData.bundle) {
return;
}

const { platforms } = hookArgs;
const { env } = liveSyncData;
return (args, originalMethod) => {
return originalMethod(...args).then(originalPatterns => {
if (!platforms || !platforms.length) {
throw new Error("Target platform should be specified!");
}

const compilationContexts = platforms.map(platform =>
getContext(hookArgs.projectData, platform, env));
const appPath = getAppPathFromProjectData(hookArgs.projectData);
const ignorePattern = `!${appPath}`;

const ignorePatterns = compilationContexts.map(
context => `!${context}`
);

return [...originalPatterns, ...ignorePatterns];
return [...originalPatterns, ignorePattern];
});
};
}

function getContext(projectData, platform, env) {
const fullEnvData = buildEnvData(projectData, platform, env);
return getCompilationContext(projectData.projectDir, fullEnvData);
}
11 changes: 3 additions & 8 deletions lib/compiler.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const utils = require("./utils");
const { spawn } = require("child_process");
const { join, resolve: pathResolve } = require("path");
const { resolve: pathResolve } = require("path");
const { existsSync } = require("fs");
const readline = require("readline");

const { messages } = require("../plugins/WatchStateLoggerPlugin");
const { buildEnvData, getCompilationContext } = require("./utils");
const { buildEnvData } = require("./utils");

let hasBeenInvoked = false;

Expand Down Expand Up @@ -84,12 +84,7 @@ exports.runWebpackCompiler = function runWebpackCompiler(config, $projectData, $
}

if (hookArgs.filesToSync && hookArgs.startSyncFilesTimeout) {
const compilationContext = getCompilationContext(projectDir, envData);
hookArgs.filesToSync.push(
...message.emittedFiles.map(
emittedFile => join(projectDir, compilationContext, emittedFile)
)
);
hookArgs.filesToSync.push(...message.emittedFiles);
hookArgs.startSyncFilesTimeout();
}
}
Expand Down
11 changes: 0 additions & 11 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const {
getAppPathFromProjectData,
getAppResourcesPathFromProjectData,
getProjectDir,
getWebpackConfig,
isAndroid,
} = require("../projectHelpers");

Expand All @@ -25,15 +24,6 @@ function buildEnvData($projectData, platform, env) {
return envData;
}

function getCompilationContext(projectDir, env) {
const config = getWebpackConfig(projectDir, env);
const { context } = config;

return context ?
path.relative(projectDir, context) :
".";
}

function shouldSnapshot(config) {
const platformSupportsSnapshot = isAndroid(config.platform);
const osSupportsSnapshot = os.type() !== "Windows_NT";
Expand All @@ -43,6 +33,5 @@ function shouldSnapshot(config) {

module.exports = {
buildEnvData,
getCompilationContext,
shouldSnapshot
};
4 changes: 3 additions & 1 deletion plugins/WatchStateLoggerPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { join } from "path";

export enum messages {
compilationComplete = "Webpack compilation complete.",
Expand Down Expand Up @@ -31,7 +32,8 @@ export class WatchStateLoggerPlugin {

const emittedFiles = Object
.keys(compilation.assets)
.filter(assetKey => compilation.assets[assetKey].emitted);
.filter(assetKey => compilation.assets[assetKey].emitted)
.map(file => join(compiler.context, file));

process.send && process.send(messages.compilationComplete, error => null);
// Send emitted files so they can be LiveSynced if need be
Expand Down
25 changes: 0 additions & 25 deletions projectHelpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const { resolve } = require("path");
const { readFileSync, writeFileSync } = require("fs");
const { EOL } = require("os");

const hook = require("nativescript-hook")(__dirname);

Expand Down Expand Up @@ -28,29 +27,6 @@ const isAngular = ({ projectDir, packageJson } = {}) => {
.some(dependency => /^@angular\b/.test(dependency));
};

const getWebpackConfig = (projectDir, env, configPath = "webpack.config.js") => {
const configAbsolutePath = resolve(projectDir, configPath);
let config;

try {
config = require(configAbsolutePath);
} catch (e) {
throw new Error(
`Couldn't load webpack config from ${configAbsolutePath}. ` +
`Original error:${EOL}${e}`
);
}
if (typeof config === "function") {
config = config(env);
}

if (!config) {
throw new Error(`Webpack config from ${configAbsolutePath} is empty!`);
}

return config;
};

const getPackageJson = projectDir => {
const packageJsonPath = getPackageJsonPath(projectDir);
return JSON.parse(readFileSync(packageJsonPath, "utf8"));
Expand Down Expand Up @@ -95,7 +71,6 @@ module.exports = {
getAppResourcesPathFromProjectData,
getPackageJson,
getProjectDir,
getWebpackConfig,
isAndroid,
isIos,
isAngular,
Expand Down