@qse/edu-scripts 1.14.8 → 1.14.9

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.
@@ -26,6 +26,7 @@ var fs = require("fs");
26
26
  var paths = require("./paths");
27
27
  var appConfig = require("../utils/appConfig");
28
28
  var getOverride = require("../utils/getOverride");
29
+ var addWebpackChunkName = require("./plugins/babel-plugin-add-webpack-chunk-name");
29
30
  var hasJsxRuntime = (() => {
30
31
  try {
31
32
  require.resolve("react/jsx-runtime");
@@ -90,7 +91,8 @@ module.exports = function getBabelConfig(opts = {}) {
90
91
  { libraryName: "lodash", libraryDirectory: "", camel2DashComponentName: false },
91
92
  "lodash"
92
93
  ],
93
- isDev && "react-refresh/babel"
94
+ isDev && "react-refresh/babel",
95
+ addWebpackChunkName
94
96
  ].filter(Boolean)
95
97
  };
96
98
  const override = getOverride();
@@ -0,0 +1,31 @@
1
+ // src/config/plugins/babel-plugin-add-webpack-chunk-name.js
2
+ function removeTrailingIndex(string) {
3
+ return string.split("_").length - 1 > 1 ? string.replace(/_index$/, "") : string;
4
+ }
5
+ function convertToKebabCase(string) {
6
+ return string.replace(/\s+/g, "_");
7
+ }
8
+ function getChunkName(filename) {
9
+ filename = filename.split("/").map((part) => part.replace(/\..*$/, "").replace(/[^a-zA-Z0-9-_/]/g, "")).filter(Boolean).map(convertToKebabCase).join("_");
10
+ return removeTrailingIndex(filename);
11
+ }
12
+ function hasComment(comment) {
13
+ return comment && comment.value.replace(/\*+/g, "").trim().startsWith("webpackChunkName");
14
+ }
15
+ function addWebpackChunkName({ types: t }) {
16
+ return {
17
+ name: "webpack-chunk-name-comments",
18
+ visitor: {
19
+ Import(path) {
20
+ const [arg] = path.parent.arguments;
21
+ if (!t.isStringLiteral(arg))
22
+ return;
23
+ const [comment] = arg.leadingComments || [];
24
+ if (!hasComment(comment)) {
25
+ t.addComment(arg, "leading", `webpackChunkName: "${getChunkName(arg.value)}"`);
26
+ }
27
+ }
28
+ }
29
+ };
30
+ }
31
+ module.exports = addWebpackChunkName;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@qse/edu-scripts",
3
- "version": "1.14.8",
3
+ "version": "1.14.9",
4
4
  "author": "Kinoko",
5
5
  "license": "MIT",
6
6
  "description": "教育工程化基础框架",
@@ -2,6 +2,7 @@ const fs = require('fs')
2
2
  const paths = require('./paths')
3
3
  const appConfig = require('../utils/appConfig')
4
4
  const getOverride = require('../utils/getOverride')
5
+ const addWebpackChunkName = require('./plugins/babel-plugin-add-webpack-chunk-name')
5
6
  const hasJsxRuntime = (() => {
6
7
  try {
7
8
  require.resolve('react/jsx-runtime')
@@ -76,6 +77,7 @@ module.exports = function getBabelConfig(opts = {}) {
76
77
  'lodash',
77
78
  ],
78
79
  isDev && 'react-refresh/babel',
80
+ addWebpackChunkName,
79
81
  ].filter(Boolean),
80
82
  }
81
83
 
@@ -0,0 +1,55 @@
1
+ // Modified from `babel-plugin-dynamic-import-chunk-name` as it used a slightly
2
+ // different naming system than what we wanted. Using the plugin and correcting
3
+ // the names after the fact would be more effort than forking. Additionally, the
4
+ // plugin does not have source code available online. While we're unlikely to run
5
+ // into issues, having a way to create fixes ourselves would be beneficial.
6
+ //
7
+ // https://www.npmjs.im/babel-plugin-dynamic-import-chunk-name
8
+ // MIT Licensed: https://www.runpkg.com/?babel-plugin-dynamic-import-chunk-name@1.0.0/LICENSE
9
+
10
+ function removeTrailingIndex(string) {
11
+ return string.split('_').length - 1 > 1 ? string.replace(/_index$/, '') : string
12
+ }
13
+
14
+ function convertToKebabCase(string) {
15
+ return string.replace(/\s+/g, '_')
16
+ }
17
+
18
+ function getChunkName(filename) {
19
+ filename = filename
20
+ .split('/')
21
+ .map((part) => part.replace(/\..*$/, '').replace(/[^a-zA-Z0-9-_/]/g, ''))
22
+ .filter(Boolean)
23
+ .map(convertToKebabCase)
24
+ .join('_')
25
+
26
+ return removeTrailingIndex(filename)
27
+ }
28
+
29
+ function hasComment(comment) {
30
+ return comment && comment.value.replace(/\*+/g, '').trim().startsWith('webpackChunkName')
31
+ }
32
+
33
+ /**
34
+ * @param {import('@babel/core')} babel
35
+ * @returns {import('@babel/core').PluginObj}
36
+ */
37
+ function addWebpackChunkName({ types: t }) {
38
+ return {
39
+ name: 'webpack-chunk-name-comments',
40
+ visitor: {
41
+ Import(path) {
42
+ const [arg] = path.parent.arguments
43
+ if (!t.isStringLiteral(arg)) return
44
+
45
+ const [comment] = arg.leadingComments || []
46
+
47
+ if (!hasComment(comment)) {
48
+ t.addComment(arg, 'leading', `webpackChunkName: "${getChunkName(arg.value)}"`)
49
+ }
50
+ },
51
+ },
52
+ }
53
+ }
54
+
55
+ module.exports = addWebpackChunkName