-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix language files for Node+ESM (#1377)
* Add babel script add-import-extension * Adjust check-publish-package script to new way of building ES * Make babel output ES build with mjs extensions * Add exports property to package.json * Fix the exports property * Tweaking package.json exports for cjs modules * Adjust package.json exports for individual language files in CJS * Add info about the source of the add-import-extension babel script * Add changelog entry * Move types key to be the first key in JSON object (exports property) * Fix publint suggestions * Add paths ./i18n/languages/xxXX to exports property in package.json * Use path ./i18n/languages/xxXX to import languages in the docs * Add paths with .js extensions to exports in package.json * Add migration guide for 3.0 * Working on the migration guide * Unpolished vershion of the migration guide * Upgrade migration instructions for Angular projects * Add more explenations to the migration guide * Add a fallback option for parcel projects to the migration guide * Undo the version number change in package.json * Update migration guide * Remove unnsecessary step from the parcel section of the migration guide * Rephrase CHANGELOG.md
- Loading branch information
Showing
9 changed files
with
634 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Based on https://github.com/handsontable/handsontable/blob/bd7628544ff83d6e74a9cc949e2c3c38fef74d76/handsontable/.config/plugin/babel/add-import-extension.js | ||
|
||
const { existsSync, lstatSync } = require('fs'); | ||
const { dirname, resolve } = require('path'); | ||
const { types } = require('@babel/core'); | ||
const { declare } = require('@babel/helper-plugin-utils'); | ||
|
||
const VALID_EXTENSIONS = ['js', 'mjs']; | ||
|
||
const hasExtension = moduleName => VALID_EXTENSIONS.some(ext => moduleName.endsWith(`.${ext}`)); | ||
const isCoreJSPolyfill = moduleName => moduleName.startsWith('core-js'); | ||
const isLocalModule = moduleName => moduleName.startsWith('.'); | ||
const isProcessableModule = (moduleName) => { | ||
return !hasExtension(moduleName) && (isCoreJSPolyfill(moduleName) || isLocalModule(moduleName)); | ||
}; | ||
|
||
const createVisitor = ({ declaration, origArgs, extension = 'js' }) => { | ||
return (path, { file }) => { | ||
const { node: { source, exportKind, importKind } } = path; | ||
const { opts: { filename } } = file; | ||
const isTypeOnly = exportKind === 'type' || importKind === 'type'; | ||
|
||
if (!source || isTypeOnly || !isProcessableModule(source.value)) { | ||
return; | ||
} | ||
|
||
const { value: moduleName } = source; | ||
const absoluteFilePath = resolve(dirname(filename), moduleName); | ||
const finalExtension = isCoreJSPolyfill(moduleName) ? 'js' : extension; | ||
|
||
let newModulePath; | ||
|
||
// Resolves a case where "import" points to a module name which exists as a file and | ||
// as a directory. For example in this case: | ||
// ``` | ||
// import { registerPlugin } from 'plugins'; | ||
// ``` | ||
// and with this directory structure: | ||
// |- editors | ||
// |- plugins | ||
// |- filters/ | ||
// |- ... | ||
// +- index.js | ||
// |- plugins.js | ||
// |- ... | ||
// +- index.js | ||
// | ||
// the plugin will rename import declaration to point to the `plugins.js` file. | ||
if (existsSync(`${absoluteFilePath}.js`)) { | ||
newModulePath = `${moduleName}.${finalExtension}`; | ||
|
||
// In a case when the file doesn't exist and the module is a directory it will | ||
// rename to `plugins/index.js`. | ||
} else if (existsSync(absoluteFilePath) && lstatSync(absoluteFilePath).isDirectory()) { | ||
newModulePath = `${moduleName}/index.${finalExtension}`; | ||
|
||
// And for other cases it simply put the extension on the end of the module path | ||
} else { | ||
newModulePath = `${moduleName}.${finalExtension}`; | ||
} | ||
|
||
path.replaceWith(declaration(...origArgs(path), types.stringLiteral(newModulePath))); | ||
}; | ||
}; | ||
|
||
module.exports = declare((api, options) => { | ||
api.assertVersion(7); | ||
|
||
return { | ||
name: 'add-import-extension', | ||
visitor: { | ||
// It covers default and named imports | ||
ImportDeclaration: createVisitor({ | ||
extension: options.extension, | ||
declaration: types.importDeclaration, | ||
origArgs: ({ node: { specifiers } }) => [specifiers], | ||
}), | ||
ExportNamedDeclaration: createVisitor({ | ||
extension: options.extension, | ||
declaration: types.exportNamedDeclaration, | ||
origArgs: ({ node: { declaration, specifiers } }) => [declaration, specifiers], | ||
}), | ||
ExportAllDeclaration: createVisitor({ | ||
extension: options.extension, | ||
declaration: types.exportAllDeclaration, | ||
origArgs: () => [], | ||
}), | ||
} | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.