@jscutlery/swc-angular
Version:
<!-- TOC -->
107 lines (102 loc) • 3.69 kB
JavaScript
import { join, dirname } from 'node:path';
import { version } from '@swc/core';
import { readFileSync } from 'node:fs';
function _extends() {
_extends = Object.assign || function assign(target) {
for(var i = 1; i < arguments.length; i++){
var source = arguments[i];
for(var key in source)if (Object.prototype.hasOwnProperty.call(source, key)) target[key] = source[key];
}
return target;
};
return _extends.apply(this, arguments);
}
class FileSystem {
readJsonFile(path) {
const content = readFileSync(path, 'utf-8');
return JSON.parse(content);
}
}
const fileSystem = new FileSystem();
assertCompatibleSwcCoreVersion(version);
function swcAngularPreset(options = {}) {
return {
jsc: {
parser: {
syntax: 'typescript',
decorators: true,
dynamicImport: true
},
transform: {
legacyDecorator: true,
decoratorMetadata: true,
useDefineForClassFields: options.useDefineForClassFields
},
experimental: {
plugins: [
[
'@jscutlery/swc-angular-plugin',
{
importStyles: options.importStyles,
styleInlineSuffix: options.styleInlineSuffix,
templateRawSuffix: options.templateRawSuffix
}
]
]
}
},
env: {
targets: [
'last 2 chrome versions'
],
include: [
'transform-async-to-generator'
]
},
swcrc: false
};
}
function swcAngularJestTransformer(options = {}) {
return [
'@swc/jest',
swcAngularPreset(options)
];
}
function swcAngularVitePreset(options = {}) {
return swcAngularPreset(_extends({
importStyles: true,
styleInlineSuffix: true,
templateRawSuffix: true
}, options));
}
function swcAngularUnpluginOptions(options = {}) {
return _extends({}, swcAngularVitePreset(options), {
/* Since we are using SWC's env option, we need to disable the tsconfigFile option.
* Otherwise `unpluggin-swc` will try to use the target from the tsconfig's `compilerOptions`,
* and make SWC produce the following error: "`env` and `jsc.target` cannot be used together".
*
* We could have added this extra option to `swcAngularVitePreset`, but forwarding any extra option
* to SWC will make SWC's configuration parser fail.
*
* Cf. https://github.com/unplugin/unplugin-swc/issues/137 */ tsconfigFile: false
});
}
/**
* @deprecated Use {@link swcAngularVitePreset}, {@link swcAngularJestTransformer} or {@link swcAngularPreset} instead.
*/ var index = swcAngularPreset();
function assertCompatibleSwcCoreVersion(version) {
/* Fallback to reading version from package.json when not exported.
* This happens on Stackblitz. */ if (version == undefined) {
const packageJsonPath = join(dirname(require.resolve('@swc/core')), 'package.json');
version = fileSystem.readJsonFile(packageJsonPath).version;
}
if (!version.startsWith('1.10.')) {
console.error(`
@swc/core version ${version} is incompatible with @jscutlery/swc-angular.
Please use @swc/core version 1.10.x
> npm add -D @swc/core@~1.10.0
`);
process.exit(1);
}
}
export { index as default, swcAngularJestTransformer, swcAngularPreset, swcAngularUnpluginOptions, swcAngularVitePreset };