Giter VIP home page Giter VIP logo

sharedworker-typscript's Introduction

Introducing Sharedworker-Loader

Getting Started

To begin, you'll need to install sharedworker-loader $ npm install --save-dev [email protected]

Modify Webpack Config

With the sharedworker-loader plug-in, you don't have to add code split config in webpack to split your worker script separately. If you are also using typescript, the only rules you needed to add are

module: {
        rules: [
            // all files with a '.ts' or '.tsx' extension will be handled by 'ts-loader'
            {
                test: /\.tsx?$/,
                use: {
                    loader: "ts-loader"
                }
            },
            {
                test: /\.worker\.js$/,
                use: { loader: 'sharedworker-loader' }
            }
            
        ]
}

Using Loader

So how the script gets split out without code spliting?

The magic lays in the require statement

import SharedWorker = require("sharedworker-loader?name=worker.js!./worker/mainworker");

The require statement contains 3 parts: loader + script bundled name + entry point.

  • loader: see if sharedworker-loader dependency is added
  • script bundled name: the name of the bundled worker script
  • entry point: the entry point for worker.

Adding a typing file for your loader

Similar what worker-loader mentioned in their readme, you need to add a custom.d.ts for typescript to resolve the type of the imported worker module. In order to do you'll need to

  • npm install --save-dev @types/sharedworker
  • create a typing definition for the module you imported
declare module "sharedworker-loader*" {
    class WebpackSharedWorker extends SharedWorker {
        constructor();
    }

    export = WebpackSharedWorker;
}

Voilà, when do npm run build, you'll see the worker script get splited out separately.

Notes:

Some of you might have come across this PR https://github.com/webpack-contrib/worker-loader/pull/98/files. The change about creating sharedworker actually only merged into their feature branch feat-mode So that's a no go for me.

sharedworker-typscript's People

Contributors

caseyyu2 avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

michtsu

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.