Skip to content

Commit

Permalink
Update readme and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Lynch committed Feb 17, 2023
1 parent 1ae841f commit f4e42e9
Show file tree
Hide file tree
Showing 34 changed files with 172 additions and 12 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ This is important if you read from stdout or stderr and for proper error
handling. The default value ensures that you can read from stdout e.g. via pipes
or you use webpack -j to generate json output.

#### references _(string[]) (default=undefined)

Support for [Typescript Project References](https://www.typescriptlang.org/docs/handbook/project-references.html).

## Typescript support

This package has typescript typings included. If your webpack config is using typescript, you can use this syntax to import the default export:
Expand Down
5 changes: 0 additions & 5 deletions example/src/use-from-ts/index.ts

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions examples/example/src/use-from-ts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { TsconfigPathsPlugin } from "../../../../src/index";
import TsconfigPathsPluginDefault from "../../../../src/index";

const plugin1 = new TsconfigPathsPlugin();
const plugin2 = new TsconfigPathsPluginDefault();
3 changes: 2 additions & 1 deletion example/tsconfig.json → examples/example/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"foo": ["./src/mapped/foo"],
"bar/*": ["./src/mapped/bar/*"],
"*": ["./src/mapped/star/*"]
}
},
"composite": true
}
}
File renamed without changes.
25 changes: 25 additions & 0 deletions examples/referenceExample/custom-fs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const fs = require("fs");
const webpack = require("webpack");

const config = require("./webpack.config");

const compiler = webpack(config);

compiler.inputFileSystem = fs;

compiler.run(function (error, stats) {
if (error) {
console.error(error);
return process.exit(1);
}

if (stats.compilation.errors.length) {
stats.compilation.errors.forEach((compilationError) => {
console.error(compilationError);
});

return process.exit(1);
}

console.log("Successfully compiled");
});
16 changes: 16 additions & 0 deletions examples/referenceExample/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as foo from "src/mapped/buzz";
import * as bar from "src/mapped/fizz/file1";
import * as myStar from "star-bar";
import * as packagedBrowser from "browser-field-package";
import * as packagedMain from "main-field-package";
import * as packagedIndex from "no-main-field-package";

console.log(
"HELLO WORLD!",
foo.message,
bar.message,
myStar.message,
packagedBrowser.message,
packagedMain.message,
packagedIndex.message
);
1 change: 1 addition & 0 deletions examples/referenceExample/src/mapped/bar/file1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const message = "fizz";
1 change: 1 addition & 0 deletions examples/referenceExample/src/mapped/foo/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const message = "GOODBYE!";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const message = "browser";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const message = "node";
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "browser-field",
"main": "node.ts",
"browser": "browser.ts"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const message = "node";
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "main-field",
"main": "node.ts"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const message = "index";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "no-main-field"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const message = "Hello Star!";
5 changes: 5 additions & 0 deletions examples/referenceExample/src/use-from-ts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { TsconfigPathsPlugin } from "../../../../src/index";
import TsconfigPathsPluginDefault from "../../../../src/index";

const plugin1 = new TsconfigPathsPlugin();
const plugin2 = new TsconfigPathsPluginDefault();
19 changes: 19 additions & 0 deletions examples/referenceExample/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
// Output
"module": "commonjs",
"target": "es5",
"outDir": "./js_out",
"baseUrl": ".",
"paths": {
"foo": ["src/mapped/bar"],
"bar/*": ["src/mapped/foo/*"],
"*": ["./src/mapped/star/*"]
}
},
"references": [
{
"path": "../example/tsconfig.json"
}
]
}
37 changes: 37 additions & 0 deletions examples/referenceExample/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const path = require("path");
const TsconfigPathsPlugin = require("../");

module.exports = {
mode: "development",
context: path.resolve(__dirname, "src"),
entry: "./index",
output: {
path: path.join(__dirname, "temp"),
filename: "bundle.js",
},
module: {
rules: [
{
test: /\\.tsx?$/,
exclude: /^node_modules/,
loader: "ts-loader",
options: {
configFile: "./example/tsconfig.json",
},
},
],
},
resolve: {
extensions: [".ts", ".tsx", ".js"],
plugins: [
new TsconfigPathsPlugin({
configFile: "./tsconfig.json",
logLevel: "info",
extensions: [".ts", ".tsx"],
mainFields: ["browser", "main"],
references: ["../example/tsconfig.json"]
// baseUrl: "/foo"
}),
],
},
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
},
"scripts": {
"prepare": "husky install",
"compile:example": "tsc -p example",
"example": "yarn build && cd example && webpack && node custom-fs.js",
"compile:example": "tsc -p examples/example",
"example": "yarn build && cd examples/example && webpack && node custom-fs.js",
"build": "rimraf lib && tsc -p .",
"lint": "eslint \"./{src,tests}/**/*.ts{,x}\" --ext .js,.ts,.tsx -f visualstudio",
"test": "jest",
Expand Down
42 changes: 38 additions & 4 deletions src/__tests__/plugins.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe(`TsconfigPathsPlugin`, () => {
const SETTINGS: Configuration = {
mode: "development",
context: path.resolve(__dirname, "src"),
entry: `${__dirname}/../../example/src/index.ts`,
entry: `${__dirname}/../../examples/example/src/index.ts`,
output: {
path: path.join(__dirname, "../../temp"),
filename: "bundle.js",
Expand All @@ -31,7 +31,7 @@ describe(`TsconfigPathsPlugin`, () => {

it(`Can initialize the plugin`, (done) => {
const testPlugin = new TsconfigPathsPlugin({
configFile: `${__dirname}/../../example/tsconfig.json`,
configFile: `${__dirname}/../../examples/example/tsconfig.json`,
logLevel: "INFO",
extensions: [".ts", ".tsx"],
mainFields: ["browser", "main"],
Expand Down Expand Up @@ -63,7 +63,7 @@ describe(`TsconfigPathsPlugin`, () => {

it(`Test to ensure Apply exists and is working`, (done) => {
const webpackSettings: Configuration = {
entry: `${__dirname}/../../example/src/index.ts`,
entry: `${__dirname}/../../examples/example/src/index.ts`,
target: "web",
output: {
path: path.join(__dirname, "../../temp"),
Expand All @@ -86,7 +86,7 @@ describe(`TsconfigPathsPlugin`, () => {
],
plugins: [
new TsconfigPathsPlugin({
configFile: `${__dirname}/../../example/tsconfig.json`,
configFile: `${__dirname}/../../examples/example/tsconfig.json`,
}),
],
},
Expand Down Expand Up @@ -117,4 +117,38 @@ describe(`TsconfigPathsPlugin`, () => {
done();
});
});

it(`Resolves project references`, (done) => {
const testPlugin = new TsconfigPathsPlugin({
configFile: `${__dirname}/../../examples/referenceExample/tsconfig.json`,
logLevel: "INFO",
extensions: [".ts", ".tsx"],
mainFields: ["browser", "main"],
references: [`${__dirname}/../../examples/example/tsconfig.json`],
});
expect(testPlugin).toBeInstanceOf(TsconfigPathsPlugin);

const testSettings: Configuration = {
...SETTINGS,
resolve: {
extensions: [".ts", ".tsx", ".js"],
plugins: [testPlugin],
},
};

const compiler = webpack(testSettings);

compiler.run((err, stats) => {
if (err) {
done(err);
return;
}
expect(stats).toBeDefined();

const details = stats?.toJson();
expect(details?.errorsCount).toEqual(0);
// TODO There should probably be a test that verifies the stats match what is expected
done();
});
});
});

0 comments on commit f4e42e9

Please sign in to comment.