├── .github └── dependabot.yml ├── .gitignore ├── LICENSE ├── README.md ├── cmd └── index.js └── package.json /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: npm 4 | directory: "/" 5 | schedule: 6 | interval: daily 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | 10 | pids 11 | logs 12 | results 13 | build 14 | coverage 15 | 16 | node_modules 17 | npm-debug.log 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2019 James Billingham 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | this software and associated documentation files (the "Software"), to deal in 5 | the Software without restriction, including without limitation the rights to 6 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 7 | the Software, and to permit persons to whom the Software is furnished to do so, 8 | subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 15 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 16 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 17 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # fix-es-imports 2 | 3 | Fixes your ES import paths - from Node-style to explicit filenames 4 | 5 | ## What 6 | 7 | Turns: 8 | 9 | ```js 10 | import fs from 'fs'; 11 | import x from './x'; 12 | import { y } from './y'; 13 | export { default as z } from './z'; 14 | ``` 15 | 16 | Into: 17 | 18 | ```js 19 | import fs from 'fs'; 20 | import x from './x.mjs'; 21 | import { y } from './y/index.js'; 22 | export { default as z } from './z.json'; 23 | ``` 24 | 25 | ## How 26 | 27 | ```bash 28 | npx fix-es-imports 29 | ``` 30 | 31 | ## Why 32 | 33 | Did you use ES module syntax before it was ~cool~ implemented? And then maybe didn't completely respect the exact rules around explicit filenames? But it was okay, right, since Babel didn't really mind... 34 | 35 | And then you used `--experimental-modules` with `.mjs` files from Node 8.9.0, and it pretty much worked perfectly. And you were super happy you could finally stop using Babel with Node code. 36 | 37 | And then Node 11.4.0 came along, and it all stopped working. They were actually enforcing the rules now! Oops. And maybe there are a few thousand references to deal with, so maybe you left it for a while. 38 | 39 | At some point, `--es-module-specifier-resolution=node` came along, and it saved the day - things worked as normal again! But you knew this wouldn't be a long term solution - one day you'd have to fix the imports once and for all. 40 | 41 | And now Node 13.2.0 has shipped. ES module support is no longer experimental. You conclude you've pushed it off long enough. The time has come. 42 | 43 | This tool is for you. 44 | 45 | ## Caveats 46 | 47 | ### Regex-based parsing 48 | 49 | Currently this tool is implemented with a fairly primitive regex, rather than any kind of real parser. As such, it is not expected to work in every situation. 50 | 51 | Some cases known not to work: 52 | 53 | - inclusion of any quote characters within the path (even if escaped) 54 | - anything prior to the declaration on the same line (including whitespace) 55 | - declarations not containing any whitespace (e.g. `import'x';`) 56 | - declarations where the path string is not immediately followed by a semicolon and the end of the line 57 | 58 | The reason for the use of regex rather than e.g. Babel is because we want to leave every other part of the file exactly as-is, with absolutely no unnecessary changes. 59 | 60 | If you're keen to implement a better approach, please open a PR! 61 | 62 | ### Non-JS modules 63 | 64 | ```js 65 | import foo from './foo.json'; 66 | ``` 67 | 68 | At this stage, using module declarations with non-JS files is [still experimental](https://nodejs.org/api/esm.html#esm_experimental_json_modules) and the syntax for doing this will almost certainly change due to [security issues affecting the web](https://github.com/w3c/webcomponents/issues/839). 69 | 70 | This tool will work with such files if the correct flag is enabled, but you should really look to load these without module declarations until the final syntax is determined. 71 | 72 | ### Package files 73 | 74 | ```js 75 | import fs from 'mz/fs'; 76 | ``` 77 | 78 | Not currently supported. Each instance found will be outputted to stderr. You will need to manually change these. 79 | 80 | In this case, to: 81 | 82 | ```js 83 | import fs from 'mz/fs.js'; 84 | ``` 85 | 86 | If you can, best not to use this kind of import for now. Likely to result in unexpected breaking changes by package developers unfamiliar with the explicit filename rules. 87 | 88 | ## Support 89 | 90 | Please open an issue on this repository. 91 | 92 | ## Authors 93 | 94 | - James Billingham 95 | 96 | ## License 97 | 98 | MIT licensed - see [LICENSE](LICENSE) file 99 | -------------------------------------------------------------------------------- /cmd/index.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env -S node --experimental-modules 2 | 3 | import path from 'path'; 4 | import resolve from 'resolve'; 5 | import glob from 'glob'; 6 | import fs from 'fs'; 7 | 8 | const moduleDeclRegex = /^((?:import|export)\s[^'";]*['"])([^'"]+)(['"];)$/gm; 9 | 10 | const paths = process.argv.slice(2); 11 | const codePaths = paths.length ? paths : glob.sync('**/*.mjs'); 12 | 13 | for (const codePath of codePaths) { 14 | const origCode = fs.readFileSync(codePath, 'utf8'); 15 | const baseDir = path.dirname(codePath); 16 | 17 | const newCode = origCode.replace(moduleDeclRegex, (str, start, origTarget, end) => { 18 | if (!origTarget.match(/^\.\.?\//)) { 19 | if (origTarget.match(/^[^@][^/\n]*\/[^/\n]+/)) { 20 | console.warn('\nunsupported: package file reference detected'); 21 | console.warn(codePath); 22 | console.warn(str); 23 | } 24 | 25 | return str; 26 | } 27 | 28 | const newTarget = rewriteTarget(origTarget, baseDir); 29 | 30 | return `${start}${newTarget}${end}`; 31 | }); 32 | 33 | if (newCode !== origCode) 34 | fs.writeFileSync(codePath, newCode, 'utf8'); 35 | } 36 | 37 | function rewriteTarget(original, baseDir) { 38 | const absolute = resolve.sync(original, { 39 | basedir: baseDir, 40 | extensions: ['.js', '.node', '.mjs', '.cjs', '.json'], 41 | }); 42 | 43 | const relative = path.relative(baseDir, absolute); 44 | 45 | return `./${relative}`.replace(/^\.\/\.\./, '..'); 46 | } 47 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fix-es-imports", 3 | "version": "0.1.4", 4 | "description": "Fixes your ES import paths - from Node-style to explicit filenames", 5 | "homepage": "https://github.com/billinghamj/fix-es-imports", 6 | "bugs": "https://github.com/billinghamj/fix-es-imports/issues", 7 | "license": "MIT", 8 | "author": "James Billingham (https://jamesbillingham.com)", 9 | "type": "module", 10 | "bin": "./cmd/index.js", 11 | "repository": { 12 | "type": "git", 13 | "url": "https://github.com/billinghamj/fix-es-imports.git" 14 | }, 15 | "dependencies": { 16 | "glob": "^7.1.6", 17 | "resolve": "^1.12.0" 18 | }, 19 | "keywords": [ 20 | "es", 21 | "modules", 22 | "import", 23 | "export", 24 | "normalize", 25 | "resolve", 26 | "rewrite" 27 | ] 28 | } 29 | --------------------------------------------------------------------------------