-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build(infra): 使用 rollup-node 替代 webpack-node
- Loading branch information
Showing
8 changed files
with
259 additions
and
222 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.