Skip to content

Commit

Permalink
Get Babel and Webpack building correctly from another directory.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeresig committed Sep 8, 2018
1 parent 26d6513 commit 927c230
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 61 deletions.
123 changes: 62 additions & 61 deletions src/bin/mongaku.js
Expand Up @@ -24,6 +24,61 @@ const getBinary = bin => {
return binPath;
};

const runBabel = watch => {
const rootDir = localFile("../..");
const srcDir = path.join(rootDir, "src");
const buildDir = path.join(rootDir, "build");
const configFile = path.join(rootDir, ".babelrc");
const binary = getBinary("babel");

const cmd = `${binary} ${srcDir} --out-dir ${buildDir} --config-file ${configFile} --verbose`;

if (watch) {
return shell.exec(`${cmd} -w`, {async: true});
}

return shell.exec(cmd);
};

const runWebpack = watch => {
const rootDir = localFile("../..");
const configFile = path.join(rootDir, "webpack.config.js");
const binary = getBinary("webpack");

const cmd = `${binary} --config ${configFile}`;

if (watch) {
return shell.exec(`${cmd} -w`, {async: true});
}

return shell.exec(cmd);
};

const runSupervisor = () => {
const cwd = process.cwd();
const localDir = localFile("..");
const serverjs = localFile("../mongaku.js");
const ignored = [
path.join(localDir, "node_modules"),
path.join(cwd, "node_modules"),
path.join(localDir, "..", "static"),
path.join(cwd, "static"),
].join(",");

const devCmd = [
getBinary("supervisor"),
`-w ${localDir},${cwd}`,
"-e js",
`-i ${ignored}`,
"--",
serverjs,
]
.concat(extraArgs)
.join(" ");

return shell.exec(devCmd, {async: true});
};

if (args.v || args.version) {
console.log(pkg.version);
} else if (cmd === "start") {
Expand Down Expand Up @@ -57,69 +112,15 @@ if (args.v || args.version) {
} else if (cmd === "restart") {
shell.exec(`${getBinary("naught")} deploy mongaku.ipc`);
} else if (cmd === "build") {
const rootDir = localFile("../..");
const srcDir = path.join(rootDir, "src");
const buildDir = path.join(rootDir, "build");

shell.exec(
`${getBinary("babel")} ${srcDir} --out-dir ${buildDir} --verbose`,
);

const webpackConfig = path.join(rootDir, "webpack.config.js");
shell.exec(`${getBinary("webpack")} --config ${webpackConfig}`);
runBabel();
runWebpack();
} else if (cmd === "build-watch") {
const rootDir = localFile("../..");
const srcDir = path.join(rootDir, "src");
const buildDir = path.join(rootDir, "build");

const webpackConfig = path.join(rootDir, "webpack.config.js");
console.log(
shell.exec(`${getBinary("webpack")} --config ${webpackConfig} -w`, {
async: true,
}),
);

shell.exec(
`${getBinary("babel")} ${srcDir} --out-dir ${buildDir} -w --verbose`,
{async: true},
);
runWebpack(true);
runBabel(true);
} else if (cmd === "dev") {
const cwd = process.cwd();
const localDir = localFile("..");
const serverjs = localFile("../mongaku.js");
const ignored = [
path.join(localDir, "node_modules"),
path.join(cwd, "node_modules"),
path.join(localDir, "..", "static"),
path.join(cwd, "static"),
].join(",");

const devCmd = [
getBinary("supervisor"),
`-w ${localDir},${cwd}`,
"-e js",
`-i ${ignored}`,
"--",
serverjs,
]
.concat(extraArgs)
.join(" ");

shell.exec(devCmd, {async: true});

const rootDir = localFile("../..");
const srcDir = path.join(rootDir, "src");
const buildDir = path.join(rootDir, "build");

shell.exec(
`${getBinary("babel")} ${srcDir} --out-dir ${buildDir} -w --verbose`,
{async: true},
);

const webpackConfig = path.join(rootDir, "webpack.config.js");
shell.exec(`${getBinary("webpack")} --config ${webpackConfig} -w`, {
async: true,
});
runSupervisor();
runBabel(true);
runWebpack(true);
} else if (cmd === "create" || cmd === "convert" || cmd === "i18n") {
const [name] = extraArgs;

Expand Down
3 changes: 3 additions & 0 deletions webpack.config.js
Expand Up @@ -73,6 +73,9 @@ module.exports = {
{
test: /\.js$/,
loader: path.resolve(__dirname, "node_modules", "babel-loader"),
options: {
babelrcRoots: __dirname,
},
},
],
},
Expand Down

0 comments on commit 927c230

Please sign in to comment.