Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix filesystem cache not working when package individually is set #1037

Merged
merged 3 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 17 additions & 3 deletions lib/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ function getStatsLogger(statsConfig, consoleLog, { log, ServerlessError }) {
};
}

function isIndividialPackaging() {
return _.get(this.serverless, 'service.package.individually');
}

function getExternalModuleName(module) {
const pathArray = /^external .*"(.*?)"$/.exec(module.identifier());
if (!pathArray) {
Expand Down Expand Up @@ -120,11 +124,21 @@ function webpackConcurrentCompile(configs, logStats, concurrency, ServerlessErro
const errors = [];
return BbPromise.map(
configs,
config =>
webpackCompile(config, logStats).catch(error => {
config => {
if (isIndividialPackaging.call(this) && _.get(config, 'cache.type') === 'filesystem') {
const clonedConfig = _.clone(config);
const entryFunc = _.find(this.entryFunctions, ['entry.key', _.keys(config.entry)[0]]);
clonedConfig.cache.name = entryFunc.func.name;
return webpackCompile(clonedConfig, logStats, ServerlessError).catch(error => {
errors.push(error);
return error.stats;
});
}
return webpackCompile(config, logStats, ServerlessError).catch(error => {
errors.push(error);
return error.stats;
}),
});
},
{ concurrency }
).then(stats => {
if (errors.length) {
Expand Down
161 changes: 161 additions & 0 deletions tests/compile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,70 @@ describe('compile', () => {
});
});

it('should work with individual compile and webpack filesystem cache', () => {
const testWebpackConfig = {
cache: {
type: 'filesystem'
},
// Below entry is inserted during validate() which happens before compile()
// Thus the assumed value
entry: {
'function-name/handler': './function-name/handler.js'
}
};
const multiStats = {
stats: [
{
compilation: {
errors: [],
compiler: {
outputPath: 'statsMock-outputPath'
},
modules: []
},
toString: jest.fn().mockReturnValue('testStats'),
hasErrors: _.constant(false)
}
]
};
module.webpackConfig = testWebpackConfig;
module.configuration = { concurrency: 1 };
module.serverless.service.package = {
individually: true
};
module.entryFunctions = [
{
handlerFile: 'function-name/handler',
funcName: 'function-name',
func: {
handler: 'function-name/handler.handler',
name: 'service-stage-function-name'
},
entry: {
key: 'function-name/handler',
value: './function-name/handler.js'
}
}
];
webpackMock.compilerMock.run.mockClear();
webpackMock.compilerMock.run.mockImplementation(cb => cb(null, multiStats));
return expect(module.compile())
.resolves.toBeUndefined()
.then(() => {
expect(webpackMock).toHaveBeenCalledWith({
cache: {
type: 'filesystem',
name: 'service-stage-function-name'
},
entry: {
'function-name/handler': './function-name/handler.js'
}
});
expect(webpackMock.compilerMock.run).toHaveBeenCalledTimes(1);
return null;
});
});

it('should work with concurrent compile', () => {
const testWebpackConfig = ['testconfig', 'testconfig2'];
const multiStats = {
Expand Down Expand Up @@ -129,6 +193,103 @@ describe('compile', () => {
});
});

it('should concurrently work with individual compile and webpack filesystem cache', () => {
const testWebpackConfig = [
{
cache: {
type: 'filesystem'
},
// Below entry is inserted during validate() which happens before compile()
// Thus the assumed value
entry: {
'function-name-1/handler': './function-name-1/handler.js'
}
},
{
cache: {
type: 'filesystem'
},
// Below entry is inserted during validate() which happens before compile()
// Thus the assumed value
entry: {
'function-name-2/handler': './function-name-2/handler.js'
}
}
];
const multiStats = {
stats: [
{
compilation: {
errors: [],
compiler: {
outputPath: 'statsMock-outputPath'
},
modules: []
},
toString: jest.fn().mockReturnValue('testStats'),
hasErrors: _.constant(false)
}
]
};
module.webpackConfig = testWebpackConfig;
module.configuration = { concurrency: 2 };
module.serverless.service.package = {
individually: true
};
module.entryFunctions = [
{
handlerFile: 'function-name-1/handler',
funcName: 'function-name-1',
func: {
handler: 'function-name-1/handler.handler',
name: 'service-stage-function-name-1'
},
entry: {
key: 'function-name-1/handler',
value: './function-name-1/handler.js'
}
},
{
handlerFile: 'function-name-2/handler',
funcName: 'function-name-2',
func: {
handler: 'function-name-2/handler.handler',
name: 'service-stage-function-name-2'
},
entry: {
key: 'function-name-2/handler',
value: './function-name-2/handler.js'
}
}
];
webpackMock.compilerMock.run.mockClear();
webpackMock.compilerMock.run.mockImplementation(cb => cb(null, multiStats));
return expect(module.compile())
.resolves.toBeUndefined()
.then(() => {
expect(webpackMock).toHaveBeenCalledWith({
cache: {
type: 'filesystem',
name: 'service-stage-function-name-1'
},
entry: {
'function-name-1/handler': './function-name-1/handler.js'
}
});
expect(webpackMock).toHaveBeenCalledWith({
cache: {
type: 'filesystem',
name: 'service-stage-function-name-2'
},
entry: {
'function-name-2/handler': './function-name-2/handler.js'
}
});
expect(webpackMock.compilerMock.run).toHaveBeenCalledTimes(2);
return null;
});
});

it('should use correct stats option', () => {
const testWebpackConfig = {
stats: 'minimal'
Expand Down