rollup-plugin-swc3
Advanced tools
Comparing version 0.9.0 to 0.9.1
import { Plugin } from 'rollup'; | ||
import { FilterPattern } from '@rollup/pluginutils'; | ||
import { Options, JsMinifyOptions } from '@swc/core'; | ||
export { default as preserveUseDirective } from 'rollup-swc-preserve-directives'; | ||
/** | ||
* Originally created by huozhi @ GitHub <https://huozhi.im/> | ||
* License: MIT | ||
* This is from huozhi's amazing zero config bundler: https://github.com/huozhi/bunchee | ||
* | ||
* https://github.com/huozhi/bunchee/blob/23cbd6ebab992b886a207a531ec54dfa0877ab39/src/plugins/directive-plugin.ts | ||
* | ||
* Following changes are made: | ||
* | ||
* - Replace `magic-string` with `@napi-rs/magic-string` | ||
* - Use a Map to track directives for each module, to support multiple entries input | ||
*/ | ||
declare function preserveUseDirective(): Plugin; | ||
type PluginOptions = { | ||
@@ -35,2 +21,2 @@ include?: FilterPattern; | ||
export { PluginOptions, swc as default, defineRollupSwcMinifyOption, defineRollupSwcOption, minify, preserveUseDirective, swc }; | ||
export { PluginOptions, swc as default, defineRollupSwcMinifyOption, defineRollupSwcOption, minify, swc }; |
@@ -11,3 +11,3 @@ 'use strict'; | ||
var getTsconfig = require('get-tsconfig'); | ||
var magicString = require('@napi-rs/magic-string'); | ||
var rollupSwcPreserveDirectives = require('rollup-swc-preserve-directives'); | ||
@@ -19,2 +19,3 @@ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; } | ||
var createDeepMerge__default = /*#__PURE__*/_interopDefault(createDeepMerge); | ||
var rollupSwcPreserveDirectives__default = /*#__PURE__*/_interopDefault(rollupSwcPreserveDirectives); | ||
@@ -46,41 +47,2 @@ const cache = new Map(); | ||
const isNonNull = (val)=>val != null; | ||
const rDirectives = /^(?:['"]use[^'"]+['"][^\n]*|#![^\n]*)/gm; | ||
function preserveUseDirective() { | ||
const fileDirectivesMap = new Map(); | ||
return { | ||
name: 'preserve-use-directive', | ||
transform (code, id) { | ||
const directives = new Set(); | ||
const replacedCode = code.replace(rDirectives, (match)=>{ | ||
// replace double quotes with single quotes | ||
directives.add(match.replace(/["]/g, '\'')); | ||
return ''; | ||
}); | ||
if (directives.size) fileDirectivesMap.set(id, directives); | ||
return { | ||
code: replacedCode, | ||
map: null | ||
}; | ||
}, | ||
renderChunk (code, chunk, { sourcemap }) { | ||
// "chunk.moduleIds" is only avaliable in rollup 3. Add a fallback to be rollup 2 compatible | ||
const moduleIds = 'moduleIds' in chunk ? chunk.moduleIds : Object.keys(chunk.modules); | ||
const outputDirectives = moduleIds.map((id)=>fileDirectivesMap.get(id)).filter(isNonNull).reduce((acc, directives)=>{ | ||
directives.forEach((directive)=>acc.add(directive)); | ||
return acc; | ||
}, new Set()); | ||
if (outputDirectives.size === 0) return null; | ||
const s = new magicString.MagicString(code); | ||
s.prepend(`${Array.from(outputDirectives).join('\n')}\n`); | ||
return { | ||
code: s.toString(), | ||
map: sourcemap ? s.generateMap({ | ||
hires: true | ||
}).toMap() : null | ||
}; | ||
} | ||
}; | ||
} | ||
const INCLUDE_REGEXP = /\.[mc]?[jt]sx?$/; | ||
@@ -211,2 +173,6 @@ const EXCLUDE_REGEXP = /node_modules/; | ||
Object.defineProperty(exports, 'preserveUseDirective', { | ||
enumerable: true, | ||
get: function () { return rollupSwcPreserveDirectives__default.default; } | ||
}); | ||
exports.default = swc; | ||
@@ -216,3 +182,2 @@ exports.defineRollupSwcMinifyOption = defineRollupSwcMinifyOption; | ||
exports.minify = minify; | ||
exports.preserveUseDirective = preserveUseDirective; | ||
exports.swc = swc; |
{ | ||
"name": "rollup-plugin-swc3", | ||
"version": "0.9.0", | ||
"version": "0.9.1", | ||
"description": "Use SWC with Rollup to transform ESNext and TypeScript code.", | ||
@@ -30,5 +30,5 @@ "main": "dist/index.js", | ||
"@fastify/deepmerge": "^1.3.0", | ||
"@napi-rs/magic-string": "^0.3.4", | ||
"@rollup/pluginutils": "^4.2.1", | ||
"get-tsconfig": "^4.6.2" | ||
"get-tsconfig": "^4.6.2", | ||
"rollup-swc-preserve-directives": "^0.3.0" | ||
}, | ||
@@ -35,0 +35,0 @@ "devDependencies": { |
@@ -8,4 +8,6 @@ <div align="center"> | ||
**New:** Build library for React Server Component is now supported! `'use client'` and `'use server'` directives now are handled properly, without triggering rollup warnings. [Start using `'use client'` and `'use server'` in your library by adding two lines in your `rollup.config.js`](#react-server-component-directives-use-client-and-use-server) | ||
**New:** Building library for React Server Component support is added in `0.9.0`! `'use client'` and `'use server'` directives now are handled properly, without triggering rollup warnings. [Start using `'use client'` and `'use server'` in your library by adding two lines in your `rollup.config.js`](#react-server-component-directives-use-client-and-use-server) | ||
> Since `0.9.1` the support for `'use client'` and `'use server'` has been separated into a standalone rollup plugin [`rollup-swc-preserve-directives`](https://github.com/huozhi/rollup-plugin-swc-preserve-directives), the previous `preserveUseDirective` named export is retained for the backward compatability. | ||
## Comparison | ||
@@ -168,8 +170,19 @@ | ||
Since version `0.9.0`, the support for `'use client'` and `'use server'` has been added: | ||
~~Since version `0.9.0`, the support for `'use client'` and `'use server'` has been added:~~ | ||
> The support for `'use client'` and `'use server'` has been separated into a standalone rollup plugin [`rollup-swc-preserve-directives`](https://github.com/huozhi/rollup-plugin-swc-preserve-directives), maintained by [@huozhi](https://github.com/huozhi) and me. The previous `preserveUseDirective` named export is retained for the backward compatibility. | ||
```bash | ||
# npm | ||
npm install -D rollup-swc-preserve-directives | ||
# yarn | ||
yarn add -D rollup-swc-preserve-directives | ||
# pnpm | ||
pnpm add -D rollup-swc-preserve-directives | ||
``` | ||
```js | ||
// rollup.config.js | ||
// Import `preserveUseDirective` from `rollup-plugin-swc3`... | ||
import { swc, preserveUseDirective } from 'rollup-plugin-swc3'; | ||
import { swc } from 'rollup-plugin-swc3'; | ||
import swcPreserveDirectives from 'rollup-swc-preserve-directives'; | ||
@@ -181,4 +194,4 @@ export default { | ||
swc(), | ||
// And add `preserveUseDirective` plugin after the `swc` plugin | ||
preserveUseDirective() | ||
// And add `swcPreserveDirectives` plugin after the `swc` plugin | ||
swcPreserveDirectives() | ||
]; | ||
@@ -190,3 +203,3 @@ } | ||
`preserveUseDirective` supports: | ||
`swcPreserveDirectives` supports: | ||
@@ -214,3 +227,3 @@ - Merging duplicated directives in the output bundles | ||
output: { file: 'dist/index.js' } | ||
plugins: [swc(), preserveUseDirective()] | ||
plugins: [swc(), swcPreserveDirectives()] | ||
} | ||
@@ -250,3 +263,3 @@ | ||
output: { dir: 'dist/', entryFileName: '[name].js' } | ||
plugins: [swc(), preserveUseDirective()] | ||
plugins: [swc(), swcPreserveDirectives()] | ||
} | ||
@@ -253,0 +266,0 @@ |
/** | ||
* Originally created by huozhi @ GitHub <https://huozhi.im/> | ||
* License: MIT | ||
* This is from huozhi's amazing zero config bundler: https://github.com/huozhi/bunchee | ||
* | ||
* https://github.com/huozhi/bunchee/blob/23cbd6ebab992b886a207a531ec54dfa0877ab39/src/plugins/directive-plugin.ts | ||
* | ||
* Following changes are made: | ||
* | ||
* - Replace `magic-string` with `@napi-rs/magic-string` | ||
* - Use a Map to track directives for each module, to support multiple entries input | ||
* preserveUseDirective is now a separate plugin, re-export to maintain the backward compatibility | ||
*/ | ||
import type { Plugin } from 'rollup'; | ||
import { MagicString } from '@napi-rs/magic-string'; | ||
import type { RenderedChunk } from 'rollup2'; | ||
const isNonNull = <T>(val: T | null | undefined): val is T => val != null; | ||
const rDirectives = /^(?:['"]use[^'"]+['"][^\n]*|#![^\n]*)/gm; | ||
export function preserveUseDirective(): Plugin { | ||
const fileDirectivesMap = new Map<string, Set<string>>(); | ||
return { | ||
name: 'preserve-use-directive', | ||
transform(code, id) { | ||
const directives = new Set<string>(); | ||
const replacedCode = code.replace(rDirectives, (match) => { | ||
// replace double quotes with single quotes | ||
directives.add(match.replace(/["]/g, '\'')); | ||
return ''; | ||
}); | ||
if (directives.size) fileDirectivesMap.set(id, directives); | ||
return { | ||
code: replacedCode, | ||
map: null | ||
}; | ||
}, | ||
renderChunk(code, chunk, { sourcemap }) { | ||
// "chunk.moduleIds" is only avaliable in rollup 3. Add a fallback to be rollup 2 compatible | ||
const moduleIds = 'moduleIds' in chunk | ||
? chunk.moduleIds | ||
: Object.keys((chunk as RenderedChunk).modules); | ||
const outputDirectives = moduleIds | ||
.map((id) => fileDirectivesMap.get(id)) | ||
.filter(isNonNull) | ||
.reduce((acc, directives) => { | ||
directives.forEach((directive) => acc.add(directive)); | ||
return acc; | ||
}, new Set<string>()); | ||
if (outputDirectives.size === 0) return null; | ||
const s = new MagicString(code); | ||
s.prepend(`${Array.from(outputDirectives).join('\n')}\n`); | ||
return { | ||
code: s.toString(), | ||
map: sourcemap ? s.generateMap({ hires: true }).toMap() : null | ||
}; | ||
} | ||
}; | ||
} | ||
import swcPreserveDirectivePlugin from 'rollup-swc-preserve-directives'; | ||
export { swcPreserveDirectivePlugin as preserveUseDirective }; |
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
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
344
44149
549
+ Addedrollup-swc-preserve-directives@0.3.2(transitive)
- Removed@napi-rs/magic-string@^0.3.4