Skip to content

Commit

Permalink
--esm flag for emitting es modules
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Jun 26, 2021
1 parent a529543 commit 7164f29
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
10 changes: 8 additions & 2 deletions src/cli.js
Expand Up @@ -5,7 +5,7 @@ const glob = require("glob");
const shebangRegEx = require("./utils/shebang");
const rimraf = require("rimraf");
const crypto = require("crypto");
const { writeFileSync, unlink, existsSync, symlinkSync } = require("fs");
const { writeFileSync, unlink, existsSync, symlinkSync, mkdir } = require("fs");
const mkdirp = require("mkdirp");
const { version: nccVersion } = require('../package.json');

Expand All @@ -24,6 +24,8 @@ Commands:
Options:
-o, --out [file] Output directory for build (defaults to dist)
--esm Create an ES Module build output
-m, --minify Minify output
-C, --no-cache Skip build cache population
-s, --source-map Generate source map
Expand Down Expand Up @@ -129,6 +131,7 @@ async function runCmd (argv, stdout, stderr) {
args = require("arg")({
"--debug": Boolean,
"-d": "--debug",
"--esm": Boolean,
"--external": [String],
"-e": "--external",
"--out": String,
Expand Down Expand Up @@ -218,6 +221,8 @@ async function runCmd (argv, stdout, stderr) {
);
if (existsSync(outDir))
rimraf.sync(outDir);
mkdirp.sync(outDir);
writeFileSync(eval('resolve')(outDir, 'package.json'), args['--esm'] ? '{ "type": "module" }' : '{}');
run = true;

// fallthrough
Expand All @@ -228,12 +233,13 @@ async function runCmd (argv, stdout, stderr) {
let startTime = Date.now();
let ps;
const buildFile = eval("require.resolve")(resolve(args._[1] || "."));
const ext = buildFile.endsWith('.cjs') ? '.cjs' : '.js';
const ext = !args['--esm'] && buildFile.endsWith('.cjs') ? '.cjs' : '.js';
const ncc = require("./index.js")(
buildFile,
{
debugLog: args["--debug"],
minify: args["--minify"],
esm: args["--esm"],
externals: args["--external"],
sourceMap: args["--source-map"] || run,
sourceMapRegister: args["--no-source-map-register"] ? false : undefined,
Expand Down
25 changes: 15 additions & 10 deletions src/index.js
Expand Up @@ -36,8 +36,9 @@ function ncc (
{
cache,
customEmit = undefined,
esm = false,
externals = [],
filename = 'index' + (entry.endsWith('.cjs') ? '.cjs' : '.js'),
filename = 'index' + (!esm && entry.endsWith('.cjs') ? '.cjs' : '.js'),
minify = false,
sourceMap = false,
sourceMapRegister = true,
Expand Down Expand Up @@ -233,7 +234,8 @@ function ncc (
},
amd: false,
experiments: {
topLevelAwait: true
topLevelAwait: true,
outputModule: true
},
optimization: {
nodeEnv: false,
Expand All @@ -258,8 +260,9 @@ function ncc (
path: "/",
// Webpack only emits sourcemaps for files ending in .js
filename: ext === '.cjs' ? filename + '.js' : filename,
libraryTarget: "commonjs2",
strictModuleExceptionHandling: true
libraryTarget: esm ? 'module' : 'commonjs2',
strictModuleExceptionHandling: true,
module: esm
},
resolve: {
extensions: SUPPORTED_EXTENSIONS,
Expand Down Expand Up @@ -465,12 +468,13 @@ function ncc (
let result;
try {
result = await terser.minify(code, {
module: esm,
compress: false,
mangle: {
keep_classnames: true,
keep_fnames: true
},
sourceMap: sourceMap ? {
sourceMap: map ? {
content: map,
filename,
url: `${filename}.map`
Expand All @@ -483,10 +487,10 @@ function ncc (

({ code, map } = {
code: result.code,
map: sourceMap ? JSON.parse(result.map) : undefined
map: map ? JSON.parse(result.map) : undefined
});
}
catch {
catch (e) {
console.log('An error occurred while minifying. The result will not be minified.');
}
}
Expand All @@ -511,9 +515,10 @@ function ncc (
`if (cachedData) process.on('exit', () => { try { writeFileSync(basename + '.cache', script.createCachedData()); } catch(e) {} });\n`;
}

if (sourceMap && sourceMapRegister) {
code = `require('./sourcemap-register${ext}');` + code;
assets[`sourcemap-register${ext}`] = { source: fs.readFileSync(`${__dirname}/sourcemap-register.js.cache.js`), permissions: defaultPermissions };
if (map && sourceMapRegister) {
const registerExt = esm ? '.cjs' : ext;
code = (esm ? `import './sourcemap-register${registerExt}';` : `require('./sourcemap-register${registerExt}');`) + code;
assets[`sourcemap-register${registerExt}`] = { source: fs.readFileSync(`${__dirname}/sourcemap-register.js.cache.js`), permissions: defaultPermissions };
}

if (shebangMatch) {
Expand Down
7 changes: 7 additions & 0 deletions test/cli.js
Expand Up @@ -103,5 +103,12 @@
expect (code, stdout) {
return code === 0 && stdout.indexOf('ncc built-in') !== -1;
},
},
{
args: ["build", "-o", "tmp", "test/fixtures/module.cjs", "--esm"],
expect (code, stdout) {
const fs = require('fs');
return code === 0 && fs.readFileSync('tmp/index.js', 'utf8').toString().indexOf('export {') !== -1;
}
}
]

0 comments on commit 7164f29

Please sign in to comment.