vite-plugin-require-support
v1.6.1
Published
a vite plugin supports the use of require syntax.
Downloads
169
Readme
A vite plugin supports the use of require syntax
in vite.
- const foo = require('bar')
+ import hash_bar from 'bar'
+ const foo = hash_bar
Usage
npm install -D vite-plugin-require-support
// vite.config.(t|j)s
import { defineConfig } from 'vite';
import requireSupport from 'vite-plugin-require-support';
export default defineConfig({
plugins: [
requireSupport({
filters: /.ts$|.js$|.tsx$|.vue$/
})
],
});
Doing
- [ ] ModuleVariable need to generate hash.
- [ ] Optimize import mode.
Design
- const foo = require('./a/b/foo.js')
+ import Xs02j_foo from './a/b/foo.js'
+ const foo = Xs02j_foo
Appointment
requirePath
'./a/b/foo.js'
regard asrequirePath
requirePath
=path
+module ID
+suffix
eg:./a/b/
+foo
+js
requirePath
have two shapes:string literal
andtemplate literal
moduleVariable
Xs02j_foo
regard asmoduleVariable
moduleVariable
=hash
+_
+originVariable
eg:Xs02j
+_
+foo
Reason
requirePath
=path
+module ID
+suffix
when there are multiple module requests with similar appearance, the design is more flexible to distinguish different module request paths.
comparison algorithm priority of module request path:
module ID
>suffix
>path
const img1 = require('./a/b/foo.png') const img2 = require('./a/s/foo.png') const img2 = require('./a/b/foo.jpg')
requirePath
have two shapes:string literal
andtemplate literal
const foo = require('foo') // string literal const a = 'bar' const zoo = require(`${a}/b/c/zoo`) // template literal
moduleVariable
=hash
+_
+originVariable
- because variable
moduleVariable
is generated internally by the plugin, in order to prevent the generated variable from conflicting with the variable declared by the user, I addhash
to variable.( or Symbol?)
- because variable
export mode transform
// case1 - const foo = require('foo') - const bar = require('foo').fn + import SDWS_foo, { fn as SDC_foo_fn } from 'foo' + const foo = SDWS_foo + const bar = SDC_foo_fn // case2 - const a = require('foo').a - const b = require('foo').b + import { a as SDC_foo_a, b as SKDsk_foo_b } from 'foo' + const a = SDC_foo_a + const b = SKDsk_foo_b
Git Contribution submission specification
feat
Add new featuresfix
Fix the problem/BUGstyle
The code style is related and does not affect the running resultperf
Optimization/performance improvementrefactor
Refactorrevert
Undo edittest
Test relateddocs
Documentation/noteschore
Dependency update/scaffolding configuration modification etc.workflow
Workflow improvementsci
Continuous integrationtypes
Type definition file changeswip
In development