Yeah, in TS it's similiar to compiled languages- you have to compile (transpile) project before using it. The common practice is to use CI/CD to transpile ts files and deploy transpiled ones to production. If you want to simplify the process of running the application in development environment I recommend you taking a look on ts-node npm package
As I far as I know,
theWORKDIR don't have to be created by yourself.
Here's the documentation for WORKDIR.
Afterwards you don't have to copy by hand to the specific folder,
because after WORKDIRcommand the copy command would copy the files for you.Therefore I suggest you to use the following Dockerfile:
FROM node:alpine
WORKDIR /usr/yourapplication-name
COPY package.json .
RUN npm install\
&& npm install typescript -g
COPY . .
RUN tsc
CMD ["node", "./dist/server.js"]
As a tiny tipp: I would use typescript as a dependency in my and then just use the following file:package.json
FROM node:alpine
WORKDIR /usr/yourapplication-name
COPY package.json .
RUN npm install
COPY . .
RUN tsc
CMD ["node", "./dist/server.js"]
I went deeper into package builder and found out: iife is the term I was looking for. A iife build allows to load a library directly via the script tag without anything else.
After hours of testing I found a perfect solution to build my library that fits my needs: rollup.js
The core npm packages are:
My solution creates a package with this structure:
|dist
|- js
|-- MyLibrary.js
|- es
|-- // ES files
|- package.json
The file can be imported easily within a MyLibrary.js tag and the Typescript related files are in the "es" folder. The package automatically directs to the <script> that means Typescript developers should be able to use auto-complete for type suggestions in their IDE.es/index.js
You can find a sample repository here: https://github.com/julianpoemp/rolltsup-sample
package.json:
{
"name": "my-lib",
"version": "0.0.1",
"description": "",
"main": "src/main.js",
"dependencies": {
"rollup": "^2.38.5"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^17.1.0",
"@rollup/plugin-typescript": "^8.1.1",
"rollup-plugin-generate-package-json": "^3.2.0",
"tslib": "^2.1.0",
"typescript": "^4.1.3"
},
"scripts": {
"build": "rollup --config rollup.config.js && rollup --config rollup.config_es.js",
"test": "echo \"Error: no test specified\" && exit 1"
}
}
rollup.config.js
import typescript from '@rollup/plugin-typescript';
// rollup.config.js
export default {
input: 'src/index.ts',
output: {
file: 'dist/js/myLibrary.js',
name: 'MyLibrary',
format: 'iife'
},
plugins: [
typescript({
target: "ES5",
declaration: true,
outDir: "dist/js",
rootDir: "src"
})
]
};
rollup.config_es.js
import typescript from '@rollup/plugin-typescript';
import generatePackageJson from 'rollup-plugin-generate-package-json'
// rollup.config.js
export default {
input: 'src/index.ts',
output: {
dir: 'dist/es',
name: 'MyLibrary',
format: 'es'
},
plugins: [
typescript({
target: "ES5",
declaration: true,
outDir: "dist/es",
rootDir: "src"
}),
generatePackageJson({
outputFolder: "dist",
baseContents: (pkg) => {
pkg.main = "es/index.js";
pkg.scripts = undefined;
return pkg;
}
})
]
};
Open and enable the next.config.js option in the typescript config:ignoreBuildErrors
module.exports = {
typescript: {
// !! WARN !!
// Dangerously allow production builds to successfully complete even if
// your project has type errors.
// !! WARN !!
ignoreBuildErrors: true,
},
}
More info on the docs: https://nextjs.org/docs/api-reference/next.config.js/ignoring-typescript-errors
It happens because you are running with npm install. Try to install your dependencies using NODE_ENV=production, it should force to install dev dependencies even in production environmentnpm install --production=false
Solution is to add the polyfill to your polyfill.ts file
if(!window.console) {
var console = {
log : function(){},
warn : function(){},
error : function(){},
time : function(){},
timeEnd : function(){}
}
}