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
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
["@babel/preset-env", {
"modules": false
}]
],
"plugins": [
"./babel-plugin-es-import-extensions"
]
},
"development": {
Expand Down
38 changes: 38 additions & 0 deletions babel-plugin-es-import-extensions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Babel plugin used by the ES module build (`npm run build:es`, see `.babelrc`).
*
* Node.js requires relative import paths in ES modules to include the file
* extension, so `import assertString from './util/assertString'` is rewritten
* to `import assertString from './util/assertString.js'`.
*/
const fs = require('fs');
const path = require('path');

function isFile(file) {
try {
return fs.statSync(file).isFile();
} catch (err) {
return false;
}
}

module.exports = function esImportExtensions() {
function addExtension(nodePath, state) {
const { source } = nodePath.node;
if (!source || !/^\.\.?\//.test(source.value)) return;
const file = path.resolve(path.dirname(state.filename), source.value);
if (isFile(file)) return;
if (!isFile(`${file}.js`)) {
throw nodePath.buildCodeFrameError(`Cannot resolve "${source.value}" to a file`);
}
source.value = `${source.value}.js`;
}

return {
visitor: {
ImportDeclaration: addExtension,
ExportNamedDeclaration: addExtension,
ExportAllDeclaration: addExtension,
},
};
};
37 changes: 37 additions & 0 deletions test/esBuild.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import assert from 'assert';
import fs from 'fs';
import path from 'path';

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

function listFiles(dir) {
return fs.readdirSync(dir).reduce((files, name) => {
const file = path.join(dir, name);
return files.concat(fs.statSync(file).isDirectory() ? listFiles(file) : [file]);
}, []);
}

describe('ES module build', () => {
it('should use the file extension in relative import paths', () => {
const files = listFiles(esDir).filter(file => path.extname(file) === '.js');
let checked = 0;
files.forEach((file) => {
const code = fs.readFileSync(file, 'utf8');
const importRegex = /^(?:import|export)\s[^;]*?\sfrom\s+['"]([^'"]+)['"]/gm;
let match = importRegex.exec(code);
while (match) {
const specifier = match[1];
if (specifier[0] === '.') {
checked += 1;
const target = path.resolve(path.dirname(file), specifier);
assert.ok(
path.extname(specifier) === '.js' && fs.existsSync(target) && fs.statSync(target).isFile(),
`${path.relative(esDir, file)} imports "${specifier}", which is not a path to a file`
);
}
match = importRegex.exec(code);
}
});
assert.ok(checked > 0, 'No relative imports found in the ES module build. Run `npm run build:es`');
});
});
Loading