使用Webpack将.ts和.css捆绑为单个文件

时间:2019-11-20 01:52:23

标签: css typescript webpack

我已经和这个人转了一圈了,我认为是时候寻求帮助了。

我有一个奇怪的用例,我需要将打字稿转换成一个单独的包,将.css打包成一个文件,还复制所有不是.js,.ts或.css的其他文件。这些文件最终被保存到服务器,我们用于表单的软件将使用该服务器。它们不能包含.html,因此我在网上找到的大多数资源在我的用例中都无法使用。

无论出于何种原因,我都遇到了错误,而且我真的不知道从这里去哪里。

我的devDependencies如下:

"extract-text-webpack-plugin": "^3.0.2",
"ts-loader": "^6.2.1",
"typescript": "^3.7.2",
"webpack": "^4.41.2",
"webpack-cli": "^3.3.10",

我的webpack.config.js看起来像这样:

const path = require('path');
const glob = require('glob');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const combineLoaders = require('webpack-combine-loaders');

module.exports = {
  entry: glob.sync('./forms/**/index.ts').reduce((acc, path) => {
    const entry = path.replace('/index.ts', '')
    acc[entry] = path
    return acc
}, {}),
  devtool: 'inline-source-map',
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: [
          {
            loader: 'ts-loader',
            options: {
              transpileOnly: true,
              experimentalWatchApi: true,
            }
          }
        ],
        exclude: /node_modules/,
      },
      {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract(
          'style-loader',
          combineLoaders([{
            loader: 'css-loader',
            query: {
              modules: true,
              localIdentName: '[name]__[local]___[hash:base64:5]'
            }
          }])
        )
      }
    ]
  },
  resolve: {
    extensions: [ '.tsx', '.ts', '.js' ],
  },
  output: {
    filename: './[name]/customBundle.js',
    path: path.resolve(__dirname, './public')
  },
  plugins: [
    new ExtractTextPlugin('styles.css')
  ]
};

这是我得到的错误:

> ci-deploy-test@1.0.0 build C:\Users\userName\Documents\git-coding\ci-deploy-test
> webpack

(node:15584) DeprecationWarning: Tapable.plugin is deprecated. Use new API on `.hooks` instead
C:\Users\userName\Documents\git-coding\ci-deploy-test\node_modules\webpack\lib\Chunk.js:866
                throw new Error(
                ^

Error: Chunk.entrypoints: Use Chunks.groupsIterable and filter by instanceof Entrypoint instead
    at Chunk.get (C:\Users\userName\Documents\git-coding\ci-deploy-test\node_modules\webpack\lib\Chunk.js:866:9)
    at C:\Users\userName\Documents\git-coding\ci-deploy-test\node_modules\extract-text-webpack-plugin\dist\index.js:176:48
    at Array.forEach (<anonymous>)
    at C:\Users\userName\Documents\git-coding\ci-deploy-test\node_modules\extract-text-webpack-plugin\dist\index.js:171:18
    at AsyncSeriesHook.eval [as callAsync] (eval at create (C:\Users\userName\Documents\git-coding\ci-deploy-test\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:7:1)
    at AsyncSeriesHook.lazyCompileHook (C:\Users\userName\Documents\git-coding\ci-deploy-test\node_modules\tapable\lib\Hook.js:154:20)
    at Compilation.seal (C:\Users\userName\Documents\git-coding\ci-deploy-test\node_modules\webpack\lib\Compilation.js:1342:27)
    at compilation.finish.err (C:\Users\userName\Documents\git-coding\ci-deploy-test\node_modules\webpack\lib\Compiler.js:675:18)
    at hooks.finishModules.callAsync.err (C:\Users\userName\Documents\git-coding\ci-deploy-test\node_modules\webpack\lib\Compilation.js:1261:4)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (C:\Users\userName\Documents\git-coding\ci-deploy-test\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:24:1)
    at AsyncSeriesHook.lazyCompileHook (C:\Users\userName\Documents\git-coding\ci-deploy-test\node_modules\tapable\lib\Hook.js:154:20)
    at Compilation.finish (C:\Users\userName\Documents\git-coding\ci-deploy-test\node_modules\webpack\lib\Compilation.js:1253:28)
    at hooks.make.callAsync.err (C:\Users\userName\Documents\git-coding\ci-deploy-test\node_modules\webpack\lib\Compiler.js:672:17)
    at _done (eval at create (C:\Users\userName\Documents\git-coding\ci-deploy-test\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:9:1)
    at _err0 (eval at create (C:\Users\userName\Documents\git-coding\ci-deploy-test\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:20:22)
    at _addModuleChain (C:\Users\userName\Documents\git-coding\ci-deploy-test\node_modules\webpack\lib\Compilation.js:1185:12)
    at processModuleDependencies.err (C:\Users\userName\Documents\git-coding\ci-deploy-test\node_modules\webpack\lib\Compilation.js:1097:9)
    at process._tickCallback (internal/process/next_tick.js:61:11)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! ci-deploy-test@1.0.0 build: `webpack`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the ci-deploy-test@1.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\userName\AppData\Roaming\npm-cache\_logs\2019-11-20T01_47_46_906Z-debug.log

更新:主要在工作。现在,我只需要能够处理.scss。

const path = require('path');
const glob = require('glob');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = {
  plugins: [
    new MiniCssExtractPlugin({
      filename: './[name]/customBundle.css',
      chunkFilename: '[id].css',
      ignoreOrder: false
    })
  ],
  entry: glob.sync('./forms/**/index.ts').reduce((acc, path) => {
    const entry = path.replace('/index.ts', '');
    acc[entry] = path;
    return acc;
  }, {}),
  devtool: 'inline-source-map',
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: [
          {
            loader: 'ts-loader',
            options: {
              transpileOnly: true,
              experimentalWatchApi: true
            }
          }
        ],
        exclude: /node_modules/
      },
      {
        test: /\.css$/,
        use: [
          {
            loader: MiniCssExtractPlugin.loader,
            options: {
              hmr: process.env.NODE_ENV === 'development'
            }
          },
          'css-loader'
        ]
      }
    ]
  },
  resolve: {
    extensions: ['.tsx', '.ts', '.js']
  },
  output: {
    filename: './[name]/customBundle.js',
    path: path.resolve(__dirname, './public')
  }
};

1 个答案:

答案 0 :(得分:0)

这很可能与在Webpack 4中使用ExtractTextPlugin有关:

https://github.com/webpack-contrib/extract-text-webpack-plugin#usage

将您的配置迁移到https://github.com/webpack-contrib/mini-css-extract-plugin