@corespeed/webpack 0.1.0-beta.2 → 0.1.0-beta.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -19,11 +19,11 @@ var isCI = require('is-ci');
19
19
  var prompts = require('@clack/prompts');
20
20
  var installPkg = require('@antfu/install-pkg');
21
21
  var castArray = require('foxts/cast-array');
22
+ var path = require('node:path');
22
23
  var LightningCSS = require('lightningcss');
23
24
  var reactCompilerWebpack = require('react-compiler-webpack');
24
- var TerserPlugin = require('terser-webpack-plugin');
25
+ var TerserWebpackPlugin = require('terser-webpack-plugin');
25
26
  var lightningcssLoader = require('lightningcss-loader');
26
- var path = require('node:path');
27
27
 
28
28
  var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
29
29
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
@@ -37,9 +37,9 @@ var WebpackBarProgressPlugin__default = /*#__PURE__*/_interopDefault(WebpackBarP
37
37
  var MiniCssExtractPlugin__default = /*#__PURE__*/_interopDefault(MiniCssExtractPlugin);
38
38
  var TsconfigPathsPlugin__default = /*#__PURE__*/_interopDefault(TsconfigPathsPlugin);
39
39
  var isCI__default = /*#__PURE__*/_interopDefault(isCI);
40
- var LightningCSS__default = /*#__PURE__*/_interopDefault(LightningCSS);
41
- var TerserPlugin__default = /*#__PURE__*/_interopDefault(TerserPlugin);
42
40
  var path__default = /*#__PURE__*/_interopDefault(path);
41
+ var LightningCSS__default = /*#__PURE__*/_interopDefault(LightningCSS);
42
+ var TerserWebpackPlugin__default = /*#__PURE__*/_interopDefault(TerserWebpackPlugin);
43
43
 
44
44
  function getSupportedBrowsers(browserlists, dir, isDevelopment) {
45
45
  if (browserlists) {
@@ -291,6 +291,44 @@ const resolve = ({ cwd, lodashTreeShaking })=>async (config)=>{
291
291
  config.resolve.fallback['lodash-es/fp'] = globalRequire.resolve('lodash/fp');
292
292
  }
293
293
  }
294
+ /**
295
+ * tailwind-merge optimization
296
+ *
297
+ * webpack do not repect the order in the `conditionNames` config
298
+ * instead, it prioritize the order in the `exports` field of package.json
299
+ *
300
+ * since tailwind-merge list its CommonJS export before ESM export, webpack will
301
+ * always piroritize CommonJS version.
302
+ *
303
+ * We can use alias to force webpack to use the ESM version
304
+ */ try {
305
+ const tailwindMergeDistDir = globalRequire.resolve('tailwind-merge');
306
+ config.resolve.alias ??= {};
307
+ if (!Array.isArray(config.resolve.alias)) {
308
+ config.resolve.alias['tailwind-merge'] = path__default.default.join(tailwindMergeDistDir, '../bundle-mjs.mjs');
309
+ }
310
+ } catch {
311
+ // tailwind-merge does not exist, do nothing
312
+ }
313
+ /**
314
+ * use-sync-external-store/shim optimization
315
+ *
316
+ * React 18 and above has built-in support for useSyncExternalStore,
317
+ * no need to include shim
318
+ */ try {
319
+ const reactPackageJson = globalRequire('react/package.json');
320
+ const reactVersion = reactPackageJson.version;
321
+ const majorVersion = Number.parseInt(reactVersion.split('.')[0], 10);
322
+ if (majorVersion >= 18 || majorVersion === 0 // experimental versions
323
+ ) {
324
+ config.resolve.alias ??= {};
325
+ if (!Array.isArray(config.resolve.alias)) {
326
+ config.resolve.alias['use-sync-external-store/shim'] = 'react';
327
+ }
328
+ }
329
+ } catch {
330
+ // react does not exist, do nothing
331
+ }
294
332
  return config;
295
333
  };
296
334
 
@@ -409,10 +447,7 @@ const loaders = ({ cwd, postcss, svgr, reactCompiler, webpackExperimentalBuiltin
409
447
  },
410
448
  {
411
449
  test: /assets\//,
412
- type: 'asset/resource',
413
- generator: {
414
- filename: '_assets/[hash][ext][query]'
415
- }
450
+ type: 'asset/resource'
416
451
  }
417
452
  ]
418
453
  });
