mirror of
https://github.com/JonasunderscoreJones/aka-worker.git
synced 2025-10-23 09:59:19 +02:00
Initial commit (by create-cloudflare CLI)
This commit is contained in:
parent
8cb86120f1
commit
fff961078a
1777 changed files with 1011798 additions and 0 deletions
50
cool-dawn-3d3b/node_modules/rollup-plugin-inject/CHANGELOG.md
generated
vendored
Executable file
50
cool-dawn-3d3b/node_modules/rollup-plugin-inject/CHANGELOG.md
generated
vendored
Executable file
|
@ -0,0 +1,50 @@
|
|||
# rollup-plugin-inject
|
||||
|
||||
## 3.0.1
|
||||
|
||||
* Generate sourcemap when sourcemap enabled
|
||||
|
||||
## 3.0.0
|
||||
|
||||
* Remove node v6 from support
|
||||
* Use modern js
|
||||
|
||||
## 2.1.0
|
||||
|
||||
* Update all dependencies ([#15](https://github.com/rollup/rollup-plugin-inject/pull/15))
|
||||
|
||||
## 2.0.0
|
||||
|
||||
* Work with all file extensions, not just `.js` (unless otherwise specified via `options.include` and `options.exclude`) ([#6](https://github.com/rollup/rollup-plugin-inject/pull/6))
|
||||
* Allow `*` imports ([#9](https://github.com/rollup/rollup-plugin-inject/pull/9))
|
||||
* Ignore replacements that are superseded (e.g. if `Buffer.isBuffer` is replaced, ignore `Buffer` replacement) ([#10](https://github.com/rollup/rollup-plugin-inject/pull/10))
|
||||
|
||||
## 1.4.1
|
||||
|
||||
* Return a `name`
|
||||
|
||||
## 1.4.0
|
||||
|
||||
* Use `string.search` instead of `regex.test` to avoid state-related mishaps ([#5](https://github.com/rollup/rollup-plugin-inject/issues/5))
|
||||
* Prevent self-importing module bug
|
||||
|
||||
## 1.3.0
|
||||
|
||||
* Windows support ([#2](https://github.com/rollup/rollup-plugin-inject/issues/2))
|
||||
* Node 0.12 support
|
||||
|
||||
## 1.2.0
|
||||
|
||||
* Generate sourcemaps by default
|
||||
|
||||
## 1.1.1
|
||||
|
||||
* Use `modules` option
|
||||
|
||||
## 1.1.0
|
||||
|
||||
* Handle shorthand properties
|
||||
|
||||
## 1.0.0
|
||||
|
||||
* First release
|
59
cool-dawn-3d3b/node_modules/rollup-plugin-inject/README.md
generated
vendored
Executable file
59
cool-dawn-3d3b/node_modules/rollup-plugin-inject/README.md
generated
vendored
Executable file
|
@ -0,0 +1,59 @@
|
|||
# rollup-plugin-inject
|
||||
|
||||
Scan modules for global variables and inject `import` statements where necessary
|
||||
|
||||
## Archived (Migration to Mono-Repo In-Process)
|
||||
|
||||
This repository has been archived and is in the process of being migrated to a new monorepo. Please bear with us as we make this transition. More information to follow.
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
npm install --save-dev rollup-plugin-inject
|
||||
```
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
import { rollup } from 'rollup';
|
||||
import inject from 'rollup-plugin-inject';
|
||||
|
||||
rollup({
|
||||
entry: 'main.js',
|
||||
plugins: [
|
||||
inject({
|
||||
// control which files this plugin applies to
|
||||
// with include/exclude
|
||||
include: '**/*.js',
|
||||
exclude: 'node_modules/**',
|
||||
|
||||
/* all other options are treated as modules...*/
|
||||
|
||||
// use the default – i.e. insert
|
||||
// import $ from 'jquery'
|
||||
$: 'jquery',
|
||||
|
||||
// use a named export – i.e. insert
|
||||
// import { Promise } from 'es6-promise'
|
||||
Promise: [ 'es6-promise', 'Promise' ],
|
||||
|
||||
// use a namespace import – i.e. insert
|
||||
// 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' ),
|
||||
|
||||
/* ...but if you want to be careful about separating modules
|
||||
from other options, supply `options.modules` instead */
|
||||
|
||||
modules: {
|
||||
$: 'jquery',
|
||||
Promise: [ 'es6-promise', 'Promise' ],
|
||||
'Object.assign': path.resolve( 'src/helpers/object-assign.js' )
|
||||
}
|
||||
})
|
||||
]
|
||||
}).then(...)
|
||||
```
|
201
cool-dawn-3d3b/node_modules/rollup-plugin-inject/dist/rollup-plugin-inject.cjs.js
generated
vendored
Executable file
201
cool-dawn-3d3b/node_modules/rollup-plugin-inject/dist/rollup-plugin-inject.cjs.js
generated
vendored
Executable file
|
@ -0,0 +1,201 @@
|
|||
'use strict';
|
||||
|
||||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
||||
|
||||
var rollupPluginutils = require('rollup-pluginutils');
|
||||
var path = require('path');
|
||||
var estreeWalker = require('estree-walker');
|
||||
var MagicString = _interopDefault(require('magic-string'));
|
||||
|
||||
const escape = str => {
|
||||
return str.replace(/[-[\]/{}()*+?.\\^$|]/g, "\\$&");
|
||||
};
|
||||
|
||||
const isReference = (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;
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
const flatten = node => {
|
||||
const parts = [];
|
||||
|
||||
while (node.type === "MemberExpression") {
|
||||
parts.unshift(node.property.name);
|
||||
node = node.object;
|
||||
}
|
||||
|
||||
const name = node.name;
|
||||
parts.unshift(name);
|
||||
|
||||
return { name, keypath: parts.join(".") };
|
||||
};
|
||||
|
||||
function inject(options) {
|
||||
if (!options) throw new Error("Missing options");
|
||||
|
||||
const filter = rollupPluginutils.createFilter(options.include, options.exclude);
|
||||
|
||||
let modules = options.modules;
|
||||
|
||||
if (!modules) {
|
||||
modules = Object.assign({}, options);
|
||||
delete modules.include;
|
||||
delete modules.exclude;
|
||||
delete modules.sourceMap;
|
||||
delete modules.sourcemap;
|
||||
}
|
||||
|
||||
const modulesMap = new Map(Object.entries(modules));
|
||||
|
||||
// Fix paths on Windows
|
||||
if (path.sep !== "/") {
|
||||
modulesMap.forEach((mod, key) => {
|
||||
modulesMap.set(
|
||||
key,
|
||||
Array.isArray(mod) ? [mod[0].split(path.sep).join("/"), mod[1]] : mod.split(path.sep).join("/")
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
const firstpass = new RegExp(
|
||||
`(?:${Array.from(modulesMap.keys())
|
||||
.map(escape)
|
||||
.join("|")})`,
|
||||
"g"
|
||||
);
|
||||
const sourceMap = options.sourceMap !== false && options.sourcemap !== false;
|
||||
|
||||
return {
|
||||
name: "inject",
|
||||
|
||||
transform(code, id) {
|
||||
if (!filter(id)) return null;
|
||||
if (code.search(firstpass) === -1) return null;
|
||||
|
||||
if (path.sep !== "/") id = id.split(path.sep).join("/");
|
||||
|
||||
let 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;
|
||||
}
|
||||
|
||||
// analyse scopes
|
||||
let scope = rollupPluginutils.attachScopes(ast, "scope");
|
||||
|
||||
const imports = new Set();
|
||||
ast.body.forEach(node => {
|
||||
if (node.type === "ImportDeclaration") {
|
||||
node.specifiers.forEach(specifier => {
|
||||
imports.add(specifier.local.name);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
const magicString = new MagicString(code);
|
||||
|
||||
const newImports = new Map();
|
||||
|
||||
function handleReference(node, name, keypath) {
|
||||
let 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;
|
||||
|
||||
const hash = `${keypath}:${mod[0]}:${mod[1]}`;
|
||||
|
||||
const importLocalName =
|
||||
name === keypath ? name : rollupPluginutils.makeLegalIdentifier(`$inject_${keypath}`);
|
||||
|
||||
if (!newImports.has(hash)) {
|
||||
if (mod[1] === "*") {
|
||||
newImports.set(hash, `import * as ${importLocalName} from '${mod[0]}';`);
|
||||
} else {
|
||||
newImports.set(hash, `import { ${mod[1]} as ${importLocalName} } from '${mod[0]}';`);
|
||||
}
|
||||
}
|
||||
|
||||
if (name !== keypath) {
|
||||
magicString.overwrite(node.start, node.end, importLocalName, {
|
||||
storeName: true
|
||||
});
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
estreeWalker.walk(ast, {
|
||||
enter(node, parent) {
|
||||
if (sourceMap) {
|
||||
magicString.addSourcemapLocation(node.start);
|
||||
magicString.addSourcemapLocation(node.end);
|
||||
}
|
||||
|
||||
if (node.scope) scope = node.scope;
|
||||
|
||||
// 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) {
|
||||
const name = node.key.name;
|
||||
handleReference(node, name, name);
|
||||
return this.skip();
|
||||
}
|
||||
|
||||
if (isReference(node, parent)) {
|
||||
const { name, keypath } = flatten(node);
|
||||
const handled = handleReference(node, name, keypath);
|
||||
if (handled) return this.skip();
|
||||
}
|
||||
},
|
||||
leave(node) {
|
||||
if (node.scope) scope = scope.parent;
|
||||
}
|
||||
});
|
||||
|
||||
if (newImports.size === 0) {
|
||||
return {
|
||||
code,
|
||||
ast,
|
||||
map: sourceMap ? magicString.generateMap({ hires: true }) : null
|
||||
};
|
||||
}
|
||||
const importBlock = Array.from(newImports.values()).join("\n\n");
|
||||
|
||||
magicString.prepend(importBlock + "\n\n");
|
||||
|
||||
return {
|
||||
code: magicString.toString(),
|
||||
map: sourceMap ? magicString.generateMap({ hires: true }) : null
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = inject;
|
197
cool-dawn-3d3b/node_modules/rollup-plugin-inject/dist/rollup-plugin-inject.es6.js
generated
vendored
Executable file
197
cool-dawn-3d3b/node_modules/rollup-plugin-inject/dist/rollup-plugin-inject.es6.js
generated
vendored
Executable file
|
@ -0,0 +1,197 @@
|
|||
import { createFilter, attachScopes, makeLegalIdentifier } from 'rollup-pluginutils';
|
||||
import { sep } from 'path';
|
||||
import { walk } from 'estree-walker';
|
||||
import MagicString from 'magic-string';
|
||||
|
||||
const escape = str => {
|
||||
return str.replace(/[-[\]/{}()*+?.\\^$|]/g, "\\$&");
|
||||
};
|
||||
|
||||
const isReference = (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;
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
const flatten = node => {
|
||||
const parts = [];
|
||||
|
||||
while (node.type === "MemberExpression") {
|
||||
parts.unshift(node.property.name);
|
||||
node = node.object;
|
||||
}
|
||||
|
||||
const name = node.name;
|
||||
parts.unshift(name);
|
||||
|
||||
return { name, keypath: parts.join(".") };
|
||||
};
|
||||
|
||||
function inject(options) {
|
||||
if (!options) throw new Error("Missing options");
|
||||
|
||||
const filter = createFilter(options.include, options.exclude);
|
||||
|
||||
let modules = options.modules;
|
||||
|
||||
if (!modules) {
|
||||
modules = Object.assign({}, options);
|
||||
delete modules.include;
|
||||
delete modules.exclude;
|
||||
delete modules.sourceMap;
|
||||
delete modules.sourcemap;
|
||||
}
|
||||
|
||||
const modulesMap = new Map(Object.entries(modules));
|
||||
|
||||
// Fix paths on Windows
|
||||
if (sep !== "/") {
|
||||
modulesMap.forEach((mod, key) => {
|
||||
modulesMap.set(
|
||||
key,
|
||||
Array.isArray(mod) ? [mod[0].split(sep).join("/"), mod[1]] : mod.split(sep).join("/")
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
const firstpass = new RegExp(
|
||||
`(?:${Array.from(modulesMap.keys())
|
||||
.map(escape)
|
||||
.join("|")})`,
|
||||
"g"
|
||||
);
|
||||
const sourceMap = options.sourceMap !== false && options.sourcemap !== false;
|
||||
|
||||
return {
|
||||
name: "inject",
|
||||
|
||||
transform(code, id) {
|
||||
if (!filter(id)) return null;
|
||||
if (code.search(firstpass) === -1) return null;
|
||||
|
||||
if (sep !== "/") id = id.split(sep).join("/");
|
||||
|
||||
let 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;
|
||||
}
|
||||
|
||||
// analyse scopes
|
||||
let scope = attachScopes(ast, "scope");
|
||||
|
||||
const imports = new Set();
|
||||
ast.body.forEach(node => {
|
||||
if (node.type === "ImportDeclaration") {
|
||||
node.specifiers.forEach(specifier => {
|
||||
imports.add(specifier.local.name);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
const magicString = new MagicString(code);
|
||||
|
||||
const newImports = new Map();
|
||||
|
||||
function handleReference(node, name, keypath) {
|
||||
let 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;
|
||||
|
||||
const hash = `${keypath}:${mod[0]}:${mod[1]}`;
|
||||
|
||||
const importLocalName =
|
||||
name === keypath ? name : makeLegalIdentifier(`$inject_${keypath}`);
|
||||
|
||||
if (!newImports.has(hash)) {
|
||||
if (mod[1] === "*") {
|
||||
newImports.set(hash, `import * as ${importLocalName} from '${mod[0]}';`);
|
||||
} else {
|
||||
newImports.set(hash, `import { ${mod[1]} as ${importLocalName} } from '${mod[0]}';`);
|
||||
}
|
||||
}
|
||||
|
||||
if (name !== keypath) {
|
||||
magicString.overwrite(node.start, node.end, importLocalName, {
|
||||
storeName: true
|
||||
});
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
walk(ast, {
|
||||
enter(node, parent) {
|
||||
if (sourceMap) {
|
||||
magicString.addSourcemapLocation(node.start);
|
||||
magicString.addSourcemapLocation(node.end);
|
||||
}
|
||||
|
||||
if (node.scope) scope = node.scope;
|
||||
|
||||
// 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) {
|
||||
const name = node.key.name;
|
||||
handleReference(node, name, name);
|
||||
return this.skip();
|
||||
}
|
||||
|
||||
if (isReference(node, parent)) {
|
||||
const { name, keypath } = flatten(node);
|
||||
const handled = handleReference(node, name, keypath);
|
||||
if (handled) return this.skip();
|
||||
}
|
||||
},
|
||||
leave(node) {
|
||||
if (node.scope) scope = scope.parent;
|
||||
}
|
||||
});
|
||||
|
||||
if (newImports.size === 0) {
|
||||
return {
|
||||
code,
|
||||
ast,
|
||||
map: sourceMap ? magicString.generateMap({ hires: true }) : null
|
||||
};
|
||||
}
|
||||
const 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 default inject;
|
51
cool-dawn-3d3b/node_modules/rollup-plugin-inject/package.json
generated
vendored
Executable file
51
cool-dawn-3d3b/node_modules/rollup-plugin-inject/package.json
generated
vendored
Executable file
|
@ -0,0 +1,51 @@
|
|||
{
|
||||
"name": "rollup-plugin-inject",
|
||||
"description": "Scan modules for global variables and inject `import` statements where necessary",
|
||||
"version": "3.0.2",
|
||||
"devDependencies": {
|
||||
"eslint": "^6.3.0",
|
||||
"mocha": "^6.2.0",
|
||||
"prettier": "^1.18.2",
|
||||
"rollup": "^1.17.0",
|
||||
"shx": "^0.3.2"
|
||||
},
|
||||
"main": "dist/rollup-plugin-inject.cjs.js",
|
||||
"module": "dist/rollup-plugin-inject.es6.js",
|
||||
"jsnext:main": "dist/rollup-plugin-inject.es6.js",
|
||||
"scripts": {
|
||||
"pretest": "npm run build",
|
||||
"test": "mocha",
|
||||
"prebuild": "shx rm -rf dist",
|
||||
"build": "rollup -c",
|
||||
"prepublishOnly": "npm run lint && npm run test",
|
||||
"prepare": "npm run build",
|
||||
"lint": "eslint --fix src test/test.js"
|
||||
},
|
||||
"files": [
|
||||
"src",
|
||||
"dist",
|
||||
"README.md"
|
||||
],
|
||||
"dependencies": {
|
||||
"estree-walker": "^0.6.1",
|
||||
"magic-string": "^0.25.3",
|
||||
"rollup-pluginutils": "^2.8.1"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/rollup/rollup-plugin-inject.git"
|
||||
},
|
||||
"keywords": [
|
||||
"rollup",
|
||||
"rollup-plugin",
|
||||
"es2015",
|
||||
"npm",
|
||||
"modules"
|
||||
],
|
||||
"author": "Rich Harris <richard.a.harris@gmail.com>",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/rollup/rollup-plugin-inject/issues"
|
||||
},
|
||||
"homepage": "https://github.com/rollup/rollup-plugin-inject#readme"
|
||||
}
|
196
cool-dawn-3d3b/node_modules/rollup-plugin-inject/src/index.js
generated
vendored
Executable file
196
cool-dawn-3d3b/node_modules/rollup-plugin-inject/src/index.js
generated
vendored
Executable file
|
@ -0,0 +1,196 @@
|
|||
import { attachScopes, createFilter, makeLegalIdentifier } from "rollup-pluginutils";
|
||||
import { sep } from "path";
|
||||
import { walk } from "estree-walker";
|
||||
|
||||
import MagicString from "magic-string";
|
||||
|
||||
const escape = str => {
|
||||
return str.replace(/[-[\]/{}()*+?.\\^$|]/g, "\\$&");
|
||||
};
|
||||
|
||||
const isReference = (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;
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
const flatten = node => {
|
||||
const parts = [];
|
||||
|
||||
while (node.type === "MemberExpression") {
|
||||
parts.unshift(node.property.name);
|
||||
node = node.object;
|
||||
}
|
||||
|
||||
const name = node.name;
|
||||
parts.unshift(name);
|
||||
|
||||
return { name, keypath: parts.join(".") };
|
||||
};
|
||||
|
||||
export default function inject(options) {
|
||||
if (!options) throw new Error("Missing options");
|
||||
|
||||
const filter = createFilter(options.include, options.exclude);
|
||||
|
||||
let modules = options.modules;
|
||||
|
||||
if (!modules) {
|
||||
modules = Object.assign({}, options);
|
||||
delete modules.include;
|
||||
delete modules.exclude;
|
||||
delete modules.sourceMap;
|
||||
delete modules.sourcemap;
|
||||
}
|
||||
|
||||
const modulesMap = new Map(Object.entries(modules));
|
||||
|
||||
// Fix paths on Windows
|
||||
if (sep !== "/") {
|
||||
modulesMap.forEach((mod, key) => {
|
||||
modulesMap.set(
|
||||
key,
|
||||
Array.isArray(mod) ? [mod[0].split(sep).join("/"), mod[1]] : mod.split(sep).join("/")
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
const firstpass = new RegExp(
|
||||
`(?:${Array.from(modulesMap.keys())
|
||||
.map(escape)
|
||||
.join("|")})`,
|
||||
"g"
|
||||
);
|
||||
const sourceMap = options.sourceMap !== false && options.sourcemap !== false;
|
||||
|
||||
return {
|
||||
name: "inject",
|
||||
|
||||
transform(code, id) {
|
||||
if (!filter(id)) return null;
|
||||
if (code.search(firstpass) === -1) return null;
|
||||
|
||||
if (sep !== "/") id = id.split(sep).join("/");
|
||||
|
||||
let 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;
|
||||
}
|
||||
|
||||
// analyse scopes
|
||||
let scope = attachScopes(ast, "scope");
|
||||
|
||||
const imports = new Set();
|
||||
ast.body.forEach(node => {
|
||||
if (node.type === "ImportDeclaration") {
|
||||
node.specifiers.forEach(specifier => {
|
||||
imports.add(specifier.local.name);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
const magicString = new MagicString(code);
|
||||
|
||||
const newImports = new Map();
|
||||
|
||||
function handleReference(node, name, keypath) {
|
||||
let 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;
|
||||
|
||||
const hash = `${keypath}:${mod[0]}:${mod[1]}`;
|
||||
|
||||
const importLocalName =
|
||||
name === keypath ? name : makeLegalIdentifier(`$inject_${keypath}`);
|
||||
|
||||
if (!newImports.has(hash)) {
|
||||
if (mod[1] === "*") {
|
||||
newImports.set(hash, `import * as ${importLocalName} from '${mod[0]}';`);
|
||||
} else {
|
||||
newImports.set(hash, `import { ${mod[1]} as ${importLocalName} } from '${mod[0]}';`);
|
||||
}
|
||||
}
|
||||
|
||||
if (name !== keypath) {
|
||||
magicString.overwrite(node.start, node.end, importLocalName, {
|
||||
storeName: true
|
||||
});
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
walk(ast, {
|
||||
enter(node, parent) {
|
||||
if (sourceMap) {
|
||||
magicString.addSourcemapLocation(node.start);
|
||||
magicString.addSourcemapLocation(node.end);
|
||||
}
|
||||
|
||||
if (node.scope) scope = node.scope;
|
||||
|
||||
// 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) {
|
||||
const name = node.key.name;
|
||||
handleReference(node, name, name);
|
||||
return this.skip();
|
||||
}
|
||||
|
||||
if (isReference(node, parent)) {
|
||||
const { name, keypath } = flatten(node);
|
||||
const handled = handleReference(node, name, keypath);
|
||||
if (handled) return this.skip();
|
||||
}
|
||||
},
|
||||
leave(node) {
|
||||
if (node.scope) scope = scope.parent;
|
||||
}
|
||||
});
|
||||
|
||||
if (newImports.size === 0) {
|
||||
return {
|
||||
code,
|
||||
ast,
|
||||
map: sourceMap ? magicString.generateMap({ hires: true }) : null
|
||||
};
|
||||
}
|
||||
const importBlock = Array.from(newImports.values()).join("\n\n");
|
||||
|
||||
magicString.prepend(importBlock + "\n\n");
|
||||
|
||||
return {
|
||||
code: magicString.toString(),
|
||||
map: sourceMap ? magicString.generateMap({ hires: true }) : null
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue