rollup-plugin-swc3
Advanced tools
Comparing version 0.10.0 to 0.10.1
@@ -1,184 +0,1 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var pluginutils = require('@rollup/pluginutils'); | ||
var core = require('@swc/core'); | ||
var createDeepMerge = require('@fastify/deepmerge'); | ||
var getTsconfig = require('get-tsconfig'); | ||
var rollupSwcPreserveDirectives = require('rollup-swc-preserve-directives'); | ||
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; } | ||
var fs__default = /*#__PURE__*/_interopDefault(fs); | ||
var path__default = /*#__PURE__*/_interopDefault(path); | ||
var createDeepMerge__default = /*#__PURE__*/_interopDefault(createDeepMerge); | ||
var rollupSwcPreserveDirectives__default = /*#__PURE__*/_interopDefault(rollupSwcPreserveDirectives); | ||
const cache = new Map(); | ||
const getOptions = (cwd, tsconfig)=>{ | ||
var _result, _result1; | ||
const cacheKey = `${cwd}:${tsconfig !== null && tsconfig !== void 0 ? tsconfig : 'undefined'}`; | ||
if (cache.has(cacheKey)) { | ||
var _cache_get; | ||
return (_cache_get = cache.get(cacheKey)) !== null && _cache_get !== void 0 ? _cache_get : {}; | ||
} | ||
if (tsconfig && path__default.default.isAbsolute(tsconfig)) { | ||
var _parseTsconfig_compilerOptions; | ||
const compilerOptions = (_parseTsconfig_compilerOptions = getTsconfig.parseTsconfig(tsconfig).compilerOptions) !== null && _parseTsconfig_compilerOptions !== void 0 ? _parseTsconfig_compilerOptions : {}; | ||
if (compilerOptions.baseUrl != null) { | ||
compilerOptions.baseUrl = path__default.default.resolve(path__default.default.dirname(tsconfig), compilerOptions.baseUrl); | ||
} | ||
cache.set(cacheKey, compilerOptions); | ||
return compilerOptions; | ||
} | ||
let result = getTsconfig.getTsconfig(cwd, tsconfig || 'tsconfig.json'); | ||
// Only fallback to `jsconfig.json` when tsconfig can not be resolved AND custom tsconfig filename is not provided | ||
if (!result && !tsconfig) { | ||
result = getTsconfig.getTsconfig(cwd, 'jsconfig.json'); | ||
} | ||
var _result_config_compilerOptions; | ||
const compilerOptions = (_result_config_compilerOptions = (_result = result) === null || _result === void 0 ? void 0 : _result.config.compilerOptions) !== null && _result_config_compilerOptions !== void 0 ? _result_config_compilerOptions : {}; | ||
if (((_result1 = result) === null || _result1 === void 0 ? void 0 : _result1.path) && compilerOptions.baseUrl != null) { | ||
compilerOptions.baseUrl = path__default.default.resolve(path__default.default.dirname(result.path), compilerOptions.baseUrl); | ||
} | ||
cache.set(cacheKey, compilerOptions); | ||
return compilerOptions; | ||
}; | ||
const INCLUDE_REGEXP = /\.[mc]?[jt]sx?$/; | ||
const EXCLUDE_REGEXP = /node_modules/; | ||
const ACCEPTED_EXTENSIONS = [ | ||
'.ts', | ||
'.tsx', | ||
'.mjs', | ||
'.js', | ||
'.cjs', | ||
'.jsx' | ||
]; | ||
const deepmerge = createDeepMerge__default.default({ | ||
all: true, | ||
mergeArray (options) { | ||
// overwrite instead of concatenating arrays | ||
return (target, source)=>options.clone(source); | ||
} | ||
}); | ||
const fileExists = (path)=>{ | ||
return fs__default.default.promises.access(path, fs__default.default.constants.F_OK).then(()=>true).catch(()=>false); | ||
}; | ||
function swc(options = {}) { | ||
const filter = pluginutils.createFilter(options.include || INCLUDE_REGEXP, options.exclude || EXCLUDE_REGEXP); | ||
const extensions = options.extensions || ACCEPTED_EXTENSIONS; | ||
const resolveFile = async (resolved, index = false)=>{ | ||
const fileWithoutExt = resolved.replace(INCLUDE_REGEXP, ''); | ||
for (const ext of extensions){ | ||
const file = index ? path.join(resolved, `index${ext}`) : `${fileWithoutExt}${ext}`; | ||
// We need to respect the order, and we only check one file at a time, and we can return early | ||
// eslint-disable-next-line no-await-in-loop -- see above | ||
if (await fileExists(file)) return file; | ||
} | ||
return null; | ||
}; | ||
return { | ||
name: 'swc', | ||
async resolveId (importee, importer) { | ||
// ignore IDs with null character, these belong to other plugins | ||
if (importee.startsWith('\0')) { | ||
return null; | ||
} | ||
if (importer && importee.startsWith('.')) { | ||
const resolved = path.resolve(importer ? path.dirname(importer) : process.cwd(), importee); | ||
let file = await resolveFile(resolved); | ||
if (file) return file; | ||
if (!file && await fileExists(resolved) && (await fs__default.default.promises.stat(resolved)).isDirectory()) { | ||
file = await resolveFile(resolved, true); | ||
if (file) return file; | ||
} | ||
} | ||
}, | ||
async transform (code, id) { | ||
var _tsconfigOptions_target; | ||
if (!filter(id)) { | ||
return null; | ||
} | ||
const ext = path.extname(id); | ||
if (!extensions.includes(ext)) return null; | ||
const isTypeScript = ext === '.ts' || ext === '.mts' || ext === '.cts' || ext === '.tsx'; | ||
const isTsx = ext === '.tsx'; | ||
const isJsx = ext === '.jsx'; | ||
const tsconfigOptions = options.tsconfig === false ? {} : getOptions(path.dirname(id), options.tsconfig); | ||
// TODO: SWC is about to add "preserve" jsx | ||
// https://github.com/swc-project/swc/pull/5661 | ||
// Respect "preserve" after swc adds the support | ||
const useReact17NewTransform = tsconfigOptions.jsx === 'react-jsx' || tsconfigOptions.jsx === 'react-jsxdev'; | ||
const swcOptionsFromTsConfig = { | ||
jsc: { | ||
externalHelpers: tsconfigOptions.importHelpers, | ||
parser: { | ||
syntax: isTypeScript ? 'typescript' : 'ecmascript', | ||
tsx: isTypeScript ? isTsx : undefined, | ||
jsx: !isTypeScript ? isJsx : undefined, | ||
decorators: tsconfigOptions.experimentalDecorators | ||
}, | ||
transform: { | ||
decoratorMetadata: tsconfigOptions.emitDecoratorMetadata, | ||
react: { | ||
runtime: useReact17NewTransform ? 'automatic' : 'classic', | ||
importSource: tsconfigOptions.jsxImportSource, | ||
pragma: tsconfigOptions.jsxFactory, | ||
pragmaFrag: tsconfigOptions.jsxFragmentFactory, | ||
development: tsconfigOptions.jsx === 'react-jsxdev' ? true : undefined | ||
} | ||
}, | ||
target: (_tsconfigOptions_target = tsconfigOptions.target) === null || _tsconfigOptions_target === void 0 ? void 0 : _tsconfigOptions_target.toLowerCase(), | ||
baseUrl: tsconfigOptions.baseUrl, | ||
paths: tsconfigOptions.paths | ||
} | ||
}; | ||
const { // @ts-expect-error -- We have to make sure that we don't pass these options to swc | ||
filename: _1, include: _2, exclude: _3, tsconfig: _4, extensions: _5, minify: _6, ...restSwcOptions } = options; | ||
const swcOption = deepmerge(swcOptionsFromTsConfig, restSwcOptions, { | ||
jsc: { | ||
minify: undefined // Disable minify on transform, do it on renderChunk | ||
}, | ||
filename: id, | ||
minify: false // Disable minify on transform, do it on renderChunk | ||
}); | ||
return core.transform(code, swcOption); | ||
}, | ||
renderChunk (code) { | ||
var _options_jsc_minify, _options_jsc, _options_jsc_minify1, _options_jsc1; | ||
if (options.minify || ((_options_jsc = options.jsc) === null || _options_jsc === void 0 ? void 0 : (_options_jsc_minify = _options_jsc.minify) === null || _options_jsc_minify === void 0 ? void 0 : _options_jsc_minify.mangle) || ((_options_jsc1 = options.jsc) === null || _options_jsc1 === void 0 ? void 0 : (_options_jsc_minify1 = _options_jsc1.minify) === null || _options_jsc_minify1 === void 0 ? void 0 : _options_jsc_minify1.compress)) { | ||
var _options_jsc2; | ||
return core.minify(code, (_options_jsc2 = options.jsc) === null || _options_jsc2 === void 0 ? void 0 : _options_jsc2.minify); | ||
} | ||
return null; | ||
} | ||
}; | ||
} | ||
function minify(options = {}) { | ||
return { | ||
name: 'swc-minify', | ||
renderChunk (code) { | ||
return core.minify(code, options); | ||
} | ||
}; | ||
} | ||
function defineRollupSwcOption(option) { | ||
return option; | ||
} | ||
function defineRollupSwcMinifyOption(option) { | ||
return option; | ||
} | ||
Object.defineProperty(exports, 'preserveUseDirective', { | ||
enumerable: true, | ||
get: function () { return rollupSwcPreserveDirectives__default.default; } | ||
}); | ||
exports.default = swc; | ||
exports.defineRollupSwcMinifyOption = defineRollupSwcMinifyOption; | ||
exports.defineRollupSwcOption = defineRollupSwcOption; | ||
exports.minify = minify; | ||
exports.swc = swc; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("fs"),t=require("path"),r=require("@rollup/pluginutils"),i=require("@swc/core"),s=require("@fastify/deepmerge"),n=require("get-tsconfig"),l=require("rollup-swc-preserve-directives");function a(e){return e&&e.__esModule?e:{default:e}}var o=a(e),u=a(t),c=a(s),d=a(l);const f=new Map,p=(e,t)=>{var r,i,s,l,a;let o=`${e}:${null!=t?t:"undefined"}`;if(f.has(o))return null!==(i=f.get(o))&&void 0!==i?i:{};if(t&&u.default.isAbsolute(t)){let e=null!==(s=n.parseTsconfig(t).compilerOptions)&&void 0!==s?s:{},r=u.default.dirname(t);return null!=e.paths&&(e.baseUrl=null!=e.baseUrl?u.default.resolve(r,e.baseUrl):r),f.set(o,e),e}let c=n.getTsconfig(e,t||"tsconfig.json");c||t||(c=n.getTsconfig(e,"jsconfig.json"));let d=null!==(l=null===(r=c)||void 0===r?void 0:r.config.compilerOptions)&&void 0!==l?l:{};if(null!=d.paths&&(null===(a=c)||void 0===a?void 0:a.path)){let e=u.default.dirname(c.path);d.baseUrl=null!=d.baseUrl?u.default.resolve(e,d.baseUrl):e}return f.set(o,d),d},m=/\.[mc]?[jt]sx?$/,v=/node_modules/,x=[".ts",".tsx",".mjs",".js",".cjs",".jsx"],j=c.default({all:!0,mergeArray:e=>(t,r)=>e.clone(r)}),g=e=>o.default.promises.access(e,o.default.constants.F_OK).then(()=>!0).catch(()=>!1);function y(e={}){let s=r.createFilter(e.include||m,e.exclude||v),n=e.extensions||x,l=async(e,r=!1)=>{let i=e.replace(m,"");for(let s of n){let n=r?t.join(e,`index${s}`):`${i}${s}`;if(await g(n))return n}return null};return{name:"swc",async resolveId(e,r){if(e.startsWith("\x00"))return null;if(r&&e.startsWith(".")){let i=t.resolve(r?t.dirname(r):process.cwd(),e),s=await l(i);if(s||!s&&await g(i)&&(await o.default.promises.stat(i)).isDirectory()&&(s=await l(i,!0)))return s}},async transform(r,l){var a;if(!s(l))return null;let o=t.extname(l);if(!n.includes(o))return null;let u=".ts"===o||".mts"===o||".cts"===o||".tsx"===o,c=".tsx"===o,d=".jsx"===o,f=!1===e.tsconfig?{}:p(t.dirname(l),e.tsconfig),m="react-jsx"===f.jsx||"react-jsxdev"===f.jsx,v={jsc:{externalHelpers:f.importHelpers,parser:{syntax:u?"typescript":"ecmascript",tsx:u?c:void 0,jsx:u?void 0:d,decorators:f.experimentalDecorators},transform:{decoratorMetadata:f.emitDecoratorMetadata,react:{runtime:m?"automatic":"classic",importSource:f.jsxImportSource,pragma:f.jsxFactory,pragmaFrag:f.jsxFragmentFactory,development:"react-jsxdev"===f.jsx||void 0}},target:null===(a=f.target)||void 0===a?void 0:a.toLowerCase(),baseUrl:f.baseUrl,paths:f.paths}},{filename:x,include:g,exclude:y,tsconfig:w,extensions:h,minify:b,...U}=e,q=j(v,U,{jsc:{minify:void 0},filename:l,minify:!1});return i.transform(r,q)},renderChunk(t){var r,s,n,l,a;return e.minify||(null===(s=e.jsc)||void 0===s?void 0:null===(r=s.minify)||void 0===r?void 0:r.mangle)||(null===(l=e.jsc)||void 0===l?void 0:null===(n=l.minify)||void 0===n?void 0:n.compress)?i.minify(t,null===(a=e.jsc)||void 0===a?void 0:a.minify):null}}}Object.defineProperty(exports,"preserveUseDirective",{enumerable:!0,get:function(){return d.default}}),exports.default=y,exports.defineRollupSwcMinifyOption=function(e){return e},exports.defineRollupSwcOption=function(e){return e},exports.minify=function(e={}){return{name:"swc-minify",renderChunk:t=>i.minify(t,e)}},exports.swc=y; |
{ | ||
"name": "rollup-plugin-swc3", | ||
"version": "0.10.0", | ||
"version": "0.10.1", | ||
"description": "Use SWC with Rollup to transform ESNext and TypeScript code.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -21,4 +21,7 @@ import { getTsconfig, parseTsconfig } from 'get-tsconfig'; | ||
if (compilerOptions.baseUrl != null) { | ||
compilerOptions.baseUrl = path.resolve(path.dirname(tsconfig), compilerOptions.baseUrl); | ||
const tsconfigDir = path.dirname(tsconfig); | ||
if (compilerOptions.paths != null) { | ||
compilerOptions.baseUrl = compilerOptions.baseUrl != null | ||
? path.resolve(tsconfigDir, compilerOptions.baseUrl) | ||
: tsconfigDir; | ||
} | ||
@@ -37,4 +40,9 @@ | ||
const compilerOptions = result?.config.compilerOptions ?? {}; | ||
if (result?.path && compilerOptions.baseUrl != null) { | ||
compilerOptions.baseUrl = path.resolve(path.dirname(result.path), compilerOptions.baseUrl); | ||
if (compilerOptions.paths != null) { | ||
if (result?.path) { | ||
const tsconfigDir = path.dirname(result.path); | ||
compilerOptions.baseUrl = compilerOptions.baseUrl != null | ||
? path.resolve(tsconfigDir, compilerOptions.baseUrl) | ||
: tsconfigDir; | ||
} | ||
} | ||
@@ -41,0 +49,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
37053
259
2