Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add .js extension in the import statement #16276

Merged
merged 4 commits into from
Dec 24, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion build/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const chalk = require('chalk');
const rollup = require('rollup');
const prePublish = require('./pre-publish');
const transformDEV = require('./transform-dev');
const preamble = require('./preamble');

async function run() {

Expand Down
44 changes: 35 additions & 9 deletions build/pre-publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const transformDEVUtil = require('./transform-dev');
const preamble = require('./preamble');
const dts = require('@lang/rollup-plugin-dts').default;
const rollup = require('rollup');
const { transformImport } = require('zrender/build/transformImport');

const ecDir = nodePath.resolve(__dirname, '..');
const tmpDir = nodePath.resolve(ecDir, 'pre-publish-tmp');
Expand Down Expand Up @@ -109,8 +110,8 @@ const compileWorkList = [
transformRootFolderInEntry(nodePath.resolve(ecDir, 'index.common.js'), esmDir);
transformRootFolderInEntry(nodePath.resolve(ecDir, 'index.simple.js'), esmDir);

await transformDistributionFiles(nodePath.resolve(ecDir, esmDir), esmDir);
await transformDistributionFiles(nodePath.resolve(ecDir, 'types'), esmDir);
await transformLibFiles(nodePath.resolve(ecDir, esmDir), esmDir);
await transformLibFiles(nodePath.resolve(ecDir, 'types'), esmDir);
fsExtra.removeSync(tmpDir);
}
},
Expand All @@ -131,7 +132,7 @@ const compileWorkList = [
fsExtra.removeSync(extensionESMDir);
},
after: async function () {
await transformDistributionFiles(extensionESMDir, 'lib');
await transformLibFiles(extensionESMDir, 'lib');
}
}
];
Expand Down Expand Up @@ -245,15 +246,15 @@ function transformRootFolderInEntry(entryFile, replacement) {
fs.writeFileSync(
entryFile,
// Also transform zrender.
singleTransformZRRootFolder(code, replacement),
singleTransformImport(code, replacement),
'utf-8'
);
}

/**
* Transform `zrender/src` to `zrender/esm` in all files
* Transform `zrender/src` to `zrender/lib` in all files
*/
async function transformDistributionFiles(rooltFolder, replacement) {
async function transformLibFiles(rooltFolder, replacement) {
const files = await readFilePaths({
patterns: ['**/*.js', '**/*.d.ts'],
cwd: rooltFolder
Expand All @@ -262,7 +263,7 @@ async function transformDistributionFiles(rooltFolder, replacement) {
// TODO More robust way?
for (let fileName of files) {
let code = fs.readFileSync(fileName, 'utf-8');
code = singleTransformZRRootFolder(code, replacement);
code = singleTransformImport(code, replacement);
// For lower ts version, not use import type
// TODO Use https://github.com/sandersn/downlevel-dts ?
// if (fileName.endsWith('.d.ts')) {
Expand All @@ -272,8 +273,33 @@ async function transformDistributionFiles(rooltFolder, replacement) {
}
}

function singleTransformZRRootFolder(code, replacement) {
return code.replace(/([\"\'])zrender\/src\//g, `$1zrender/${replacement}/`);
/**
* 1. Transform zrender/src to zrender/lib
* 2. Add .js extensions
*/
function singleTransformImport(code, replacement) {
return transformImport(
code.replace(/([\"\'])zrender\/src\//g, `$1zrender/${replacement}/`),
(moduleName) => {
// Ignore 'tslib' and 'echarts' in the extensions.
if (moduleName === 'tslib' || moduleName === 'echarts') {
return moduleName;
}
else if (moduleName === 'zrender/lib/export') {
throw new Error('Should not import the whole zrender library.');
}
else if (moduleName.endsWith('.ts')) {
// Replace ts with js
return moduleName.replace(/\.ts$/, '.js');
}
else if (moduleName.endsWith('.js')) {
return moduleName;
}
else {
return moduleName + '.js'
}
}
);
}

// function singleTransformImportType(code) {
Expand Down
111 changes: 111 additions & 0 deletions test/browser-esm-partial.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

109 changes: 109 additions & 0 deletions test/browser-esm-partial2.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/browser-esm.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading