无法将打字稿 class 导出到 webpack 项目
Cannot export a typescript class to a webpack project
我继承了一个只有一些 JS 的项目(用 typescript 库编写)。我正在尝试更新项目以使用 Webpack,但在将 ts 库加载到我的 JS 脚本时遇到问题。
模块已通过 Yarn/NPM 安装好(及其依赖项),我将打字稿 class 更新为以下内容:
import { fabric } from 'fabric';
export class PlatePreview {
我还更新了 tsconfig.json 以将其识别为模块
{
"compilerOptions": {
"target": "es5",
"watch": true,
"module": "CommonJS",
"sourceMap": false,
"rootDir": ".",
"isolatedModules": false,
"removeComments": true,
"preserveConstEnums": true,
"noImplicitUseStrict": false,
"declaration": false
},
"include": [
"src/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
我的完整 webpack 配置如下:
const Path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const PATHS = {
MODULES: Path.resolve('node_modules'), // your node_modules folder name, or full path
SC: Path.resolve('../silvercommerce/src'),
FILES_PATH: '../', // relative path from your css files to your other files, such as images and fonts
ROOT: Path.resolve(), // the root path, where your webpack.config.js is located.
SRC: Path.resolve('src'), // the root path, where your webpack.config.js is located.
SCSS: Path.resolve('src/scss'), // the root path to your source files
CSS: Path.resolve('css'), // the root path to your source files
JS: Path.resolve('javascript'), // the root path to your built files
};
module.exports = {
entry: {
bundle: [
`${PATHS.MODULES}/jquery/dist/jquery.js`,
`${PATHS.MODULES}/popper.js/dist/umd/popper.js`,
`${PATHS.MODULES}/tether/dist/js/tether.js`,
`${PATHS.MODULES}/bootstrap/dist/js/bootstrap.js`,
`${PATHS.MODULES}/imagesloaded/imagesloaded.pkgd.js`,
`${PATHS.MODULES}/jquery-match-height/dist/jquery.matchHeight-min.js`,
`${PATHS.MODULES}/jquery-zoom/jquery.zoom.min.js`,
`${PATHS.MODULES}/@fortawesome/fontawesome-free/js/all.js`
],
script: [
`${PATHS.MODULES}/fabric/dist/fabric.js`,
`${PATHS.MODULES}/@ilateral/bestplate-previewer/src/plate-preview.ts`,
`${PATHS.SC}/javascript/script.js`,
`${PATHS.SRC}/javascript/script.js`
],
bundlecss: [
`${PATHS.SCSS}/bundle.scss`
],
editor: [
`${PATHS.SCSS}/editor.scss`
]
},
output: {
path: PATHS.ROOT,
filename: 'javascript/[name].js',
publicPath: PATHS.ROOT
},
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'ts-loader'
},
{
test: /\.(sa|sc|c)ss$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
hmr: process.env.NODE_ENV === 'development',
},
},
'css-loader',
'sass-loader',
]
},
{
test: /\.(png|jpe?g|gif)$/,
use: [
{
loader: 'file-loader',
options: {
outputPath: 'images',
publicPath: '../images',
name: '[name].[ext]?[hash]'
},
},
]
},
{
test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'fonts/',
publicPath: '../fonts'
}
}
]
}
]
},
plugins: [
new MiniCssExtractPlugin(
{
path: PATHS.ROOT,
filename: 'css/[name].css',
chunkFilename: 'css/[id].css',
}
)
],
resolve: {
// Expose Jquery Globally
alias: {
jquery: "jquery/src/jquery",
fabric: 'fabric'
}
}
};
其他一切正常,webpack 生成 JS 文件,但是当我将以下内容添加到我的 script.js:
import PlatePreview from '@ilateral/bestplate-previewer';
var preview = new PlatePreview(`PlatePreview${id}`);
我收到错误 Uncaught TypeError: _ilateral_bestplate_previewer__WEBPACK_IMPORTED_MODULE_0___default.a is not a constructor
我也尝试了 new PlatePreview.PlateReview(
PlatePreview${id});
,但也产生了类似的错误。
我也尝试将导入更改为 import { PlatePreview } from '@ilateral/bestplate-previewer';
但仍然出现相同的错误。
有人知道我做错了什么吗?
非常感谢,
莫
所以没有让它正常工作,但如果有人偶然发现类似的问题,我设法通过改变我的 tsconfig
导出 module: "ES2015"
来取得一些成功,例如:
{
"compilerOptions": {
"target": "es5",
"watch": true,
"module": "ES2015",
"sourceMap": false,
"rootDir": ".",
"isolatedModules": false,
"removeComments": true,
"preserveConstEnums": true,
"noImplicitUseStrict": false,
"declaration": false
},
"include": [
"src/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
这会生成一个带有正确导出的 js 库,然后我可以将其包含在我的 webpack 脚本中。
这仍然有点繁琐,但似乎可以满足我的需要。
我继承了一个只有一些 JS 的项目(用 typescript 库编写)。我正在尝试更新项目以使用 Webpack,但在将 ts 库加载到我的 JS 脚本时遇到问题。
模块已通过 Yarn/NPM 安装好(及其依赖项),我将打字稿 class 更新为以下内容:
import { fabric } from 'fabric';
export class PlatePreview {
我还更新了 tsconfig.json 以将其识别为模块
{
"compilerOptions": {
"target": "es5",
"watch": true,
"module": "CommonJS",
"sourceMap": false,
"rootDir": ".",
"isolatedModules": false,
"removeComments": true,
"preserveConstEnums": true,
"noImplicitUseStrict": false,
"declaration": false
},
"include": [
"src/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
我的完整 webpack 配置如下:
const Path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const PATHS = {
MODULES: Path.resolve('node_modules'), // your node_modules folder name, or full path
SC: Path.resolve('../silvercommerce/src'),
FILES_PATH: '../', // relative path from your css files to your other files, such as images and fonts
ROOT: Path.resolve(), // the root path, where your webpack.config.js is located.
SRC: Path.resolve('src'), // the root path, where your webpack.config.js is located.
SCSS: Path.resolve('src/scss'), // the root path to your source files
CSS: Path.resolve('css'), // the root path to your source files
JS: Path.resolve('javascript'), // the root path to your built files
};
module.exports = {
entry: {
bundle: [
`${PATHS.MODULES}/jquery/dist/jquery.js`,
`${PATHS.MODULES}/popper.js/dist/umd/popper.js`,
`${PATHS.MODULES}/tether/dist/js/tether.js`,
`${PATHS.MODULES}/bootstrap/dist/js/bootstrap.js`,
`${PATHS.MODULES}/imagesloaded/imagesloaded.pkgd.js`,
`${PATHS.MODULES}/jquery-match-height/dist/jquery.matchHeight-min.js`,
`${PATHS.MODULES}/jquery-zoom/jquery.zoom.min.js`,
`${PATHS.MODULES}/@fortawesome/fontawesome-free/js/all.js`
],
script: [
`${PATHS.MODULES}/fabric/dist/fabric.js`,
`${PATHS.MODULES}/@ilateral/bestplate-previewer/src/plate-preview.ts`,
`${PATHS.SC}/javascript/script.js`,
`${PATHS.SRC}/javascript/script.js`
],
bundlecss: [
`${PATHS.SCSS}/bundle.scss`
],
editor: [
`${PATHS.SCSS}/editor.scss`
]
},
output: {
path: PATHS.ROOT,
filename: 'javascript/[name].js',
publicPath: PATHS.ROOT
},
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'ts-loader'
},
{
test: /\.(sa|sc|c)ss$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
hmr: process.env.NODE_ENV === 'development',
},
},
'css-loader',
'sass-loader',
]
},
{
test: /\.(png|jpe?g|gif)$/,
use: [
{
loader: 'file-loader',
options: {
outputPath: 'images',
publicPath: '../images',
name: '[name].[ext]?[hash]'
},
},
]
},
{
test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'fonts/',
publicPath: '../fonts'
}
}
]
}
]
},
plugins: [
new MiniCssExtractPlugin(
{
path: PATHS.ROOT,
filename: 'css/[name].css',
chunkFilename: 'css/[id].css',
}
)
],
resolve: {
// Expose Jquery Globally
alias: {
jquery: "jquery/src/jquery",
fabric: 'fabric'
}
}
};
其他一切正常,webpack 生成 JS 文件,但是当我将以下内容添加到我的 script.js:
import PlatePreview from '@ilateral/bestplate-previewer';
var preview = new PlatePreview(`PlatePreview${id}`);
我收到错误 Uncaught TypeError: _ilateral_bestplate_previewer__WEBPACK_IMPORTED_MODULE_0___default.a is not a constructor
我也尝试了 new PlatePreview.PlateReview(
PlatePreview${id});
,但也产生了类似的错误。
我也尝试将导入更改为 import { PlatePreview } from '@ilateral/bestplate-previewer';
但仍然出现相同的错误。
有人知道我做错了什么吗?
非常感谢,
莫
所以没有让它正常工作,但如果有人偶然发现类似的问题,我设法通过改变我的 tsconfig
导出 module: "ES2015"
来取得一些成功,例如:
{
"compilerOptions": {
"target": "es5",
"watch": true,
"module": "ES2015",
"sourceMap": false,
"rootDir": ".",
"isolatedModules": false,
"removeComments": true,
"preserveConstEnums": true,
"noImplicitUseStrict": false,
"declaration": false
},
"include": [
"src/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
这会生成一个带有正确导出的 js 库,然后我可以将其包含在我的 webpack 脚本中。
这仍然有点繁琐,但似乎可以满足我的需要。