This repository was archived by the owner on Jul 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
93 lines (83 loc) · 2.3 KB
/
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const fs = require('fs');
const path = require('path');
const pkg = require('./package.json');
const tsc = require('./tsconfig.json');
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
const paths = {
src: './src',
dist: './dist',
entry: './src/index.ts',
assets: './assets',
example: './example'
};
Object.keys(paths).forEach(key => paths[key] = path.join(__dirname, paths[key]));
module.exports = {
entry: paths.entry,
mode: 'development',
devtool: "inline-source-maps",
output: {
filename: 'index.js',
path: paths.dist
},
devServer: {
contentBase: paths.dist,
compress: true,
port: 9000,
hot: true
},
resolve: {
enforceExtension: false,
extensions: [".ts",".tsx",".js",".jsx",".json"],
plugins: [
new TsconfigPathsPlugin({})
]
},
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/
}
]
},
plugins: [
new DtsBundlePlugin(),
new RemoveEmptyDirsPlugin(),
new DtsRemoveTsPathImportsPlugin()
]
};
function DtsBundlePlugin(){}
DtsBundlePlugin.prototype.apply = function (compiler) {
compiler.plugin('done', function(){
const dts = require('dts-bundle');
dts.bundle({
name: pkg.name,
main: pkg.types,
out: '../' + pkg.types,
indent: ' ',
newline: '\n',
removeSource: true,
outputAsModuleFolder: true // to use npm in-package typings
});
});
};
function RemoveEmptyDirsPlugin(){}
RemoveEmptyDirsPlugin.prototype.apply = function(compiler) {
const removeEmpty = require('remove-empty-directories');
compiler.plugin('done', function() {
removeEmpty(paths.dist);
});
};
function DtsRemoveTsPathImportsPlugin(){}
DtsRemoveTsPathImportsPlugin.prototype.apply = function(compiler) {
compiler.plugin('done', function() {
const regexBase = "import\\s+[^\"'`]*[\"'`]MODULEBASE[^\"'`]*[\"'`]\s*;?[\s]*";
const regexes = Object.keys(tsc.compilerOptions.paths).map(path => {
return new RegExp(regexBase.replace("MODULEBASE", path.replace('*', '').replace(/\\/g, "\\\\$0")), "g");
});
const dtsSource = fs.readFileSync(pkg.types).toString('utf8');
const stripped = regexes.reduce((src, regex) => src.replace(regex, ''), dtsSource);
fs.writeFileSync(pkg.types, stripped);
})
};