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-09-28 12:29:22 +02:00
|
|
|
esm = false,
|
2022-04-12 16:32:42 +02:00
|
|
|
} = {}
|
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'
|
|
|
|
|
2022-07-26 21:23:19 +02:00
|
|
|
/** @type {import('@swc/core').Options} */
|
2021-08-17 09:18:08 +02:00
|
|
|
const swcClientOptions = {
|
|
|
|
module: {
|
2022-09-28 12:29:22 +02:00
|
|
|
type: esm ? 'es6' : '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-07-26 21:23:19 +02:00
|
|
|
externalHelpers: true,
|
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
|
|
|
|
2022-07-26 21:23:19 +02:00
|
|
|
/** @type {import('@swc/core').Options} */
|
2021-08-17 09:18:08 +02:00
|
|
|
const swcServerOptions = {
|
|
|
|
module: {
|
2022-09-28 12:29:22 +02:00
|
|
|
type: esm ? 'es6' : '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-07-26 21:23:19 +02:00
|
|
|
// Do not enable external helpers on server-side files build
|
|
|
|
// _is_native_funtion helper is not compatible with edge runtime (need investigate)
|
|
|
|
externalHelpers: false,
|
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,
|
|
|
|
}
|
|
|
|
|
2022-09-17 00:12:59 +02:00
|
|
|
const source = file.data.toString('utf-8')
|
|
|
|
const output = yield transform(source, options)
|
2021-08-17 09:18:08 +02:00
|
|
|
const ext = path.extname(file.base)
|
|
|
|
|
2022-09-17 00:12:59 +02:00
|
|
|
// Make sure the output content keeps the `"client"` directive.
|
|
|
|
// TODO: Remove this once SWC fixes the issue.
|
2022-09-18 02:00:16 +02:00
|
|
|
if (/^['"]client['"]/.test(source)) {
|
2022-09-17 00:12:59 +02:00
|
|
|
output.code = '"client";\n' + output.code
|
|
|
|
}
|
|
|
|
|
2021-08-17 09:18:08 +02:00
|
|
|
// 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-09-28 12:29:22 +02:00
|
|
|
if (interopClientDefaultExport && !esm) {
|
2022-04-12 16:32:42 +02:00
|
|
|
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}"`
|
|
|
|
)
|
|
|
|
}
|