Skip to content

Commit

Permalink
fix: fix nodejs bundle issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Oct 2, 2023
1 parent 202441c commit 6b45f25
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 26 deletions.
6 changes: 3 additions & 3 deletions 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, solveCjsLoaders, hasTypeModule } from './utils';
import { filterPlugins, removeLoaders, hasTypeModule } from './utils';

function help() {
const { version } = require('../package.json');
Expand Down Expand Up @@ -212,7 +212,7 @@ process.on('exit', (code) => {

const oPaths = { appBuild: outDir, appIndexJs: inputFile, appPublic: publicFolder };
const isWeb = /^(web|browserslist)$/.test(argvs.target);
const target = isWeb ? argvs.target : argvs.target ? ['node14', argvs.target] : 'node14';
const target = isWeb ? argvs.target : argvs.target ? ['node16', argvs.target] : 'node16';
fs.ensureDirSync(outDir);
overridePaths(undefined, { ...oPaths });
argvs.overridesWebpack = (conf, env, options) => {
Expand All @@ -221,7 +221,7 @@ process.on('exit', (code) => {
if (isWeb) {
conf = removeLoaders(conf);
} else {
conf = solveCjsLoaders(conf);
conf.module.rules = [];
}
conf.entry = inputFile;
if (argvs.sourceMap) {
Expand Down
23 changes: 0 additions & 23 deletions core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,6 @@ 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
2 changes: 2 additions & 0 deletions examples/github-action/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Basic Example for github-actions
===
18 changes: 18 additions & 0 deletions examples/github-action/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "@template/github-actions",
"version": "1.0.15",
"private": true,
"scripts": {
"build": "npm run bundle && npm run bundle:min",
"bundle": "ncc build src/action.ts",
"watch": "ncc watch src/action.ts",
"bundle:min": "ncc build src/action.ts -m"
},
"engines": {
"node": ">=16"
},
"dependencies": {
"@actions/core": "~1.10.0",
"markdown-to-html-cli": "^4.0.0"
}
}
13 changes: 13 additions & 0 deletions examples/github-action/src/action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import fs from 'fs';
import path from 'path';
import { RunArgvs, formatConfig, create } from 'markdown-to-html-cli';

(async () => {
try {
const outputPath = path.resolve('output');
const htmlStr = create({});
fs.writeFileSync(outputPath, htmlStr);
} catch (error) {
console.log('error::', error);
}
})();
20 changes: 20 additions & 0 deletions examples/github-action/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"declaration": true,
"target": "es2017",
"noImplicitAny": true,
"resolveJsonModule": true,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": false,
"strict": false,
"skipLibCheck": true,
"outDir": "lib",
"allowJs": true,
"baseUrl": "."
},
"include": ["src/**/*"]
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"-----↓↓↓↓↓↓↓↓↓↓↓↓↓↓-----": "----------",
"build:bundle": "lerna exec \"npm run build:bundle\" --scope @template/bundle",
"build:bundle:node": "lerna exec \"npm run build\" --scope @template/bundle-node",
"build:bundle:action": "lerna exec \"npm run bundle\" --scope @template/github-actions",
"-----↑↑↑↑↑↑↑↑↑↑↑↑↑↑-----": "----------",
"watch": "lerna exec \"tsbb watch\" --scope @kkt/ncc",
"build": "lerna exec \"tsbb build\" --scope @kkt/ncc",
Expand Down

0 comments on commit 6b45f25

Please sign in to comment.