UNPKG

5.89 kBMarkdownView Raw
1<div align="center">
2 <a href="https://github.com/webpack/webpack">
3 <img width="200" height="200" src="https://webpack.js.org/assets/icon-square-big.svg">
4 </a>
5 <h1>Transpile Webpack Plugin</h1>
6</div>
7
8[![npm][npm]][npm-url]
9[![node][node]][node-url]
10[![download][download]][npm-url]
11[![license][license]][license-url]
12[![size][size]][size-url]
13[![cicd][cicd]][cicd-url]
14
15# Transpile Webpack Plugin
16
17The webpack plugin that transpiles input files into output files individually without bundling together.
18
19Input files are collected from files directly or indirectly imported by the [entry](https://webpack.js.org/configuration/entry-context/#entry), then get compiled and ouputted keeping the same directory structure in the output directory.
20
21Transpiling with webpack could be especially helpful when building SSR(server side rendering) in a heavyweight NodeJS server that involves source file path based logics. Please make best of it.
22
23Note that this plugin replies on features of webpack v5. The latest webpack is supposed to be used when possible.
24
25## Getting Started
26
27To begin, you'll need to install `transpile-webpack-plugin`:
28
29```sh
30npm i -D transpile-webpack-plugin
31```
32
33Or, with any package manager you prefer.
34
35Then, add the plugin to your webpack config. For example:
36
37```js
38const TranspilePlugin = require('transpile-webpack-plugin');
39
40module.exports = {
41 entry: './src/index.js',
42 output: {
43 path: __dirname + '/dist',
44 },
45 target: 'node',
46 plugins: [new TranspilePlugin(/* options */)],
47};
48```
49
50The [target](https://webpack.js.org/configuration/target/) should be node-compatible (because output files are imported by each other with `require` at runtime and output with other value is not runnable in most cases).
51
52Assuming the entry `src/index.js` imports another file `src/constants/greeting.js`, input files will be `src/index.js` and `src/constants/greeting.js`. And after compilation, output files will be `dist/index.js` and `dist/constants/greeting.js`. The common dir of input files is used as the base dir to evaluate the relative paths of output files in output dir.
53
54## Options
55
56- **[exclude](#exclude)**
57- **[hoistNodeModules](#hoistNodeModules)**
58- **[longestCommonDir](#longestCommonDir)**
59
60### `exclude`
61
62Type: `{string|RegExp|((p: string) => boolean)|string[]|RegExp[]|((p: string) => boolean)[]}`
63
64Default: `[]`
65
66Option `exclude` indicates files to be excluded. Import statements to the excluded in input files are properly adjusted in output files to keep workable. It's useful when you copy third-party files and want to use them as they are.
67
68Though, don't use it to exclude `node_modules` because some helpers of webpack loaders living there are not runnable before compiled. For this case, use the [externals](https://webpack.js.org/configuration/externals/) instead.
69
70With this option as string, input files whose aboslute paths begin with it will be excluded. With this option as regular expression, input files whose absolute paths match it will be excluded. With this option as function, input files whose absolute paths are passed into the call of it and end up with `true` will be excluded.
71
72### `hoistNodeModules`
73
74Type: `{boolean}`
75
76Default: `true`
77
78Option `hoistNodeModules` indicates whether to evaluate output paths for input files from or not from `node_modules` separately, and keep input files from `node_modules` outputted into `node_modules` just under output dir. It's usable to flatten the output directory structure a little bit.
79
80Given input files `src/index.js` `node_modules/lodash/lodash.js` and output dir `dist`, with this option `true`, output files will be `dist/index.js` `dist/node_modules/lodash/lodash.js`. But with this option `false`, output files will be `dist/src/index.js` `dist/node_modules/lodash/lodash.js`.
81
82### `longestCommonDir`
83
84Type: `{string|undefined}`
85
86Default: `undefined`
87
88Option `longestCommonDir` indicates the limit of the common dir to evaluate relative paths of output files in output dir. When this option is shorter than the common dir of input files, this option is used against input files to evaluate relative paths of output files in output dir. Otherwise, the common dir of input files is used.
89
90Given input files `src/server/index.js` `src/server/constants/greeting.js` and output dir `dist`, with this option `undefined`, output files will be `dist/index.js` `dist/constants/greeting.js`. But with this option `'./src'`, output files will be `dist/server/index.js` `dist/server/constants/greeting.js`.
91
92Though, given input files `src/index.js` `src/server/constants/greeting.js` and output dir `dist`, with this option `'./src/server'`, output files will still be `dist/index.js` `dist/server/constants/greeting.js` because the common dir of input files is shorter than this option.
93
94## Contributing
95
96Please take a moment to read our contributing guidelines if you haven't yet done so.
97[CONTRIBUTING][contributing-url]
98
99## License
100
101[MIT][license-url]
102
103[npm]: https://img.shields.io/npm/v/transpile-webpack-plugin.svg
104[npm-url]: https://npmjs.com/package/transpile-webpack-plugin
105[node-url]: https://nodejs.org/
106[node]: https://img.shields.io/node/v/transpile-webpack-plugin.svg
107[download]: https://img.shields.io/npm/dw/transpile-webpack-plugin
108[license]: https://img.shields.io/github/license/licg9999/transpile-webpack-plugin
109[license-url]: https://github.com/licg9999/transpile-webpack-plugin/blob/master/LICENSE
110[size]: https://packagephobia.com/badge?p=transpile-webpack-plugin
111[size-url]: https://packagephobia.com/result?p=transpile-webpack-plugin
112[cicd]: https://github.com/licg9999/transpile-webpack-plugin/actions/workflows/verify-and-release.yml/badge.svg
113[cicd-url]: https://github.com/licg9999/transpile-webpack-plugin/actions/workflows/verify-and-release.yml
114[contributing-url]: https://github.com/licg9999/transpile-webpack-plugin/blob/master/CONTRIBUTING.md