vite-plugin-require-support v1.6.1
A vite plugin supports the use of require syntax in vite.
- const foo = require('bar')
+ import hash_bar from 'bar'
+ const foo = hash_barUsage
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_fooAppointment
requirePath
'./a/b/foo.js'regard asrequirePathrequirePath=path+module ID+suffixeg:./a/b/+foo+jsrequirePathhave two shapes:string literalandtemplate literal
moduleVariable
Xs02j_fooregard asmoduleVariablemoduleVariable=hash+_+originVariableeg:Xs02j+_+foo
Reason
requirePath=path+module ID+suffixwhen 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')requirePathhave two shapes:string literalandtemplate literalconst foo = require('foo') // string literal const a = 'bar' const zoo = require(`${a}/b/c/zoo`) // template literalmoduleVariable=hash+_+originVariable- because variable
moduleVariableis generated internally by the plugin, in order to prevent the generated variable from conflicting with the variable declared by the user, I addhashto 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
featAdd new featuresfixFix the problem/BUGstyleThe code style is related and does not affect the running resultperfOptimization/performance improvementrefactorRefactorrevertUndo edittestTest relateddocsDocumentation/noteschoreDependency update/scaffolding configuration modification etc.workflowWorkflow improvementsciContinuous integrationtypesType definition file changeswipIn development
License
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago