Added taskr to packages/local

This commit is contained in:
LFades 2022-02-08 17:06:48 -05:00
parent 4a79400907
commit da42f55591
4 changed files with 1210 additions and 11 deletions

View File

@ -3,10 +3,17 @@
"version": "0.0.1", "version": "0.0.1",
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"build": "rm -fr dist/* && tsc", "release": "taskr release",
"dev": "npm run build -- --watch", "build": "taskr build",
"dev": "taskr",
"types": "tsc --declaration --emitDeclarationOnly --declarationDir dist",
"prettier-fix": "prettier --write ." "prettier-fix": "prettier --write ."
}, },
"taskr": {
"requires": [
"./taskfile-swc.cjs"
]
},
"sideEffects": false, "sideEffects": false,
"type": "module", "type": "module",
"exports": { "exports": {
@ -54,6 +61,10 @@
"react-dom": "^17" "react-dom": "^17"
}, },
"devDependencies": { "devDependencies": {
"@swc/core": "^1.2.138",
"@taskr/clear": "^1.1.0",
"@taskr/esnext": "^1.1.0",
"@taskr/watch": "^1.1.0",
"@types/node": "^17.0.8", "@types/node": "^17.0.8",
"@types/react": "^17.0.38", "@types/react": "^17.0.38",
"lint-staged": "^12.1.7", "lint-staged": "^12.1.7",
@ -61,6 +72,7 @@
"prettier": "^2.5.1", "prettier": "^2.5.1",
"react": "^17.0.2", "react": "^17.0.2",
"react-dom": "^17.0.2", "react-dom": "^17.0.2",
"taskr": "^1.1.0",
"typescript": "^4.5.4" "typescript": "^4.5.4"
}, },
"lint-staged": { "lint-staged": {

View File

@ -0,0 +1,120 @@
// taskr babel plugin with Babel 7 support
// https://github.com/lukeed/taskr/pull/305
const path = require('path')
// eslint-disable-next-line import/no-extraneous-dependencies
const transform = require('@swc/core').transform
module.exports = function (task) {
// eslint-disable-next-line require-yield
task.plugin(
'swc',
{},
function* (file, serverOrClient, { stripExtension, dev, outDir = 'dist', baseUrl = '' } = {}) {
// Don't compile .d.ts
if (file.base.endsWith('.d.ts')) return
const isClient = serverOrClient === 'client'
const swcClientOptions = {
module: {
type: 'es6',
ignoreDynamic: true,
},
jsc: {
loose: true,
target: 'es2016',
parser: {
syntax: 'typescript',
dynamicImport: true,
tsx: file.base.endsWith('.tsx'),
},
transform: {
react: {
pragma: 'React.createElement',
pragmaFrag: 'React.Fragment',
throwIfNamespace: true,
development: false,
useBuiltins: true,
},
},
},
}
const swcServerOptions = {
module: {
type: 'es6',
ignoreDynamic: true,
},
env: {
targets: {
node: '12.0.0',
},
},
jsc: {
loose: true,
parser: {
syntax: 'typescript',
dynamicImport: true,
tsx: file.base.endsWith('.tsx'),
},
transform: {
react: {
pragma: 'React.createElement',
pragmaFrag: 'React.Fragment',
throwIfNamespace: true,
development: false,
useBuiltins: true,
},
},
},
}
const swcOptions = isClient ? swcClientOptions : swcServerOptions
// Using `outDir` and `baseUrl` build a relative path from `outDir` to
// the `baseUrl` path for source maps
const filePath = path.join(file.dir, file.base)
const basePath = path.join(__dirname, baseUrl)
const relativeFilePath = path.relative(basePath, filePath)
const fullFilePath = path.join(__dirname, filePath)
const distFilePath = path.dirname(path.join(__dirname, outDir, relativeFilePath))
const options = {
filename: filePath,
sourceMaps: true,
sourceFileName: path.relative(distFilePath, fullFilePath),
...swcOptions,
}
const output = yield transform(file.data.toString('utf-8'), options)
const ext = path.extname(file.base)
// Replace `.ts|.tsx` with `.js` in files with an extension
if (ext) {
const extRegex = new RegExp(ext.replace('.', '\\.') + '$', 'i')
// Remove the extension if stripExtension is enabled or replace it with `.js`
file.base = file.base.replace(extRegex, stripExtension ? '' : '.js')
}
if (output.map) {
const map = `${file.base}.map`
output.code += Buffer.from(`\n//# sourceMappingURL=${map}`)
// add sourcemap to `files` array
this._.files.push({
base: map,
dir: file.dir,
data: Buffer.from(output.map),
})
}
file.data = Buffer.from(output.code)
}
)
}

View File

@ -0,0 +1,17 @@
export async function build(task, opts) {
await task
.source('src/**/*.+(ts|tsx|js|cjs)')
.swc('server', { dev: opts.dev, outDir: 'dist', baseUrl: 'src' })
.target('dist')
task.$.log('Compiled src files')
}
export async function release(task) {
await task.clear('dist').start('build')
}
export default async function dev(task) {
const opts = { dev: true }
await task.clear('dist')
await task.start('build', opts)
}

1068
yarn.lock

File diff suppressed because it is too large Load Diff