@ts-united/webpack
v1.0.0
Published
Helps one TypeScript project (folder containing a tsconfig.json file) import TypeScript/JavaScript code from any other project, while checking each project's code and resolving imports in each project using that project's tsconfig.json.
Downloads
6
Maintainers
Readme
TS United: Webpack Edition
Building shared code and reinstalling it into all parts of a monorepo (backend, frontend, mobile)? Incrementing version and publishing shared code to NPM on every small change? Wasting time? Forget about it with TS United!
TS United helps one TypeScript project (folder containing a tsconfig.json
file) import TypeScript/JavaScript code from any other project, while checking each project's code and resolving imports in each project using that project's tsconfig.json.
Note: Webpack Edition is compatible with Webpack bundler's runtime. If you're using another runtime, go to the list of editions and choose the edition you need.
Webpack Edition provides a loader and plugins that allow Webpack to import Typescript/JavaScript code from other projects.
Sounds great? Let's get started!
Adding TS United to your Webpack project
Step one: Install the @ts-united/webpack package from NPM into your main (aka root) project. Root project is the project you'll run npm start
in when starting the whole app. For example, the frontend part of your app will be a root project.
Step two: Create a TS United config object inside your webpack.config.js
like this:
const TS_UNITED_CONFIG = {
// ...config
};
module.exports = {
// ...webpack config
};
See the Config options section below for complete explanation of config options. It also shows an example config.
Step three: Register TS United loader, resolver plugin and, if you want, UnitedForkTsCheckerPlugin
which moves TypeScript checks into another thread and speeds up your compilations. Pass the TS United config to them as options.
UnitedForkTsCheckerPlugin
also accepts additional ForkTsCheckerWebpackPlugin
options, applied to all projects.
const {
UnitedPlugin,
UnitedForkTsCheckerPlugin,
} = require("@ts-united/webpack");
const TS_UNITED_CONFIG = {
// ...config
};
module.exports = {
// ...webpack config
resolve: {
plugins: [
// ...
new UnitedPlugin(TS_UNITED_CONFIG),
// ...
],
},
plugins: [
// ...
new UnitedForkTsCheckerPlugin(TS_UNITED_CONFIG, {
// Here you can set additional ForkTsCheckerWebpackPlugin options
}),
// ...
],
module: {
rules: [
// ...
{
test: /\.[jt]sx?$/,
use: {
loader: "@ts-united/webpack",
options: TS_UNITED_CONFIG,
},
},
// ...
],
},
};
That's all! Now your project uses TS United! Import files from other projects and then run npm start
and npm run build
as usual.
Tip: Any project can import any other project, listed in config file. Related projects can import each other and can import root project (importing root project from related projects is not recommended).
Also you can see a complete example project on GitHub: https://github.com/R-Mielamud/TsUnited/tree/main/example/webpack-frontend
Config options
| Option | Data type | Default value | Description |
| ------------ | ----------------------------------- | ----------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
| cwd
| string(path)?
| process.cwd()
| CWD is a base directory for all other directories in config. It can be either absolute or relative. If it's relative, it'll be relative to process.cwd()
. |
| extensions
| Array<string(file extension)>?
| [".js", ".jsx", ".ts", ".tsx", ".json"]
| Array of extensions path aliases will be implicitly resolved to. |
| projects
| Array<Project>
| | Array containing all projects info, including main aka root project (see Project schema |
Project schema
| Option | Data type | Default value | Description |
| ------------ | -------------------------------- | --------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| name
| string, unique
| | Any unique project name (id). |
| path
| string(path)
| | The path tsconfig.json file is located in or any child path. Importing files that are outside all projects' paths is forbidden. The path can be absolute or relative to CWD. |
| extensions
| Array<string(file extension)>?
| The top-level extensions
option | Array of extensions path aliases of this project will be implicitly resolved to. Each extension must begin with a dot. |
Example folder structure and config:
|---myproject
|---shared
|--- ...
|---tsconfig.json
|---frontend - the root project
|--- ...
|---webpack.config.js
|---tsconfig.json
|---tsconfig.base.json
// /myproject/frontend/webpack.config.js
const TS_UNITED_CONFIG = {
cwd: "../", // /myproject
extensions: [".ts", ".tsx"], // `~/myfile` can become either `./myfile.ts` or `./myfile.tsx`
projects: [
{
// The root project
name: "frontend",
path: "./frontend", // /myproject/frontend
extensions: [".ts", ".tsx"], // Set to default value
},
{
name: "shared",
path: "./shared", // /myproject/shared
},
],
};
module.exports = {
// ...webpack config
};
Compatibility
- This package is completely incompatible with
ModuleScopePlugin
. The're trying to solve the same problem - importing files from outside the root project, whileModuleScopePlugin
just forbids to do it, but TS United actually solves the problem. - Use
@ts-united/webpack
instead ofts-loader
/babel-loader
andUnitedForkTsCheckerPlugin
instead ofForkTsCheckerWebpackPlugin