M
M
mix-92020-01-26 19:10:30
JavaScript
mix-9, 2020-01-26 19:10:30

How to open typescript module with export in global scope?

Good day. Typescript + webpack when compiling a simple file with a class, this class is available for global access. When adding an export/import, the file is executed, but the class is not available globally. How to fix?
I suppose that it is possible to somehow compile ts files separately without importing, then compile them into one, but how to set it up, I didn’t use the words import / export, did it work anyway?

const glob = require("glob");

module.exports = {
  entry: glob.sync('./src/*.js'),
  output: {
    filename: 'module.js',
    //path: path.resolve(__dirname, 'dist'),
  },
  optimization: {
      minimize: false
  }
};

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Robur, 2020-01-27
@Robur

I didn't understand anything, but
- "exporting" something to the global scope is bad practice, you need a good reason to do it
- write in a simple way - without 'module'
- configure webpack so that it compiles and collects ts files for you. you don't need to compile them separately and build them separately. how to do this is described here https://github.com/TypeStrong/ts-loader

M
mix-9, 2020-01-27
@mix-9

Seems to have found the answer. For webpack added to the configuration

output: {
    library: 'MyModule',
    filename: 'example3.js',

Plus, I found an example of collecting files into one
import {Example1} from './example1';
import {Example2} from './example2';

export {Example1} from './example1';
export {Example2} from './example2';

export class Example3 {
    constructor().....

Although outputting to the global scope is probably bad, so I probably won’t use the second part

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question