HTML」カテゴリーアーカイブ

Webpackで静的なファイルをコピーする方法

CopyWebpackPluginを使って、build時に、JSONファイルなどをバンドルせずに、単体でコピーする方法。

インストール方法

$ npm i -S copy-webpack-plugin write-file-webpack-plugin

file-webpack-pluginを入れることで、debug時に、実際のdistディレクトリにも書き出されるようになります。

webpack.config.js

importを追加

const CopyFilePlugin = require("copy-webpack-plugin");
const WriteFilePlugin = require("write-file-webpack-plugin");

pluginの項目に追加

new CopyFilePlugin(
    [
        {
            context: "src",
            from: "**/*.json",
            to: path.resolve(__dirname, "dist")
        }
    ],
    { copyUnmodified: true }
),
new WriteFilePlugin()

src以下のJSONファイルをdist以下にコピーするという設定。contextで、対象となるディレクトリ/ファイルの親ディレクトリを指定。

参考にしたサイト

https://qiita.com/pepoipod/items/e41105e8f3afd47dc01c
https://codeday.me/jp/qa/20190515/828253.html

TypeScript + node.js + VisualStudioCodeの開発環境

node.jsでアプリケーションをつくるのに、TypeScriptで書いて、Visual Studio Codeを開発環境とする方法を探る

参考になったサイト

https://qiita.com/notakaos/items/3bbd2293e2ff286d9f49
https://qiita.com/kurogelee/items/cf7954f6c23294600ef2
https://github.com/TypeStrong/ts-node/issues/46#issuecomment-437758378

ここのボイラープレートをテンプレートにすると便利です。必要なnpmパッケージ、設定ファイルがはいっています。git cloneなどでローカルに。
https://github.com/notakaos/typescript-node-base

package.json

{
  "name": "typescript-node-base",
  "keywords": [],

  "version": "0.1.0",
  "main": "dist/index.js",
  "scripts": {
    "dev": "ts-node ./src/index.ts",
    "dev:watch": "ts-node-dev --respawn src/index.ts",
    "clean": "rimraf dist/*",
    "tsc": "tsc",
    "build": "npm-run-all clean tsc",
    "start": "node ."
  },
  "keywords": [],
  "author": "YOUR NAME",
  "license": "ISC",
  "description": "",
  "dependencies": {},
  "devDependencies": {
    "@types/node": "^12.7.4",
    "npm-run-all": "^4.1.5",
    "rimraf": "^2.7.1",
    "ts-node": "^8.3.0",
    "ts-node-dev": "^1.0.0-pre.42",
    "typescript": "^3.6.2"
  }
}

L9: --respawnを指定することで、サーバ型ではないスクリプトでも、実行後終了せずに待機させ、更新→再読み込み→再起動が可能に。

tsconfig.json

{
  "compilerOptions": {
    "target": "ES2019",
    "module": "commonjs",
    "sourceMap": true,
    "outDir": "./dist",
    "strict": true,
    "noImplicitAny": true,
    "esModuleInterop": true
  },
  "include": ["src/**/*"]
}

sourceMapは有効にしましょう。Debugが効くようになります。

launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "ts-node[debug]",
      "type": "node",
      "request": "launch",
      "args": ["${workspaceFolder}/src/index.ts"],
      "runtimeArgs": ["--nolazy", "-r", "ts-node/register"],
      "sourceMaps": true,
      "cwd": "${workspaceRoot}",
      "restart": true,
      "protocol": "inspector"
    }
  ]
}

L8: 任意のファイルを指定

https://github.com/TypeStrong/ts-node/issues/46#issuecomment-437758378
の投稿が参考になりました。

テキストを選択できなくする方法

<a>タグではなく、マウスイベントをjQuery経由でつけた場合、<div>の中のテキストが選択されてしまうのはよろしくないので、選択できないようにする方法。

scss(compass)で以下のようにミックスインで指定

@include user-select(none);

Google Calendarを自分のサイトで公開

Google Calendar

Display a calendar on your site

If you choose to share your calendar with others, you can create a webpage where they can browse through your calendar 〓 even if they don’t have a Google Calendar account. You can also embed an interactive calendar of your events into your website or blog.

自分のGoogle CalendarをGoogleアカウントがないユーザにも、手軽に公開できないかと思っていたら、新機能で実現出来るようになったようだ。便利。