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

Sourcemaps support #61

Merged
merged 9 commits into from
Dec 1, 2018
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ $ ncc build input.js -o dist
Outputs the build of `input.js` into `dist/index.js`.

```bash
$ ncc run input.js
$ ncc run input.js --source-map
rauchg marked this conversation as resolved.
Show resolved Hide resolved
```

Build to a temporary folder and run the built JS file through Node.js.
Build to a temporary folder and run the built JS file through Node.js, with source maps support for debugging.

### Node.js

Expand Down
5 changes: 4 additions & 1 deletion scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ async function main() {

const { code: assetRelocator, assets: assetRelocatorAssets } = await ncc(__dirname + "/../src/asset-relocator");

if (Object.keys(cliAssets).length || Object.keys(indexAssets).length || Object.keys(assetRelocatorAssets).length) {
const { code: sourcemapSupport, assets: sourcemapAssets } = await ncc(require.resolve("source-map-support/register"));

if (Object.keys(cliAssets).length || Object.keys(indexAssets).length || Object.keys(assetRelocatorAssets).length || Object.keys(sourcemapAssets).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);
writeFileSync(__dirname + "/../dist/ncc/asset-relocator.js", assetRelocator);
writeFileSync(__dirname + "/../dist/ncc/sourcemap-register.js", sourcemapSupport);

// copy webpack buildin
await copy(
Expand Down
18 changes: 13 additions & 5 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Commands:
Options:
-o, --out [file] Output directory for build (defaults to dist)
-M, --no-minify Skip output minification
-s, --source-map Output the source map for debugging
-e, --external [mod] Skip bundling 'mod'. Can be used many times
-q, --quiet Disable build summaries / non-error outputs
`;
Expand All @@ -24,6 +25,8 @@ try {
"-o": "--out",
"--no-minify": Boolean,
"-M": "--no-minify",
"--source-map": Boolean,
"-s": "--source-map",
"--quiet": Boolean,
"-q": "--quiet"
});
Expand All @@ -36,7 +39,7 @@ catch (e) {
}

function renderSummary (code, assets, outDir, buildTime) {
if (!outDir.endsWith(sep))
if (outDir && !outDir.endsWith(sep))
outDir += sep;
const codeSize = Math.round(Buffer.byteLength(code, 'utf8') / 1024);
const assetSizes = Object.create(null);
Expand Down Expand Up @@ -105,15 +108,18 @@ switch (args._[0]) {
const startTime = Date.now();
const ncc = require("./index.js")(eval("require.resolve")(resolve(args._[1] || ".")), {
minify: !args["--no-minify"],
externals: args["--external"]
externals: args["--external"],
sourcemap: args["--source-map"]
});

ncc.then(({ code, assets }) => {
ncc.then(({ code, map, assets }) => {
outDir = outDir || resolve("dist");
const fs = require("fs");
const mkdirp = require("mkdirp");
mkdirp.sync(outDir);
fs.writeFileSync(outDir + "/index.js", code);
if (map)
fs.writeFileSync(outDir + "/index.js.map", map);

for (const asset of Object.keys(assets)) {
mkdirp.sync(dirname(asset));
fs.writeFileSync(outDir + "/" + asset, assets[asset]);
Expand All @@ -123,7 +129,9 @@ switch (args._[0]) {
console.log(renderSummary(code, assets, run ? '' : relative(process.cwd(), outDir), Date.now() - startTime));

if (run) {
const ps = require("child_process").fork(outDir + "/index.js");
const ps = require("child_process").fork(outDir + "/index.js", {
execArgv: map ? ["-r", resolve(__dirname, "../dist/ncc/sourcemap-register")] : []
});
ps.on("close", () => require("rimraf").sync(outDir));
}
}, (err) => {
Expand Down
16 changes: 8 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function resolveModule(context, request, callback, forcedExternals = []) {
preserveSymlinks: true,
extensions: SUPPORTED_EXTENSIONS
};

if (new Set(forcedExternals).has(request)) {
console.error(`ncc: Skipping bundling "${request}" per config`);
return callback(null, `commonjs ${request}`);
Expand All @@ -44,20 +44,20 @@ function resolveModule(context, request, callback, forcedExternals = []) {
});
}

module.exports = async (entry, { externals = [], minify = true, sourceMap = false } = {}) => {
module.exports = async (entry, { externals = [], minify = true, sourcemap = false, filename = "index.js" } = {}) => {
const mfs = new MemoryFS();
const compiler = webpack({
entry,
optimization: {
nodeEnv: false,
minimize: false
},
devtool: sourceMap ? "cheap-source-map" : false,
devtool: sourcemap ? "cheap-source-map" : false,
mode: "production",
target: "node",
output: {
path: "/",
filename: "out.js",
filename,
libraryTarget: "commonjs2"
},
resolve: {
Expand Down Expand Up @@ -131,10 +131,10 @@ module.exports = async (entry, { externals = [], minify = true, sourceMap = fals
}
const assets = Object.create(null);
getFlatFiles(mfs.data, assets);
delete assets["out.js"];
delete assets["out.js.map"];
const code = mfs.readFileSync("/out.js", "utf8");
const map = sourceMap ? mfs.readFileSync("/out.js.map", "utf8") : null;
delete assets["index.js"];
delete assets["index.js.map"];
const code = mfs.readFileSync("/index.js", "utf8");
const map = sourcemap ? mfs.readFileSync("/index.js.map", "utf8") : null;
resolve({
code,
map,
Expand Down