@@ -12,6 +12,7 @@ const Serverless = require('serverless');
1212const childProcessMockFactory = require ( './mocks/child_process.mock' ) ;
1313const fsExtraMockFactory = require ( './mocks/fs-extra.mock' ) ;
1414const packageMock = require ( './mocks/package.mock.json' ) ;
15+ const packageLocalRefMock = require ( './mocks/packageLocalRef.mock.json' ) ;
1516
1617chai . use ( require ( 'chai-as-promised' ) ) ;
1718chai . use ( require ( 'sinon-chai' ) ) ;
@@ -151,6 +152,50 @@ describe('packExternalModules', () => {
151152 }
152153 ]
153154 } ;
155+ const statsWithFileRef = {
156+ stats : [
157+ {
158+ compilation : {
159+ chunks : [
160+ {
161+ modules : [
162+ {
163+ identifier : _ . constant ( '"crypto"' )
164+ } ,
165+ {
166+ identifier : _ . constant ( '"uuid/v4"' )
167+ } ,
168+ {
169+ identifier : _ . constant ( 'external "eslint"' )
170+ } ,
171+ {
172+ identifier : _ . constant ( '"mockery"' )
173+ } ,
174+ {
175+ identifier : _ . constant ( '"@scoped/vendor/module1"' )
176+ } ,
177+ {
178+ identifier : _ . constant ( 'external "@scoped/vendor/module2"' )
179+ } ,
180+ {
181+ identifier : _ . constant ( 'external "uuid/v4"' )
182+ } ,
183+ {
184+ identifier : _ . constant ( 'external "localmodule"' )
185+ } ,
186+ {
187+ identifier : _ . constant ( 'external "bluebird"' )
188+ } ,
189+ ]
190+ }
191+ ] ,
192+ compiler : {
193+ outputPath : '/my/Service/Path/.webpack/service'
194+ }
195+ }
196+ }
197+ ]
198+ } ;
154199
155200 it ( 'should do nothing if webpackIncludeModules is not set' , ( ) => {
156201 _ . unset ( serverless , 'service.custom.webpackIncludeModules' ) ;
@@ -212,6 +257,62 @@ describe('packExternalModules', () => {
212257 ] ) ) ;
213258 } ) ;
214259
260+ it ( 'should rebase file references' , ( ) => {
261+ const expectedCompositePackageJSON = {
262+ name : 'test-service' ,
263+ version : '1.0.0' ,
264+ description : 'Packaged externals for test-service' ,
265+ private : true ,
266+ dependencies : {
267+ '@scoped/vendor' : '1.0.0' ,
268+ uuid : '^5.4.1' ,
269+ localmodule : 'file:../../locals/../../mymodule' ,
270+ bluebird : '^3.4.0'
271+ }
272+ } ;
273+ const expectedPackageJSON = {
274+ dependencies : {
275+ '@scoped/vendor' : '1.0.0' ,
276+ uuid : '^5.4.1' ,
277+ localmodule : 'file:../../locals/../../mymodule' ,
278+ bluebird : '^3.4.0'
279+ }
280+ } ;
281+
282+ _ . set ( serverless , 'service.custom.webpackIncludeModules.packagePath' , path . join ( 'locals' , 'package.json' ) ) ;
283+ module . webpackOutputPath = 'outputPath' ;
284+ fsExtraMock . pathExists . yields ( null , false ) ;
285+ fsExtraMock . copy . yields ( ) ;
286+ childProcessMock . exec . onFirstCall ( ) . yields ( null , '{}' , '' ) ;
287+ childProcessMock . exec . onSecondCall ( ) . yields ( null , '' , '' ) ;
288+ childProcessMock . exec . onThirdCall ( ) . yields ( ) ;
289+ module . compileStats = statsWithFileRef ;
290+
291+ sandbox . stub ( process , 'cwd' ) . returns ( path . join ( '/my/Service/Path' ) ) ;
292+ mockery . registerMock ( path . join ( process . cwd ( ) , 'locals' , 'package.json' ) , packageLocalRefMock ) ;
293+
294+ return expect ( module . packExternalModules ( ) ) . to . be . fulfilled
295+ . then ( ( ) => BbPromise . all ( [
296+ // The module package JSON and the composite one should have been stored
297+ expect ( writeFileSyncStub ) . to . have . been . calledTwice ,
298+ expect ( writeFileSyncStub . firstCall . args [ 1 ] ) . to . equal ( JSON . stringify ( expectedCompositePackageJSON , null , 2 ) ) ,
299+ expect ( writeFileSyncStub . secondCall . args [ 1 ] ) . to . equal ( JSON . stringify ( expectedPackageJSON , null , 2 ) ) ,
300+ // The modules should have been copied
301+ expect ( fsExtraMock . copy ) . to . have . been . calledOnce ,
302+ // npm ls and npm prune should have been called
303+ expect ( childProcessMock . exec ) . to . have . been . calledThrice ,
304+ expect ( childProcessMock . exec . firstCall ) . to . have . been . calledWith (
305+ 'npm ls -prod -json -depth=1'
306+ ) ,
307+ expect ( childProcessMock . exec . secondCall ) . to . have . been . calledWith (
308+ 'npm install'
309+ ) ,
310+ expect ( childProcessMock . exec . thirdCall ) . to . have . been . calledWith (
311+ 'npm prune'
312+ )
313+ ] ) ) ;
314+ } ) ;
315+
215316 it ( 'should skip module copy for Google provider' , ( ) => {
216317 const expectedCompositePackageJSON = {
217318 name : 'test-service' ,
0 commit comments