This repository has been archived by the owner on Jul 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for ECMAModules import (#112)
The commit includes the following changes: * Upgrade dependencies; * Upgrade the process of building the dist files. The package from now on should be ready for ECMAModules spec; * Remove unused label declaration (The label declaration syntax causes transpilation errors) * Add "jsdelivr" and "unpkg" fields to the package.json file Issue: #112
- Loading branch information
Showing
12 changed files
with
7,447 additions
and
6,514 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 |
---|---|---|
@@ -1,34 +1,19 @@ | ||
{ | ||
"plugins": [ | ||
["transform-es2015-template-literals", { "loose": true }], | ||
"transform-es2015-literals", | ||
"transform-es2015-function-name", | ||
"transform-es2015-arrow-functions", | ||
"transform-es2015-block-scoped-functions", | ||
["transform-es2015-classes", { "loose": true }], | ||
"transform-es2015-object-super", | ||
"transform-es2015-shorthand-properties", | ||
["transform-es2015-computed-properties", { "loose": true }], | ||
["transform-es2015-for-of", { "loose": true }], | ||
"transform-es2015-sticky-regex", | ||
"transform-es2015-unicode-regex", | ||
"check-es2015-constants", | ||
["transform-es2015-spread", { "loose": true }], | ||
"transform-es2015-parameters", | ||
["transform-es2015-destructuring", { "loose": true }], | ||
"transform-es2015-block-scoping", | ||
"transform-object-rest-spread", | ||
"transform-es3-member-expression-literals", | ||
"transform-es3-property-literals" | ||
"presets": [ | ||
["@babel/preset-env", { | ||
modules: false, | ||
}] | ||
], | ||
"env": { | ||
"commonjs": { | ||
"plugins": [ | ||
["transform-es2015-modules-commonjs", { "loose": true }] | ||
["@babel/plugin-transform-modules-commonjs", { loose: true }] | ||
] | ||
} | ||
}, | ||
"ignore": [ | ||
|
||
] | ||
}, | ||
"es": { | ||
"plugins": [ | ||
["./.config/plugin/babel/add-import-extension.js", { extension: "mjs" }] | ||
] | ||
}, | ||
} | ||
} |
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,100 @@ | ||
const { types } = require('@babel/core'); | ||
const { declare } = require('@babel/helper-plugin-utils'); | ||
const { existsSync, lstatSync } = require('fs'); | ||
const { dirname, resolve } = require('path'); | ||
|
||
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 isNodeModule = (moduleName) => { | ||
try { | ||
require.resolve(moduleName); | ||
|
||
return true; | ||
} catch (ex) { | ||
if (ex.code === 'MODULE_NOT_FOUND') { | ||
return false; | ||
} | ||
} | ||
}; | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Makes sure that Git automatically detects what files are considered "text" and use LF as the line ending in all OS | ||
* text=auto eol=lf |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.