如何使用webpack编译单个文件?

时间:2019-03-06 07:01:56

标签: javascript node.js typescript webpack

我正在设置一个从javascript文件迁移的打字稿项目。下面是结构文件夹

src
--featureA
----script1.ts
----script2.ts
----index.ts
--featureB
----script1.ts
----index.ts
tsconfig.ts
webpack.config.js
package.json

我的tsconfig.ts

{
  "compileOnSave": true,
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "rootDir": "src",
    "outDir": "dist",
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "alwaysStrict": true,
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noFallthroughCasesInSwitch": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "suppressExcessPropertyErrors": false,
    "suppressImplicitAnyIndexErrors": false,
    "strictFunctionTypes": true,
    "strictNullChecks": true,
    "strictPropertyInitialization": true,
    "lib": ["es5", "dom"],
    "typeRoots": [
      "node_modules/@types"
    ],
  },
  "include": [
    "./src/**/*"
  ],
  "exclude": [
    "node_modules"
  ]
}

我的webpack配置

const { CheckerPlugin } = require("awesome-typescript-loader");

module.exports = {
  mode: "development",

  resolve: {
    extensions: [".ts", ".tsx", ".js", ".jsx"]
  },

  devtool: "source-map",

  module: {
    rules: [
      {
        test: /\.tsx?$/,
        loaders: ["awesome-typescript-loader"]
      }
    ]
  },

  plugins: [
    new CheckerPlugin()
  ],

  devServer: {
    before(app) {
    },
    contentBase: "web"
  }
};

我的package.json

"scripts": {
    "start": "webpack-dev-server"
  },
  "devDependencies": {
    "awesome-typescript-loader": "^3.5.0",
    "typescript": "^2.7.2",
    "typewiz": "^1.2.3",
    "typewiz-webpack": "^1.2.3",
    "webpack": "^4.20.0",
    "webpack-cli": "^3.1.1",
    "webpack-dev-server": "^3.1.0"
  }

功能A和B是独立的,我没有main.js文件作为入口点。如果我编译并缩小特征A,则将获得featureA.js文件并进行部署。这是我想要实现的结果。

我仍然不知道此设置是否可以满足我的要求?以及如何使用webpack按需编译每个功能,所需的命令如下所示:

Input: npm compile featureA
Output: featureA.js (with minified)

你能帮我吗?

1 个答案:

答案 0 :(得分:0)

也许您可以使用多个入口点?像这样:

  entry: {
    featureA: './src/featureA/index.ts',
    featureB: './src/featureB/index.ts'
  }

Webpack会将这两个文件编译为2个单独的包。