This commit is contained in:
darenhsu
2022-07-17 13:16:16 +08:00
parent 84759556ff
commit befd344ab0
28070 changed files with 4008428 additions and 1 deletions
+25
View File
@@ -0,0 +1,25 @@
'use strict';
var UNNAMED = /(^|;|\s+)import\s*['"]([^'"]+)['"](?=($|;|\s))/gi;
var NAMED = /(^|[;\s]+)?import\s*(\*\s*as)?\s*(\w*?)\s*,?\s*(?:\{([\s\S]*?)\})?\s*from\s*['"]([^'"]+)['"];?/gi;
function destruct(keys, target) {
var out=[];
while (keys.length) out.push(keys.shift().trim().replace(/ as /g, ':'));
return 'const { ' + out.join(', ') + ' } = ' + target + ';';
}
function generate(keys, dep, base, fn) {
dep = fn + "('" + dep + "')";
if (keys.length && !base) return destruct(keys, dep);
return 'const ' + base + ' = ' + dep + ';' + (keys.length ? '\n' + destruct(keys, base) : '');
}
module.exports = function (str, fn) {
fn = fn || 'require';
return str
.replace(UNNAMED, "$1" + fn + "('$2')")
.replace(NAMED, function (x, y, z, base, req, dep) {
return (y || '') + generate(req ? req.split(',') : [], dep, base, fn);
});
}
+1
View File
@@ -0,0 +1 @@
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e["rewrite-imports"]=t()}(this,function(){"use strict";var e=/(^|;|\s+)import\s*['"]([^'"]+)['"](?=($|;|\s))/gi,t=/(^|[;\s]+)?import\s*(\*\s*as)?\s*(\w*?)\s*,?\s*(?:\{([\s\S]*?)\})?\s*from\s*['"]([^'"]+)['"];?/gi;function n(e,t){for(var n=[];e.length;)n.push(e.shift().trim().replace(/ as /g,":"));return"const { "+n.join(", ")+" } = "+t+";"}return function(r,s){return s=s||"require",r.replace(e,"$1"+s+"('$2')").replace(t,function(e,t,r,i,o,u){return(t||"")+function(e,t,r,s){return t=s+"('"+t+"')",e.length&&!r?n(e,t):"const "+r+" = "+t+";"+(e.length?"\n"+n(e,r):"")}(o?o.split(","):[],u,i,s)})}});
+25
View File
@@ -0,0 +1,25 @@
'use strict';
var UNNAMED = /(^|;|\s+)import\s*['"]([^'"]+)['"](?=($|;|\s))/gi;
var NAMED = /(^|[;\s]+)?import\s*(\*\s*as)?\s*(\w*?)\s*,?\s*(?:\{([\s\S]*?)\})?\s*from\s*['"]([^'"]+)['"];?/gi;
function destruct(keys, target) {
var out=[];
while (keys.length) out.push(keys.shift().trim().replace(/ as /g, ':'));
return 'const { ' + out.join(', ') + ' } = ' + target + ';';
}
function generate(keys, dep, base, fn) {
dep = fn + "('" + dep + "')";
if (keys.length && !base) return destruct(keys, dep);
return 'const ' + base + ' = ' + dep + ';' + (keys.length ? '\n' + destruct(keys, base) : '');
}
export default function (str, fn) {
fn = fn || 'require';
return str
.replace(UNNAMED, "$1" + fn + "('$2')")
.replace(NAMED, function (x, y, z, base, req, dep) {
return (y || '') + generate(req ? req.split(',') : [], dep, base, fn);
});
}
+9
View File
@@ -0,0 +1,9 @@
MIT License
Copyright (c) Luke Edwards <luke.edwards05@gmail.com> (lukeed.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+74
View File
@@ -0,0 +1,74 @@
{
"_args": [
[
"rewrite-imports@2.0.3",
"/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series"
]
],
"_from": "rewrite-imports@2.0.3",
"_id": "rewrite-imports@2.0.3",
"_inBundle": false,
"_integrity": "sha512-R7ICJEeP3y+d/q4C8YEJj9nRP0JyiSqG07uc0oQh8JvAe706dDFVL95GBZYCjADqmhArZWWjfM/5EcmVu4/B+g==",
"_location": "/rewrite-imports",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "rewrite-imports@2.0.3",
"name": "rewrite-imports",
"escapedName": "rewrite-imports",
"rawSpec": "2.0.3",
"saveSpec": null,
"fetchSpec": "2.0.3"
},
"_requiredBy": [
"/dimport"
],
"_resolved": "https://registry.npmjs.org/rewrite-imports/-/rewrite-imports-2.0.3.tgz",
"_spec": "2.0.3",
"_where": "/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series",
"author": {
"name": "Luke Edwards",
"email": "luke.edwards05@gmail.com",
"url": "lukeed.com"
},
"bugs": {
"url": "https://github.com/lukeed/rewrite-imports/issues"
},
"description": "Rewrite `import` statements as `require()`s; via RegExp",
"devDependencies": {
"bundt": "^0.3.0",
"tap-spec": "^4.1.1",
"tape": "^4.7.0"
},
"engines": {
"node": ">=6"
},
"files": [
"dist"
],
"homepage": "https://github.com/lukeed/rewrite-imports#readme",
"keywords": [
"regex",
"rewrite",
"modules",
"imports",
"requires",
"aliases"
],
"license": "MIT",
"main": "dist/rewrite.js",
"module": "dist/rewrite.mjs",
"name": "rewrite-imports",
"repository": {
"type": "git",
"url": "git+https://github.com/lukeed/rewrite-imports.git"
},
"scripts": {
"build": "bundt",
"pretest": "npm run build",
"test": "tape test/*.js | tap-spec"
},
"unpkg": "dist/rewrite.min.js",
"version": "2.0.3"
}
+77
View File
@@ -0,0 +1,77 @@
# rewrite-imports [![Build Status](https://travis-ci.org/lukeed/rewrite-imports.svg?branch=master)](https://travis-ci.org/lukeed/rewrite-imports)
Transforms various `import` statements into `require()` calls, using regular expressions.
> ***Looking for something _more_ backwards compatible?*** <br>
> Check out [`v1.4.0`](https://github.com/lukeed/rewrite-imports/tree/v1.4.0) which does not rely on destructured assignment!
## Caveats
This module returns a string and **does not** provide a runtime nor does it evaluate the output.
> :bulb: For this behavior, use [`rewrite-module`](https://github.com/lukeed/rewrite-module) or check out [`@taskr/esnext`](https://github.com/lukeed/taskr/tree/master/packages/esnext) for an example.
The output requires a JavaScript runtime that supports `require` calls and [destructuring assignments](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Object_destructuring) with Objects.
* At least `Node 6.x` is required
* Or, for browsers:
* A `require` shim is always needed see [`fn`](#fn)
* Ensure your target browsers support destructuring  see [chart](https://kangax.github.io/compat-table/es6/#test-destructuring,_assignment)
If you have [false positives](https://github.com/lukeed/rewrite-imports/issues/8), you may want to use an AST to find actual `import` statements before transformation.
> Check out an [example implementation](https://github.com/styleguidist/react-styleguidist/blob/82f22d217044dee6215e60696c39791ee168fc14/src/client/utils/transpileImports.js).
## Install
```
$ npm install --save rewrite-imports
```
## Usage
```js
const rImports = require('rewrite-imports');
rImports(`import foo from '../bar'`);
//=> const foo = require('../bar');
rImports(`import { foo } from 'bar'`);
//=> const { foo } = require('bar');
rImports(`import * as path from 'path';`);
//=> const path = require('path');
rImports(`import { foo as bar, baz as bat, lol } from 'quz';`);
//=> const { foo:bar, baz:bat, lol } = require('quz');
rImports(`import foobar, { foo as FOO, bar } from 'foobar';`);
//=> const foobar = require('foobar');
//=> const { foo:FOO, bar } = foobar;
```
## API
### rImports(input, fn)
#### input
Type: `String`
The `import` statement(s) or the code containing `import` statement(s).
> See [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) for valid `import` statement syntax.
#### fn
Type: `String`<br>
Default: `'require'`
The `require`-like function name to use. Defaults to `require` but you may choose to pass the name of a custom shim function; for example, `__webpack_require__` may work for webpack in the browser.
## License
MIT © [Luke Edwards](https://lukeed.com)