Skip to content

Instantly share code, notes, and snippets.

@katai5plate
Last active November 13, 2019 06:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katai5plate/6dbbe1881629be75a99b76fe62af6fc3 to your computer and use it in GitHub Desktop.
Save katai5plate/6dbbe1881629be75a99b76fe62af6fc3 to your computer and use it in GitHub Desktop.
TypeScript + Webpack + Webpackプラグイン+ BrowserSync + ESLint + Prettier 導入メモ
  1. Install Node.js, Yarn, VSCode, VSCodeExtention: ESLint, Prettier
  2. yarn add -D npm-run-all rimraf browser-sync typescript webpack webpack-cli ts-loader html-webpack-plugin style-loader css-loader file-loader
  3. Add tsconfig.json
{
  "compilerOptions": {
    "sourceMap": true,
    "target": "es5",
    "moduleResolution": "node",
    "lib": ["es2019", "dom"],
    "resolveJsonModule": true
  }
}
  1. Add webpack.config.js
const HtmlWebpackPlugin = require("html-webpack-plugin");
const config = {
  dist: `${__dirname}/dist`,
  title: "title",
//  favicon: `${__dirname}/favicon.ico`,
  favicon: void 0,
  splitSize: void 0,
}
module.exports = {
  mode: "development",
  devtool: "inline-source-map",
  entry: {
    main: "./src/index.ts"
  },
  optimization: {
    splitChunks: {
      maxSize: config.splitSize,
      name: "libs",
      chunks: "initial"
    }
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: config.title,
      favicon: config.favicon
    })
  ],
  output: {
    path: config.dist,
    filename: "[name].js"
  },
  module: {
    rules: [
      {
        test: /\.ts$/,
        use: "ts-loader"
      },
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"]
      },
      {
        test: /\.(png|jpe?g|gif|svg|ogg|mp3|wav|mpe?g|webm)$/i,
        loader: "file-loader",
        options: {
          name: "[contenthash].[ext]"
        }
      }
    ]
  },
  resolve: {
    extensions: [".ts", ".js"]
  }
};
  1. Add index.html
<p>Redirect...</p>
<script>
  const dist = "dist";
  window._redirect = () => (location.href = `./${dist}/index.html`);
  window._fn = () => {
    setTimeout(() => {
      fetch(`/${dist}/`).then(x => {
        if (x.ok) {
          setTimeout(window._redirect, 1000);
          return;
        }
        window._fn();
      });
    }, 1000);
  };
  window._fn();
</script>
  1. Add bs-config.js
module.exports = {
  files: "./dist/**/*",
  watch: true,
  server: true
};
  1. Modify package.json
{
  "scripts": {
    "start": "run-p watch serve",
    "build": "rimraf ./dist && webpack",
    "watch": "rimraf ./dist && webpack -w",
    "serve": "browser-sync start --config ./bs-config.js"
  }
}
  1. Add .eslintrc
{
  "extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:@typescript-eslint/eslint-recommended",
    "plugin:prettier/recommended",
    "prettier/@typescript-eslint"
  ],
  "plugins": ["@typescript-eslint"],
  "parser": "@typescript-eslint/parser",
  "env": { "browser": true, "node": true, "es6": true },
  "parserOptions": {
    "sourceType": "module"
  },
  "rules": {}
}
  1. Add .eslintignore
webpack.config.js
  1. Add .vscode/setting.json
{
  "eslint.autoFixOnSave": true,
  "eslint.validate": [
    "javascript",
    "javascriptreact",
    { "language": "typescript", "autoFix": true },
    { "language": "typescriptreact", "autoFix": true }
  ],
  "editor.formatOnSave": false,
  "prettier.semi": true,
  "[json]":{
    "editor.formatOnSave": true
  },
  "[jsonc]":{
    "editor.formatOnSave": true
  },
  "[markdown]":{
    "editor.formatOnSave": true
  }
}
  1. Add @types/assets.d.ts
// example: import * as image from "./image.png";
declare module "*.png";
declare module "*.jpeg";
declare module "*.jpg";
declare module "*.gif";
declare module "*.svg";
declare module "*.ogg";
declare module "*.mp3";
declare module "*.wav";
declare module "*.mpg";
declare module "*.mpeg";
declare module "*.mp4";
declare module "*.webm";
  1. Add ./src/index.ts
const div = document.createElement("h1");
div.innerText = "Hello, World!";
document.body.appendChild(div);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment