I want to create a service worker.
DISCLAMER: I don't wanna use serviceworker-webpack-plugin, it adds a lot of overhead that I simply don't need. I'm 100% sure that webpack is able to serve transpiled js/ts files as static files.
So I have main.ts
and sw.ts
:
main.ts
should be transpiled viats-loader
and injected intoindex.html
, when I runwebpack-dev-server
main.ts
should support autoreload , as dev-server does by default.sw.ts
on the other hand should be transpiled viats-loader
as well and it SHOUDN'T be injected intoindex.html
. Also if I runwebpack-dev-server
, the dev-server shouldn't add hotreload js code there. I just wantsw.js
to be transplied.- Also I would like to have a reference to
sw.js
during runtime frommain.js
. File-loader should provide it the same way as it does it for.css files
.
Attemp #1
My first attemp was to create 2 entries in webpack config like below:
entry: {'main': './src/main.ts', 'sw':'./src/sw.ts'},
To prevent sw.ts
from injecting I could use index.ejs
and manually check js filename for if. This scenario works but while being on dev-server, webpack-dev-server adds hotreload code and sw.js
fails at runtime within window not defined
, which is logical, since service workers don't have window object.
Attemp #2, link to github
But then I tried file-loader
, which looks like below:
webpack.config.js:
const HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
entry: ['./src/main.ts'],
plugins: [
new HtmlWebpackPlugin({hash: true, template: 'src/index.html'}),
],
devtool: '#source-map',
module: {
rules: [
{
test: /sw\.ts$/,
exclude: /node_modules/,
loader: 'file-loader',
options: {
outputPath: '',
name: 'sw.js?[sha512:hash:base64:6]',
}
},
{
test: /\.ts$/,
loader: 'ts-loader',
},
],
},
};
tsconfig.json:
{
"compilerOptions": {
"lib": ["es2017", "dom"],
"module": "es2015",
"moduleResolution": "node",
"target": "es5",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true
},
"exclude": [
"./node_modules"
]
}
main.ts;
import './sw.ts'
console.log('main.ts');
sw.ts:
import {LoggerFactory, LogStrict} from 'lines-logger';
console.log('sw file');
let loggerFactory: LoggerFactory = new LoggerFactory(LogStrict.LOG_WITH_WARNINGS);
Output sw.js:
import { LoggerFactory, LogStrict } from 'lines-logger';
console.log('sw file');
var loggerFactory = new LoggerFactory(LogStrict.LOG_WITH_WARNINGS);
Why webpack doesn't resolve module dependencies? Is there another approach to handle service worker?