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

Directory-based builds / output #36

Merged
merged 1 commit into from
Nov 22, 2018
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"mailgun": "^0.5.0",
"mariadb": "^2.0.1-beta",
"memcached": "^2.2.2",
"mkdirp": "^0.5.1",
"mongoose": "^5.3.12",
"mysql": "^2.16.0",
"pdfkit": "^0.8.3",
Expand Down
8 changes: 6 additions & 2 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@ const glob = promisify(require("glob"));
const bytes = require("bytes");

async function main() {
const cli = await ncc(__dirname + "/../src/cli", {
const { code: cli, assets: cliAssets } = await ncc(__dirname + "/../src/cli", {
externals: ["./index.js"]
});
const index = await ncc(__dirname + "/../src/index", {
const { code: index, assets: indexAssets } = await ncc(__dirname + "/../src/index", {
// we dont care about watching, so we don't want
// to bundle it. even if we did want watching and a bigger
// bundle, webpack (and therefore ncc) cannot currently bundle
// chokidar, which is quite convenient
externals: ["chokidar"]
});

if (Object.keys(cliAssets).length || Object.keys(indexAssets).length) {
console.error('Assets emitted by core build, these need to be written into the dist directory');
}

writeFileSync(__dirname + "/../dist/ncc/cli.js", cli);
writeFileSync(__dirname + "/../dist/ncc/index.js", index);

Expand Down
60 changes: 39 additions & 21 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,50 @@ const args = require("arg")({
M: "--no-minify"
});

const usage = `ncc build <input-file> [opts]
Options:
-M, --no-minify Skip output minification
-e, --external [mod] Skip bundling 'mod'. Can be used many times
-o, --out [file] Output directory (defaults to dist)
-h, --help Show help
`;

if (args["--help"]) {
console.error(`ncc <input-file> [opts]
Options:
-M, --no-minify Skip output minification
-e, --external [mod] Skip bundling 'mod'. Can be used many times
-o, --out [file] Output file (defaults to stdout)
-h, --help Show help
`);
console.error(usage);
process.exit(2);
}

if (args._.length !== 1) {
console.error(`Error: invalid arguments number (${args._.length})`);
console.error("Usage: ncc <input-file> [opts]");
if (args._.length === 0) {
console.error(`Error: No command specified\n${usage}`);
process.exit(1);
}

const ncc = require("./index.js")(resolve(args._[0]), {
minify: !args["--no-minify"],
externals: args["--external"]
});
switch (args._[0]) {
case "build":
if (args._.length > 2) {
console.error(`Error: Too many build arguments provided\n${usage}`);
process.exit(1);
}

ncc.then(code => {
if (args["--out"]) {
require("fs").writeFileSync(args["--out"], code);
} else {
process.stdout.write(code);
}
});
const ncc = require("./index.js")(require.resolve(args._[1] || "."), {
minify: !args["--no-minify"],
externals: args["--external"]
});

ncc.then(({ code, assets }) => {
const outDir = args["--out"] || resolve(dist);
const fs = require("fs");
const mkdirp = require("mkdirp");
mkdirp.sync(outDir);
fs.writeFileSync(outDir + "/index.js", code);
Object.keys(assets).forEach(asset => {
mkdirp.sync(path.dirname(asset));
fs.writeFileSync(outDir + "/" + asset, assets[asset]);
});
});
break;

default:
console.error(`Error: Invalid command "${args._[0]}"\n${usage}`);
process.exit(1);
}
22 changes: 21 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,32 @@ module.exports = async (entry, { externals = [], minify = true } = {}) => {
compiler.outputFileSystem = mfs;
compiler.resolvers.normal.fileSystem = mfs;
return new Promise((resolve, reject) => {
const assets = Object.create(null);
getFlatFiles(mfs.data, assets);
delete assets['/out.js'];
compiler.run((err, stats) => {
if (err) return reject(err);
if (stats.hasErrors()) {
return reject(new Error(stats.toString()));
}
resolve(mfs.readFileSync("/out.js", "utf8"));
resolve({
code: mfs.readFileSync("/out.js", "utf8"),
assets
});
});
});
};

// this could be rewritten with actual FS apis / globs, but this is simpler
function getFlatFiles (mfsData, output, curBase = '') {
for (const path of Object.keys(mfsData)) {
const item = mfsData[path];
const curPath = curBase + '/' + path;
// directory
if (item[""] = true)
getFlatFiles(item, output, curPath);
// file
else
output[curPath] = mfsData[path];
}
}
2 changes: 1 addition & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ for (const integrationTest of fs.readdirSync(__dirname + "/integration")) {
// ignore e.g.: `.json` files
if (!integrationTest.endsWith(".js")) continue;
it(`should evaluate ${integrationTest} without errors`, async () => {
const code = await ncc(__dirname + "/integration/" + integrationTest);
const { code, assets } = await ncc(__dirname + "/integration/" + integrationTest);
module.exports = null;
eval(code);
if ("function" !== typeof module.exports) {
Expand Down