@@ -181,6 +181,134 @@ describe('Require Path Resolution', () => {
181181 expect ( compiledCode ) . not . toMatch ( / r e q u i r e \( [ ' " ] @ v a n t / )
182182 } )
183183
184+ it ( 'should resolve bare npm package requires via package entry' , async ( ) => {
185+ fs . mkdirSync ( 'pages/npm-entry' , { recursive : true } )
186+ fs . mkdirSync ( 'miniprogram_npm/westore' , { recursive : true } )
187+
188+ fs . writeFileSync ( 'app.json' , JSON . stringify ( {
189+ pages : [ 'pages/npm-entry/index' ]
190+ } ) )
191+
192+ fs . writeFileSync ( 'miniprogram_npm/westore/package.json' , JSON . stringify ( {
193+ main : 'index.js'
194+ } ) )
195+
196+ fs . writeFileSync ( 'miniprogram_npm/westore/index.js' , `
197+ module.exports = {
198+ create: function() {
199+ return 'westore'
200+ }
201+ }
202+ ` )
203+
204+ fs . writeFileSync ( 'pages/npm-entry/index.js' , `
205+ const westore = require('westore')
206+
207+ Page({
208+ onLoad() {
209+ console.log(westore.create())
210+ }
211+ })
212+ ` )
213+
214+ fs . writeFileSync ( 'pages/npm-entry/index.json' , JSON . stringify ( {
215+ navigationBarTitleText : 'NPM Entry'
216+ } ) )
217+
218+ storeInfo ( tempDir )
219+
220+ const progress = { completedTasks : 0 }
221+ const result = await compileJS ( [ { path : 'pages/npm-entry/index' } ] , null , null , progress )
222+
223+ const modulePaths = result . map ( module => module . path )
224+ expect ( modulePaths ) . toContain ( '/miniprogram_npm/westore/index' )
225+
226+ const pageModule = result . find ( module => module . path === 'pages/npm-entry/index' )
227+ expect ( pageModule . code ) . toContain ( 'require("/miniprogram_npm/westore/index")' )
228+ } )
229+
230+ it ( 'should resolve npm directory requires to index modules' , async ( ) => {
231+ fs . mkdirSync ( 'pages/npm-dir' , { recursive : true } )
232+ fs . mkdirSync ( 'miniprogram_npm/@vant/weapp/util' , { recursive : true } )
233+
234+ fs . writeFileSync ( 'app.json' , JSON . stringify ( {
235+ pages : [ 'pages/npm-dir/index' ]
236+ } ) )
237+
238+ fs . writeFileSync ( 'miniprogram_npm/@vant/weapp/util/index.js' , `
239+ exports.getName = function() {
240+ return 'vant-util'
241+ }
242+ ` )
243+
244+ fs . writeFileSync ( 'pages/npm-dir/index.js' , `
245+ const { getName } = require('@vant/weapp/util')
246+
247+ Page({
248+ onLoad() {
249+ console.log(getName())
250+ }
251+ })
252+ ` )
253+
254+ fs . writeFileSync ( 'pages/npm-dir/index.json' , JSON . stringify ( {
255+ navigationBarTitleText : 'NPM Dir'
256+ } ) )
257+
258+ storeInfo ( tempDir )
259+
260+ const progress = { completedTasks : 0 }
261+ const result = await compileJS ( [ { path : 'pages/npm-dir/index' } ] , null , null , progress )
262+
263+ const modulePaths = result . map ( module => module . path )
264+ expect ( modulePaths ) . toContain ( '/miniprogram_npm/@vant/weapp/util/index' )
265+
266+ const pageModule = result . find ( module => module . path === 'pages/npm-dir/index' )
267+ expect ( pageModule . code ) . toContain ( 'require("/miniprogram_npm/@vant/weapp/util/index")' )
268+ } )
269+
270+ it ( 'should prefer the nearest miniprogram_npm directory for bare package requires' , async ( ) => {
271+ fs . mkdirSync ( 'pages/near/index/miniprogram_npm/westore' , { recursive : true } )
272+ fs . mkdirSync ( 'miniprogram_npm/westore' , { recursive : true } )
273+
274+ fs . writeFileSync ( 'app.json' , JSON . stringify ( {
275+ pages : [ 'pages/near/index/index' ]
276+ } ) )
277+
278+ fs . writeFileSync ( 'pages/near/index/miniprogram_npm/westore/package.json' , JSON . stringify ( {
279+ main : 'local.js'
280+ } ) )
281+ fs . writeFileSync ( 'pages/near/index/miniprogram_npm/westore/local.js' , 'module.exports = "local"' )
282+
283+ fs . writeFileSync ( 'miniprogram_npm/westore/package.json' , JSON . stringify ( {
284+ main : 'root.js'
285+ } ) )
286+ fs . writeFileSync ( 'miniprogram_npm/westore/root.js' , 'module.exports = "root"' )
287+
288+ fs . writeFileSync ( 'pages/near/index/index.js' , `
289+ const westore = require('westore')
290+
291+ Page({
292+ onLoad() {
293+ console.log(westore)
294+ }
295+ })
296+ ` )
297+
298+ fs . writeFileSync ( 'pages/near/index/index.json' , JSON . stringify ( {
299+ navigationBarTitleText : 'Nearest NPM'
300+ } ) )
301+
302+ storeInfo ( tempDir )
303+
304+ const progress = { completedTasks : 0 }
305+ const result = await compileJS ( [ { path : 'pages/near/index/index' } ] , null , null , progress )
306+
307+ const pageModule = result . find ( module => module . path === 'pages/near/index/index' )
308+ expect ( pageModule . code ) . toContain ( 'require("/pages/near/index/miniprogram_npm/westore/local")' )
309+ expect ( pageModule . code ) . not . toContain ( 'require("/miniprogram_npm/westore/root")' )
310+ } )
311+
184312 it ( 'should handle mixed import and require statements' , async ( ) => {
185313 // 创建测试文件结构
186314 fs . mkdirSync ( 'pages/mixed' , { recursive : true } )
0 commit comments