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

Exports support, dynamic import from CJS support, package boundary emission #129

Merged
merged 27 commits into from
Jul 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
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 @@ -59,6 +59,7 @@
"consolidate": "^0.15.1",
"copy": "^0.3.2",
"cowsay": "^1.4.0",
"es-get-iterator": "^1.1.0",
"esm": "^3.2.25",
"express": "^4.17.1",
"fast-glob": "^3.1.1",
Expand Down
28 changes: 28 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,34 @@ const { fileList } = await nodeFileTrace(files, {

By default `processCwd` is the same as `base`.

#### Exports

By default tracing of the [Node.js "exports" field](https://nodejs.org/dist/latest-v14.x/docs/api/esm.html#esm_package_entry_points) is supported, with the `"node"`, `"require"`, `"import"` and `"default"` conditions traced as defined.

Alternatively the explicit list of exports can be provided:

```js
const { fileList } = await nodeFileTrace(files, {
exports: ['node', 'production']
});
```

Only the `"node"` export should be explicitly included (if needed) when specifying the exact export condition list. The `"require"`, `"import"` and `"default"` conditions will always be traced as defined, no matter what custom conditions are set.

#### Exports Only

When tracing exports the `"main"` / index field will still be traced for Node.js versions without `"exports"` support.

This can be disabled with the `exportsOnly` option:

```js
const { fileList } = await nodeFileTrace(files, {
exportsOnly: true
});
```

Any package with `"exports"` will then only have its exports traced, and the main will not be included at all. This can reduce the output size when targeting [Node.js 12.17.0)(https://github.com/nodejs/node/blob/master/doc/changelogs/CHANGELOG_V12.md#12.17.0) or newer.

#### Paths

> Status: Experimental. May change at any time.
Expand Down
25 changes: 13 additions & 12 deletions src/analyze.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ const repeatGlobRegEx = /([\/\\]\*\*[\/\\]\*)+/g
module.exports = async function (id, code, job) {
const assets = new Set();
const deps = new Set();
const imports = new Set();

const dir = path.dirname(id);
// if (typeof options.production === 'boolean' && staticProcess.env.NODE_ENV === UNKNOWN)
Expand Down Expand Up @@ -229,7 +230,7 @@ module.exports = async function (id, code, job) {
catch (e) {
job.warnings.add(new Error(`Failed to parse ${id} as module:\n${e && e.message}`));
// Parser errors just skip analysis
return { assets, deps, isESM: false };
return { assets, deps, imports, isESM: false };
}
}

Expand Down Expand Up @@ -365,15 +366,15 @@ module.exports = async function (id, code, job) {
});
}

function processRequireArg (expression) {
function processRequireArg (expression, isImport) {
if (expression.type === 'ConditionalExpression') {
processRequireArg(expression.consequent);
processRequireArg(expression.alternate);
processRequireArg(expression.consequent, isImport);
processRequireArg(expression.alternate, isImport);
return;
}
if (expression.type === 'LogicalExpression') {
processRequireArg(expression.left);
processRequireArg(expression.right);
processRequireArg(expression.left, isImport);
processRequireArg(expression.right, isImport);
return;
}

Expand All @@ -382,15 +383,15 @@ module.exports = async function (id, code, job) {

if (typeof computed.value === 'string') {
if (!computed.wildcards)
deps.add(computed.value);
(isImport ? imports : deps).add(computed.value);
else if (computed.wildcards.length >= 1)
emitWildcardRequire(computed.value);
}
else {
if (typeof computed.then === 'string')
deps.add(computed.then);
(isImport ? imports : deps).add(computed.then);
if (typeof computed.else === 'string')
deps.add(computed.else);
(isImport ? imports : deps).add(computed.else);
}
}

Expand Down Expand Up @@ -443,8 +444,8 @@ module.exports = async function (id, code, job) {
}
}
}
else if ((isESM || job.mixedModules) && node.type === 'ImportExpression') {
processRequireArg(node.source);
else if (node.type === 'ImportExpression') {
processRequireArg(node.source, true);
return;
}
// Call expression cases and asset triggers
Expand Down Expand Up @@ -723,7 +724,7 @@ module.exports = async function (id, code, job) {
});

await assetEmissionPromises;
return { assets, deps, isESM };
return { assets, deps, imports, isESM };

function emitAssetPath (assetPath) {
// verify the asset file / directory exists
Expand Down
66 changes: 59 additions & 7 deletions src/node-file-trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = async function (files, opts = {}) {
await Promise.all(files.map(file => {
const path = resolve(file);
job.emitFile(job.realpath(path), 'initial');
if (path.endsWith('.js') || path.endsWith('.node') || job.ts && (path.endsWith('.ts') || path.endsWith('.tsx')))
if (path.endsWith('.js') || path.endsWith('.cjs') || path.endsWith('.mjs') || path.endsWith('.node') || job.ts && (path.endsWith('.ts') || path.endsWith('.tsx')))
return job.emitDependency(path);
}));

Expand All @@ -43,6 +43,8 @@ class Job {
constructor ({
base = process.cwd(),
processCwd,
exports = ['node'],
exportsOnly = false,
paths = {},
ignore,
log = false,
Expand Down Expand Up @@ -73,6 +75,8 @@ class Job {
}
this.base = base;
this.cwd = resolve(processCwd || base);
this.exports = exports;
this.exportsOnly = exportsOnly;
const resolvedPaths = {};
for (const path of Object.keys(paths)) {
const trailer = paths[path].endsWith('/');
Expand Down Expand Up @@ -224,6 +228,16 @@ class Job {
return true;
}

getPjsonBoundary (path) {
const rootSeparatorIndex = path.indexOf(sep);
let separatorIndex;
while ((separatorIndex = path.lastIndexOf(sep)) > rootSeparatorIndex) {
path = path.substr(0, separatorIndex);
if (this.isFile(path + sep + 'package.json'))
return path;
}
}

async emitDependency (path, parent) {
if (this.processed.has(path)) return;
this.processed.add(path);
Expand All @@ -233,21 +247,29 @@ class Job {
if (path.endsWith('.json')) return;
if (path.endsWith('.node')) return await sharedlibEmit(path, this);

let deps, assets, isESM;
// js files require the "type": "module" lookup, so always emit the package.json
if (path.endsWith('.js')) {
const pjsonBoundary = this.getPjsonBoundary(path);
if (pjsonBoundary)
this.emitFile(pjsonBoundary + sep + 'package.json', 'resolve', path);
}

let deps, imports, assets, isESM;

const cachedAnalysis = this.analysisCache.get(path);
if (cachedAnalysis) {
({ deps, assets, isESM } = cachedAnalysis);
({ deps, imports, assets, isESM } = cachedAnalysis);
}
else {
const source = this.readFile(path);
if (source === null) throw new Error('File ' + path + ' does not exist.');
({ deps, assets, isESM } = await analyze(path, source, this));
this.analysisCache.set(path, { deps, assets, isESM });
({ deps, imports, assets, isESM } = await analyze(path, source, this));
this.analysisCache.set(path, { deps, imports, assets, isESM });
}

if (isESM)
this.esmFileList.add(relative(this.base, path));

await Promise.all([
...[...assets].map(async asset => {
const ext = extname(asset);
Expand All @@ -259,15 +281,45 @@ class Job {
}),
...[...deps].map(async dep => {
try {
var resolved = await resolveDependency(dep, path, this);
var resolved = await resolveDependency(dep, path, this, !isESM);
}
catch (e) {
this.warnings.add(new Error(`Failed to resolve dependency ${dep}:\n${e && e.message}`));
return;
}
if (Array.isArray(resolved)) {
for (const item of resolved) {
// ignore builtins
if (item.startsWith('node:')) return;
await this.emitDependency(item, path);
}
}
else {
// ignore builtins
if (resolved.startsWith('node:')) return;
await this.emitDependency(resolved, path);
}
}),
...[...imports].map(async dep => {
try {
var resolved = await resolveDependency(dep, path, this, false);
}
catch (e) {
this.warnings.add(new Error(`Failed to resolve dependency ${dep}:\n${e && e.message}`));
return;
}
await this.emitDependency(resolved, path);
if (Array.isArray(resolved)) {
for (const item of resolved) {
// ignore builtins
if (item.startsWith('node:')) return;
await this.emitDependency(item, path);
}
}
else {
// ignore builtins
if (resolved.startsWith('node:')) return;
await this.emitDependency(resolved, path);
}
})
]);
}
Expand Down
Loading