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

esm commonjs #160

Open
yongheng2016 opened this issue Apr 25, 2023 · 0 comments
Open

esm commonjs #160

yongheng2016 opened this issue Apr 25, 2023 · 0 comments

Comments

@yongheng2016
Copy link
Owner

yongheng2016 commented Apr 25, 2023

moduleResolution 总结


mkdir my-project
cd my-project
npm init -y
git init
npx mrm gitignore
npm i lodash lodash-es typescript
  • tsconfig.json
{
  "compilerOptions": {
    "allowJs": true,  // 允许编译js,引入不报错
    "target": "ES6",
    "module": "CommonJS",
    "esModuleInterop": true,  // 用于控制模块导入的方式。当esModuleInterop被设置为true时,TypeScript编译器将使用__importDefault()函数来导入默认导出。这使得在使用CommonJS模块时,可以像使用ES6模块一样导入默认导出。
    "strict": true,
    "outDir": "dist"
  },
  "include": [
    "src/**/*"
  ]
}
  • src/index.ts
// 引入 CommonJS 版本的 lodash
// const _ = require('lodash');
import  _ from "lodash"  // CommonJS版本lodash,没有allowSyntheticDefaultImports情况下 import * as _ from 'lodash'
import {test} from "./test.js"

console.log('commonjs',_.chunk([1, 2, 3, 4], 2));
console.log( test())


// 引入 ESM 版本的 lodash
// import { chunk } from 'lodash-es';
// console.log('esm',chunk([1, 2, 3, 4], 2));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant