Updated swc plugin

This commit is contained in:
LFades 2022-02-08 17:39:37 -05:00
parent b13fb6af53
commit fcba5e9996
2 changed files with 10 additions and 16 deletions

View File

@ -2,21 +2,19 @@
// https://github.com/lukeed/taskr/pull/305 // https://github.com/lukeed/taskr/pull/305
const path = require('path') const path = require('path')
// eslint-disable-next-line import/no-extraneous-dependencies
const transform = require('@swc/core').transform const transform = require('@swc/core').transform
module.exports = function (task) { module.exports = function (task) {
// eslint-disable-next-line require-yield
task.plugin( task.plugin(
'swc', 'swc',
{}, {},
function* (file, serverOrClient, { stripExtension, dev, outDir = 'dist', baseUrl = '' } = {}) { function* (
file,
{ server = true, stripExtension, dev, outDir = 'dist', baseUrl = '' } = {}
) {
// Don't compile .d.ts // Don't compile .d.ts
if (file.base.endsWith('.d.ts')) return if (file.base.endsWith('.d.ts')) return
const isClient = serverOrClient === 'client'
const swcClientOptions = { const swcClientOptions = {
module: { module: {
type: 'es6', type: 'es6',
@ -24,7 +22,6 @@ module.exports = function (task) {
}, },
jsc: { jsc: {
loose: true, loose: true,
target: 'es2016', target: 'es2016',
parser: { parser: {
syntax: 'typescript', syntax: 'typescript',
@ -42,7 +39,6 @@ module.exports = function (task) {
}, },
}, },
} }
const swcServerOptions = { const swcServerOptions = {
module: { module: {
type: 'es6', type: 'es6',
@ -50,12 +46,11 @@ module.exports = function (task) {
}, },
env: { env: {
targets: { targets: {
node: '12.0.0', node: '14.0.0',
}, },
}, },
jsc: { jsc: {
loose: true, loose: true,
parser: { parser: {
syntax: 'typescript', syntax: 'typescript',
dynamicImport: true, dynamicImport: true,
@ -73,7 +68,7 @@ module.exports = function (task) {
}, },
} }
const swcOptions = isClient ? swcClientOptions : swcServerOptions const swcOptions = server ? swcServerOptions : swcClientOptions
// Using `outDir` and `baseUrl` build a relative path from `outDir` to // Using `outDir` and `baseUrl` build a relative path from `outDir` to
// the `baseUrl` path for source maps // the `baseUrl` path for source maps
@ -81,16 +76,15 @@ module.exports = function (task) {
const basePath = path.join(__dirname, baseUrl) const basePath = path.join(__dirname, baseUrl)
const relativeFilePath = path.relative(basePath, filePath) const relativeFilePath = path.relative(basePath, filePath)
const fullFilePath = path.join(__dirname, filePath) const fullFilePath = path.join(__dirname, filePath)
const distFilePath = path.dirname(path.join(__dirname, outDir, relativeFilePath)) const distFilePath = path.dirname(
path.join(__dirname, outDir, relativeFilePath)
)
const options = { const options = {
filename: filePath, filename: filePath,
sourceMaps: true, sourceMaps: true,
sourceFileName: path.relative(distFilePath, fullFilePath), sourceFileName: path.relative(distFilePath, fullFilePath),
...swcOptions, ...swcOptions,
} }
const output = yield transform(file.data.toString('utf-8'), options) const output = yield transform(file.data.toString('utf-8'), options)
const ext = path.extname(file.base) const ext = path.extname(file.base)

View File

@ -1,7 +1,7 @@
export async function build(task, opts) { export async function build(task, opts) {
await task await task
.source('src/**/*.+(ts|tsx|js)') .source('src/**/*.+(ts|tsx|js)')
.swc('server', { dev: opts.dev, outDir: 'dist', baseUrl: 'src' }) .swc({ dev: opts.dev, outDir: 'dist', baseUrl: 'src' })
.target('dist') .target('dist')
.source('src/**/*.+(json|cjs)') .source('src/**/*.+(json|cjs)')
.target('dist') .target('dist')