Code Monkey home page Code Monkey logo

webpack-project-references-alias's Introduction

Webpack alias generator for TypeScript project references

Description

TypeScript introduced a feature called Project References in version 3.0.

Project References required compiled .d.ts files either checked-in or generated after each branch switch. Not doing this would result in a broken IntelliSense experience.

This drawback was fixed in version 3.7. The TypeScript language server can now inderstand project references. This improves the IntelliSense experience across projects while removing the need to have the compiled files on disk.

The goal of this package is to provide a tool to make webpack understand project references as well. This is needed when optimizing build time when using transpile-only loaders (ts-loader in transpile-only mode or babel-loader) without the need to have the generated files on disk.

This package create aliases that makes webpack resolve the TypeScript files instead of the generated JavaScript files.

For instance, when package A is a referenced project, the alias will let you do the following import:

import "A/lib/foo"; // webpack will resolve A/src/foo.ts

Usage

const { getAliasForProject } = require("@microsoft/webpack-project-references-alias");

module.exports = {
  entry: "./src/index.ts",
  mode: "production",
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "bundle.js"
  },
  resolve: {
    extensions: [".ts"],
    alias: getAliasForProject()
  },
  module: {
    rules: [
      { test: /\.ts$/, loader: "ts-loader", options: { transpileOnly: true } }
    ]
  }
};

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

webpack-project-references-alias's People

Contributors

alexpomsft avatar microsoft-github-operations[bot] avatar microsoftopensource avatar vincentbailly avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

webpack-project-references-alias's Issues

Doesn't handle tsconfig.json with comments

The JSON parsing of tsconfig.json files doesn't seem to having comments in them, and errors with a JSON parse error.

Here's the patch I'm using to work around it (with patch-package):

diff --git a/node_modules/@microsoft/webpack-project-references-alias/lib/index.js b/node_modules/@microsoft/webpack-project-references-alias/lib/index.js
index f88fe3a..1d33585 100644
--- a/node_modules/@microsoft/webpack-project-references-alias/lib/index.js
+++ b/node_modules/@microsoft/webpack-project-references-alias/lib/index.js
@@ -3,6 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
 const fs = require("fs");
 const path = require("path");
 const functional_1 = require("./functional");
+const typescript = require("typescript");
+
 function getAliasForProject(project) {
     if (typeof project === "undefined") {
         project = process.cwd();
@@ -33,7 +35,7 @@ const getReferencedProjectsRecursive = functional_1.memoize((tsConfigPath) => {
 function getReferencedProjects(tsConfigPath) {
     var _a;
     const projectDir = path.dirname(tsConfigPath);
-    const config = require(tsConfigPath);
+    const config = readConfigFile(tsConfigPath)
     const references = (_a = config.references) === null || _a === void 0 ? void 0 : _a.map(o => o.path).map(p => path.join(projectDir, p)).map(resolveTsConfig);
     return (references !== null && references !== void 0 ? references : []);
 }
@@ -62,7 +64,7 @@ function tryGetPackageInfo(dir) {
 }
 function getAliasFor(tsConfigPath) {
     const projectRootDir = path.dirname(tsConfigPath);
-    const config = require(tsConfigPath);
+    const config = readConfigFile(tsConfigPath);
     const hasExplicitOutDir = typeof config.compilerOptions.outDir !== "undefined";
     const rootDir = path.join(projectRootDir, config.compilerOptions.rootDir || "");
     const outDir = hasExplicitOutDir
@@ -82,3 +84,6 @@ function getAliasFor(tsConfigPath) {
     alias[outDir] = rootDir;
     return alias;
 }
+function readConfigFile(tsConfigPath) {
+  return typescript.readConfigFile(tsConfigPath, path => fs.readFileSync(path, 'utf-8')).config
+}

It totally ignores errors which is naive, but good enough as a temporary workaround.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.