F
F
fiter2019-11-18 06:08:25
typescript
fiter, 2019-11-18 06:08:25

Typescript - how to create an import bundle without typescript?

All the best!
There is a typescript lib, let's call it my-sample-lib, that webpack builds. At the output, I want to get a bundle that can be either simply included in the <script> tag or imported into another assembly that does not use typescript:

// примерно так
import MySampleLib from 'vendor/my-sample-lib/dist/js/my-sample-lib.js'
window.MySampleLib = MySampleLib;

webpack.config.js:
entry: {
  'my-sample-lib': './src/index.ts',
},
  output: {
    filename: './dist/js/[name].js',
    path: path.resolve( __dirname ),
}

src/index.ts:
import './scss/my-sample-lib/main.scss';
import MySampleLib from './ts/my-sample-lib';
export default MySampleLib;

With such a config, the bundle does not work either in the assembly or in the tag. If you finish the config to this state:
output: {
    filename: './dist/js/[name].js',
    path: path.resolve( __dirname ),
    library: 'MySampleLib',
    libraryTarget: 'umd',
}

Then the bundle is imported into the assembly, but does not work through the <script> tag:
<script src="/dist/js/my-sample-lib.js'">
<script>
console.log( MySampleLib ); // {__esModule: true, default: ƒ}
</script>

How to set up webpack so that the bundle can be both simply included in the <script> tag and imported into the assembly?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question