Skip to content

Commit

Permalink
fix(windows): fixes issues with building and generating declarations …
Browse files Browse the repository at this point in the history
…on Windows. Fixes #19, #21. Fixes issue with transitive barrel exports. Fixes #10
  • Loading branch information
wessberg committed Jun 6, 2019
1 parent 59ce5b3 commit d41f915
Show file tree
Hide file tree
Showing 44 changed files with 634 additions and 317 deletions.
35 changes: 24 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"clean": "npm run clean:dist",
"lint": "tsc --noEmit && tslint -c tslint.json --project tsconfig.json",
"prettier": "prettier --write '{src,test,documentation}/**/*.{js,ts,json,html,xml,css,md}'",
"test": "ava **/*.test.* --fail-fast",
"prebuild": "npm run clean:dist",
"build": "npm run rollup",
"watch": "npm run rollup -- --watch",
Expand Down Expand Up @@ -48,15 +49,17 @@
],
"license": "MIT",
"devDependencies": {
"@wessberg/scaffold": "^1.0.17",
"@wessberg/ts-config": "^0.0.39",
"@wessberg/scaffold": "^1.0.18",
"@wessberg/ts-config": "^0.0.40",
"standard-changelog": "^2.0.11",
"@wessberg/rollup-plugin-ts": "^1.1.53",
"tslint": "^5.16.0",
"@wessberg/rollup-plugin-ts": "^1.1.54",
"tslint": "^5.17.0",
"prettier": "^1.17.1",
"pretty-quick": "^1.11.0",
"husky": "^2.3.0",
"np": "^5.0.2"
"husky": "^2.4.0",
"np": "^5.0.3",
"ts-node": "8.2.0",
"ava": "2.0.0"
},
"dependencies": {
"@babel/core": "^7.4.5",
Expand All @@ -70,17 +73,18 @@
"@babel/preset-env": "^7.4.5",
"@babel/runtime": "^7.4.5",
"@types/mkdirp": "^0.5.2",
"@types/node": "^12.0.3",
"@types/node": "^12.0.6",
"@types/resolve": "0.0.8",
"@wessberg/browserslist-generator": "1.0.20",
"@wessberg/browserslist-generator": "1.0.21",
"@wessberg/stringutil": "^1.0.18",
"browserslist": "^4.6.1",
"find-up": "^4.0.0",
"magic-string": "^0.25.2",
"mkdirp": "^0.5.1",
"resolve": "^1.11.0",
"rollup": "^1.12.4",
"rollup-pluginutils": "^2.7.1",
"resolve": "^1.11.1",
"rollup": "^1.14.3",
"rollup-pluginutils": "^2.8.1",
"slash": "^3.0.0",
"tslib": "^1.9.3",
"typescript": "^3.5.1"
},
Expand All @@ -105,5 +109,14 @@
"hooks": {
"pre-commit": "pretty-quick --staged"
}
},
"ava": {
"compileEnhancements": false,
"extensions": [
"ts"
],
"require": [
"ts-node/register"
]
}
}
2 changes: 2 additions & 0 deletions src/plugin/i-typescript-plugin-options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {ParsedCommandLine, CustomTransformers, CompilerOptions} from "typescript";
import {IBabelInputOptions} from "./i-babel-options";
import {CustomTransformersFunction} from "../util/merge-transformers/i-custom-transformer-options";
import {FileSystem} from "../util/file-system/file-system";

export type Transpiler = "typescript" | "babel";

Expand Down Expand Up @@ -30,6 +31,7 @@ export interface ITypescriptPluginBaseOptions {
include: string[] | string;
exclude: string[] | string;
transpileOnly?: boolean;
fileSystem: FileSystem;
}

