Skip to content

Commit

Permalink
Sourcemaps support (#61)
Browse files Browse the repository at this point in the history
* sourcemaps support

* sourcemap by default

* `sourcemap` -> `sourceMap` since it's `--source-map`

* `sourcemap` -> `sourceMap` since it's `--source-map`

* `sourcemap` -> `sourceMap` since it's `--source-map`

* `sourcemap` -> `sourceMap` since it's `--source-map`
  • Loading branch information
guybedford authored and rauchg committed Dec 1, 2018
1 parent ab31c82 commit e50336f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 16 deletions.
5 changes: 3 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ Outputs the build of `input.js` into `dist/index.js`.
$ ncc run input.js
```

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

```js
require('@zeit/ncc')('/path/to/input', {
minify: true, // default
// externals to leave as requires of the build
externals: ["externalpackage"]
externals: ["externalpackage"],
sourceMap: true // default
}).then(({ code, assets }) => {
console.log(code);
// assets is an object of asset file names to sources
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, --no-source-map Skip source map output
-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",
"--no-source-map": Boolean,
"-S": "--no-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["--no-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
14 changes: 7 additions & 7 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,7 +44,7 @@ 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,
Expand All @@ -57,7 +57,7 @@ module.exports = async (entry, { externals = [], minify = true, sourceMap = fals
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
2 changes: 1 addition & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,4 @@ for (const integrationTest of fs.readdirSync(__dirname + "/integration")) {
// remove me when node.js makes this the default behavior
process.on("unhandledRejection", e => {
throw e;
});
});

0 comments on commit e50336f

Please sign in to comment.