Skip to content

why does ts loader can't transifier export default xx to module.export? #583

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

Closed
MrKou47 opened this issue Jul 14, 2017 · 2 comments
Closed
Labels

Comments

@MrKou47
Copy link

MrKou47 commented Jul 14, 2017

I am writing a npm package which will running in node environment. and i find a problem.

if my ts code like:

class TestModule {}
export default TestModule;

this code will transifier to:

// transifier-index.js
// some code...
export.default = TestModule;

And then if i want to use this package like this:

const testModule = require('transifier-index.js');
console.log(testModule); // { default: [Function: TestModule] }
const obj = new testModule() // error: testModule is not a constructor

but if i write .ts file like this:

class TestModule {}
export = TestModule;

then i will get the current module.export:

// transifier-index.js
// some code...
module.export = TestModule;

Question: what't different between export = xxx and export default xxx? How can i transfer export default xxx to module.export =xxx?

some version info:

node: 7.7.1
webpack: 2.6.1
ts-loader: last version
tsc: 2.4.1

some config:

// tsconfig.json
{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "lib": ["es2015", "es2017", "dom"],
    "declaration": true,
    "declarationDir": "./wechat-pay-sdk",
    "strict": true,
    "noImplicitAny": false,
    "moduleResolution": "node",
    "experimentalDecorators": true
  },
  "include": [
    "./src"
  ],
  "exclude": [
    "node_modules"
  ]
}
// webpack.config.js
const webpack = require('webpack');
const path = require('path');

const config = {
  entry: path.join(__dirname, './src/index.ts'),
  output: {
    filename: 'index.js',
    path: path.join(__dirname, './lib'),
    libraryTarget: 'umd',
  },
  resolve: {
    extensions: ['.ts', '.tsx', '.js'],
  },
  externals: {
    md5: 'md5',
    url: 'url',
    request: 'request',
    xml2json: 'xml2json',
    js2xmlparser: 'js2xmlparser',
  },
  module: {
    loaders: [{
      test: /\.ts/,
      loaders: 'ts-loader',
      exclude: /node_modules/
    }]
  },
  target: 'node'
}

module.exports = config;
@HerringtonDarkholme
Copy link
Contributor

This is TypeScript's design. Please read microsoft/TypeScript#15957

@mouday
Copy link

mouday commented Dec 3, 2021

i don't get resolve about this solution

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

No branches or pull requests

4 participants