esbuild-auto-path-plugin 0.15.1-beta.5107
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +65 -0
- package/lib/index.d.ts +6 -0
- package/lib/index.js +74 -0
- package/lib/index.js.map +1 -0
- package/package.json +43 -0
- package/set-path.js +22 -0
- package/src/index.ts +84 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 - 2022 smapiot
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# `esbuild-auto-path-plugin`
|
|
2
|
+
|
|
3
|
+
A plugin for `esbuild` to allow referencing files relative to the path of the JavaSCript. This can be useful to work in a micro frontend setup where the individual files of one micro frontend are served from a different path than the other micro frontends, i.e., no common public path can be determined.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
Install the plugin:
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm i esbuild-auto-path-plugin --save-dev
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Now in your `esbuild` configuration file you can do:
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
const { build } = require('esbuild');
|
|
17
|
+
const { autoPathPlugin } = require('esbuild-auto-path-plugin');
|
|
18
|
+
|
|
19
|
+
build({
|
|
20
|
+
// ...
|
|
21
|
+
plugins: [autoPathPlugin()],
|
|
22
|
+
});
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
At this point you can reference an asset file (e.g., `.jpg`) in any file, e.g., in a TypeScript asset
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
import someImage from './my.jpg';
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
The reference to the generated asset file will be done via the automatically (i.e., at runtime) determined path.
|
|
32
|
+
|
|
33
|
+
### File Extensions
|
|
34
|
+
|
|
35
|
+
This plugin takes all extensions mapped to the `file` type in your esbuild configuration. However, you can also use the plugin to generate you this list of files. By default (i.e., if you don't specify any option and have not introduced any custom loader mapping) the following extensions will be mapped:
|
|
36
|
+
|
|
37
|
+
- `.png`
|
|
38
|
+
- `.svg`
|
|
39
|
+
- `.jpg`
|
|
40
|
+
- `.jpeg`
|
|
41
|
+
- `.webp`
|
|
42
|
+
- `.mp4`
|
|
43
|
+
- `.mp3`
|
|
44
|
+
- `.ogg`
|
|
45
|
+
- `.wav`
|
|
46
|
+
- `.ogv`
|
|
47
|
+
- `.wasm`
|
|
48
|
+
- `.gif`
|
|
49
|
+
|
|
50
|
+
To override this list you can just pass in the extensions like this:
|
|
51
|
+
|
|
52
|
+
```js
|
|
53
|
+
const { build } = require('esbuild');
|
|
54
|
+
const { autoPathPlugin } = require('esbuild-auto-path-plugin');
|
|
55
|
+
|
|
56
|
+
build({
|
|
57
|
+
plugins: [autoPathPlugin({
|
|
58
|
+
defaultExtensions: [".png", ".mov", ".woff"]
|
|
59
|
+
})],
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## License
|
|
64
|
+
|
|
65
|
+
This plugin is released using the MIT license. For more information see the [LICENSE file](LICENSE).
|
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.autoPathPlugin = void 0;
|
|
4
|
+
const promises_1 = require("fs/promises");
|
|
5
|
+
const path_1 = require("path");
|
|
6
|
+
const fallbackExtensions = [
|
|
7
|
+
".png",
|
|
8
|
+
".svg",
|
|
9
|
+
".jpg",
|
|
10
|
+
".jpeg",
|
|
11
|
+
".webp",
|
|
12
|
+
".mp4",
|
|
13
|
+
".mp3",
|
|
14
|
+
".ogg",
|
|
15
|
+
".wav",
|
|
16
|
+
".ogv",
|
|
17
|
+
".wasm",
|
|
18
|
+
".gif",
|
|
19
|
+
];
|
|
20
|
+
function makeExtensions(extensions = fallbackExtensions) {
|
|
21
|
+
return extensions.reduce((obj, ext) => {
|
|
22
|
+
obj[ext] = "file";
|
|
23
|
+
return obj;
|
|
24
|
+
}, {});
|
|
25
|
+
}
|
|
26
|
+
const autoPathPlugin = (options = {}) => ({
|
|
27
|
+
name: "auto-path-plugin",
|
|
28
|
+
setup(build) {
|
|
29
|
+
const { loader = makeExtensions(options.defaultExtensions) } = build.initialOptions;
|
|
30
|
+
const extensions = [];
|
|
31
|
+
for (const ext of Object.keys(loader)) {
|
|
32
|
+
const l = loader[ext];
|
|
33
|
+
if (l === "file") {
|
|
34
|
+
extensions.push(ext);
|
|
35
|
+
delete loader[ext];
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const filter = new RegExp(`(${extensions.map((ext) => `\\${ext}`).join("|")})$`);
|
|
39
|
+
build.onResolve({ filter }, (args) => {
|
|
40
|
+
if (args.namespace === "ref-stub") {
|
|
41
|
+
return {
|
|
42
|
+
path: args.path,
|
|
43
|
+
namespace: "ref-binary",
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
else if (args.resolveDir !== "") {
|
|
47
|
+
return {
|
|
48
|
+
path: (0, path_1.isAbsolute)(args.path)
|
|
49
|
+
? args.path
|
|
50
|
+
: (0, path_1.join)(args.resolveDir, args.path),
|
|
51
|
+
// for CSS we'll just use the path; no intermediate module needed
|
|
52
|
+
namespace: args.kind === "url-token" ? "ref-binary" : "ref-stub",
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
return; // Ignore unresolvable paths
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
build.onLoad({ filter: /.*/, namespace: "ref-stub" }, async (args) => ({
|
|
60
|
+
resolveDir: (0, path_1.resolve)(__dirname),
|
|
61
|
+
contents: [
|
|
62
|
+
`import path from ${JSON.stringify(args.path)}`,
|
|
63
|
+
`import { __bundleUrl__ } from ${JSON.stringify("../set-path.js")}`,
|
|
64
|
+
`export default __bundleUrl__ + path;`,
|
|
65
|
+
].join("\n"),
|
|
66
|
+
}));
|
|
67
|
+
build.onLoad({ filter: /.*/, namespace: "ref-binary" }, async (args) => ({
|
|
68
|
+
contents: await (0, promises_1.readFile)(args.path),
|
|
69
|
+
loader: "file",
|
|
70
|
+
}));
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
exports.autoPathPlugin = autoPathPlugin;
|
|
74
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AACA,0CAAuC;AACvC,+B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A,cAAc,kBAqDxB"}
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "esbuild-auto-path-plugin",
|
|
3
|
+
"version": "0.15.1-beta.5107",
|
|
4
|
+
"description": "Plugin for transforming bundles to use an auto path for assets.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"plugin",
|
|
7
|
+
"esbuild",
|
|
8
|
+
"build",
|
|
9
|
+
"microfrontend"
|
|
10
|
+
],
|
|
11
|
+
"author": "smapiot",
|
|
12
|
+
"homepage": "https://piral.io",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"main": "lib/index.js",
|
|
15
|
+
"typings": "lib/index.d.ts",
|
|
16
|
+
"engines": {
|
|
17
|
+
"node": ">=12.0"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"lib",
|
|
21
|
+
"src",
|
|
22
|
+
"set-path.js"
|
|
23
|
+
],
|
|
24
|
+
"funding": {
|
|
25
|
+
"type": "github",
|
|
26
|
+
"url": "https://github.com/sponsors/smapiot"
|
|
27
|
+
},
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "git+https://github.com/smapiot/piral-cli-esbuild.git"
|
|
31
|
+
},
|
|
32
|
+
"bugs": {
|
|
33
|
+
"url": "https://github.com/smapiot/piral-cli-esbuild/issues"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsc",
|
|
37
|
+
"test": "echo \"Error: run tests from root\" && exit 1"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"esbuild": "^0.13.6"
|
|
41
|
+
},
|
|
42
|
+
"gitHead": "5f5af4ab4f6dc4996df9f989d156b69fe2736747"
|
|
43
|
+
}
|
package/set-path.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
function computePath() {
|
|
2
|
+
try {
|
|
3
|
+
throw new Error();
|
|
4
|
+
} catch (t) {
|
|
5
|
+
const e = ("" + t.stack).match(
|
|
6
|
+
/(https?|file|ftp|chrome-extension|moz-extension):\/\/[^)\n]+/g
|
|
7
|
+
);
|
|
8
|
+
|
|
9
|
+
if (e) {
|
|
10
|
+
return (
|
|
11
|
+
e[0].replace(
|
|
12
|
+
/^((?:https?|file|ftp|chrome-extension|moz-extension):\/\/.+)\/[^\/]+$/,
|
|
13
|
+
"$1"
|
|
14
|
+
) + "/"
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return "/";
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const __bundleUrl__ = computePath();
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { Plugin } from "esbuild";
|
|
2
|
+
import { readFile } from "fs/promises";
|
|
3
|
+
import { isAbsolute, join, resolve } from "path";
|
|
4
|
+
|
|
5
|
+
const fallbackExtensions = [
|
|
6
|
+
".png",
|
|
7
|
+
".svg",
|
|
8
|
+
".jpg",
|
|
9
|
+
".jpeg",
|
|
10
|
+
".webp",
|
|
11
|
+
".mp4",
|
|
12
|
+
".mp3",
|
|
13
|
+
".ogg",
|
|
14
|
+
".wav",
|
|
15
|
+
".ogv",
|
|
16
|
+
".wasm",
|
|
17
|
+
".gif",
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
interface AutoPathPluginOptions {
|
|
21
|
+
defaultExtensions?: Array<string>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function makeExtensions(extensions = fallbackExtensions) {
|
|
25
|
+
return extensions.reduce((obj, ext) => {
|
|
26
|
+
obj[ext] = "file";
|
|
27
|
+
return obj;
|
|
28
|
+
}, {} as Record<string, "file">);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const autoPathPlugin = (options: AutoPathPluginOptions = {}): Plugin => ({
|
|
32
|
+
name: "auto-path-plugin",
|
|
33
|
+
setup(build) {
|
|
34
|
+
const { loader = makeExtensions(options.defaultExtensions) } =
|
|
35
|
+
build.initialOptions;
|
|
36
|
+
const extensions: Array<string> = [];
|
|
37
|
+
|
|
38
|
+
for (const ext of Object.keys(loader)) {
|
|
39
|
+
const l = loader[ext];
|
|
40
|
+
|
|
41
|
+
if (l === "file") {
|
|
42
|
+
extensions.push(ext);
|
|
43
|
+
delete loader[ext];
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const filter = new RegExp(
|
|
48
|
+
`(${extensions.map((ext) => `\\${ext}`).join("|")})$`
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
build.onResolve({ filter }, (args) => {
|
|
52
|
+
if (args.namespace === "ref-stub") {
|
|
53
|
+
return {
|
|
54
|
+
path: args.path,
|
|
55
|
+
namespace: "ref-binary",
|
|
56
|
+
};
|
|
57
|
+
} else if (args.resolveDir !== "") {
|
|
58
|
+
return {
|
|
59
|
+
path: isAbsolute(args.path)
|
|
60
|
+
? args.path
|
|
61
|
+
: join(args.resolveDir, args.path),
|
|
62
|
+
// for CSS we'll just use the path; no intermediate module needed
|
|
63
|
+
namespace: args.kind === "url-token" ? "ref-binary" : "ref-stub",
|
|
64
|
+
};
|
|
65
|
+
} else {
|
|
66
|
+
return; // Ignore unresolvable paths
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
build.onLoad({ filter: /.*/, namespace: "ref-stub" }, async (args) => ({
|
|
71
|
+
resolveDir: resolve(__dirname),
|
|
72
|
+
contents: [
|
|
73
|
+
`import path from ${JSON.stringify(args.path)}`,
|
|
74
|
+
`import { __bundleUrl__ } from ${JSON.stringify("../set-path.js")}`,
|
|
75
|
+
`export default __bundleUrl__ + path;`,
|
|
76
|
+
].join("\n"),
|
|
77
|
+
}));
|
|
78
|
+
|
|
79
|
+
build.onLoad({ filter: /.*/, namespace: "ref-binary" }, async (args) => ({
|
|
80
|
+
contents: await readFile(args.path),
|
|
81
|
+
loader: "file",
|
|
82
|
+
}));
|
|
83
|
+
},
|
|
84
|
+
});
|