Skip to content
This repository has been archived by the owner on Jul 15, 2021. It is now read-only.

Commit

Permalink
Add support for ECMAModules import (#112)
Browse files Browse the repository at this point in the history
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
budnix authored Jan 11, 2021
1 parent d056ea2 commit 93ffa2b
Show file tree
Hide file tree
Showing 12 changed files with 7,447 additions and 6,514 deletions.
39 changes: 12 additions & 27 deletions .babelrc
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" }]
]
},
}
}
100 changes: 100 additions & 0 deletions .config/plugin/babel/add-import-extension.js
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: () => [],
}),
}
};
});
2 changes: 2 additions & 0 deletions .gitattributes
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
4 changes: 3 additions & 1 deletion .release.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
"release_message": true,
"remote": "origin",
"pre_commit_commands": [
"npm run clean",
"npm run build",
"npm run test"
],
"post_commit_commands": [],
"post_complete_commands": [
"npm publish"
"cd tmp && npm publish",
"npm run clean"
],
"files_to_commit": [
"./dist/**/*"
Expand Down
7 changes: 4 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ Your contributions to the project are very welcome. If you would like to fix a b

To help us merge your Pull Request, please make sure you follow these points:

1. Please make your fix on a separate branch based on `develop` branch. This makes merging much easier.
2. Do not edit files in `dist/` directory (e.g: `formula-parser.js`, `formula-parser.min.js`). Instead, edit files inside the `src/` directory and then use `gulp` or `npm scripts` to make a build.
3. **Very important:** For any change that you make, **please try to also add a test case(s)** in `tests/unit/` or `test/integration/`. This helps us understand the issue and make sure that it will stay fixed forever.
1. Please make sure that you're using the NodeJS in the proper version. The project requires version 10.
2. Make your fix on a separate branch based on `develop` branch. This makes merging much easier.
3. Do not edit files in `dist/` directory (e.g: `formula-parser.js`, `formula-parser.min.js`). Instead, edit files inside the `src/` directory and then use `gulp` or `npm scripts` to make a build.
4. **Very important:** For any change that you make, **please try to also add a test case(s)** in `tests/unit/` or `test/integration/`. This helps us understand the issue and make sure that it will stay fixed forever.
5. Describe the problem in the Pull Request description (of course you would do it, why do I mention that?).
6. **Very important:** Make Pull Request ready to merge into `develop` branch.

Expand Down
11 changes: 0 additions & 11 deletions loader/empty-loader.js

This file was deleted.

Loading

0 comments on commit 93ffa2b

Please sign in to comment.