rollup-plugin-preserve-directives
Advanced tools
Comparing version 0.1.1 to 0.2.0
@@ -1,2 +0,3 @@ | ||
import { Plugin } from "rollup"; | ||
import { Plugin } from 'rollup'; | ||
type PreserveDirectivesOptions = { | ||
@@ -12,3 +13,4 @@ supressPreserveModulesWarning?: boolean; | ||
*/ | ||
export default function preserveDirectives({ supressPreserveModulesWarning, }?: PreserveDirectivesOptions): Plugin; | ||
export {}; | ||
declare function preserveDirectives({ supressPreserveModulesWarning, }?: PreserveDirectivesOptions): Plugin; | ||
export { preserveDirectives as default }; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
var __create = Object.create; | ||
var __defProp = Object.defineProperty; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __getProtoOf = Object.getPrototypeOf; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __export = (target, all) => { | ||
for (var name in all) | ||
__defProp(target, name, { get: all[name], enumerable: true }); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const magic_string_1 = __importDefault(require("magic-string")); | ||
/** | ||
* This is a plugin that preserves directives like "use client" at the top of files. | ||
* Can only be used with preserveModules: true. | ||
* | ||
* @param {Object} options - Plugin options | ||
* @param {boolean} options.supressPreserveModulesWarning - Disable the warning when preserveModules is false | ||
*/ | ||
function preserveDirectives({ supressPreserveModulesWarning, } = {}) { | ||
return { | ||
name: "preserve-directives", | ||
// Capture directives metadata during the transform phase | ||
transform(code) { | ||
var _a; | ||
const ast = this.parse(code); | ||
if (ast.type === "Program" && ast.body) { | ||
const directives = []; | ||
let i = 0; | ||
// Nodes in body should never be falsy, but issue #5 tells us otherwise | ||
// so just in case we filter them out here | ||
const filteredBody = ast.body.filter(Boolean); | ||
// .type must be defined according to the spec, but just in case.. | ||
while (((_a = filteredBody[i]) === null || _a === void 0 ? void 0 : _a.type) === "ExpressionStatement") { | ||
const node = filteredBody[i]; | ||
if (node.directive) { | ||
directives.push(node.directive); | ||
} | ||
i += 1; | ||
} | ||
if (directives.length > 0) { | ||
return { | ||
code, | ||
ast, | ||
map: null, | ||
meta: { preserveDirectives: directives }, | ||
}; | ||
} | ||
var __copyProps = (to, from, except, desc) => { | ||
if (from && typeof from === "object" || typeof from === "function") { | ||
for (let key of __getOwnPropNames(from)) | ||
if (!__hasOwnProp.call(to, key) && key !== except) | ||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
} | ||
return to; | ||
}; | ||
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( | ||
// If the importer is in node compatibility mode or this is not an ESM | ||
// file that has been converted to a CommonJS file using a Babel- | ||
// compatible transform (i.e. "__esModule" has not been set), then set | ||
// "default" to the CommonJS "module.exports" for node compatibility. | ||
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, | ||
mod | ||
)); | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
// src/index.ts | ||
var src_exports = {}; | ||
__export(src_exports, { | ||
default: () => preserveDirectives | ||
}); | ||
module.exports = __toCommonJS(src_exports); | ||
var import_magic_string = __toESM(require("magic-string")); | ||
function preserveDirectives({ | ||
supressPreserveModulesWarning | ||
} = {}) { | ||
return { | ||
name: "preserve-directives", | ||
// Capture directives metadata during the transform phase | ||
transform(code) { | ||
var _a; | ||
const ast = this.parse(code); | ||
if (ast.type === "Program" && ast.body) { | ||
const directives = []; | ||
let i = 0; | ||
const filteredBody = ast.body.filter(Boolean); | ||
while (((_a = filteredBody[i]) == null ? void 0 : _a.type) === "ExpressionStatement") { | ||
const node = filteredBody[i]; | ||
if (node.directive) { | ||
directives.push(node.directive); | ||
} | ||
i += 1; | ||
} | ||
if (directives.length > 0) { | ||
return { | ||
code, | ||
ast, | ||
map: null, | ||
meta: { preserveDirectives: directives } | ||
}; | ||
} | ||
} | ||
return { code, ast, map: null }; | ||
}, | ||
// We check if this chunk has a module with extracted directives | ||
// and add that to the top. | ||
// Because we only run this when preserveModules: true there should | ||
// only be one module per chunk. | ||
// Banners will already have been inserted here, so directives always | ||
// ends up at the absolute top. | ||
renderChunk: { | ||
order: "post", | ||
handler(code, chunk, options) { | ||
var _a, _b; | ||
if (!options.preserveModules) { | ||
if (!supressPreserveModulesWarning) { | ||
this.warn( | ||
"This plugin only works with the option preserveModules: true, if you want to add directives to the top of a bundled build, add it in a banner." | ||
); | ||
} | ||
return void 0; | ||
} | ||
let chunkHasDirectives = false; | ||
if ("modules" in chunk) { | ||
for (const moduleId of Object.keys(chunk.modules)) { | ||
const directives = (_b = (_a = this.getModuleInfo(moduleId)) == null ? void 0 : _a.meta) == null ? void 0 : _b.preserveDirectives; | ||
if (directives) { | ||
chunkHasDirectives = directives; | ||
} | ||
// Return code and ast to avoid having to re-parse and | ||
// `map: null` to preserve source maps since we haven't modified anything | ||
return { code, ast, map: null }; | ||
}, | ||
// We check if this chunk has a module with extracted directives | ||
// and add that to the top. | ||
// Because we only run this when preserveModules: true there should | ||
// only be one module per chunk. | ||
// Banners will already have been inserted here, so directives always | ||
// ends up at the absolute top. | ||
renderChunk: { | ||
order: "post", | ||
handler(code, chunk, options) { | ||
var _a, _b; | ||
if (!options.preserveModules) { | ||
if (!supressPreserveModulesWarning) { | ||
this.warn("This plugin only works with the option preserveModules: true, if you want to add directives to the top of a bundled build, add it in a banner."); | ||
} | ||
return undefined; | ||
} | ||
let chunkHasDirectives = false; | ||
// Only do this for OutputChunks, not OutputAssets | ||
if ("modules" in chunk) { | ||
for (const moduleId of Object.keys(chunk.modules)) { | ||
const directives = (_b = (_a = this.getModuleInfo(moduleId)) === null || _a === void 0 ? void 0 : _a.meta) === null || _b === void 0 ? void 0 : _b.preserveDirectives; | ||
if (directives) { | ||
chunkHasDirectives = directives; | ||
} | ||
} | ||
if (chunkHasDirectives) { | ||
const directiveStrings = chunkHasDirectives | ||
.map((directive) => `'${directive}'`) | ||
.join(";\n"); | ||
const s = new magic_string_1.default(code); | ||
s.prepend(`${directiveStrings};\n`); | ||
const srcMap = s.generateMap({ includeContent: true }); | ||
return { code: s.toString(), map: srcMap }; | ||
} | ||
} | ||
return null; | ||
}, | ||
}, | ||
}; | ||
} | ||
if (chunkHasDirectives) { | ||
const directiveStrings = chunkHasDirectives.map((directive) => `'${directive}'`).join(";\n"); | ||
const s = new import_magic_string.default(code); | ||
s.prepend(`${directiveStrings}; | ||
`); | ||
const srcMap = s.generateMap({ includeContent: true }); | ||
return { code: s.toString(), map: srcMap }; | ||
} | ||
} | ||
return null; | ||
} | ||
} | ||
}; | ||
} | ||
exports.default = preserveDirectives; |
{ | ||
"name": "rollup-plugin-preserve-directives", | ||
"description": "A Rollup plugin to preserve directives like \"use client\" when preserveModules is true.", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"author": "Fredrik Höglund <fredrik.hoglund@gmail.com>", | ||
@@ -13,4 +13,14 @@ "license": "MIT", | ||
}, | ||
"type": "commonjs", | ||
"types": "dist/index.d.ts", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"module": "dist/index.mjs", | ||
"exports": { | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"import": "./dist/index.mjs", | ||
"require": "./dist/index.js" | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"repository": { | ||
@@ -23,4 +33,5 @@ "type": "git", | ||
"scripts": { | ||
"build": "tsc", | ||
"watch": "tsc --watch" | ||
"check": "tsc", | ||
"build": "tsup src/index.ts --dts --format cjs,esm", | ||
"watch": "npm run build --watch" | ||
}, | ||
@@ -35,10 +46,11 @@ "peerDependencies": { | ||
"rollup": "^3.19.1", | ||
"tsup": "^6.7.0", | ||
"typescript": "^4.9.5" | ||
}, | ||
"files": [ | ||
"dist/index.js", | ||
"dist/index.d.ts", | ||
"README.md", | ||
"LICENSE" | ||
"dist", | ||
"src", | ||
"LICENSE", | ||
"README.md" | ||
] | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
16923
7
289
3
1