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

Commit 5212eac

Browse files
authored
Add vue functionality (#653)
* Added vue template Changed refresh message Do not filter css and scss files for hmr * use nativescript-vue as vue alias
1 parent 60a212e commit 5212eac

File tree

6 files changed

+280
-5
lines changed

6 files changed

+280
-5
lines changed

Diff for: hot.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const log = console;
2-
const refresh = 'Please restart the application.';
2+
const refresh = 'Application needs to be restarted in order to apply the changes.';
33
const hotOptions = {
44
ignoreUnaccepted: true,
55
ignoreDeclined: true,

Diff for: plugins/WatchStateLoggerPlugin.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,9 @@ export class WatchStateLoggerPlugin {
9898
* but only the update chunks
9999
*/
100100
static getUpdatedEmittedFiles(emittedFiles) {
101+
// TODO: Once we are able to determine whether this is a vue project do not always include .css and .scss files, but only for vue projects
101102
if(emittedFiles.some(x => x.endsWith('.hot-update.json'))) {
102-
return emittedFiles.filter(x => x.indexOf('.hot-update.') > 0);
103+
return emittedFiles.filter(x => x.indexOf('.hot-update.') > 0 || x.endsWith('css'));
103104
} else {
104105
return emittedFiles;
105106
}
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { reload } = require("./hot-loader-helper");
22

33
module.exports = function (source) {
4-
return `${source};${reload('page')}`;
4+
return `${source};${reload('script')}`;
55
};

Diff for: templates/webpack.javascript.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ module.exports = env => {
153153

154154
{
155155
test: /-page\.js$/,
156-
use: "nativescript-dev-webpack/page-hot-loader"
156+
use: "nativescript-dev-webpack/script-hot-loader"
157157
},
158158

159159
{

Diff for: templates/webpack.typescript.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ module.exports = env => {
155155

156156
{
157157
test: /-page\.ts$/,
158-
use: "nativescript-dev-webpack/page-hot-loader"
158+
use: "nativescript-dev-webpack/script-hot-loader"
159159
},
160160

161161
{

Diff for: templates/webpack.vue.js

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

0 commit comments

Comments
 (0)