vite-plugin-absolute-dynamic-imports 0.1.0
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 +57 -0
- package/index.js +57 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nicolas Ramirez
|
|
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,57 @@
|
|
|
1
|
+
# vite-plugin-absolute-dynamic-imports
|
|
2
|
+
|
|
3
|
+
Rewrites relative import specifiers (`import`, `export ... from`, and dynamic
|
|
4
|
+
`import()`) inside built JS chunks to absolute URLs prefixed with a `base`
|
|
5
|
+
you provide.
|
|
6
|
+
|
|
7
|
+
## Why
|
|
8
|
+
|
|
9
|
+
Vite already lets you set a `base` for the entry HTML and asset URLs, but
|
|
10
|
+
chunk-to-chunk imports inside the built JS are still emitted as relative
|
|
11
|
+
paths (`./chunks/foo-abc123.js`). That's fine when your build output is
|
|
12
|
+
served from the same directory it was built into — it breaks when it isn't.
|
|
13
|
+
|
|
14
|
+
This is the common case for AEM: your `vite build` output lands in `dist/`,
|
|
15
|
+
but gets deployed and served from a clientlib path like
|
|
16
|
+
`/etc.clientlibs/my-site/clientlibs/clientlib-site/resources/`. Relative
|
|
17
|
+
chunk imports resolve against the *page* the script runs on, not against
|
|
18
|
+
`dist/`, so they 404 in production even though everything works with `vite
|
|
19
|
+
preview` locally.
|
|
20
|
+
|
|
21
|
+
## Install
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
npm install --save-dev vite-plugin-absolute-dynamic-imports
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
// vite.config.js
|
|
31
|
+
import { defineConfig } from 'vite'
|
|
32
|
+
import absoluteDynamicImports from 'vite-plugin-absolute-dynamic-imports'
|
|
33
|
+
|
|
34
|
+
const CLIENTLIB_BASE = '/etc.clientlibs/my-site/clientlibs/clientlib-site/resources/'
|
|
35
|
+
|
|
36
|
+
export default defineConfig({
|
|
37
|
+
plugins: [
|
|
38
|
+
absoluteDynamicImports(CLIENTLIB_BASE)
|
|
39
|
+
]
|
|
40
|
+
})
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
The plugin only runs during `vite build` (it sets `apply: 'build'`
|
|
44
|
+
internally) — no need to conditionally include it based on `command`.
|
|
45
|
+
|
|
46
|
+
## How it works
|
|
47
|
+
|
|
48
|
+
After Rollup/Rolldown writes the bundle (`writeBundle`), the plugin parses
|
|
49
|
+
each emitted JS chunk with [es-module-lexer](https://github.com/guybedford/es-module-lexer)
|
|
50
|
+
and rewrites every import specifier that starts with `.` to
|
|
51
|
+
`base + <resolved path>`, based on the chunk's own location in the output
|
|
52
|
+
directory. Non-relative specifiers (bare package names, already-absolute
|
|
53
|
+
URLs) are left untouched.
|
|
54
|
+
|
|
55
|
+
## License
|
|
56
|
+
|
|
57
|
+
MIT
|
package/index.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { writeFile } from "node:fs/promises";
|
|
3
|
+
import { init, parse } from "es-module-lexer";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Rewrites relative import specifiers in built JS chunks to absolute URLs
|
|
7
|
+
* prefixed with `base`. Useful when the build output is deployed under a
|
|
8
|
+
* different path than where it's requested from (e.g. an AEM clientlib
|
|
9
|
+
* served from `/etc.clientlibs/...` instead of the build's own `dist/`).
|
|
10
|
+
*
|
|
11
|
+
* Only active during `vite build` (not `vite dev`/`vite preview`).
|
|
12
|
+
*
|
|
13
|
+
* @param {string} base absolute URL prefix, e.g. '/etc.clientlibs/my-site/clientlibs/clientlib-site/resources/'
|
|
14
|
+
* @returns {import('vite').Plugin}
|
|
15
|
+
*/
|
|
16
|
+
export default function absoluteDynamicImports(base) {
|
|
17
|
+
if (!base) {
|
|
18
|
+
throw new Error(
|
|
19
|
+
"vite-plugin-absolute-dynamic-imports: a base URL is required",
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
if (!base.endsWith("/")) base += "/";
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
name: "absolute-dynamic-imports",
|
|
26
|
+
apply: "build",
|
|
27
|
+
async writeBundle(options, bundle) {
|
|
28
|
+
await init();
|
|
29
|
+
const chunks = Object.values(bundle).filter((c) => c.type === "chunk");
|
|
30
|
+
// Independent files -- no reason to write them one at a time. `chunk.code` is the same
|
|
31
|
+
// in-memory source Rollup/Rolldown just wrote to disk, so there's no need to read it back.
|
|
32
|
+
await Promise.all(
|
|
33
|
+
chunks.map(async (chunk) => {
|
|
34
|
+
const dir = path.posix.dirname(chunk.fileName);
|
|
35
|
+
let code = chunk.code;
|
|
36
|
+
const [imports] = parse(code);
|
|
37
|
+
const relativeImports = imports
|
|
38
|
+
.filter((imp) => imp.specifier && imp.specifier.startsWith("."))
|
|
39
|
+
.sort((a, b) => b.start - a.start);
|
|
40
|
+
if (!relativeImports.length) return;
|
|
41
|
+
for (const imp of relativeImports) {
|
|
42
|
+
const resolved =
|
|
43
|
+
base + path.posix.normalize(path.posix.join(dir, imp.specifier));
|
|
44
|
+
// Dynamic import()'s start/end include the quotes; other forms don't.
|
|
45
|
+
const replacement =
|
|
46
|
+
imp.type === "dynamic"
|
|
47
|
+
? `${code[imp.start]}${resolved}${code[imp.start]}`
|
|
48
|
+
: resolved;
|
|
49
|
+
code = code.slice(0, imp.start) + replacement + code.slice(imp.end);
|
|
50
|
+
}
|
|
51
|
+
const filePath = path.join(options.dir, chunk.fileName);
|
|
52
|
+
await writeFile(filePath, code);
|
|
53
|
+
}),
|
|
54
|
+
);
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "vite-plugin-absolute-dynamic-imports",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Rewrites relative import specifiers in built chunks to absolute URLs — useful when your build output is served from a different base path than where it's built (e.g. AEM clientlibs).",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"vite",
|
|
7
|
+
"vite-plugin",
|
|
8
|
+
"aem",
|
|
9
|
+
"clientlib",
|
|
10
|
+
"dynamic-import",
|
|
11
|
+
"absolute-url"
|
|
12
|
+
],
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"author": "Nicolas Ramirez",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/ramirezcgn/vite-plugin-absolute-dynamic-imports.git"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/ramirezcgn/vite-plugin-absolute-dynamic-imports/issues"
|
|
21
|
+
},
|
|
22
|
+
"homepage": "https://github.com/ramirezcgn/vite-plugin-absolute-dynamic-imports#readme",
|
|
23
|
+
"type": "module",
|
|
24
|
+
"main": "index.js",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": "./index.js"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"index.js",
|
|
30
|
+
"README.md",
|
|
31
|
+
"LICENSE"
|
|
32
|
+
],
|
|
33
|
+
"scripts": {
|
|
34
|
+
"lint": "eslint .",
|
|
35
|
+
"lint:fix": "eslint . --fix",
|
|
36
|
+
"test": "vitest run"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"vite": ">=5"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"es-module-lexer": "^3.0.2"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@eslint/js": "^9.19.0",
|
|
46
|
+
"eslint": "^9.19.0",
|
|
47
|
+
"vitest": "^5.0.1"
|
|
48
|
+
}
|
|
49
|
+
}
|