Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

Commit 64745cb

Browse files
committed
fix: generate tsconfig based on the nativescript-dev-webpack dependency
1 parent 5fa8c73 commit 64745cb

File tree

4 files changed

+284
-5
lines changed

4 files changed

+284
-5
lines changed

Diff for: projectFilesManager.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const path = require("path");
22
const fs = require("fs");
33

4-
const { isAngular, isVue } = require("./projectHelpers");
4+
const { isTypeScript, isAngular, isVue } = require("./projectHelpers");
55

66
function addProjectFiles(projectDir) {
77
const projectTemplates = getProjectTemplates(projectDir);
@@ -65,8 +65,10 @@ function getProjectTemplates(projectDir) {
6565
templates = getAngularTemplates(WEBPACK_CONFIG_NAME, TSCONFIG_TNS_NAME);
6666
} else if (isVue({ projectDir })) {
6767
templates = getVueTemplates(WEBPACK_CONFIG_NAME);
68-
} else {
68+
} else if (isTypeScript({ projectDir })) {
6969
templates = getTypeScriptTemplates(WEBPACK_CONFIG_NAME, TSCONFIG_TNS_NAME);
70+
} else {
71+
templates = getJavaScriptTemplates(WEBPACK_CONFIG_NAME);
7072
}
7173

7274
return getFullTemplatesPath(projectDir, templates);
@@ -92,6 +94,12 @@ function getVueTemplates(webpackConfigName) {
9294
};
9395
}
9496

