Skip to content

Commit

Permalink
build(infra): 使用 rollup-node 替代 webpack-node
Browse files Browse the repository at this point in the history
  • Loading branch information
EAGzzyCSL committed Mar 17, 2024
1 parent 41f63cd commit 0355bce
Show file tree
Hide file tree
Showing 8 changed files with 259 additions and 222 deletions.
70 changes: 70 additions & 0 deletions packages/rollup-node/bin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env node
import fs from 'fs'
import path from 'path'

import nodeResolve from '@rollup/plugin-node-resolve'
import typescript from '@rollup/plugin-typescript'
import { rollup } from 'rollup'

const compileToJs = async (entryPath, tsConfigPath) => {
const bundle = await rollup({
input: path.resolve(entryPath),
cache: false,
plugins: [
nodeResolve(),
typescript({
tsconfig: tsConfigPath,
sourceMap: false,
}),
{
name: 'plugin-static',
load(id) {
if (
id &&
['.png', '.svg', '.jpg', '.jpeg', '.webp'].includes(
path.extname(id),
)
) {
return 'export default {}'
}
return null
},
},
],
})
const result = await bundle.generate({
format: 'iife',
sourcemap: false,
})
const { code } = result.output[0]
return code
}

const run = async (entryPath, tsConfigPath) => {
const scriptContent = await compileToJs(entryPath, tsConfigPath)

// 利用eval执行脚本获取返回值
// eslint-disable-next-line no-eval
return eval(scriptContent)
}

const [, , entryPath, tsConfigPath] = process.argv

const getPathAndCheck = (name, rPath) => {
if (!rPath) {
throw new Error(`${name} ${rPath} 未定义`)
}

const aPath = path.resolve(process.cwd(), rPath)

if (fs.existsSync(aPath)) {
return aPath
}

throw new Error(`${name} ${rPath} 不存在`)
}

run(
getPathAndCheck('入口文件', entryPath),
getPathAndCheck('tsconfig', tsConfigPath),
)
19 changes: 19 additions & 0 deletions packages/rollup-node/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "@mine/rollup-node",
"version": "0.0.0",
"private": true,
"type": "module",
"bin": {
"rollup-node": "bin.js"
},
"scripts": {
"lint:es": "eslint --ext .js,.ts,.tsx ./",
"lint:es-fix": "npm run lint:es -- --fix"
},
"dependencies": {
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-typescript": "^11.1.6",
"rollup": "^4.13.0",
"typescript": "^5.1.3"
}
}
4 changes: 2 additions & 2 deletions packages/site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"lint:es": "eslint --ext .js,.mjs,.ts,.tsx ./",
"lint:es-fix": "npm run lint:es -- --fix",
"qr": "ts-node ./scripts/qr.mjs",
"route": "webpack-node ./scripts/route.ts ./scripts/tsconfig.route.json",
"route": "rollup-node ./scripts/route.ts ./scripts/tsconfig.route.json",
"serve": "next start",
"storybook": "storybook dev -p 6006",
"ts:check": "tsc --noEmit",
Expand All @@ -33,7 +33,7 @@
"@emotion/react": "^11.11.0",
"@emotion/styled": "^11.11.0",
"@mine/markdown-loader": "^0.0.0",
"@mine/webpack-node": "^0.0.0",
"@mine/rollup-node": "^0.0.0",
"@mui/icons-material": "^5.15.5",
"@mui/lab": "^5.0.0-alpha.165",
"@mui/material": "^5.15.5",
Expand Down
6 changes: 4 additions & 2 deletions packages/site/scripts/tsconfig.route.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"extends": "../tsconfig.json",
"compilerOptions": {
"rootDir": "../",
"importHelpers": false
"importHelpers": false,
"target": "ESNext",
"incremental": false
},
"include": ["../route.ts", "../global.d.ts"]
"include": ["./route.ts", "../global.d.ts"]
}
26 changes: 0 additions & 26 deletions packages/webpack-node/bin.js

This file was deleted.

121 changes: 0 additions & 121 deletions packages/webpack-node/index.js

This file was deleted.

24 changes: 0 additions & 24 deletions packages/webpack-node/package.json

This file was deleted.

Loading

0 comments on commit 0355bce

Please sign in to comment.