Skip to content
New issue

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

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

Already on GitHub? # to your account

feat(createFile): support async content #299

Merged
merged 2 commits into from
Jun 9, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ import { ProjectContext } from '../../../../projectContext';

const { getAllFiles, getFirstModuleExport } = fileUtils;

export default (context: ProjectContext) =>
createFileWithoutName(() => {
const { app } = context.userModule;
const candidates = Array.isArray(app) ? app : [app];
return {
dependencies: candidates,
content: () => {
return getFirstModuleExport(getAllFiles(candidates), candidates, true);
}
};
export default (context: ProjectContext) => {
const { app } = context.userModule;
const candidates = Array.isArray(app) ? app : [app];
return createFileWithoutName({
dependencies: candidates,
content: () => {
return getFirstModuleExport(getAllFiles(candidates), candidates, true);
}
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ import { ProjectContext } from '../../../../projectContext';

const { getAllFiles, getFirstModuleExport } = fileUtils;

export default (context: ProjectContext) =>
createFileWithoutName(() => {
const { error } = context.userModule;
const candidates = Array.isArray(error) ? error : [error];
return {
dependencies: candidates,
content: () => {
return getFirstModuleExport(getAllFiles(candidates), candidates, true);
}
};
export default (context: ProjectContext) => {
const { error } = context.userModule;
const candidates = Array.isArray(error) ? error : [error];
return createFileWithoutName({
dependencies: candidates,
content: () => {
return getFirstModuleExport(getAllFiles(candidates), candidates, true);
}
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ import { ProjectContext } from '../../../../projectContext';

const { getAllFiles, getFirstModuleExport } = fileUtils;

export default (context: ProjectContext) =>
createFileWithoutName(() => {
const { runtime } = context.userModule;
const candidates = Array.isArray(runtime) ? runtime : [runtime];
return {
dependencies: candidates,
content: () => {
return getFirstModuleExport(getAllFiles(candidates), candidates);
}
};
export default (context: ProjectContext) => {
const { runtime } = context.userModule;
const candidates = Array.isArray(runtime) ? runtime : [runtime];
return createFileWithoutName({
dependencies: candidates,
content: () => {
return getFirstModuleExport(getAllFiles(candidates), candidates);
}
});
};
26 changes: 20 additions & 6 deletions packages/platform-web/src/lib/features/html-render/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
getFirstModuleExport,
getAllFiles
} from '@shuvi/service/lib/project/file-utils';
import { build } from '@shuvi/toolpack/lib/utils/build-loaders';
import { buildToString } from '@shuvi/toolpack/lib/utils/build-loaders';
import * as fs from 'fs';
import * as path from 'path';
import { extendedHooks } from './hooks';
import {
Expand Down Expand Up @@ -146,17 +147,30 @@ const core = createPlugin({
},
dependencies: serverCandidates
});
const loadersFileName = path.join(
context.paths.appDir,
'files',
'loaders.js'
);
const loadersBuildFile = createFile({
name: 'loaders-build.js',
content: async () => {
if (fs.existsSync(loadersFileName)) {
return await buildToString(loadersFileName);
}
return '';
},
dependencies: [paths.pagesDir, loadersFileName]
});
return [
userDocumentFile,
routerConfigFile,
routesFile,
userServerFile,
userDocumentFile,
loadersFile
loadersFile,
loadersBuildFile
];
},
afterShuviAppBuild: async context => {
await build(path.join(context.paths.appDir, 'files'), context.mode);
},
addRuntimeService: () => [
{
source: require.resolve(
Expand Down
1 change: 0 additions & 1 deletion packages/service/src/core/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ class Api {
this._initArtifacts()
]);
await this._projectBuilder.build(this._paths.privateDir);
await this.pluginManager.runner.afterShuviAppBuild();
}

addRuntimeFile(options: FileOptions): void {
Expand Down
2 changes: 0 additions & 2 deletions packages/service/src/core/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
import { IPluginContext } from './apiTypes';

const afterInit = createAsyncParallelHook<void>();
const afterShuviAppBuild = createAsyncParallelHook<void>();
const afterBuild = createAsyncParallelHook<void>();
const afterDestroy = createAsyncParallelHook<void>();
const afterBundlerDone = createAsyncParallelHook<BundlerDoneExtra>();
Expand Down Expand Up @@ -58,7 +57,6 @@ const addRuntimeService = createAsyncParallelHook<

const builtinPluginHooks = {
afterInit,
afterShuviAppBuild,
afterBuild,
afterDestroy,
afterBundlerDone,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ afterEach(async () => {

describe('createFile', () => {
describe('should work with watching files', () => {
describe('should work without using context', () => {
describe('should work without using context, and sync content', () => {
test('should update when single dependency file update', async () => {
const fileManager = getFileManager({ watch: true });
fileManager.addFile(
Expand Down Expand Up @@ -145,6 +145,104 @@ describe('createFile', () => {
});
});

describe('should work without using context, and async content', () => {
test('should update when single dependency file update', async () => {
const fileManager = getFileManager({ watch: true });
fileManager.addFile(
createFile({
name: FILE_RESULT,
content() {
return fs.readFileSync(fileA, 'utf8') as string;
},
dependencies: fileA
})
);
await fileManager.mount(resolveFixture('createFile'));
expect(fs.readFileSync(file(FILE_RESULT), 'utf8')).toBe('a\n');
fs.writeFileSync(fileA, 'aa\n', 'utf8');
await wait(500);
expect(fs.readFileSync(file(FILE_RESULT), 'utf8')).toBe('aa\n');
await fileManager.unmount();
});

test('should update when multiple dependency files update', async () => {
const fileManager = getFileManager({ watch: true });
const dependencies = [fileA, fileB, unexistedFileC];
fileManager.addFile(
createFile({
name: FILE_RESULT,
async content() {
await wait(1000);
let content = '';
dependencies.forEach(file => {
if (fs.existsSync(file)) {
content += fs.readFileSync(file, 'utf8');
}
});
return content;
},
dependencies
})
);
await fileManager.mount(resolveFixture('createFile'));
expect(fs.readFileSync(file(FILE_RESULT), 'utf8')).toBe('a\nb\n');
fs.writeFileSync(fileA, 'aa\n', 'utf8');
fs.writeFileSync(fileB, 'bb\n', 'utf8');
await wait(1200);
expect(fs.readFileSync(file(FILE_RESULT), 'utf8')).toBe('aa\nbb\n');
fs.writeFileSync(unexistedFileC, 'cc\n', 'utf8');
await wait(1200);
expect(fs.readFileSync(file(FILE_RESULT), 'utf8')).toBe('aa\nbb\ncc\n');
await fileManager.unmount();
await safeDelete(unexistedFileC);
});

test('should update when dependency files and directories update', async () => {
const fileManager = getFileManager({ watch: true });
const dependencies = [
fileA,
fileB,
unexistedFileC,
directoryA,
directoryB
];
fileManager.addFile(
createFile({
name: FILE_RESULT,
async content() {
await wait(1000);
let content = '';
const allFiles = getAllFiles(dependencies);
allFiles.forEach(file => {
if (fs.existsSync(file)) {
content += fs.readFileSync(file, 'utf8');
}
});
return content;
},
dependencies
})
);
await fileManager.mount(resolveFixture('createFile'));
expect(fs.readFileSync(file(FILE_RESULT), 'utf8')).toBe(
'a\nb\ndaa\ndab\n'
);
fs.writeFileSync(fileA, 'aa\n', 'utf8');
fs.writeFileSync(fileB, 'bb\n', 'utf8');
fs.writeFileSync(unexistedFileC, 'cc\n', 'utf8');
fs.writeFileSync(daa, 'daaa\n', 'utf8');
fs.writeFileSync(dba, 'dba\n', 'utf8');
await wait(1200);
expect(fs.readFileSync(file(FILE_RESULT), 'utf8')).toBe(
'aa\nbb\ncc\ndaaa\ndab\ndba\n'
);
await fileManager.unmount();
await safeDelete(unexistedFileC);
await safeDelete(dba);
fs.writeFileSync(daa, 'daa\n', 'utf8');
});
});

describe('should work with using context', () => {
test('should update when context as dependencies', async () => {
const context = reactive({
Expand Down
Loading