Skip to content

Commit

Permalink
fix(next): copy cmp assets to correct directory
Browse files Browse the repository at this point in the history
Closes #2120
  • Loading branch information
adamdbradley committed Jan 8, 2020
1 parent 5d98de5 commit 8887579
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 35 deletions.
9 changes: 4 additions & 5 deletions src/compiler_next/output-targets/copy/assets-copy-tasks.ts
Expand Up @@ -2,7 +2,7 @@ import * as d from '../../../declarations';
import { normalizePath } from '@utils';


export function getComponentAssetsCopyTasks(config: d.Config, buildCtx: d.BuildCtx, dest: string, collectionsPath: boolean) {
export const getComponentAssetsCopyTasks = (config: d.Config, buildCtx: d.BuildCtx, dest: string, collectionsPath: boolean) => {
if (!dest) {
return [];
}
Expand Down Expand Up @@ -43,10 +43,9 @@ export function getComponentAssetsCopyTasks(config: d.Config, buildCtx: d.BuildC
buildCtx.debug(`getComponentAssetsCopyTasks: ${copyTasks.length}`);

return copyTasks;
}
};


export function canSkipAssetsCopy(config: d.Config, compilerCtx: d.CompilerCtx, entryModules: d.EntryModule[], filesChanged: string[]) {
export const canSkipAssetsCopy = (config: d.Config, compilerCtx: d.CompilerCtx, entryModules: d.EntryModule[], filesChanged: string[]) => {
if (!compilerCtx.hasSuccessfulBuild) {
// always copy assets if we haven't had a successful build yet
// cannot skip build
Expand Down Expand Up @@ -85,4 +84,4 @@ export function canSkipAssetsCopy(config: d.Config, compilerCtx: d.CompilerCtx,
});

return shouldSkipAssetsCopy;
}
};
4 changes: 2 additions & 2 deletions src/compiler_next/output-targets/copy/hashed-copy.ts
@@ -1,6 +1,6 @@
import * as d from '../../../declarations';

export async function generateHashedCopy(config: d.Config, compilerCtx: d.CompilerCtx, path: string) {
export const generateHashedCopy = async (config: d.Config, compilerCtx: d.CompilerCtx, path: string) => {
try {
const content = await compilerCtx.fs.readFile(path);
const hash = await config.sys.generateContentHash(content, config.hashedFileNameLength);
Expand All @@ -12,4 +12,4 @@ export async function generateHashedCopy(config: d.Config, compilerCtx: d.Compil
return hashedFileName;
} catch (e) {}
return undefined;
}
};
9 changes: 4 additions & 5 deletions src/compiler_next/output-targets/copy/local-copy-tasks.ts
@@ -1,14 +1,13 @@
import * as d from '../../../declarations';

export function getSrcAbsPath(config: d.Config, src: string) {
export const getSrcAbsPath = (config: d.Config, src: string) => {
if (config.sys.path.isAbsolute(src)) {
return src;
}
return config.sys.path.join(config.srcDir, src);
}
};


export function getDestAbsPath(config: d.Config, src: string, destAbsPath: string, destRelPath: string) {
export const getDestAbsPath = (config: d.Config, src: string, destAbsPath: string, destRelPath: string) => {
if (destRelPath) {
if (config.sys.path.isAbsolute(destRelPath)) {
return destRelPath;
Expand All @@ -23,4 +22,4 @@ export function getDestAbsPath(config: d.Config, src: string, destAbsPath: strin
}

return destAbsPath;
}
};
17 changes: 8 additions & 9 deletions src/compiler_next/output-targets/copy/output-copy.ts
Expand Up @@ -6,7 +6,7 @@ import { isOutputTargetCopy } from '../../../compiler/output-targets/output-util
import minimatch from 'minimatch';


export async function outputCopy(config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx) {
export const outputCopy = async (config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx) => {
const outputTargets = config.outputTargets.filter(isOutputTargetCopy);
if (outputTargets.length === 0) {
return;
Expand Down Expand Up @@ -49,9 +49,9 @@ export async function outputCopy(config: d.Config, compilerCtx: d.CompilerCtx, b
}
timespan.finish(`copy finished (${copiedFiles} file${copiedFiles === 1 ? '' : 's'})`);
}
}
};

function getCopyTasks(config: d.Config, buildCtx: d.BuildCtx, o: d.OutputTargetCopy, changedFiles: string[]) {
const getCopyTasks = (config: d.Config, buildCtx: d.BuildCtx, o: d.OutputTargetCopy, changedFiles: string[]) => {
if (!Array.isArray(o.copy)) {
return [];
}
Expand All @@ -60,10 +60,9 @@ function getCopyTasks(config: d.Config, buildCtx: d.BuildCtx, o: d.OutputTargetC
: filterCopyTasks(config, o.copy, changedFiles);

return copyTasks.map(t => transformToAbs(config, t, o.dir));
}
};


function filterCopyTasks(config: d.Config, tasks: d.CopyTask[], changedFiles: string[]) {
const filterCopyTasks = (config: d.Config, tasks: d.CopyTask[], changedFiles: string[]) => {
if (Array.isArray(tasks)) {
return tasks.filter(copy => {
let copySrc = copy.src;
Expand All @@ -83,13 +82,13 @@ function filterCopyTasks(config: d.Config, tasks: d.CopyTask[], changedFiles: st
});
}
return [];
}
};

function transformToAbs(config: d.Config, copyTask: d.CopyTask, dest: string): Required<d.CopyTask> {
const transformToAbs = (config: d.Config, copyTask: d.CopyTask, dest: string): Required<d.CopyTask> => {
return {
src: copyTask.src,
dest: getDestAbsPath(config, copyTask.src, dest, copyTask.dest),
keepDirStructure: typeof copyTask.keepDirStructure === 'boolean' ? copyTask.keepDirStructure : copyTask.dest == null,
warn: copyTask.warn !== false
};
}
};
19 changes: 11 additions & 8 deletions src/compiler_next/transpile/run-program.ts
Expand Up @@ -6,7 +6,7 @@ import { getComponentsFromModules, isOutputTargetDistTypes } from '../../compile
import { resolveComponentDependencies } from '../../compiler/entries/resolve-component-dependencies';
import { updateComponentBuildConditionals } from '../build/app-data';
import { updateModule } from './static-to-meta/parse-static';
import path from 'path';
import { join, relative } from 'path';
import ts from 'typescript';


Expand All @@ -25,14 +25,17 @@ export const runTsProgram = async (config: d.Config, compilerCtx: d.CompilerCtx,
const tsProgram = tsBuilder.getProgram();
const tsTypeChecker = tsProgram.getTypeChecker();
const typesOutputTarget = config.outputTargets.filter(isOutputTargetDistTypes);
const emitCallback: ts.WriteFileCallback = (filepath, data, _w, _e, tsSourceFiles) => {
filepath = filepath.replace(config.cacheDir, '');
if (filepath.endsWith('.js')) {
updateModule(config, compilerCtx, buildCtx, tsSourceFiles[0], data, filepath, tsTypeChecker, null);
} else if (filepath.endsWith('.d.ts')) {

const emitCallback: ts.WriteFileCallback = (emitFilePath, data, _w, _e, tsSourceFiles) => {
if (emitFilePath.endsWith('.js')) {
updateModule(config, compilerCtx, buildCtx, tsSourceFiles[0], data, emitFilePath, tsTypeChecker, null);

} else if (emitFilePath.endsWith('.d.ts')) {
const relativeEmitFilepath = emitFilePath.replace(config.cacheDir, '');

typesOutputTarget.forEach(o => {
compilerCtx.fs.writeFile(
path.join(o.typesDir, filepath),
join(o.typesDir, relativeEmitFilepath),
data
);
});
Expand Down Expand Up @@ -98,7 +101,7 @@ const validateUniqueTagNames = (config: d.Config, buildCtx: d.BuildCtx) => {
const err = buildError(buildCtx.diagnostics);
err.header = `Component Tag Name "${tagName}" Must Be Unique`;
err.messageText = `Please update the components so "${tagName}" is only used once: ${
cmpsWithTagName.map(c => path.relative(config.rootDir, c.sourceFilePath)).join(' ')
cmpsWithTagName.map(c => relative(config.rootDir, c.sourceFilePath)).join(' ')
}`;
}
});
Expand Down
16 changes: 10 additions & 6 deletions src/compiler_next/transpile/static-to-meta/parse-static.ts
@@ -1,11 +1,11 @@
import * as d from '../../../declarations';
import { normalizePath } from '@utils';
import { parseCallExpression } from '../../../compiler/transformers/static-to-meta/call-expression';
import { parseImport } from './import';
import { parseStaticComponentMeta } from '../../../compiler/transformers/static-to-meta/component';
import { parseStringLiteral } from '../../../compiler/transformers/static-to-meta/string-literal';
import path from 'path';
import { dirname, basename, join } from 'path';
import ts from 'typescript';
import { normalizePath } from '@utils';


export const updateModule = (
Expand All @@ -14,18 +14,22 @@ export const updateModule = (
buildCtx: d.BuildCtx,
tsSourceFile: ts.SourceFile,
sourceFileText: string,
emitFilepath: string,
emitFilePath: string,
typeChecker: ts.TypeChecker,
collection: d.CollectionCompilerMeta
) => {
const sourceFilePath = normalizePath(tsSourceFile.fileName);
const prevModuleFile = getModule(compilerCtx, sourceFilePath);

if (prevModuleFile && prevModuleFile.staticSourceFileText === sourceFileText) {
return prevModuleFile;
}

const dirPath = path.dirname(sourceFilePath);
const moduleFile = createModule(tsSourceFile, sourceFileText, emitFilepath);
const srcDirPath = dirname(sourceFilePath);
const emitFileName = basename(emitFilePath);
emitFilePath = normalizePath(join(srcDirPath, emitFileName));

const moduleFile = createModule(tsSourceFile, sourceFileText, emitFilePath);
compilerCtx.moduleMap.set(moduleFile.sourceFilePath, moduleFile);
compilerCtx.changedModules.add(moduleFile.sourceFilePath);

Expand All @@ -34,7 +38,7 @@ export const updateModule = (
parseStaticComponentMeta(config, compilerCtx, typeChecker, node, moduleFile, compilerCtx.nodeMap);
return;
} else if (ts.isImportDeclaration(node)) {
parseImport(config, compilerCtx, buildCtx, moduleFile, dirPath, node);
parseImport(config, compilerCtx, buildCtx, moduleFile, srcDirPath, node);
return;
} else if (ts.isCallExpression(node)) {
parseCallExpression(moduleFile, node);
Expand Down

0 comments on commit 8887579

Please sign in to comment.