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

Commit 93ffa2b

Browse files
authored
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
1 parent d056ea2 commit 93ffa2b

File tree

12 files changed

+7447
-6514
lines changed

12 files changed

+7447
-6514
lines changed

.babelrc

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,19 @@
11
{
2-
"plugins": [
3-
["transform-es2015-template-literals", { "loose": true }],
4-
"transform-es2015-literals",
5-
"transform-es2015-function-name",
6-
"transform-es2015-arrow-functions",
7-
"transform-es2015-block-scoped-functions",
8-
["transform-es2015-classes", { "loose": true }],
9-
"transform-es2015-object-super",
10-
"transform-es2015-shorthand-properties",
11-
["transform-es2015-computed-properties", { "loose": true }],
12-
["transform-es2015-for-of", { "loose": true }],
13-
"transform-es2015-sticky-regex",
14-
"transform-es2015-unicode-regex",
15-
"check-es2015-constants",
16-
["transform-es2015-spread", { "loose": true }],
17-
"transform-es2015-parameters",
18-
["transform-es2015-destructuring", { "loose": true }],
19-
"transform-es2015-block-scoping",
20-
"transform-object-rest-spread",
21-
"transform-es3-member-expression-literals",
22-
"transform-es3-property-literals"
2+
"presets": [
3+
["@babel/preset-env", {
4+
modules: false,
5+
}]
236
],
247
"env": {
258
"commonjs": {
269
"plugins": [
27-
["transform-es2015-modules-commonjs", { "loose": true }]
10+
["@babel/plugin-transform-modules-commonjs", { loose: true }]
2811
]
29-
}
30-
},
31-
"ignore": [
32-
33-
]
12+
},
13+
"es": {
14+
"plugins": [
15+
["./.config/plugin/babel/add-import-extension.js", { extension: "mjs" }]
16+
]
17+
},
18+
}
3419
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
const { types } = require('@babel/core');
2+
const { declare } = require('@babel/helper-plugin-utils');
3+
const { existsSync, lstatSync } = require('fs');
4+
const { dirname, resolve } = require('path');
5+
6+
const VALID_EXTENSIONS = ['js', 'mjs'];
7+
8+
const hasExtension = (moduleName) => VALID_EXTENSIONS.some(ext => moduleName.endsWith(`.${ext}`));
9+
const isCoreJSPolyfill = (moduleName) => moduleName.startsWith('core-js');
10+
const isLocalModule = (moduleName) => moduleName.startsWith('.');
11+
const isNodeModule = (moduleName) => {
12+
try {
13+
require.resolve(moduleName);
14+
15+
return true;
16+
} catch (ex) {
17+
if (ex.code === 'MODULE_NOT_FOUND') {
18+
return false;
19+
}
20+
}
21+
};
22+
23+
const isProcessableModule = (moduleName) => {
24+
return !hasExtension(moduleName) && (isCoreJSPolyfill(moduleName) || isLocalModule(moduleName));
25+
}
26+
27+
const createVisitor = ({ declaration, origArgs, extension = 'js' }) => {
28+
return (path, { file }) => {
29+
const { node: { source, exportKind, importKind } } = path;
30+
const { opts: { filename } } = file;
31+
const isTypeOnly = exportKind === 'type' || importKind === 'type';
32+
33+
if (!source || isTypeOnly || !isProcessableModule(source.value)) {
34+
return;
35+
}
36+
37+
const { value: moduleName } = source;
38+
const absoluteFilePath = resolve(dirname(filename), moduleName);
39+
const finalExtension = isCoreJSPolyfill(moduleName) ? 'js' : extension;
40+
41+
let newModulePath;
42+
43+
// Resolves a case where "import" points to a module name which exists as a file and
44+
// as a directory. For example in this case:
45+
// ```
46+
// import { registerPlugin } from 'plugins';
47+
// ```
48+
// and with this directory structure:
49+
// |- editors
50+
// |- plugins
51+
// |- filters/
52+
// |- ...
53+
// +- index.js
54+
// |- plugins.js
55+
// |- ...
56+
// +- index.js
57+
//
58+
// the plugin will rename import declaration to point to the `plugins.js` file.
59+
if (existsSync(`${absoluteFilePath}.js`)) {
60+
newModulePath = `${moduleName}.${finalExtension}`;
61+
62+
// In a case when the file doesn't exist and the module is a directory it will
63+
// rename to `plugins/index.js`.
64+
} else if (existsSync(absoluteFilePath) && lstatSync(absoluteFilePath).isDirectory()) {
65+
newModulePath = `${moduleName}/index.${finalExtension}`;
66+
67+
// And for other cases it simply put the extension on the end of the module path
68+
} else {
69+
newModulePath = `${moduleName}.${finalExtension}`;
70+
}
71+
72+
path.replaceWith(declaration(...origArgs(path), types.stringLiteral(newModulePath)));
73+
};
74+
};
75+
76+
module.exports = declare((api, options) => {
77+
api.assertVersion(7);
78+
79+
return {
80+
name: 'add-import-extension',
81+
visitor: {
82+
// It covers default and named imports
83+
ImportDeclaration: createVisitor({
84+
extension: options.extension,
85+
declaration: types.importDeclaration,
86+
origArgs: ({ node: { specifiers } }) => [specifiers],
87+
}),
88+
ExportNamedDeclaration: createVisitor({
89+
extension: options.extension,
90+
declaration: types.exportNamedDeclaration,
91+
origArgs: ({ node: { declaration, specifiers } }) => [declaration, specifiers],
92+
}),
93+
ExportAllDeclaration: createVisitor({
94+
extension: options.extension,
95+
declaration: types.exportAllDeclaration,
96+
origArgs: () => [],
97+
}),
98+
}
99+
};
100+
});

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Makes sure that Git automatically detects what files are considered "text" and use LF as the line ending in all OS
2+
* text=auto eol=lf

.release.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
"release_message": true,
77
"remote": "origin",
88
"pre_commit_commands": [
9+
"npm run clean",
910
"npm run build",
1011
"npm run test"
1112
],
1213
"post_commit_commands": [],
1314
"post_complete_commands": [
14-
"npm publish"
15+
"cd tmp && npm publish",
16+
"npm run clean"
1517
],
1618
"files_to_commit": [
1719
"./dist/**/*"

CONTRIBUTING.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ Your contributions to the project are very welcome. If you would like to fix a b
44

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

7-
1. Please make your fix on a separate branch based on `develop` branch. This makes merging much easier.
8-
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.
9-
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.
7+
1. Please make sure that you're using the NodeJS in the proper version. The project requires version 10.
8+
2. Make your fix on a separate branch based on `develop` branch. This makes merging much easier.
9+
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.
10+
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.
1011
5. Describe the problem in the Pull Request description (of course you would do it, why do I mention that?).
1112
6. **Very important:** Make Pull Request ready to merge into `develop` branch.
1213

loader/empty-loader.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)