2021-07-14 20:12:04 +02:00
|
|
|
// 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
|
2021-08-17 09:18:08 +02:00
|
|
|
task.plugin(
|
|
|
|
'swc',
|
|
|
|
{},
|
2022-03-06 21:41:22 +01:00
|
|
|
function* (
|
|
|
|
file,
|
|
|
|
serverOrClient,
|
2022-04-12 16:32:42 +02:00
|
|
|
{
|
|
|
|
stripExtension,
|
|
|
|
keepImportAssertions = false,
|
|
|
|
interopClientDefaultExport = false,
|
|
|
|
} = {}
|
2022-03-06 21:41:22 +01:00
|
|
|
) {
|
2021-08-17 09:18:08 +02:00
|
|
|
// Don't compile .d.ts
|
|
|
|
if (file.base.endsWith('.d.ts')) return
|
|
|
|
|
|
|
|
const isClient = serverOrClient === 'client'
|
|
|
|
|
|
|
|
const swcClientOptions = {
|
|
|
|
module: {
|
|
|
|
type: 'commonjs',
|
2021-10-16 14:22:42 +02:00
|
|
|
ignoreDynamic: true,
|
2021-07-28 17:10:51 +02:00
|
|
|
},
|
2021-08-17 09:18:08 +02:00
|
|
|
jsc: {
|
|
|
|
loose: true,
|
2022-06-19 20:19:23 +02:00
|
|
|
|
2021-08-17 09:18:08 +02:00
|
|
|
target: 'es2016',
|
|
|
|
parser: {
|
|
|
|
syntax: 'typescript',
|
|
|
|
dynamicImport: true,
|
2022-03-06 21:41:22 +01:00
|
|
|
importAssertions: true,
|
2021-08-17 09:18:08 +02:00
|
|
|
tsx: file.base.endsWith('.tsx'),
|
|
|
|
},
|
2022-03-06 21:41:22 +01:00
|
|
|
experimental: {
|
|
|
|
keepImportAssertions,
|
|
|
|
},
|
2021-08-17 09:18:08 +02:00
|
|
|
transform: {
|
|
|
|
react: {
|
|
|
|
pragma: 'React.createElement',
|
|
|
|
pragmaFrag: 'React.Fragment',
|
|
|
|
throwIfNamespace: true,
|
|
|
|
development: false,
|
|
|
|
useBuiltins: true,
|
|
|
|
},
|
2021-07-28 17:10:51 +02:00
|
|
|
},
|
|
|
|
},
|
2021-08-17 09:18:08 +02:00
|
|
|
}
|
2021-07-28 17:10:51 +02:00
|
|
|
|
2021-08-17 09:18:08 +02:00
|
|
|
const swcServerOptions = {
|
|
|
|
module: {
|
|
|
|
type: 'commonjs',
|
2021-10-16 14:22:42 +02:00
|
|
|
ignoreDynamic: true,
|
2021-07-14 20:12:04 +02:00
|
|
|
},
|
2021-08-17 09:18:08 +02:00
|
|
|
env: {
|
|
|
|
targets: {
|
|
|
|
node: '12.0.0',
|
2021-07-14 20:12:04 +02:00
|
|
|
},
|
|
|
|
},
|
2021-08-17 09:18:08 +02:00
|
|
|
jsc: {
|
|
|
|
loose: true,
|
2022-06-19 20:19:23 +02:00
|
|
|
|
2021-08-17 09:18:08 +02:00
|
|
|
parser: {
|
|
|
|
syntax: 'typescript',
|
|
|
|
dynamicImport: true,
|
2022-03-06 21:41:22 +01:00
|
|
|
importAssertions: true,
|
2021-08-17 09:18:08 +02:00
|
|
|
tsx: file.base.endsWith('.tsx'),
|
|
|
|
},
|
2022-03-06 21:41:22 +01:00
|
|
|
experimental: {
|
|
|
|
keepImportAssertions,
|
|
|
|
},
|
2021-08-17 09:18:08 +02:00
|
|
|
transform: {
|
|
|
|
react: {
|
|
|
|
pragma: 'React.createElement',
|
|
|
|
pragmaFrag: 'React.Fragment',
|
|
|
|
throwIfNamespace: true,
|
|
|
|
development: false,
|
|
|
|
useBuiltins: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
const swcOptions = isClient ? swcClientOptions : swcServerOptions
|
|
|
|
|
|
|
|
const filePath = path.join(file.dir, file.base)
|
|
|
|
const fullFilePath = path.join(__dirname, filePath)
|
|
|
|
const distFilePath = path.dirname(path.join(__dirname, 'dist', filePath))
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
filename: path.join(file.dir, file.base),
|
|
|
|
sourceMaps: true,
|
2022-03-06 21:41:22 +01:00
|
|
|
inlineSourcesContent: false,
|
2021-08-17 09:18:08 +02:00
|
|
|
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) {
|
2022-04-12 16:32:42 +02:00
|
|
|
if (interopClientDefaultExport) {
|
|
|
|
output.code += `
|
2022-05-18 11:41:13 +02:00
|
|
|
if ((typeof exports.default === 'function' || (typeof exports.default === 'object' && exports.default !== null)) && typeof exports.default.__esModule === 'undefined') {
|
2022-05-13 18:39:38 +02:00
|
|
|
Object.defineProperty(exports.default, '__esModule', { value: true });
|
2022-04-12 16:32:42 +02:00
|
|
|
Object.assign(exports.default, exports);
|
|
|
|
module.exports = exports.default;
|
|
|
|
}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
2021-08-17 09:18:08 +02:00
|
|
|
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(setNextVersion(output.code))
|
2021-07-14 20:12:04 +02:00
|
|
|
}
|
2021-08-17 09:18:08 +02:00
|
|
|
)
|
2021-07-14 20:12:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function setNextVersion(code) {
|
|
|
|
return code.replace(
|
|
|
|
/process\.env\.__NEXT_VERSION/g,
|
|
|
|
`"${require('./package.json').version}"`
|
|
|
|
)
|
|
|
|
}
|