Skip to content

Commit

Permalink
feat: support npm style entrypoint mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalsadhu committed Jun 29, 2020
1 parent f5eca5e commit 5578f16
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 32 deletions.
11 changes: 11 additions & 0 deletions classes/publish/package/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,19 @@ module.exports = class PublishApp {
this.version = version;
this.level = level;
this.map = map;

this.js = js;
if (typeof js === 'string') {
this.js = {
'./index.js': js,
};
}
this.css = css;
if (typeof css === 'string') {
this.css = {
'./index.css': css,
};
}
this.dryRun = dryRun;
this.out = out;
this.path = isAbsolute(out) ? out : join(cwd, out);
Expand Down
40 changes: 22 additions & 18 deletions classes/publish/package/tasks/check-bundle-sizes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,30 @@ module.exports = class CheckBundleSizes extends Task {
this.log.debug('Checking bundle file sizes');
try {
if (js) {
const path = isAbsolute(js) ? js : join(cwd, js);
const jsEntrypointFile = compressedSize(
fs.readFileSync(path, 'utf8'),
);
this.log.debug(
` ==> JavaScript entrypoint size: ${bytes(
jsEntrypointFile,
)}`,
);
for (const [key, val] of Object.entries(js)) {
const path = isAbsolute(val) ? val : join(cwd, val);
const jsEntrypointFile = compressedSize(
fs.readFileSync(path, 'utf8'),
);
this.log.debug(
` ==> JavaScript entrypoint size (${key} => ${val}): ${bytes(
jsEntrypointFile,
)}`,
);
}
}
if (css) {
const path = isAbsolute(css) ? css : join(cwd, css);
const cssEntrypointFile = compressedSize(
fs.readFileSync(path, 'utf8'),
);
this.log.debug(
` ==> CSS entrypoint size: ${bytes(
cssEntrypointFile,
)}`,
);
for (const [key, val] of Object.entries(css)) {
const path = isAbsolute(val) ? val : join(cwd, val);
const cssEntrypointFile = compressedSize(
fs.readFileSync(path, 'utf8'),
);
this.log.debug(
` ==> CSS entrypoint size (${key} => ${val}): ${bytes(
cssEntrypointFile,
)}`,
);
}
}
} catch (err) {
throw new Error(`Failed to check bundle sizes: ${err.message}`);
Expand Down
10 changes: 7 additions & 3 deletions classes/publish/package/tasks/check-if-already-published.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

'use strict';

const { join, basename } = require('path');
const { join } = require('path');
const {
compareHashes,
fetchPackageMeta,
Expand Down Expand Up @@ -47,10 +47,14 @@ module.exports = class CheckIfAlreadyPublished extends Task {
try {
const localFiles = [join(path, './eik.json')];
if (js) {
localFiles.push(join(path, basename(js)));
for (const key of Object.keys(js)) {
localFiles.push(join(path, key));
}
}
if (css) {
localFiles.push(join(path, basename(css)));
for (const key of Object.keys(css)) {
localFiles.push(join(path, key));
}
}
localHash = await calculateFilesHash(localFiles);
} catch (err) {
Expand Down
18 changes: 10 additions & 8 deletions classes/publish/package/tasks/create-zip-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,23 @@ module.exports = class CreateZipFile extends Task {

if (js) {
try {
const jsPathSrc = isAbsolute(js) ? js : join(cwd, js);
const jsPathDest = join(path, basename(js));
copyFileSync(jsPathSrc, jsPathDest);
filesToZip.push(basename(jsPathDest));
for (const [key, val] of Object.entries(js)) {
const jsPathSrc = isAbsolute(val) ? val : join(cwd, val);
copyFileSync(jsPathSrc, join(path, key));
filesToZip.push(key);
}
} catch (err) {
throw new Error(`Failed to zip JavaScripts: ${err.message}`);
}
}

if (css) {
try {
const cssPathSrc = isAbsolute(css) ? css : join(cwd, css);
const cssPathDest = join(path, basename(css));
copyFileSync(cssPathSrc, cssPathDest);
filesToZip.push(basename(cssPathDest));
for (const [key, val] of Object.entries(css)) {
const cssPathSrc = isAbsolute(val) ? val : join(cwd, val);
copyFileSync(cssPathSrc, join(path, key));
filesToZip.push(key);
}
} catch (err) {
throw new Error(`Failed to zip CSS: ${err.message}`);
}
Expand Down
10 changes: 9 additions & 1 deletion classes/publish/package/tasks/dry-run.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@

'use strict';

const {join} = require('path');
const Task = require('./task');

module.exports = class DryRun extends Task {
async process(incoming = {}, outgoing = {}) {
const { dryRun, path, zipFile } = incoming;
const { dryRun, path, zipFile, js, css } = incoming;
if (dryRun) {
outgoing.files = [
{ pathname: path, type: 'temporary directory' },
{ pathname: zipFile, type: 'package archive' },
]

for (const key of Object.keys(js)) {
outgoing.files.push({ pathname: join(path, key), type: 'package file' })
}
for (const key of Object.keys(css)) {
outgoing.files.push({ pathname: join(path, key), type: 'package file' })
}
}

return outgoing;
Expand Down
4 changes: 2 additions & 2 deletions classes/publish/package/tasks/validate-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ module.exports = class ValidateInput extends Task {
);
}

if (js && typeof js !== 'string') {
if (js && typeof js !== 'string' && typeof js !== 'object') {
throw new ValidationError('Parameter "js" is not valid');
}

if (css && typeof css !== 'string') {
if (css && typeof css !== 'string' && typeof css !== 'object') {
throw new ValidationError('Parameter "css" is not valid');
}

Expand Down

0 comments on commit 5578f16

Please sign in to comment.