Skip to content

Commit 6a5d02d

Browse files
authored
Webpack v4 (#89)
* Implement Webpack v4 plugin * fix version * Finally add link in each README.md for both Webpack v4 and v5
1 parent 19b72f7 commit 6a5d02d

File tree

9 files changed

+7643
-8
lines changed

9 files changed

+7643
-8
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,5 @@ console.log(rtiTranspiler)
5252
2) https://www.npmjs.com/package/@runtime-type-inspector/transpiler
5353
3) https://www.npmjs.com/package/@runtime-type-inspector/parcel-transformer
5454
4) https://www.npmjs.com/package/@runtime-type-inspector/plugin-rollup
55-
5) https://www.npmjs.com/package/@runtime-type-inspector/plugin-webpack5
55+
5) https://www.npmjs.com/package/@runtime-type-inspector/plugin-webpack4
56+
6) https://www.npmjs.com/package/@runtime-type-inspector/plugin-webpack5

plugin-webpack4/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Webpack 4 plugin for `@runtime-type-inspector/transpiler`.
2+
3+
Installation:
4+
5+
```sh
6+
npm install @runtime-type-inspector/plugin-webpack4
7+
```
8+
9+
Example of integration into your build script:
10+
11+
- https://github.com/xenova/transformers.js/pull/409
12+
13+
Have fun! If you run into any issues, please open an issue at https://github.com/kungfooman/RuntimeTypeInspector.js

plugin-webpack4/index.cjs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const {Compilation, Compiler} = require('webpack');
2+
/**
3+
* @typedef RuntimeTypeInspectorPluginOptions
4+
* @property {RegExp} [test] - Test for file extensions.
5+
* @property {RegExp} [exclude] - Test for exclusion.
6+
*/
7+
class RuntimeTypeInspectorPlugin {
8+
static pluginName = 'RuntimeTypeInspectorPlugin';
9+
// Default options
10+
options = {
11+
// These file extensions are transpiled by default.
12+
test: /\.[cm]?js(\?.*)?$/i,
13+
// By default, files under node_modules are not processed.
14+
exclude: /node_modules/
15+
};
16+
/**
17+
* @param {RuntimeTypeInspectorPluginOptions} options - The options.
18+
*/
19+
constructor(options) {
20+
Object.assign(this.options, options);
21+
}
22+
/**
23+
* @param {Compiler} compiler - The compiler.
24+
*/
25+
apply(compiler) {
26+
compiler.hooks.thisCompilation.tap(
27+
RuntimeTypeInspectorPlugin.pluginName,
28+
/**
29+
* @param {Compilation} compilation - The compilation.
30+
*/
31+
(compilation) => {
32+
compilation.hooks.normalModuleLoader.tap(
33+
//compiler.webpack.NormalModule.getCompilationHooks(compilation).loader.tap(
34+
RuntimeTypeInspectorPlugin.pluginName,
35+
/**
36+
* @param {object} loaderContext - The loader context.
37+
* @param {import('webpack').NormalModule} module - The module.
38+
*/
39+
(loaderContext, module) => {
40+
const {userRequest} = module;
41+
const {test, exclude} = this.options;
42+
const retTest = test.test(userRequest);
43+
const retExclude = exclude.test(userRequest);
44+
// console.log('userRequest', userRequest, {retTest, retExclude});
45+
if (!retTest || retExclude) {
46+
return;
47+
}
48+
const {options} = this;
49+
const loader = require.resolve(__dirname + '/loader.cjs');
50+
module.loaders.push({options, loader});
51+
}
52+
);
53+
}
54+
);
55+
}
56+
}
57+
module.exports = {RuntimeTypeInspectorPlugin};

plugin-webpack4/index.mjs

Lines changed: 0 additions & 4 deletions
This file was deleted.

plugin-webpack4/loader.cjs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const {addTypeChecks, expandType} = require('@runtime-type-inspector/transpiler');
2+
const loaderUtils = require('loader-utils');
3+
/**
4+
* @param {string} source - The source.
5+
*/
6+
module.exports = function (source) {
7+
/** @type {import('@runtime-type-inspector/transpiler').Options} */
8+
const options = loaderUtils.getOptions(this);
9+
source = addTypeChecks(source, {...options, expandType});
10+
this.callback(null, source);
11+
};

0 commit comments

Comments
 (0)