97+
function getJavaScriptTemplates(webpackConfigName) {
98+
return {
99+
"webpack.javascript.js": webpackConfigName,
100+
};
101+
}
102+
95103
function getFullTemplatesPath(projectDir, templates) {
96104
let updatedTemplates = {};
97105

Diff for: projectHelpers.js

+15
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@ const PROJECT_DATA_GETTERS = {
88
appResourcesPath: "getAppResourcesRelativeDirectoryPath",
99
};
1010

11+
const isTypeScript = ({ projectDir, packageJson } = {}) => {
12+
packageJson = packageJson || getPackageJson(projectDir);
13+
14+
return (
15+
packageJson.dependencies &&
16+
(packageJson.dependencies.hasOwnProperty("nativescript-dev-typescript") ||
17+
packageJson.dependencies.hasOwnProperty("typescript"))
18+
) || (
19+
packageJson.devDependencies &&
20+
(packageJson.devDependencies.hasOwnProperty("nativescript-dev-typescript") ||
21+
packageJson.devDependencies.hasOwnProperty("typescript"))
22+
) || isAngular({ packageJson });
23+
};
24+
1125
const isAngular = ({ projectDir, packageJson } = {}) => {
1226
packageJson = packageJson || getPackageJson(projectDir);
1327

@@ -80,6 +94,7 @@ module.exports = {
8094
isIos,
8195
isAngular,
8296
isVue,
97+
isTypeScript,
8398
writePackageJson,
8499
convertSlashesInPath
85100
};

Diff for: templates/webpack.javascript.js

+256
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
const { join, relative, resolve, sep } = require("path");
2+
3+
const webpack = require("webpack");
4+
const nsWebpack = require("nativescript-dev-webpack");
5+
const nativescriptTarget = require("nativescript-dev-webpack/nativescript-target");
6+
const CleanWebpackPlugin = require("clean-webpack-plugin");
7+
const CopyWebpackPlugin = require("copy-webpack-plugin");
8+
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
9+
const { NativeScriptWorkerPlugin } = require("nativescript-worker-loader/NativeScriptWorkerPlugin");
10+
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
11+
12+
module.exports = env => {
13+
// Add your custom Activities, Services and other android app components here.
14+
const appComponents = [
15+
"tns-core-modules/ui/frame",
16+
"tns-core-modules/ui/frame/activity",
17+
];
18+
19+
const platform = env && (env.android && "android" || env.ios && "ios");
20+
if (!platform) {
21+
throw new Error("You need to provide a target platform!");
22+
}
23+
24+
const platforms = ["ios", "android"];
25+
const projectRoot = __dirname;
26+
27+
// Default destination inside platforms/<platform>/...
28+
const dist = resolve(projectRoot, nsWebpack.getAppPath(platform, projectRoot));
29+
const appResourcesPlatformDir = platform === "android" ? "Android" : "iOS";
30+
31+
const {
32+
// The 'appPath' and 'appResourcesPath' values are fetched from
33+
// the nsconfig.json configuration file
34+
// when bundling with `tns run android|ios --bundle`.
35+
appPath = "app",
36+
appResourcesPath = "app/App_Resources",
37+
38+
// You can provide the following flags when running 'tns run android|ios'
39+
snapshot, // --env.snapshot
40+
uglify, // --env.uglify
41+
report, // --env.report
42+
sourceMap, // --env.sourceMap
43+
hmr, // --env.hmr,
44+
} = env;
45+
const externals = (env.externals || []).map((e) => { // --env.externals
46+
return new RegExp(e + ".*");
47+
});
48+
49+
const appFullPath = resolve(projectRoot, appPath);
50+
const appResourcesFullPath = resolve(projectRoot, appResourcesPath);
51+
52+
const entryModule = nsWebpack.getEntryModule(appFullPath);
53+
const entryPath = `.${sep}${entryModule}.js`;
54+
55+
const config = {
56+
mode: uglify ? "production" : "development",
57+
context: appFullPath,
58+
externals,
59+
watchOptions: {
60+
ignored: [
61+
appResourcesFullPath,
62+
// Don't watch hidden files
63+
"**/.*",
64+
]
65+
},
66+
target: nativescriptTarget,
67+
entry: {
68+
bundle: entryPath,
69+
},
70+
output: {
71+
pathinfo: false,
72+
path: dist,
73+
libraryTarget: "commonjs2",
74+
filename: "[name].js",
75+
globalObject: "global",
76+
},
77+
resolve: {
78+
extensions: [".js", ".scss", ".css"],
79+
// Resolve {N} system modules from tns-core-modules
80+
modules: [
81+
"node_modules/tns-core-modules",
82+
"node_modules",
83+
],
84+
alias: {
85+
'~': appFullPath
86+
},
87+
// don't resolve symlinks to symlinked modules
88+
symlinks: false
89+
},
90+
resolveLoader: {
91+
// don't resolve symlinks to symlinked loaders
92+
symlinks: false
93+
},
94+
node: {
95+
// Disable node shims that conflict with NativeScript
96+
"http": false,
97+
"timers": false,
98+
"setImmediate": false,
99+
"fs": "empty",
100+
"__dirname": false,
101+
},
102+
devtool: sourceMap ? "inline-source-map" : "none",
103+
optimization: {
104+
splitChunks: {
105+
cacheGroups: {
106+
vendor: {
107+
name: "vendor",
108+
chunks: "all",
109+
test: (module, chunks) => {
110+
const moduleName = module.nameForCondition ? module.nameForCondition() : '';
111+
return /[\\/]node_modules[\\/]/.test(moduleName) ||
112+
appComponents.some(comp => comp === moduleName);
113+
114+
},
115+
enforce: true,
116+
},
117+
}
118+
},
119+
minimize: !!uglify,
120+
minimizer: [
121+
new UglifyJsPlugin({
122+
parallel: true,
123+
cache: true,
124+
uglifyOptions: {
125+
output: {
126+
comments: false,
127+
},
128+
compress: {
129+
// The Android SBG has problems parsing the output
130+
// when these options are enabled
131+
'collapse_vars': platform !== "android",
132+
sequences: platform !== "android",
133+
}
134+
}
135+
})
136+
],
137+
},
138+
module: {
139+
rules: [
140+
{
141+
test: new RegExp(entryPath),
142+
use: [
143+
// Require all Android app components
144+
platform === "android" && {
145+
loader: "nativescript-dev-webpack/android-app-components-loader",
146+
options: { modules: appComponents }
147+
},
148+
149+
{
150+
loader: "nativescript-dev-webpack/bundle-config-loader",
151+
options: {
152+
loadCss: !snapshot, // load the application css if in debug mode
153+
}
154+
},
155+
].filter(loader => !!loader)
156+
},
157+
158+
{
159+
test: /-page\.js$/,
160+
use: "nativescript-dev-webpack/script-hot-loader"
161+
},
162+
163+
{
164+
test: /\.(css|scss)$/,
165+
use: "nativescript-dev-webpack/style-hot-loader"
166+
},
167+
168+
{
169+
test: /\.(html|xml)$/,
170+
use: "nativescript-dev-webpack/markup-hot-loader"
171+
},
172+
173+
{ test: /\.(html|xml)$/, use: "nativescript-dev-webpack/xml-namespace-loader" },
174+
175+
{
176+
test: /\.css$/,
177+
use: { loader: "css-loader", options: { minimize: false, url: false } }
178+
},
179+
180+
{
181+
test: /\.scss$/,
182+
use: [
183+
{ loader: "css-loader", options: { minimize: false, url: false } },
184+
"sass-loader"
185+
]
186+
},
187+
]
188+
},
189+
plugins: [
190+
// Define useful constants like TNS_WEBPACK
191+
new webpack.DefinePlugin({
192+
"global.TNS_WEBPACK": "true",
193+
"process": undefined,
194+
}),
195+
// Remove all files from the out dir.
196+
new CleanWebpackPlugin([`${dist}/**/*`]),
197+
// Copy native app resources to out dir.
198+
new CopyWebpackPlugin([
199+
{
200+
from: `${appResourcesFullPath}/${appResourcesPlatformDir}`,
201+
to: `${dist}/App_Resources/${appResourcesPlatformDir}`,
202+
context: projectRoot
203+
},
204+
]),
205+
// Copy assets to out dir. Add your own globs as needed.
206+
new CopyWebpackPlugin([
207+
{ from: { glob: "fonts/**" } },
208+
{ from: { glob: "**/*.jpg" } },
209+
{ from: { glob: "**/*.png" } },
210+
], { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] }),
211+
// Generate a bundle starter script and activate it in package.json
212+
new nsWebpack.GenerateBundleStarterPlugin([
213+
"./vendor",
214+
"./bundle",
215+
]),
216+
// For instructions on how to set up workers with webpack
217+
// check out https://github.com/nativescript/worker-loader
218+
new NativeScriptWorkerPlugin(),
219+
new nsWebpack.PlatformFSPlugin({
220+
platform,
221+
platforms,
222+
}),
223+
// Does IPC communication with the {N} CLI to notify events when running in watch mode.
224+
new nsWebpack.WatchStateLoggerPlugin(),
225+
],
226+
};
227+
228+
if (report) {
229+
// Generate report files for bundles content
230+
config.plugins.push(new BundleAnalyzerPlugin({
231+
analyzerMode: "static",
232+
openAnalyzer: false,
233+
generateStatsFile: true,
234+
reportFilename: resolve(projectRoot, "report", `report.html`),
235+
statsFilename: resolve(projectRoot, "report", `stats.json`),
236+
}));
237+
}
238+
239+
if (snapshot) {
240+
config.plugins.push(new nsWebpack.NativeScriptSnapshotPlugin({
241+
chunk: "vendor",
242+
requireModules: [
243+
"tns-core-modules/bundle-entry-points",
244+
],
245+
projectRoot,
246+
webpackConfig: config,
247+
}));
248+
}
249+
250+
if (hmr) {
251+
config.plugins.push(new webpack.HotModuleReplacementPlugin());
252+
}
253+
254+
255+
return config;
256+
};

Diff for: templates/webpack.typescript.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ module.exports = env => {
5050
const appResourcesFullPath = resolve(projectRoot, appResourcesPath);
5151

5252
const entryModule = nsWebpack.getEntryModule(appFullPath);
53-
const entryPath = `.${sep}${entryModule}`;
53+
const entryPath = `.${sep}${entryModule}.ts`;
5454

5555
const config = {
5656
mode: uglify ? "production" : "development",
@@ -140,7 +140,7 @@ module.exports = env => {
140140
module: {
141141
rules: [
142142
{
143-
test: new RegExp(entryPath + "\.[jt]s"),
143+
test: new RegExp(entryPath),
144144
use: [
145145
// Require all Android app components
146146
platform === "android" && {
@@ -158,7 +158,7 @@ module.exports = env => {
158158
},
159159

160160
{
161-
test: /-page\.[jt]s$/,
161+
test: /-page\.ts$/,
162162
use: "nativescript-dev-webpack/script-hot-loader"
163163
},
164164

0 commit comments

Comments
 (0)