From 695d78014fc0d1454c5d71b2cddf0315dc946e7b Mon Sep 17 00:00:00 2001 From: Geoffrey Booth Date: Thu, 18 Apr 2019 00:18:11 -0700 Subject: [PATCH] esm: add utility method for detecting ES module syntax --- doc/api/modules.md | 24 ++++++++++ lib/internal/modules/cjs/loader.js | 40 +++++++++++++++++ .../test-esm-contains-module-syntax.js | 44 +++++++++++++++++++ .../ambiguous-with-import-expression.js | 3 ++ .../detect/cjs-with-import-expression.js | 5 +++ .../detect/cjs-with-property-named-export.js | 11 +++++ .../detect/cjs-with-property-named-import.js | 14 ++++++ .../es-modules/detect/cjs-with-require.js | 3 ++ .../cjs-with-string-containing-import.js | 7 +++ .../detect/esm-with-export-statement.js | 6 +++ .../detect/esm-with-import-expression.js | 5 +++ .../detect/esm-with-import-statement.js | 2 + .../esm-with-indented-import-statement.js | 2 + test/fixtures/es-modules/detect/package.json | 1 + .../es-modules/detect/print-version.js | 1 + .../es-modules/detect/syntax-error.js | 2 + 16 files changed, 170 insertions(+) create mode 100644 test/es-module/test-esm-contains-module-syntax.js create mode 100644 test/fixtures/es-modules/detect/ambiguous-with-import-expression.js create mode 100644 test/fixtures/es-modules/detect/cjs-with-import-expression.js create mode 100644 test/fixtures/es-modules/detect/cjs-with-property-named-export.js create mode 100644 test/fixtures/es-modules/detect/cjs-with-property-named-import.js create mode 100644 test/fixtures/es-modules/detect/cjs-with-require.js create mode 100644 test/fixtures/es-modules/detect/cjs-with-string-containing-import.js create mode 100644 test/fixtures/es-modules/detect/esm-with-export-statement.js create mode 100644 test/fixtures/es-modules/detect/esm-with-import-expression.js create mode 100644 test/fixtures/es-modules/detect/esm-with-import-statement.js create mode 100644 test/fixtures/es-modules/detect/esm-with-indented-import-statement.js create mode 100644 test/fixtures/es-modules/detect/package.json create mode 100644 test/fixtures/es-modules/detect/print-version.js create mode 100644 test/fixtures/es-modules/detect/syntax-error.js diff --git a/doc/api/modules.md b/doc/api/modules.md index 9266d43f49..bbc1540a06 100644 --- a/doc/api/modules.md +++ b/doc/api/modules.md @@ -912,6 +912,29 @@ by the [module wrapper][]. To access it, require the `Module` module: const builtin = require('module').builtinModules; ``` +### module.containsModuleSyntax(source) + + +* `source` {string} JavaScript source code +* Returns: {boolean} + +Detect whether input JavaScript source code contains [ECMAScript Module][] +syntax, defined as `import` or `export` statements. Returns `true` as soon as +the first `import` or `export` statement is encountered, or `false` if none are +found. Note that dynamic `import()` is not an `import` statement. + +```js +const { containsModuleSyntax } = require('module'); + +containsModuleSyntax('import { fn } from "pkg"'); // true +containsModuleSyntax('console.log(process.version)'); // false + +containsModuleSyntax('import "./file.mjs"'); // true +containsModuleSyntax('import("./file.mjs")'); // false +``` + ### module.createRequire(filename)