Skip to content

Commit 1a4207a

Browse files
WIP. remake devextreme build to produce ESM lib package
1 parent ac9057c commit 1a4207a

File tree

4 files changed

+80
-14
lines changed

4 files changed

+80
-14
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
const fs = require('fs');
5+
const TS_OUTPUT_BASE_DIR = 'artifacts/dist_ts';
6+
7+
module.exports = function addImportExtensions() {
8+
return {
9+
name: 'add-import-extensions',
10+
visitor: {
11+
'ImportDeclaration|ExportNamedDeclaration|ExportAllDeclaration'(astPath) {
12+
const source = astPath.node.source;
13+
14+
if (!source) return;
15+
16+
const value = source.value;
17+
18+
if (!value || (!value.startsWith('./') && !value.startsWith('../'))) {
19+
return;
20+
}
21+
22+
if (value.match(/\.(js|mjs|json|css)$/)) {
23+
return;
24+
}
25+
26+
if (value.endsWith('/')) {
27+
source.value = value + 'index.js';
28+
return;
29+
}
30+
31+
const currentFile = astPath.hub?.file?.opts?.filename;
32+
const distPathRegExp = new RegExp(TS_OUTPUT_BASE_DIR)
33+
34+
if (currentFile) {
35+
const currentDir = path.dirname(currentFile);
36+
const resolvedPath = path.resolve(currentDir, value).replace(distPathRegExp,'js');
37+
38+
let jsFilePath = resolvedPath + '.js';
39+
40+
if ( fs.existsSync(jsFilePath)
41+
|| fs.existsSync(jsFilePath = resolvedPath + '.ts')
42+
) {
43+
const stat = fs.statSync(jsFilePath);
44+
45+
if (stat.isFile()) {
46+
source.value = value + '.js';
47+
return;
48+
}
49+
}
50+
51+
if (fs.existsSync(resolvedPath)) {
52+
const stat = fs.statSync(resolvedPath);
53+
54+
if (stat.isDirectory()) {
55+
const indexPath = path.join(resolvedPath, 'index.js');
56+
57+
if (fs.existsSync(indexPath)) {
58+
source.value = value + '/index.js';
59+
return;
60+
}
61+
}
62+
}
63+
}
64+
65+
source.value = value + '.js';
66+
}
67+
}
68+
};
69+
};
70+

packages/devextreme/build/gulp/npm.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ const gulpRename = require('gulp-rename');
1515
const compressionPipes = require('./compression-pipes.js');
1616
const ctx = require('./context.js');
1717
const env = require('./env-variables.js');
18-
const fixImports = require('./fix-imports-path');
1918
const dataUri = require('./gulp-data-uri').gulpPipe;
2019
const headerPipes = require('./header-pipes.js');
2120
const { packageDir, packageDistDir, isEsmPackage, stringSrc, devextremeDistDir } = require('./utils');
@@ -166,7 +165,6 @@ function collectExports(baseDir) {
166165
const packageJsonPath = path.join(currentDir, 'package.json');
167166

168167
if (fs.existsSync(packageJsonPath) && !/(cjs|esm)$/.test(currentDir)) {
169-
console.log('----packageJsonPath------>',currentDir)
170168
try {
171169
const pkg = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
172170
const exportEntry = {};
@@ -219,7 +217,7 @@ gulp.task('npm-dist', () => gulp
219217
.pipe(gulp.dest(distPath))
220218
);
221219

222-
gulp.task('patch-as-esm-lib', () => gulp
220+
gulp.task('add-exports-to-package-json', () => gulp
223221
.src(`${packagePath}/package.json`)
224222
.pipe(
225223
through.obj((file, enc, callback) => {
@@ -233,11 +231,6 @@ gulp.task('patch-as-esm-lib', () => gulp
233231
.pipe(gulp.dest(packagePath))
234232
);
235233

236-
gulp.task('fix-ext-in-imports', function(done) {
237-
fixImports.addExtensionToImportsPath(`${packagePath}/esm`);
238-
done();
239-
});
240-
241234
const scssDir = `${packagePath}/scss`;
242235

243236
gulp.task('npm-sass', gulp.series(
@@ -257,5 +250,5 @@ gulp.task('npm-sass', gulp.series(
257250
)
258251
));
259252

260-
gulp.task('npm', gulp.series('npm-sources', 'npm-dist', 'ts-check-public-modules', 'npm-sass'/*, ''patch-as-esm-lib', fix-ext-in-imports'*/));
253+
gulp.task('npm', gulp.series('npm-sources', 'npm-dist', 'ts-check-public-modules', 'npm-sass', 'add-exports-to-package-json'));
261254

packages/devextreme/build/gulp/side-effects-finder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class SideEffectFinder {
4646
.forEach((str) => {
4747
let importPath = str.match(relativePathRegExp)[0].replace(/(^['"]|['"]$)/g, '');
4848

49-
importPath = path.join(path.dirname(modulePath), importPath) + '.js';
49+
importPath = path.join(path.dirname(modulePath), importPath);
5050

5151
if(!fs.existsSync(importPath)) {
5252
importPath = importPath.replace(/\.js$/, '/index.js');

packages/devextreme/build/gulp/transpile-config.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
const addImportExtensions = require('./babel-plugin-add-import-extensions');
4+
35
const common = {
46
plugins: [
57
['babel-plugin-inferno', { 'imports': true }],
@@ -33,11 +35,12 @@ module.exports = {
3335
esm: Object.assign({}, common, {
3436
// eslint-disable-next-line spellcheck/spell-checker
3537
presets: [['@babel/preset-env', { targets, modules: false }]],
36-
plugins: common.plugins.concat(
37-
[['@babel/plugin-transform-runtime', {
38+
plugins: common.plugins.concat([
39+
addImportExtensions,
40+
['@babel/plugin-transform-runtime', {
3841
useESModules: true,
3942
version: '7.5.0' // https://github.com/babel/babel/issues/10261#issuecomment-514687857
40-
}]]
41-
)
43+
}]
44+
])
4245
})
4346
};

0 commit comments

Comments
 (0)