@@ -485,19 +520,21 @@ const loaders = ({ cwd, postcss, svgr, reactCompiler, webpackExperimentalBuiltin
485
520
  // };
486
521
  };
487
522
 
488
- const optimization = ({ development: isDevelopment, browserlists }, { supportedBrowsers })=>(config)=>{
523
+ const optimization = ({ development: isDevelopment, browserlists, dropConsoleInProduction }, { supportedBrowsers })=>(config)=>{
489
524
  config.optimization ??= {};
490
525
  config.optimization.emitOnErrors = !isDevelopment;
491
526
  config.optimization.checkWasmTypes = false;
492
527
  config.optimization.nodeEnv = false; // we manually bring our own DefinePlugin
493
528
  config.optimization.minimizer ??= [];
494
- config.optimization.minimizer.push(new TerserPlugin__default.default({
495
- minify: TerserPlugin__default.default.swcMinify,
529
+ config.optimization.minimizer.push(new TerserWebpackPlugin__default.default({
530
+ minify: TerserWebpackPlugin__default.default.swcMinify,
496
531
  terserOptions: {
497
532
  compress: {
498
533
  ecma: 2018,
499
534
  comparisons: false,
500
- inline: 2 // https://github.com/vercel/next.js/issues/7178#issuecomment-493048965
535
+ inline: 2,
536
+ // inline: 1,
537
+ drop_console: isDevelopment ? false : dropConsoleInProduction
501
538
  },
502
539
  mangle: {
503
540
  safari10: true
@@ -630,6 +667,7 @@ const splitChunks = ({ cwd, topLevelFrameworkPackages })=>(config)=>{
630
667
  plugins: [],
631
668
  dotenv: {},
632
669
  lodashTreeShaking: false,
670
+ dropConsoleInProduction: true,
633
671
  ...options
634
672
  };
635
673
  // dotenv
package/dist/index.d.ts CHANGED
@@ -46,10 +46,12 @@ interface CreateWebpackOptions {
46
46
  * Path to HTML template file.
47
47
  *
48
48
  * This will be used by `html-webpack-plugin` to generate the final `index.html`.
49
+ * Default to `./src/index.html`, but you can set it to `null`, `undefined` or `false` to disable `html-webpack-plugin`.
49
50
  *
50
- * If not specificied, the `html-webpack-plugin` will not be enabled
51
+ * @default './src/index.html'
52
+
51
53
  */
52
- htmlTemplatePath?: string;
54
+ htmlTemplatePath?: string | null | undefined | false;
53
55
  output?: {
54
56
  /**
55
57
  * `output.path`, where the built files will be emitted to.
@@ -199,6 +201,15 @@ interface CreateWebpackOptions {
199
201
  */
200
202
  plugins?: Configuration['plugins'];
201
203
  dotenv?: boolean | DotenvConfigOptions;
204
+ /**
205
+ * Remove all `console.*` calls in production build.
206
+ * Uses `swc` compiler's built-in capability to do so.
207
+ *
208
+ * If you need console statements in production, set this option to `false`.
209
+ *
210
+ * @default true
211
+ */
212
+ dropConsoleInProduction?: boolean;
202
213
  /**
203
214
  * By default we will use "browserlists" package to looking for config, like `.browserslistrc` file or `browserslist` field in `package.json`, etc.
204
215
  *
package/dist/index.mjs CHANGED
@@ -17,11 +17,11 @@ import isCI from 'is-ci';
17
17
  import { confirm } from '@clack/prompts';
18
18
  import { installPackage } from '@antfu/install-pkg';
19
19
  import { castArray } from 'foxts/cast-array';
20
+ import path from 'node:path';
20
21
  import LightningCSS, { browserslistToTargets } from 'lightningcss';
21
22
  import { defineReactCompilerLoaderOption, reactCompilerLoader } from 'react-compiler-webpack';
22
- import TerserPlugin from 'terser-webpack-plugin';
23
+ import TerserWebpackPlugin from 'terser-webpack-plugin';
23
24
  import { LightningCssMinifyPlugin } from 'lightningcss-loader';
24
- import path from 'node:path';
25
25
 
26
26
  function getSupportedBrowsers(browserlists, dir, isDevelopment) {
27
27
  if (browserlists) {
@@ -273,6 +273,44 @@ const resolve = ({ cwd, lodashTreeShaking })=>async (config)=>{
273
273
  config.resolve.fallback['lodash-es/fp'] = globalRequire.resolve('lodash/fp');
274
274
  }
275
275
  }
276
+ /**
277
+ * tailwind-merge optimization
278
+ *
279
+ * webpack do not repect the order in the `conditionNames` config
280
+ * instead, it prioritize the order in the `exports` field of package.json
281
+ *
282
+ * since tailwind-merge list its CommonJS export before ESM export, webpack will
283
+ * always piroritize CommonJS version.
284
+ *
285
+ * We can use alias to force webpack to use the ESM version
286
+ */ try {
287
+ const tailwindMergeDistDir = globalRequire.resolve('tailwind-merge');
288
+ config.resolve.alias ??= {};
289
+ if (!Array.isArray(config.resolve.alias)) {
290
+ config.resolve.alias['tailwind-merge'] = path.join(tailwindMergeDistDir, '../bundle-mjs.mjs');
291
+ }
292
+ } catch {
293
+ // tailwind-merge does not exist, do nothing
294
+ }
295
+ /**
296
+ * use-sync-external-store/shim optimization
297
+ *
298
+ * React 18 and above has built-in support for useSyncExternalStore,
299
+ * no need to include shim
300
+ */ try {
301
+ const reactPackageJson = globalRequire('react/package.json');
302
+ const reactVersion = reactPackageJson.version;
303
+ const majorVersion = Number.parseInt(reactVersion.split('.')[0], 10);
304
+ if (majorVersion >= 18 || majorVersion === 0 // experimental versions
305
+ ) {
306
+ config.resolve.alias ??= {};
307
+ if (!Array.isArray(config.resolve.alias)) {
308
+ config.resolve.alias['use-sync-external-store/shim'] = 'react';
309
+ }
310
+ }
311
+ } catch {
312
+ // react does not exist, do nothing
313
+ }
276
314
  return config;
277
315
  };
278
316
 
@@ -393,10 +431,7 @@ const loaders = ({ cwd, postcss, svgr, reactCompiler, webpackExperimentalBuiltin
393
431
  },
394
432
  {
395
433
  test: /assets\//,
396
- type: 'asset/resource',
397
- generator: {
398
- filename: '_assets/[hash][ext][query]'
399
- }
434
+ type: 'asset/resource'
400
435
  }
401
436
  ]
402
437
  });
@@ -469,19 +504,21 @@ const loaders = ({ cwd, postcss, svgr, reactCompiler, webpackExperimentalBuiltin
469
504
  // };
470
505
  };
471
506
 
472
- const optimization = ({ development: isDevelopment, browserlists }, { supportedBrowsers })=>(config)=>{
507
+ const optimization = ({ development: isDevelopment, browserlists, dropConsoleInProduction }, { supportedBrowsers })=>(config)=>{
473
508
  config.optimization ??= {};
474
509
  config.optimization.emitOnErrors = !isDevelopment;
475
510
  config.optimization.checkWasmTypes = false;
476
511
  config.optimization.nodeEnv = false; // we manually bring our own DefinePlugin
477
512
  config.optimization.minimizer ??= [];
478
- config.optimization.minimizer.push(new TerserPlugin({
479
- minify: TerserPlugin.swcMinify,
513
+ config.optimization.minimizer.push(new TerserWebpackPlugin({
514
+ minify: TerserWebpackPlugin.swcMinify,
480
515
  terserOptions: {
481
516
  compress: {
482
517
  ecma: 2018,
483
518
  comparisons: false,
484
- inline: 2 // https://github.com/vercel/next.js/issues/7178#issuecomment-493048965
519
+ inline: 2,
520
+ // inline: 1,
521
+ drop_console: isDevelopment ? false : dropConsoleInProduction
485
522
  },
486
523
  mangle: {
487
524
  safari10: true
@@ -616,6 +653,7 @@ __node_cjsModule.createRequire(import.meta.url);
616
653
  plugins: [],
617
654
  dotenv: {},
618
655
  lodashTreeShaking: false,
656
+ dropConsoleInProduction: true,
619
657
  ...options
620
658
  };
621
659
  // dotenv
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@corespeed/webpack",
3
- "version": "0.1.0-beta.2",
3
+ "version": "0.1.0-beta.4",
4
4
  "description": "The opinionated webpack configuration for CoreSpeed projects.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -32,7 +32,7 @@
32
32
  "@clack/prompts": "^0.11.0",
33
33
  "@pmmmwh/react-refresh-webpack-plugin": "^0.6.2",
34
34
  "@sentry/webpack-plugin": "^4.6.1",
35
- "@swc/core": "^1.15.7",
35
+ "@swc/core": "^1.15.8",
36
36
  "@types/webpack-bundle-analyzer": "^4.7.0",
37
37
  "browserslist": "^4.28.1",
38
38
  "clean-webpack-plugin": "^4.0.0",
@@ -51,20 +51,20 @@
51
51
  "swc-loader": "^0.2.6",
52
52
  "terser-webpack-plugin": "^5.3.16",
53
53
  "tsconfig-paths-webpack-plugin": "^4.2.0",
54
- "webpack-bundle-analyzer": "^5.1.0",
54
+ "webpack-bundle-analyzer": "^5.1.1",
55
55
  "webpackbar": "^7.0.0"
56
56
  },
57
57
  "devDependencies": {
58
58
  "@svgr/webpack": "^8.1.0",
59
59
  "@swc/types": "^0.1.25",
60
- "@types/node": "^25.0.3",
60
+ "@types/node": "^25.0.6",
61
61
  "@types/webpack": "^5.28.5",
62
62
  "bumpp": "^10.3.2",
63
- "bunchee": "^6.7.0",
63
+ "bunchee": "^6.9.3",
64
64
  "core-js": "^3.47.0",
65
65
  "eslint": "^9.39.2",
66
- "eslint-config-sukka": "^8.0.6",
67
- "eslint-formatter-sukka": "^8.0.6",
66
+ "eslint-config-sukka": "^8.1.0",
67
+ "eslint-formatter-sukka": "^8.1.0",
68
68
  "typescript": "^5.9.3",
69
69
  "webpack": "^5.104.1",
70
70
  "webpack-dev-server": "^5.2.2"