export interface ITypescriptPluginTypescriptOptions extends ITypescriptPluginBaseOptions {
Expand Down
38 changes: 27 additions & 11 deletions src/plugin/typescript-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {emitDeclarations} from "../util/emit-declarations/emit-declarations";
import {emitDiagnosticsThroughRollup} from "../util/diagnostic/emit-diagnostics-through-rollup";
import {getSupportedExtensions} from "../util/get-supported-extensions/get-supported-extensions";
import {ensureRelative, getExtension, isBabelHelper, isRollupPluginMultiEntry} from "../util/path/path-util";
import {join} from "path";
import {ModuleResolutionHost} from "../service/module-resolution-host/module-resolution-host";
import {takeBundledFilesNames} from "../util/take-bundled-filenames/take-bundled-filenames";
import {TypescriptPluginOptions} from "./i-typescript-plugin-options";
Expand Down Expand Up @@ -39,6 +38,7 @@ import {getOutDir} from "../util/get-out-dir/get-out-dir";
import {GetParsedCommandLineResult} from "../util/get-parsed-command-line/get-parsed-command-line-result";
import {takeBrowserslistOrComputeBasedOnCompilerOptions} from "../util/take-browserslist-or-compute-based-on-compiler-options/take-browserslist-or-compute-based-on-compiler-options";
import {matchAll} from "@wessberg/stringutil";
import {join, normalize} from "path";

/**
* The name of the Rollup plugin
Expand All @@ -55,7 +55,7 @@ export default function typescriptRollupPlugin(pluginInputOptions: Partial<Types
const {include, exclude, tsconfig, cwd, browserslist} = pluginOptions;
const transformers = pluginOptions.transformers == null ? [] : ensureArray(pluginOptions.transformers);
// Make sure to normalize the received Browserslist
const normalizedBrowserslist = getBrowserslist({browserslist, cwd});
const normalizedBrowserslist = getBrowserslist({browserslist, cwd, fileSystem: pluginOptions.fileSystem});

/**
* The ParsedCommandLine to use with Typescript
Expand Down Expand Up @@ -109,7 +109,7 @@ export default function typescriptRollupPlugin(pluginInputOptions: Partial<Types
* The ResolveCache to use
* @type {ResolveCache}
*/
const resolveCache: IResolveCache = new ResolveCache();
const resolveCache: IResolveCache = new ResolveCache({fileSystem: pluginOptions.fileSystem});

/**
* The filter function to use
Expand Down Expand Up @@ -158,8 +158,9 @@ export default function typescriptRollupPlugin(pluginInputOptions: Partial<Types
// Make sure we have a proper ParsedCommandLine to work with
parsedCommandLineResult = getParsedCommandLine({
tsconfig,
cwd,
forcedCompilerOptions: getForcedCompilerOptions({pluginOptions, rollupInputOptions, browserslist: normalizedBrowserslist}),
cwd
fileSystem: pluginOptions.fileSystem
});

// Prepare a Babel config if Babel should be the transpiler
Expand All @@ -186,12 +187,13 @@ export default function typescriptRollupPlugin(pluginInputOptions: Partial<Types

// Hook up a LanguageServiceHost and a LanguageService
languageServiceHost = new IncrementalLanguageService({
parsedCommandLine: parsedCommandLineResult.parsedCommandLine,
transformers: mergeTransformers(...transformers, getTypeOnlyImportTransformers()),
cwd,
emitCache,
rollupInputOptions,
supportedExtensions: SUPPORTED_EXTENSIONS,
fileSystem: pluginOptions.fileSystem,
parsedCommandLine: parsedCommandLineResult.parsedCommandLine,
transformers: mergeTransformers(...transformers, getTypeOnlyImportTransformers()),
languageService: () => languageService
});

Expand Down Expand Up @@ -363,11 +365,24 @@ export default function typescriptRollupPlugin(pluginInputOptions: Partial<Types
const outDir = join(cwd, getOutDir(cwd, outputOptions));
const generateMap = Boolean(parsedCommandLineResult.parsedCommandLine.options.declarationMap);

const chunkToOriginalFileMap: Map<string, string[]> = new Map(chunks.map<[string, string[]]>(chunk => [join(declarationOutDir, chunk.fileName), Object.keys(chunk.modules)]));
const moduleNames = [...new Set(([] as string[]).concat.apply([], chunks.map(chunk => Object.keys(chunk.modules).filter(canEmitForFile))))];
const chunkToOriginalFileMap: Map<string, string[]> = new Map(
chunks.map<[string, string[]]>(chunk => [join(declarationOutDir, normalize(chunk.fileName)), Object.keys(chunk.modules).map(normalize)])
);
const moduleNames = [
...new Set(
([] as string[]).concat.apply(
[],
chunks.map(chunk =>
Object.keys(chunk.modules)
.filter(canEmitForFile)
.map(normalize)
)
)
)
];

chunks.forEach((chunk: OutputChunk) => {
const rawLocalModuleNames = Object.keys(chunk.modules);
const rawLocalModuleNames = Object.keys(chunk.modules).map(normalize);
const localModuleNames = rawLocalModuleNames.filter(canEmitForFile);
const rawEntryFileName = rawLocalModuleNames.slice(-1)[0];
let entryFileNames = [localModuleNames.slice(-1)[0]];
Expand All @@ -385,7 +400,6 @@ export default function typescriptRollupPlugin(pluginInputOptions: Partial<Types

emitDeclarations({
chunk,
pluginContext: this,
generateMap,
declarationOutDir,
outDir,
Expand All @@ -398,7 +412,9 @@ export default function typescriptRollupPlugin(pluginInputOptions: Partial<Types
moduleNames,
localModuleNames,
entryFileNames,
supportedExtensions: SUPPORTED_EXTENSIONS
pluginContext: this,
supportedExtensions: SUPPORTED_EXTENSIONS,
fileSystem: pluginOptions.fileSystem
});
});
}
Expand Down
31 changes: 20 additions & 11 deletions src/service/cache/resolve-cache/resolve-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import {ensureAbsolute, isBabelHelper, isTslib, setExtension} from "../../../uti
import {sync} from "find-up";
import {takeBestSubModuleFromPackage} from "../../../util/take-best-sub-module-from-package/take-best-sub-module-from-package";
import {join} from "path";
import {ensureDirectory, readFileSync} from "../../../util/file-system/file-system";
import {JS_EXTENSION, PACKAGE_JSON_FILENAME} from "../../../constant/constant";
import {FileSystem} from "../../../util/file-system/file-system";

export interface ResolveCacheOptions {
fileSystem: FileSystem;
}

/**
* A Cache over resolved modules
Expand All @@ -18,6 +22,8 @@ export class ResolveCache implements IResolveCache {
*/
private readonly RESOLVE_CACHE: Map<string, Map<string, string | null>> = new Map();

constructor(private options: ResolveCacheOptions) {}

/**
* Gets the resolved path for an id from a parent
* @param {string} id
Expand Down Expand Up @@ -147,16 +153,19 @@ export class ResolveCache implements IResolveCache {
const packageJsonPath = sync(PACKAGE_JSON_FILENAME, {cwd: resolvedModule.resolvedFileName});
if (packageJsonPath != null) {
// Resolve the package.json file
const pkg = JSON.parse(readFileSync(packageJsonPath).toString());
// Compute the absolute path to the path pointed to by the 'main' property
const mainPath = pkg.main == null ? "index.js" : join(ensureDirectory(packageJsonPath), pkg.main);

// If the resolved file name is equal to that of the main property, check if there is another path on the "module" field (which will be better suited for Rollup)
if (mainPath != null && resolvedModule.resolvedFileName === mainPath) {
const submodule = takeBestSubModuleFromPackage(pkg);
if (submodule != null) {
// Rollup plays nice with ES-modules by default, so if there is any module property in the package, chances are that is using ES modules
resolvedModule.resolvedFileName = join(ensureDirectory(packageJsonPath), submodule);
const packageJsonText = this.options.fileSystem.readFile(packageJsonPath);
if (packageJsonText != null) {
const pkg = JSON.parse(packageJsonText.toString());
// Compute the absolute path to the path pointed to by the 'main' property
const mainPath = pkg.main == null ? "index.js" : join(this.options.fileSystem.ensureDirectory(packageJsonPath), pkg.main);

// If the resolved file name is equal to that of the main property, check if there is another path on the "module" field (which will be better suited for Rollup)
if (mainPath != null && resolvedModule.resolvedFileName === mainPath) {
const submodule = takeBestSubModuleFromPackage(pkg);
if (submodule != null) {
// Rollup plays nice with ES-modules by default, so if there is any module property in the package, chances are that is using ES modules
resolvedModule.resolvedFileName = join(this.options.fileSystem.ensureDirectory(packageJsonPath), submodule);
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/service/language-service/i-language-service-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {IEmitCache} from "../cache/emit-cache/i-emit-cache";
import {InputOptions} from "rollup";
import {TypescriptPluginOptions} from "../../plugin/i-typescript-plugin-options";
import {CustomTransformersFunction} from "../../util/merge-transformers/i-custom-transformer-options";
import {FileSystem} from "../../util/file-system/file-system";

export interface ILanguageServiceOptions {
parsedCommandLine: ParsedCommandLine;
Expand All @@ -12,4 +13,5 @@ export interface ILanguageServiceOptions {
rollupInputOptions: InputOptions;
supportedExtensions: string[];
languageService(): LanguageService;
fileSystem: FileSystem;
}
Loading

0 comments on commit d41f915

Please sign in to comment.