Skip to content

Commit

Permalink
fix(bug): fix issue on Windows with generating declaration maps
Browse files Browse the repository at this point in the history
  • Loading branch information
wessberg committed Nov 13, 2019
1 parent ca5aa78 commit 3e971bc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
15 changes: 10 additions & 5 deletions src/declaration/emit-declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
import {CompilerOptions, createPrinter} from "typescript";
import {OutputBundle, OutputOptions, PluginContext, SourceDescription} from "rollup";
import {ModuleDependencyMap} from "../util/module/get-module-dependencies";
import {setExtension} from "../util/path/path-util";
import {ensurePosix, setExtension} from "../util/path/path-util";
import {DECLARATION_EXTENSION, DECLARATION_MAP_EXTENSION, ROLLUP_PLUGIN_MULTI_ENTRY} from "../constant/constant";
import {TypescriptPluginOptions} from "../plugin/i-typescript-plugin-options";
import {Resolver} from "../util/resolve-id/resolver";
Expand Down Expand Up @@ -108,17 +108,22 @@ export function emitDeclarations(options: EmitDeclarationsOptions) {
? declarationMapFilename
: parse(rewrittenAbsoluteDeclarationMapFilename).base;

let emitFileDeclarationFilename = join(relative(relativeOutDir, relativeDeclarationOutDir), rewrittenDeclarationFilename);
let emitFileDeclarationMapFilename = join(relative(relativeOutDir, relativeDeclarationOutDir), rewrittenDeclarationMapFilename);
// We'll need to work with POSIX paths for now
let emitFileDeclarationFilename = ensurePosix(join(relative(relativeOutDir, relativeDeclarationOutDir), rewrittenDeclarationFilename));
let emitFileDeclarationMapFilename = ensurePosix(join(relative(relativeOutDir, relativeDeclarationOutDir), rewrittenDeclarationMapFilename));

// Rollup does not allow emitting files outside of the root of the whatever 'dist' directory that has been provided.
while (emitFileDeclarationFilename.startsWith("../")) {
while (emitFileDeclarationFilename.startsWith("../") || emitFileDeclarationFilename.startsWith("..\\")) {
emitFileDeclarationFilename = emitFileDeclarationFilename.slice("../".length);
}
while (emitFileDeclarationMapFilename.startsWith("../")) {
while (emitFileDeclarationMapFilename.startsWith("../") || emitFileDeclarationMapFilename.startsWith("..\\")) {
emitFileDeclarationMapFilename = emitFileDeclarationMapFilename.slice("../".length);
}

// Now, make sure to normalize the file names again
emitFileDeclarationFilename = normalize(emitFileDeclarationFilename);
emitFileDeclarationMapFilename = normalize(emitFileDeclarationMapFilename);

const rawLocalModuleNames = chunk.modules;
const localModuleNames = rawLocalModuleNames.filter(options.canEmitForFile);
const rawEntryFileName = rawLocalModuleNames.slice(-1)[0];
Expand Down
7 changes: 5 additions & 2 deletions src/declaration/pre-bundle-declarations-for-chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export function preBundleDeclarationsForChunk(options: PreBundleDeclarationsForC
program.emit(
undefined,
(file, data) => {
const replacedFile = normalize(file.replace(generatedOutDir, "").replace(options.cwd, ""));
const normalizedFile = normalize(file);
const replacedFile = normalize(normalizedFile.replace(generatedOutDir, "").replace(options.cwd, ""));
const replacedFileDir = normalize(dirname(replacedFile));

if (replacedFile.endsWith(DECLARATION_MAP_EXTENSION)) {
Expand All @@ -56,7 +57,9 @@ export function preBundleDeclarationsForChunk(options: PreBundleDeclarationsForC
const absoluteSource = join(options.absoluteDeclarationMapDirname, source);
const chunkFileNameResult = getChunkFilename({...options, fileName: absoluteSource});
return chunkFileNameResult != null && chunkFileNameResult.fileName === options.absoluteChunkFileName;
});
})
// Make sure that the paths are POSIX-based
.map(ensurePosix);

// If there are sources for this chunk, include it
if (parsedData.sources.length > 0) {
Expand Down

0 comments on commit 3e971bc

Please sign in to comment.