Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,17 @@ import validator from 'validator';
Or, import only a subset of the library:

```javascript
import isEmail from 'validator/lib/isEmail';
import isEmail from 'validator/lib/isEmail.js';
```

#### Tree-shakeable ES imports

```javascript
import isEmail from 'validator/es/lib/isEmail';
import isEmail from 'validator/es/lib/isEmail.js';
```

When using native Node.js ESM, include the `.js` file extension in import specifiers.

### Client-side usage

The library can be loaded either as a standalone script, or through an [AMD][amd]-compatible loader
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"clean": "run-p clean:*",
"minify": "uglifyjs validator.js -o validator.min.js --compress --mangle --comments /Copyright/",
"build:browser": "node --require @babel/register build-browser && npm run minify",
"build:es": "babel src -d es --env-name=es",
"build:es": "babel src -d es --env-name=es && node scripts/add-es-import-extensions.js",
"build:node": "babel src -d .",
"build": "run-p build:*",
"pretest": "npm run build && npm run lint",
Expand Down
41 changes: 41 additions & 0 deletions scripts/add-es-import-extensions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const fs = require('fs');
const path = require('path');

const ES_ROOT = path.join(__dirname, '..', 'es');

function addExtension(specifier) {
if (specifier.endsWith('.js')) {
return specifier;
}

return `${specifier}.js`;
}

function fixFile(filePath) {
const source = fs.readFileSync(filePath, 'utf8');
const updated = source.replace(
/from ['"](\.\.?\/[^'"]+)['"]/g,
(match, specifier) => match.replace(specifier, addExtension(specifier)),
);

if (updated !== source) {
fs.writeFileSync(filePath, updated);
}
}

function walk(directory) {
for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
const entryPath = path.join(directory, entry.name);

if (entry.isDirectory()) {
walk(entryPath);
continue;
}

if (entry.name.endsWith('.js')) {
fixFile(entryPath);
}
}
}

walk(ES_ROOT);
Loading