Webpack 如何解决';错误:您需要提供一个目标平台';尝试设置网页包时
我是手机应用程序开发新手,正在尝试在我当前的手机应用程序中设置Webpack。我正在遵循Nativescript网站上的步骤。初始安装后,我收到1个警告和2个错误,如下所示: 我很难理解为什么找不到我的模块。我想我需要编辑我的webpack.config.js文件,但到目前为止还没有成功 在阅读在线帖子(当然还有webpack--help)后,我发现了以下命令: “$webpack--显示错误详细信息”Webpack 如何解决';错误:您需要提供一个目标平台';尝试设置网页包时,webpack,nativescript,Webpack,Nativescript,我是手机应用程序开发新手,正在尝试在我当前的手机应用程序中设置Webpack。我正在遵循Nativescript网站上的步骤。初始安装后,我收到1个警告和2个错误,如下所示: 我很难理解为什么找不到我的模块。我想我需要编辑我的webpack.config.js文件,但到目前为止还没有成功 在阅读在线帖子(当然还有webpack--help)后,我发现了以下命令: “$webpack--显示错误详细信息” 并在此处看到了一些附加信息: 错误详细信息告诉我提供一个目标平台。我尝试过将错误列表中的
并在此处看到了一些附加信息: 错误详细信息告诉我提供一个目标平台。我尝试过将错误列表中的项目添加到appComponents数组中,但没有成功,也没有添加到webpack.config.js文件中的其他位置 我当前的webpack.config.js文件:
const { join, relative, resolve, sep } = require("path");
const webpack = require("webpack");
const nsWebpack = require("nativescript-dev-webpack");
const nativescriptTarget = require("nativescript-dev-webpack/nativescript-target");
const CleanWebpackPlugin = require("clean-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
const { NativeScriptWorkerPlugin } = require("nativescript-worker-loader/NativeScriptWorkerPlugin");
const TerserPlugin = require("terser-webpack-plugin");
const hashSalt = Date.now().toString();
module.exports = env => {
// Add your custom Activities, Services and other Android app components here.
const appComponents = [
"tns-core-modules/ui/frame",
"tns-core-modules/ui/frame/activity",
];
const platform = env && (env.android && "android" || env.ios && "ios");
if (!platform) {
throw new Error("You need to provide a target platform!");
}
const platforms = ["ios", "android"];
const projectRoot = __dirname;
// Default destination inside platforms/<platform>/...
const dist = resolve(projectRoot, nsWebpack.getAppPath(platform, projectRoot));
const appResourcesPlatformDir = platform === "android" ? "Android" : "iOS";
const {
// The 'appPath' and 'appResourcesPath' values are fetched from
// the nsconfig.json configuration file
// when bundling with `tns run android|ios --bundle`.
appPath = "app",
appResourcesPath = "app/App_Resources",
// You can provide the following flags when running 'tns run android|ios'
snapshot, // --env.snapshot
uglify, // --env.uglify
report, // --env.report
sourceMap, // --env.sourceMap
hiddenSourceMap, // --env.hiddenSourceMap
hmr, // --env.hmr,
unitTesting, // --env.unitTesting
} = env;
const isAnySourceMapEnabled = !!sourceMap || !!hiddenSourceMap;
const externals = nsWebpack.getConvertedExternals(env.externals);
const appFullPath = resolve(projectRoot, appPath);
const appResourcesFullPath = resolve(projectRoot, appResourcesPath);
const entryModule = nsWebpack.getEntryModule(appFullPath, platform);
const entryPath = `.${sep}${entryModule}.ts`;
const entries = { bundle: entryPath };
const tsConfigPath = resolve(projectRoot, "tsconfig.tns.json");
if (platform === "ios") {
entries["tns_modules/tns-core-modules/inspector_modules"] = "inspector_modules.js";
};
let sourceMapFilename = nsWebpack.getSourceMapFilename(hiddenSourceMap, __dirname, dist);
const config = {
mode: uglify ? "production" : "development",
context: appFullPath,
externals,
watchOptions: {
ignored: [
appResourcesFullPath,
// Don't watch hidden files
"**/.*",
]
},
target: nativescriptTarget,
entry: entries,
output: {
pathinfo: false,
path: dist,
sourceMapFilename,
libraryTarget: "commonjs2",
filename: "[name].js",
globalObject: "global",
hashSalt
},
resolve: {
extensions: [".ts", ".js", ".scss", ".css"],
// Resolve {N} system modules from tns-core-modules
modules: [
resolve(__dirname, "node_modules/tns-core-modules"),
resolve(__dirname, "node_modules"),
"node_modules/tns-core-modules",
"node_modules",
],
alias: {
'~': appFullPath
},
// resolve symlinks to symlinked modules
symlinks: true
},
resolveLoader: {
// don't resolve symlinks to symlinked loaders
symlinks: false
},
node: {
// Disable node shims that conflict with NativeScript
"http": false,
"timers": false,
"setImmediate": false,
"fs": "empty",
"__dirname": false,
},
devtool: hiddenSourceMap ? "hidden-source-map" : (sourceMap ? "inline-source-map" : "none"),
optimization: {
runtimeChunk: "single",
splitChunks: {
cacheGroups: {
vendor: {
name: "vendor",
chunks: "all",
test: (module, chunks) => {
const moduleName = module.nameForCondition ? module.nameForCondition() : '';
return /[\\/]node_modules[\\/]/.test(moduleName) ||
appComponents.some(comp => comp === moduleName);
},
enforce: true,
},
}
},
minimize: !!uglify,
minimizer: [
new TerserPlugin({
parallel: true,
cache: true,
sourceMap: isAnySourceMapEnabled,
terserOptions: {
output: {
comments: false,
semicolons: !isAnySourceMapEnabled
},
compress: {
// The Android SBG has problems parsing the output
// when these options are enabled
'collapse_vars': platform !== "android",
sequences: platform !== "android",
}
}
})
],
},
module: {
rules: [
{
test: nsWebpack.getEntryPathRegExp(appFullPath, entryPath),
use: [
// Require all Android app components
platform === "android" && {
loader: "nativescript-dev-webpack/android-app-components-loader",
options: { modules: appComponents }
},
{
loader: "nativescript-dev-webpack/bundle-config-loader",
options: {
loadCss: !snapshot, // load the application css if in debug mode
unitTesting,
appFullPath,
projectRoot,
}
},
].filter(loader => !!loader)
},
{
test: /-page\.ts$/,
use: "nativescript-dev-webpack/script-hot-loader"
},
{
test: /\.(css|scss)$/,
use: "nativescript-dev-webpack/style-hot-loader"
},
{
test: /\.(html|xml)$/,
use: "nativescript-dev-webpack/markup-hot-loader"
},
{ test: /\.(html|xml)$/, use: "nativescript-dev-webpack/xml-namespace-loader" },
{
test: /\.css$/,
use: { loader: "css-loader", options: { url: false } }
},
{
test: /\.scss$/,
use: [
{ loader: "css-loader", options: { url: false } },
"sass-loader"
]
},
{
test: /\.ts$/,
use: {
loader: "ts-loader",
options: {
configFile: tsConfigPath,
// https://github.com/TypeStrong/ts-loader/blob/ea2fcf925ec158d0a536d1e766adfec6567f5fb4/README.md#faster-builds
// https://github.com/TypeStrong/ts-loader/blob/ea2fcf925ec158d0a536d1e766adfec6567f5fb4/README.md#hot-module-replacement
transpileOnly: true,
allowTsInNodeModules: true,
compilerOptions: {
sourceMap: isAnySourceMapEnabled,
declaration: false
}
},
}
},
]
},
plugins: [
// Define useful constants like TNS_WEBPACK
new webpack.DefinePlugin({
"global.TNS_WEBPACK": "true",
"process": undefined,
}),
// Remove all files from the out dir.
new CleanWebpackPlugin([`${dist}/**/*`]),
// Copy assets to out dir. Add your own globs as needed.
new CopyWebpackPlugin([
{ from: { glob: "fonts/**" } },
{ from: { glob: "**/*.jpg" } },
{ from: { glob: "**/*.png" } },
], { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] }),
// Generate a bundle starter script and activate it in package.json
new nsWebpack.GenerateBundleStarterPlugin(
// Don't include `runtime.js` when creating a snapshot. The plugin
// configures the WebPack runtime to be generated inside the snapshot
// module and no `runtime.js` module exist.
(snapshot ? [] : ["./runtime"])
.concat([
"./vendor",
"./bundle",
])
),
// For instructions on how to set up workers with webpack
// check out https://github.com/nativescript/worker-loader
new NativeScriptWorkerPlugin(),
new nsWebpack.PlatformFSPlugin({
platform,
platforms,
}),
// Does IPC communication with the {N} CLI to notify events when running in watch mode.
new nsWebpack.WatchStateLoggerPlugin(),
// https://github.com/TypeStrong/ts-loader/blob/ea2fcf925ec158d0a536d1e766adfec6567f5fb4/README.md#faster-builds
// https://github.com/TypeStrong/ts-loader/blob/ea2fcf925ec158d0a536d1e766adfec6567f5fb4/README.md#hot-module-replacement
new ForkTsCheckerWebpackPlugin({
tsconfig: tsConfigPath,
async: false,
useTypescriptIncrementalApi: true,
memoryLimit: 4096
})
],
};
// Copy the native app resources to the out dir
// only if doing a full build (tns run/build) and not previewing (tns preview)
if (!externals || externals.length === 0) {
config.plugins.push(new CopyWebpackPlugin([
{
from: `${appResourcesFullPath}/${appResourcesPlatformDir}`,
to: `${dist}/App_Resources/${appResourcesPlatformDir}`,
context: projectRoot
},
]));
}
if (report) {
// Generate report files for bundles content
config.plugins.push(new BundleAnalyzerPlugin({
analyzerMode: "static",
openAnalyzer: false,
generateStatsFile: true,
reportFilename: resolve(projectRoot, "report", `report.html`),
statsFilename: resolve(projectRoot, "report", `stats.json`),
}));
}
if (snapshot) {
config.plugins.push(new nsWebpack.NativeScriptSnapshotPlugin({
chunk: "vendor",
requireModules: [
"tns-core-modules/bundle-entry-points",
],
projectRoot,
webpackConfig: config,
}));
}
if (hmr) {
config.plugins.push(new webpack.HotModuleReplacementPlugin());
}
return config;
};
const{join,relative,resolve,sep}=require(“路径”);
const webpack=需要(“webpack”);
const nsWebpack=require(“nativescript开发网页包”);
const nativescriptTarget=require(“nativescript开发网页包/nativescript目标”);
const CleanWebpackPlugin=require(“clean webpack plugin”);
const CopyWebpackPlugin=require(“复制网页包插件”);
const ForkTsCheckerWebpackPlugin=require('fork-ts-checker-webpack-plugin');
const{BundleAnalyzerPlugin}=require(“网页包包分析器”);
const{NativeScriptWorkerPlugin}=require(“nativescript工作加载程序/NativeScriptWorkerPlugin”);
const TerserPlugin=require(“terser网页包插件”);
const hashSalt=Date.now().toString();
module.exports=env=>{
//在此处添加自定义活动、服务和其他Android应用程序组件。
常量appComponents=[
“tns核心模块/ui/frame”,
“tns核心模块/ui/frame/activity”,
];
const platform=env&&(env.android&“android”| | env.ios&“ios”);
如果(!平台){
抛出新错误(“您需要提供一个目标平台!”);
}
const platforms=[“ios”、“android”];
const projectRoot=\uuu dirname;
//平台内的默认目标/。。。
const dist=resolve(projectRoot,nsWebpack.getAppPath(platform,projectRoot));
const AppResourcePlatformDir=平台===“android”?“android”:“iOS”;
常数{
//“appPath”和“AppResourcePath”值是从
//nsconfig.json配置文件
//与“tns运行android”ios捆绑时——捆绑”。
appPath=“app”,
AppResourcePath=“app/app\u资源”,
//运行“tns run android | ios”时,可以提供以下标志
快照,//--env.snapshot
丑陋的,丑陋的
报告,//--env.report
sourceMap,//--env.sourceMap
hiddenSourceMap,//--env.hiddenSourceMap
hmr,//--env.hmr,
单元测试,//--env.unitTesting
}=环境;
const isAnySourceMapEnabled=!!sourceMap | |!!hiddenSourceMap;
const externals=nsWebpack.getConvertedExternals(env.externals);
const appFullPath=resolve(projectRoot,appPath);
const appResourcesFullPath=resolve(projectRoot,appResourcesPath);
const entryModule=nsWebpack.getEntryModule(appFullPath,平台);
const entryPath=`.${sep}${entryModule}.ts`;
const entries={bundle:entryPath};
const tsconfig路径=resolve(projectRoot,“tsconfig.tns.json”);
如果(平台==“ios”){
条目[“tns_模块/tns核心模块/inspector_模块”]=“inspector_modules.js”;
};
让sourceMapFilename=nsWebpack.getSourceMapFilename(hiddenSourceMap,目录名,dist);
常量配置={
模式:丑陋?“生产”:“开发”,
上下文:appFullPath,
外表,
监视选项:{
忽略:[
通知资源完整路径,
//不要看隐藏的文件
"**/.*",
]
},
目标:nativescriptTarget,
条目:条目,
输出:{
路径信息:错误,
路径:dist,
sourceMapFilename,
libraryTarget:“commonjs2”,
文件名:“[name].js”,
全球对象:“全球”,
大麻盐
},
决心:{
扩展名:[“.ts”、“.js”、“.scss”、“.css”],
//从tns核心模块解析{N}系统模块
模块:[
解析(uuu dirname,“节点模块/tns核心模块”),
解析(uuu dirname,“节点模块”),
“节点单元模块/tns核心模块”,
“节点_模块”,
],
别名:{
“~”:appFullPath
},
//将符号链接解析为符号链接模块
符号链接:正确
},
解析加载程序:{
//不将符号链接解析为符号链接加载程序
符号链接:false
},
节点:{
//禁用与NativeScript冲突的节点垫片
“http”:false,
“计时器”:错误,
“setImmediate”:false,
“fs”:“空”,
“_dirname”:false,
},
devtool:hiddenSourceMap?“隐藏源映射”:(源映射?“内联源映射”:“无”),
优化:{
runtimeChunk:“单个”,
分割块:{
缓存组:{
供应商:{
名称:“供应商”,
块:“全部”,
测试:(模块,块)=>{
const moduleName=module.nameForCondition?module.nameForCondition():“”;
返回/[\/]节点单元模块[\\/]/.test(moduleName)||
一些(comp=>comp===moduleName);
},
是的,
},
}
},
迷你