@rtvision/esbuild-dynamic-import
Advanced tools
Comparing version
@@ -6,3 +6,4 @@ import { Plugin } from 'esbuild'; | ||
filter?: RegExp; | ||
loader?: string; | ||
} | ||
export default function (config: DynamicImportConfig): Plugin; |
@@ -5,6 +5,6 @@ "use strict"; | ||
const fs_1 = require("fs"); | ||
const path_1 = (0, tslib_1.__importDefault)(require("path")); | ||
const fast_glob_1 = (0, tslib_1.__importDefault)(require("fast-glob")); | ||
const path_1 = tslib_1.__importDefault(require("path")); | ||
const fast_glob_1 = tslib_1.__importDefault(require("fast-glob")); | ||
function default_1(config) { | ||
var _a; | ||
var _a, _b; | ||
if (!Array.isArray(config.transformExtensions) && !config.changeRelativeToAbsolute) { | ||
@@ -14,2 +14,3 @@ throw new Error('Either transformExtensions needs to be supplied or changeRelativeToAbsolute needs to be true'); | ||
const filter = (_a = config.filter) !== null && _a !== void 0 ? _a : /\.js$/; | ||
const loader = (_b = config.loader) !== null && _b !== void 0 ? _b : 'js'; | ||
return { | ||
@@ -26,3 +27,3 @@ name: 'rtvision:dynamic-import', | ||
const contents = await replaceImports(fileContents, resolveDir, config); | ||
value = { fileContents, output: { contents } }; | ||
value = { fileContents, output: { contents, loader } }; | ||
cache.set(args.path, value); | ||
@@ -29,0 +30,0 @@ } |
{ | ||
"name": "@rtvision/esbuild-dynamic-import", | ||
"description": "Plugin for transform dynamic imports in esbuild", | ||
"version": "0.1.5", | ||
"version": "0.2.0", | ||
"author": "Kalvens (@kalvenschraut)", | ||
"bugs": "https://github.com/RtVision/esbuild-dynamic-imports/issues", | ||
"scripts": { | ||
"prepare": "tsc" | ||
}, | ||
"peerDependencies": { | ||
@@ -14,21 +11,21 @@ "esbuild": "^0.13.14" | ||
"dependencies": { | ||
"fast-glob": "^3.2.7", | ||
"tslib": "^2.3.1" | ||
"fast-glob": "^3.2.11", | ||
"tslib": "^2.4.0" | ||
}, | ||
"devDependencies": { | ||
"@types/chai": "^4.2.22", | ||
"@types/mocha": "^9.0.0", | ||
"@types/node": "^16.11.8", | ||
"@typescript-eslint/eslint-plugin": "^5.4.0", | ||
"@typescript-eslint/parser": "^5.4.0", | ||
"chai": "^4.3.4", | ||
"esbuild": "^0.13.14", | ||
"eslint": "^8.2.0", | ||
"eslint-config-standard": "^16.0.3", | ||
"eslint-plugin-import": "^2.25.3", | ||
"@types/chai": "^4.3.1", | ||
"@types/mocha": "^9.1.1", | ||
"@types/node": "^18.0.0", | ||
"@typescript-eslint/eslint-plugin": "^5.28.0", | ||
"@typescript-eslint/parser": "^5.28.0", | ||
"chai": "^4.3.6", | ||
"esbuild": "^0.14.45", | ||
"eslint": "^8.17.0", | ||
"eslint-config-standard": "^17.0.0", | ||
"eslint-plugin-import": "^2.26.0", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-promise": "^5.1.1", | ||
"mocha": "^9.1.3", | ||
"eslint-plugin-promise": "^6.0.0", | ||
"mocha": "^10.0.0", | ||
"nyc": "^15.1.0", | ||
"typescript": "^4.5.2" | ||
"typescript": "^4.7.3" | ||
}, | ||
@@ -48,3 +45,4 @@ "engines": { | ||
"repository": "RtVision/esbuild-dynamic-import", | ||
"types": "lib/index.d.ts" | ||
} | ||
"types": "lib/index.d.ts", | ||
"scripts": {} | ||
} |
@@ -1,15 +0,15 @@ | ||
Esbuild Dynamic Import | ||
============= | ||
# Esbuild Dynamic Import | ||
[](https://npmjs.org/package/@rtvision/esbuild-dynamic-import) | ||
[](https://npmjs.org/package/@rtvision/esbuild-dynamic-import) | ||
[](https://github.com/RtVision/esbuild-dynamic-import/blob/master/package.json) | ||
[](https://npmjs.org/package/@rtvision/esbuild-dynamic-import) | ||
[](https://npmjs.org/package/@rtvision/esbuild-dynamic-import) | ||
[](https://github.com/RtVision/esbuild-dynamic-import/blob/master/package.json) | ||
### Features | ||
### Features | ||
-Transforms dynamic imports that contain a template string variable into static imports to be processed by esbuild | ||
-Rewrites dynamic imports of js files that contain a template string variable to use absolute path instead | ||
## Install | ||
```sh | ||
@@ -31,29 +31,38 @@ npm i @rtvision/esbuild-dynamic-import | ||
### transformExtensions | ||
Transforms all dynamic imports that contain a template varable and the imported file extension | ||
is included in transformExtensions. I.E. `import(``../../${file}.vue``)` | ||
All imports found will be turned into static imports of every possible valid import it could be. | ||
It then uses the static imports instead of node dynamically importing the file at runtime | ||
### transformExtensions | ||
My use case for this was I wanted esbuild to process the possible imports that are | ||
.vue (SFC vue) files so they can be processed by the EsbuildVue plugin and made into | ||
valid javascript that nodejs can run. | ||
### changeRelativeToAbsolute | ||
If there exists a dynamic import like `import(``../../$file}.js``)` | ||
then that in theory could be resolved at runtime just fine by nodejs. The main | ||
issue is that the relative file path is now different due to the bundled file produced by | ||
esbuild being in a likely different file location. changeRelativeToAbsolute will fix this issue | ||
by changing all relative dynamic imports that contain a template literal variable to absolute ones. | ||
Transforms all dynamic imports that contain a template varable and the imported file extension | ||
is included in transformExtensions. I.E. ` import(``../../${file}.vue``) ` | ||
All imports found will be turned into static imports of every possible valid import it could be. | ||
It then uses the static imports instead of node dynamically importing the file at runtime | ||
For my use case dynamic imports while using vite are needed to be relative for production builds | ||
especially since Rollup is currently used and Rollup requires all dynamic imports to be relative. | ||
### Filter | ||
The Filter parameter is used to reduce the scope of the file search. | ||
My use case for this was I wanted esbuild to process the possible imports that are | ||
.vue (SFC vue) files so they can be processed by the EsbuildVue plugin and made into | ||
valid javascript that nodejs can run. | ||
I set it to just our local source directory, but by default it will search through every | ||
js file that is not marked as external | ||
### changeRelativeToAbsolute | ||
If there exists a dynamic import like ` import(``../../${file}.js``) ` | ||
then that in theory could be resolved at runtime just fine by nodejs. The main | ||
issue is that the relative file path is now different due to the bundled file produced by | ||
esbuild being in a likely different file location. changeRelativeToAbsolute will fix this issue | ||
by changing all relative dynamic imports that contain a template literal variable to absolute ones. | ||
For my use case dynamic imports while using vite are needed to be relative for production builds | ||
especially since Rollup is currently used and Rollup requires all dynamic imports to be relative. | ||
### Filter | ||
The Filter parameter is used to reduce the scope of the file search. | ||
I set it to just search our local source directory, but by default it will search through every | ||
js file that is not marked as external | ||
### Loader | ||
Need to specify if you need one of esbuild's internal loaders to transform the file after | ||
the dynamic imports are handled by this plugin. i.e. if your filter is /\.jsx$/ then you need to specify jsx as your loader | ||
## License | ||
MIT © RtVision |
9666
2.34%110
1.85%68
15.25%Updated
Updated