Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
fix(one-app-server-bundler): serve-module read and write the module m…
Browse files Browse the repository at this point in the history
…ap once

O(1) rather than O(n)
  • Loading branch information
PixnBits committed Mar 18, 2024
1 parent 62854c6 commit 0ad0156
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const setup = (modulePath) => {
jest.resetModules();
jest.clearAllMocks();
jest.doMock('node:util', () => ({
parseArgs: jest.fn(() => ({ positionals: [modulePath] })),
parseArgs: jest.fn(() => ({ positionals: modulePath ? [modulePath] : [] })),
}));

fs = require('node:fs');
Expand All @@ -45,6 +45,12 @@ describe('serve-module', () => {
setPlatform(originalPlatform);
});

it('should throw if no module path is provided', () => {
setup();
fs._.setFiles({});
expect(() => require('../../bin/serve-module')).toThrowErrorMatchingInlineSnapshot('"serve-module(s) expects paths to modules to give to one-app to serve"');
});

it('should throw if the module doesn\'t have a version', () => {
fs._.setFiles({
'../my-module-name/package.json': JSON.stringify({ name: 'my-module-name' }),
Expand Down
62 changes: 34 additions & 28 deletions packages/one-app-server-bundler/bin/serve-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,21 @@ const symModulesPath = path.join(publicPath, 'modules');

fs.mkdirSync(symModulesPath, { recursive: true });

util.parseArgs({ allowPositionals: true }).positionals.forEach((modulePath) => {
const modulePaths = util.parseArgs({ allowPositionals: true }).positionals;

if (modulePaths.length === 0) {
throw new Error('serve-module(s) expects paths to modules to give to one-app to serve');
}

const moduleMapPath = path.join(publicPath, 'module-map.json');
let moduleMap;
try {
moduleMap = JSON.parse(fs.readFileSync(moduleMapPath));
} catch (e) {
moduleMap = { key: 'not-used-in-development', modules: {} };
}

modulePaths.forEach((modulePath) => {
const pkg = JSON.parse(fs.readFileSync(path.join(modulePath, 'package.json')));
const absoluteModulePath = modulePath.startsWith('/')
? modulePath : path.join(process.cwd(), modulePath);
Expand Down Expand Up @@ -58,33 +72,25 @@ util.parseArgs({ allowPositionals: true }).positionals.forEach((modulePath) => {
fs.symlinkSync(sourceBundlePath, symBundlePath, type);
}

const moduleMapPath = path.join(publicPath, 'module-map.json');
try {
fs.accessSync(moduleMapPath);
} catch (e) {
fs.writeFileSync(moduleMapPath, JSON.stringify({ key: 'not-used-in-development', modules: {} }, null, 2));
} finally {
const moduleMap = JSON.parse(fs.readFileSync(moduleMapPath));
const generalConfig = {
browser: {
integrity: browserSri,
url: `[one-app-dev-cdn-url]/static/modules/${moduleName}/${version}/${moduleName}.browser.js`,
},
node: {
integrity: nodeSri,
url: `[one-app-dev-cdn-url]/static/modules/${moduleName}/${version}/${moduleName}.node.js`,
},
};

const legacyConfig = legacyBrowserSri ? {
legacyBrowser: {
integrity: legacyBrowserSri,
url: `[one-app-dev-cdn-url]/static/modules/${moduleName}/${version}/${moduleName}.legacy.browser.js`,
},
} : {};
const generalConfig = {
browser: {
integrity: browserSri,
url: `[one-app-dev-cdn-url]/static/modules/${moduleName}/${version}/${moduleName}.browser.js`,
},
node: {
integrity: nodeSri,
url: `[one-app-dev-cdn-url]/static/modules/${moduleName}/${version}/${moduleName}.node.js`,
},
};

moduleMap.modules[moduleName] = { ...generalConfig, ...legacyConfig };
const legacyConfig = legacyBrowserSri ? {
legacyBrowser: {
integrity: legacyBrowserSri,
url: `[one-app-dev-cdn-url]/static/modules/${moduleName}/${version}/${moduleName}.legacy.browser.js`,
},
} : {};

fs.writeFileSync(moduleMapPath, JSON.stringify(moduleMap, null, 2));
}
moduleMap.modules[moduleName] = { ...generalConfig, ...legacyConfig };
});

fs.writeFileSync(moduleMapPath, JSON.stringify(moduleMap, null, 2));

0 comments on commit 0ad0156

Please sign in to comment.