UNPKG

6.14 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 is especially helpful when both user interface logics and source file path related logics are involved, such as building SSR(server side rendering) in a heavyweight NodeJS server. 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 plugins: [new TranspilePlugin(/* options */)],
46};
47```
48
49Assuming the entry `src/index.js` imports another file `src/constants/greeting.js`, input files will be `src/index.js` `src/constants/greeting.js`. After compilation, output files will be `dist/index.js` `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.
50
51Just a reminder, if to run output files with NodeJS, don't forget to set the [target](https://webpack.js.org/configuration/target/) as `node` or a node-compatible value so that no breaking code is generated by webpack unexpectedly.
52
53## Options
54
55- **[exclude](#exclude)**
56- **[hoistNodeModules](#hoistNodeModules)**
57- **[longestCommonDir](#longestCommonDir)**
58
59### `exclude`
60
61Type: `{string|RegExp|((p: string) => boolean)|string[]|RegExp[]|((p: string) => boolean)[]}`
62
63Default: `[]`
64
65Option `exclude` indicates files to be excluded. Import statements to the excluded in input files are properly adjusted in output files and kept workable. It's similar to the [externals](https://webpack.js.org/configuration/externals/) except that the import path auto adjustment. It'll be useful when you copy some third-party files and want to use them as they are.
66
67By the way, excluding `node_modules` with this option is a bit trivial because some helpers of webpack loaders also living there are not runnable before compiled. If you need to exclude dependencies in `node_modules`, using [webpack-node-externals](https://github.com/liady/webpack-node-externals) might be a better choice.
68
69With 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.
70
71### `hoistNodeModules`
72
73Type: `{boolean}`
74
75Default: `true`
76
77Option `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.
78
79Given 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`.
80
81### `longestCommonDir`
82
83Type: `{string|undefined}`
84
85Default: `undefined`
86
87Option `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.
88
89Given 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`.
90
91Though, 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.
92
93## Contributing
94
95Please take a moment to read our contributing guidelines if you haven't yet done so.
96[CONTRIBUTING][contributing-url]
97
98## License
99
100[MIT][license-url]
101
102[npm]: https://img.shields.io/npm/v/transpile-webpack-plugin.svg
103[npm-url]: https://npmjs.com/package/transpile-webpack-plugin
104[node-url]: https://nodejs.org/
105[node]: https://img.shields.io/node/v/transpile-webpack-plugin.svg
106[download]: https://img.shields.io/npm/dw/transpile-webpack-plugin
107[license]: https://img.shields.io/github/license/licg9999/transpile-webpack-plugin
108[license-url]: https://github.com/licg9999/transpile-webpack-plugin/blob/master/LICENSE
109[size]: https://packagephobia.com/badge?p=transpile-webpack-plugin
110[size-url]: https://packagephobia.com/result?p=transpile-webpack-plugin
111[cicd]: https://github.com/licg9999/transpile-webpack-plugin/actions/workflows/verify-and-release.yml/badge.svg
112[cicd-url]: https://github.com/licg9999/transpile-webpack-plugin/actions/workflows/verify-and-release.yml
113[contributing-url]: https://github.com/licg9999/transpile-webpack-plugin/blob/master/CONTRIBUTING.md