webpack.config.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. const fs = require("fs"),
  2. _ = require("lodash"),
  3. terserPlugin = require("terser-webpack-plugin"),
  4. webpack = require("webpack");
  5. function createBanner() {
  6. const pkg = JSON.parse(fs.readFileSync("./package.json"));
  7. return (
  8. "[name]\n" +
  9. `${pkg.homepage}\n` +
  10. `Copyright (c) 2010 - ${new Date().getFullYear()} ${pkg.author.name}\n` +
  11. `Licensed under the ${pkg.license} license\n` +
  12. `Version: ${pkg.version}`
  13. );
  14. }
  15. const rules = {
  16. js: {
  17. test: /\.js$/,
  18. loader: "babel-loader",
  19. exclude: /(node_modules)/,
  20. options: {
  21. presets: ["@babel/preset-env"],
  22. plugins: ["@babel/plugin-transform-modules-commonjs"],
  23. passPerPreset: true
  24. }
  25. },
  26. ts: {
  27. test: /\.tsx?$/,
  28. loader: "babel-loader",
  29. exclude: /(node_modules)/,
  30. options: {
  31. presets: ["@babel/preset-typescript"],
  32. passPerPreset: true
  33. }
  34. }
  35. };
  36. module.exports = function (env, argv) {
  37. const config = {
  38. name: "main",
  39. entry: {
  40. "dist/inputmask": "./bundle.js",
  41. "dist/inputmask.min": "./bundle.js",
  42. "qunit/qunit": "./qunit/index.js"
  43. },
  44. // experiments: {
  45. // outputModule: true,
  46. // },
  47. output: {
  48. path: __dirname,
  49. filename: "[name].js",
  50. library: {
  51. type: "umd2"
  52. },
  53. globalObject: "typeof self !== 'undefined' ? self : this"
  54. },
  55. externals: {
  56. jquery: {
  57. commonjs: "jquery",
  58. commonjs2: "jquery",
  59. amd: "jquery",
  60. root: "jQuery"
  61. },
  62. jqlite: "jqlite",
  63. qunit: "QUnit",
  64. window: "window"
  65. },
  66. optimization: {
  67. minimize: env === "production",
  68. minimizer: [
  69. new terserPlugin({
  70. include: /\.min\.js$/,
  71. terserOptions: {
  72. sourceMap: env !== "production",
  73. format: {
  74. ascii_only: true,
  75. beautify: false,
  76. comments: /^!/
  77. },
  78. compress: {
  79. drop_console: env === "production"
  80. }
  81. },
  82. extractComments: false
  83. }),
  84. new terserPlugin({
  85. exclude: /\.min\.js$/,
  86. terserOptions: {
  87. sourceMap: env !== "production",
  88. format: {
  89. ascii_only: true,
  90. beautify: true,
  91. comments: /^!/
  92. },
  93. compress: {
  94. drop_console: env === "production"
  95. }
  96. },
  97. extractComments: false
  98. })
  99. ]
  100. },
  101. module: {
  102. rules: [rules.js, rules.ts]
  103. },
  104. resolve: {
  105. extensions: [".wasm", ".mjs", ".js", ".ts", ".json"],
  106. alias: {
  107. // "./dependencyLibs/inputmask.dependencyLib": "./dependencyLibs/inputmask.dependencyLib.jquery"
  108. }
  109. },
  110. devtool: env === "production" ? undefined : "source-map",
  111. plugins: [
  112. new webpack.BannerPlugin({
  113. banner: createBanner,
  114. entryOnly: true
  115. })
  116. ],
  117. bail: true,
  118. mode: env === "production" ? "production" : "none",
  119. target: ["web", "es5"]
  120. },
  121. jqueryConfig = _.defaultsDeep({}, config);
  122. jqueryConfig.entry = {};
  123. _.assignIn(jqueryConfig, {
  124. name: "jquery",
  125. resolve: {
  126. extensions: [".wasm", ".mjs", ".js", ".ts", ".json"],
  127. alias: {
  128. "./dependencyLibs/inputmask.dependencyLib":
  129. "./dependencyLibs/inputmask.dependencyLib.jquery"
  130. }
  131. },
  132. entry: {
  133. "dist/jquery.inputmask": "./bundle.jquery.js",
  134. "dist/jquery.inputmask.min": "./bundle.jquery.js",
  135. "qunit/qunit": "./qunit/index.js"
  136. }
  137. });
  138. const colorMaskConfig = _.defaultsDeep({}, config);
  139. colorMaskConfig.entry = {};
  140. _.assignIn(colorMaskConfig, {
  141. name: "colormask",
  142. entry: {
  143. "dist/colormask": "./bundle.colormask.js",
  144. "dist/colormask.min": "./bundle.colormask.js"
  145. }
  146. });
  147. return [config, jqueryConfig, colorMaskConfig];
  148. };