Website Structure

This commit is contained in:
supalerk-ar66 2026-01-13 10:46:40 +07:00
parent 62812f2090
commit 71f0676a62
22365 changed files with 4265753 additions and 791 deletions

View file

@ -0,0 +1,96 @@
[npm]: https://img.shields.io/npm/v/@rollup/plugin-inject
[npm-url]: https://www.npmjs.com/package/@rollup/plugin-inject
[size]: https://packagephobia.now.sh/badge?p=@rollup/plugin-inject
[size-url]: https://packagephobia.now.sh/result?p=@rollup/plugin-inject
[![npm][npm]][npm-url]
[![size][size]][size-url]
[![libera manifesto](https://img.shields.io/badge/libera-manifesto-lightgrey.svg)](https://liberamanifesto.com)
# @rollup/plugin-inject
🍣 A Rollup plugin which scans modules for global variables and injects `import` statements where necessary.
## Requirements
This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v14.0.0+) and Rollup v1.20.0+.
## Install
Using npm:
```console
npm install @rollup/plugin-inject --save-dev
```
## Usage
Create a `rollup.config.js` [configuration file](https://www.rollupjs.org/guide/en/#configuration-files) and import the plugin:
```js
import inject from '@rollup/plugin-inject';
export default {
input: 'src/index.js',
output: {
dir: 'output',
format: 'cjs'
},
plugins: [
inject({
Promise: ['es6-promise', 'Promise']
})
]
};
```
Then call `rollup` either via the [CLI](https://www.rollupjs.org/guide/en/#command-line-reference) or the [API](https://www.rollupjs.org/guide/en/#javascript-api).
This configuration above will scan all your files for global Promise usage and plugin will add import to desired module (`import { Promise } from 'es6-promise'` in this case).
Examples:
```js
{
// import { Promise } from 'es6-promise'
Promise: [ 'es6-promise', 'Promise' ],
// import { Promise as P } from 'es6-promise'
P: [ 'es6-promise', 'Promise' ],
// import $ from 'jquery'
$: 'jquery',
// import * as fs from 'fs'
fs: [ 'fs', '*' ],
// use a local module instead of a third-party one
'Object.assign': path.resolve( 'src/helpers/object-assign.js' ),
}
```
Typically, `@rollup/plugin-inject` should be placed in `plugins` _before_ other plugins so that they may apply optimizations, such as dead code removal.
## Options
In addition to the properties and values specified for injecting, users may also specify the options below.
### `exclude`
Type: `String` | `Array[...String]`<br>
Default: `null`
A [picomatch pattern](https://github.com/micromatch/picomatch), or array of patterns, which specifies the files in the build the plugin should _ignore_. By default no files are ignored.
### `include`
Type: `String` | `Array[...String]`<br>
Default: `null`
A [picomatch pattern](https://github.com/micromatch/picomatch), or array of patterns, which specifies the files in the build the plugin should operate on. By default all files are targeted.
## Meta
[CONTRIBUTING](/.github/CONTRIBUTING.md)
[LICENSE (MIT)](/LICENSE)

View file

@ -0,0 +1,218 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var path = require('path');
var pluginutils = require('@rollup/pluginutils');
var estreeWalker = require('estree-walker');
var MagicString = require('magic-string');
var escape = function (str) { return str.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&'); };
var isReference = function (node, parent) {
if (node.type === 'MemberExpression') {
return !node.computed && isReference(node.object, node);
}
if (node.type === 'Identifier') {
// TODO is this right?
if (parent.type === 'MemberExpression') { return parent.computed || node === parent.object; }
// disregard the `bar` in { bar: foo }
if (parent.type === 'Property' && node !== parent.value) { return false; }
// disregard the `bar` in `class Foo { bar () {...} }`
if (parent.type === 'MethodDefinition') { return false; }
// disregard the `bar` in `export { foo as bar }`
if (parent.type === 'ExportSpecifier' && node !== parent.local) { return false; }
// disregard the `bar` in `import { bar as foo }`
if (parent.type === 'ImportSpecifier' && node === parent.imported) {
return false;
}
return true;
}
return false;
};
var flatten = function (startNode) {
var parts = [];
var node = startNode;
while (node.type === 'MemberExpression') {
parts.unshift(node.property.name);
node = node.object;
}
var name = node.name;
parts.unshift(name);
return { name: name, keypath: parts.join('.') };
};
function inject(options) {
if (!options) { throw new Error('Missing options'); }
var filter = pluginutils.createFilter(options.include, options.exclude);
var modules = options.modules;
if (!modules) {
modules = Object.assign({}, options);
delete modules.include;
delete modules.exclude;
delete modules.sourceMap;
delete modules.sourcemap;
}
var modulesMap = new Map(Object.entries(modules));
// Fix paths on Windows
if (path.sep !== '/') {
modulesMap.forEach(function (mod, key) {
modulesMap.set(
key,
Array.isArray(mod) ? [mod[0].split(path.sep).join('/'), mod[1]] : mod.split(path.sep).join('/')
);
});
}
var firstpass = new RegExp(("(?:" + (Array.from(modulesMap.keys()).map(escape).join('|')) + ")"), 'g');
var sourceMap = options.sourceMap !== false && options.sourcemap !== false;
return {
name: 'inject',
transform: function transform(code, id) {
if (!filter(id)) { return null; }
if (code.search(firstpass) === -1) { return null; }
if (path.sep !== '/') { id = id.split(path.sep).join('/'); } // eslint-disable-line no-param-reassign
var ast = null;
try {
ast = this.parse(code);
} catch (err) {
this.warn({
code: 'PARSE_ERROR',
message: ("rollup-plugin-inject: failed to parse " + id + ". Consider restricting the plugin to particular files via options.include")
});
}
if (!ast) {
return null;
}
var imports = new Set();
ast.body.forEach(function (node) {
if (node.type === 'ImportDeclaration') {
node.specifiers.forEach(function (specifier) {
imports.add(specifier.local.name);
});
}
});
// analyse scopes
var scope = pluginutils.attachScopes(ast, 'scope');
var magicString = new MagicString(code);
var newImports = new Map();
function handleReference(node, name, keypath) {
var mod = modulesMap.get(keypath);
if (mod && !imports.has(name) && !scope.contains(name)) {
if (typeof mod === 'string') { mod = [mod, 'default']; }
// prevent module from importing itself
if (mod[0] === id) { return false; }
var hash = keypath + ":" + (mod[0]) + ":" + (mod[1]);
var importLocalName =
name === keypath ? name : pluginutils.makeLegalIdentifier(("$inject_" + keypath));
if (!newImports.has(hash)) {
// escape apostrophes and backslashes for use in single-quoted string literal
var modName = mod[0].replace(/[''\\]/g, '\\$&');
if (mod[1] === '*') {
newImports.set(hash, ("import * as " + importLocalName + " from '" + modName + "';"));
} else {
newImports.set(hash, ("import { " + (mod[1]) + " as " + importLocalName + " } from '" + modName + "';"));
}
}
if (name !== keypath) {
magicString.overwrite(node.start, node.end, importLocalName, {
storeName: true
});
}
return true;
}
return false;
}
estreeWalker.walk(ast, {
enter: function enter(node, parent) {
if (sourceMap) {
magicString.addSourcemapLocation(node.start);
magicString.addSourcemapLocation(node.end);
}
if (node.scope) {
scope = node.scope; // eslint-disable-line prefer-destructuring
}
// special case shorthand properties. because node.key === node.value,
// we can't differentiate once we've descended into the node
if (node.type === 'Property' && node.shorthand && node.value.type === 'Identifier') {
var ref = node.key;
var name = ref.name;
handleReference(node, name, name);
this.skip();
return;
}
if (isReference(node, parent)) {
var ref$1 = flatten(node);
var name$1 = ref$1.name;
var keypath = ref$1.keypath;
var handled = handleReference(node, name$1, keypath);
if (handled) {
this.skip();
}
}
},
leave: function leave(node) {
if (node.scope) {
scope = scope.parent;
}
}
});
if (newImports.size === 0) {
return {
code: code,
ast: ast,
map: sourceMap ? magicString.generateMap({ hires: true }) : null
};
}
var importBlock = Array.from(newImports.values()).join('\n\n');
magicString.prepend((importBlock + "\n\n"));
return {
code: magicString.toString(),
map: sourceMap ? magicString.generateMap({ hires: true }) : null
};
}
};
}
exports.default = inject;
module.exports = Object.assign(exports.default, exports);
//# sourceMappingURL=index.js.map

View file

@ -0,0 +1,213 @@
import { sep } from 'path';
import { createFilter, attachScopes, makeLegalIdentifier } from '@rollup/pluginutils';
import { walk } from 'estree-walker';
import MagicString from 'magic-string';
var escape = function (str) { return str.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&'); };
var isReference = function (node, parent) {
if (node.type === 'MemberExpression') {
return !node.computed && isReference(node.object, node);
}
if (node.type === 'Identifier') {
// TODO is this right?
if (parent.type === 'MemberExpression') { return parent.computed || node === parent.object; }
// disregard the `bar` in { bar: foo }
if (parent.type === 'Property' && node !== parent.value) { return false; }
// disregard the `bar` in `class Foo { bar () {...} }`
if (parent.type === 'MethodDefinition') { return false; }
// disregard the `bar` in `export { foo as bar }`
if (parent.type === 'ExportSpecifier' && node !== parent.local) { return false; }
// disregard the `bar` in `import { bar as foo }`
if (parent.type === 'ImportSpecifier' && node === parent.imported) {
return false;
}
return true;
}
return false;
};
var flatten = function (startNode) {
var parts = [];
var node = startNode;
while (node.type === 'MemberExpression') {
parts.unshift(node.property.name);
node = node.object;
}
var name = node.name;
parts.unshift(name);
return { name: name, keypath: parts.join('.') };
};
function inject(options) {
if (!options) { throw new Error('Missing options'); }
var filter = createFilter(options.include, options.exclude);
var modules = options.modules;
if (!modules) {
modules = Object.assign({}, options);
delete modules.include;
delete modules.exclude;
delete modules.sourceMap;
delete modules.sourcemap;
}
var modulesMap = new Map(Object.entries(modules));
// Fix paths on Windows
if (sep !== '/') {
modulesMap.forEach(function (mod, key) {
modulesMap.set(
key,
Array.isArray(mod) ? [mod[0].split(sep).join('/'), mod[1]] : mod.split(sep).join('/')
);
});
}
var firstpass = new RegExp(("(?:" + (Array.from(modulesMap.keys()).map(escape).join('|')) + ")"), 'g');
var sourceMap = options.sourceMap !== false && options.sourcemap !== false;
return {
name: 'inject',
transform: function transform(code, id) {
if (!filter(id)) { return null; }
if (code.search(firstpass) === -1) { return null; }
if (sep !== '/') { id = id.split(sep).join('/'); } // eslint-disable-line no-param-reassign
var ast = null;
try {
ast = this.parse(code);
} catch (err) {
this.warn({
code: 'PARSE_ERROR',
message: ("rollup-plugin-inject: failed to parse " + id + ". Consider restricting the plugin to particular files via options.include")
});
}
if (!ast) {
return null;
}
var imports = new Set();
ast.body.forEach(function (node) {
if (node.type === 'ImportDeclaration') {
node.specifiers.forEach(function (specifier) {
imports.add(specifier.local.name);
});
}
});
// analyse scopes
var scope = attachScopes(ast, 'scope');
var magicString = new MagicString(code);
var newImports = new Map();
function handleReference(node, name, keypath) {
var mod = modulesMap.get(keypath);
if (mod && !imports.has(name) && !scope.contains(name)) {
if (typeof mod === 'string') { mod = [mod, 'default']; }
// prevent module from importing itself
if (mod[0] === id) { return false; }
var hash = keypath + ":" + (mod[0]) + ":" + (mod[1]);
var importLocalName =
name === keypath ? name : makeLegalIdentifier(("$inject_" + keypath));
if (!newImports.has(hash)) {
// escape apostrophes and backslashes for use in single-quoted string literal
var modName = mod[0].replace(/[''\\]/g, '\\$&');
if (mod[1] === '*') {
newImports.set(hash, ("import * as " + importLocalName + " from '" + modName + "';"));
} else {
newImports.set(hash, ("import { " + (mod[1]) + " as " + importLocalName + " } from '" + modName + "';"));
}
}
if (name !== keypath) {
magicString.overwrite(node.start, node.end, importLocalName, {
storeName: true
});
}
return true;
}
return false;
}
walk(ast, {
enter: function enter(node, parent) {
if (sourceMap) {
magicString.addSourcemapLocation(node.start);
magicString.addSourcemapLocation(node.end);
}
if (node.scope) {
scope = node.scope; // eslint-disable-line prefer-destructuring
}
// special case shorthand properties. because node.key === node.value,
// we can't differentiate once we've descended into the node
if (node.type === 'Property' && node.shorthand && node.value.type === 'Identifier') {
var ref = node.key;
var name = ref.name;
handleReference(node, name, name);
this.skip();
return;
}
if (isReference(node, parent)) {
var ref$1 = flatten(node);
var name$1 = ref$1.name;
var keypath = ref$1.keypath;
var handled = handleReference(node, name$1, keypath);
if (handled) {
this.skip();
}
}
},
leave: function leave(node) {
if (node.scope) {
scope = scope.parent;
}
}
});
if (newImports.size === 0) {
return {
code: code,
ast: ast,
map: sourceMap ? magicString.generateMap({ hires: true }) : null
};
}
var importBlock = Array.from(newImports.values()).join('\n\n');
magicString.prepend((importBlock + "\n\n"));
return {
code: magicString.toString(),
map: sourceMap ? magicString.generateMap({ hires: true }) : null
};
}
};
}
export { inject as default };
//# sourceMappingURL=index.js.map

View file

@ -0,0 +1 @@
{"type":"module"}

View file

@ -0,0 +1,85 @@
{
"name": "@rollup/plugin-inject",
"version": "5.0.5",
"publishConfig": {
"access": "public"
},
"description": "Scan modules for global variables and injects `import` statements where necessary",
"license": "MIT",
"repository": {
"url": "rollup/plugins",
"directory": "packages/inject"
},
"author": "Rich Harris <richard.a.harris@gmail.com>",
"homepage": "https://github.com/rollup/plugins/tree/master/packages/inject#readme",
"bugs": "https://github.com/rollup/plugins/issues",
"main": "./dist/cjs/index.js",
"module": "./dist/es/index.js",
"exports": {
"types": "./types/index.d.ts",
"import": "./dist/es/index.js",
"default": "./dist/cjs/index.js"
},
"engines": {
"node": ">=14.0.0"
},
"scripts": {
"build": "rollup -c",
"ci:coverage": "nyc pnpm test && nyc report --reporter=text-lcov > coverage.lcov",
"ci:lint": "pnpm build && pnpm lint",
"ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}",
"ci:test": "pnpm test -- --verbose && pnpm test:ts",
"prebuild": "del-cli dist",
"prepare": "if [ ! -d 'dist' ]; then pnpm build; fi",
"prerelease": "pnpm build",
"pretest": "pnpm build",
"release": "pnpm --workspace-root plugin:release --pkg $npm_package_name",
"test": "ava",
"test:ts": "tsc types/index.d.ts test/types.ts --noEmit"
},
"files": [
"dist",
"!dist/**/*.map",
"types",
"README.md",
"LICENSE"
],
"keywords": [
"rollup",
"plugin",
"inject",
"es2015",
"npm",
"modules"
],
"peerDependencies": {
"rollup": "^1.20.0||^2.0.0||^3.0.0||^4.0.0"
},
"peerDependenciesMeta": {
"rollup": {
"optional": true
}
},
"dependencies": {
"@rollup/pluginutils": "^5.0.1",
"estree-walker": "^2.0.2",
"magic-string": "^0.30.3"
},
"devDependencies": {
"@rollup/plugin-buble": "^1.0.0",
"del-cli": "^5.0.0",
"locate-character": "^2.0.5",
"rollup": "^4.0.0-24",
"source-map": "^0.7.4",
"typescript": "^4.8.3"
},
"types": "./types/index.d.ts",
"ava": {
"files": [
"!**/fixtures/**",
"!**/helpers/**",
"!**/recipes/**",
"!**/types.ts"
]
}
}

View file

@ -0,0 +1,42 @@
import type { Plugin } from 'rollup';
type Injectment = string | [string, string];
export interface RollupInjectOptions {
/**
* All other options are treated as `string: injectment` injectrs,
* or `string: (id) => injectment` functions.
*/
[str: string]:
| Injectment
| RollupInjectOptions['include']
| RollupInjectOptions['sourceMap']
| RollupInjectOptions['modules'];
/**
* A picomatch pattern, or array of patterns, of files that should be
* processed by this plugin (if omitted, all files are included by default)
*/
include?: string | RegExp | ReadonlyArray<string | RegExp> | null;
/**
* Files that should be excluded, if `include` is otherwise too permissive.
*/
exclude?: string | RegExp | ReadonlyArray<string | RegExp> | null;
/**
* If false, skips source map generation. This will improve performance.
* @default true
*/
sourceMap?: boolean;
/**
* You can separate values to inject from other options.
*/
modules?: { [str: string]: Injectment };
}
/**
* inject strings in files while bundling them.
*/
export default function inject(options?: RollupInjectOptions): Plugin;