Skip to content

Commit

Permalink
fix: fix cjs issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Oct 2, 2023
1 parent 4c6787b commit 202441c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
4 changes: 3 additions & 1 deletion core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { overridePaths } from 'kkt/lib/overrides/paths';
import { sync as gzipSize } from 'gzip-size';
import filesize from 'filesize';
import './overrides';
import { filterPlugins, removeLoaders, hasTypeModule } from './utils';
import { filterPlugins, removeLoaders, solveCjsLoaders, hasTypeModule } from './utils';

function help() {
const { version } = require('../package.json');
Expand Down Expand Up @@ -220,6 +220,8 @@ process.on('exit', (code) => {
conf = filterPlugins(conf, argvs.minify, isWeb ? fileName : null);
if (isWeb) {
conf = removeLoaders(conf);
} else {
conf = solveCjsLoaders(conf);
}
conf.entry = inputFile;
if (argvs.sourceMap) {
Expand Down
23 changes: 23 additions & 0 deletions core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,29 @@ export function removeLoaders(conf: WebpackConfiguration) {
return conf;
}

/** Solve cjs issue */
export function solveCjsLoaders(conf: WebpackConfiguration) {
conf.module.rules = conf.module.rules.map((rule) => {
if (typeof rule === 'object' && rule.oneOf) {
rule.oneOf = rule.oneOf
.map((item) =>
item &&
item.exclude &&
/@babel(?:\/|\\{1,2})runtime/.toString() === item.exclude.toString() &&
item.test.toString() === /\.(js|mjs)$/.toString()
? {
...item,
test: /\.(js|cjs|mjs)$/,
}
: item,
)
.filter(Boolean);
}
return rule;
});
return conf;
}

export function hasTypeModule(path: string) {
while (path !== (path = PATH.resolve(path, '..'))) {
try {
Expand Down
8 changes: 4 additions & 4 deletions examples/bundle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
"react-dom": "^18.2.0"
},
"devDependencies": {
"@kkt/less-modules": "^7.4.9",
"@kkt/less-modules": "^7.5.3",
"@kkt/ncc": "^1.0.15",
"@types/react": "^18.0.31",
"@types/react-dom": "^18.0.11",
"kkt": "^7.4.9"
"@types/react": "^18.2.24",
"@types/react-dom": "^18.2.8",
"kkt": "^7.5.3"
},
"eslintConfig": {
"extends": [
Expand Down

0 comments on commit 202441c

Please sign in to comment.