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

Commit 8da8ccf

Browse files
vtrifonovsis0k0
authored andcommitted
feat: add Vue bundling support (#676)
* added webpack vue support
1 parent 56a0a10 commit 8da8ccf

File tree

4 files changed

+51
-25
lines changed

4 files changed

+51
-25
lines changed

Diff for: apply-css-loader.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = function(content) {
2+
if (this.request.match(/\/app\.(css|scss|less|sass)$/)) {
3+
return content;
4+
}
5+
content += `
6+
const application = require("tns-core-modules/application");
7+
require("tns-core-modules/ui/styling/style-scope");
8+
9+
exports.forEach(cssExport => {
10+
if (cssExport.length > 1 && cssExport[1]) {
11+
// applying the second item of the export as it contains the css contents
12+
application.addCss(cssExport[1]);
13+
}
14+
});
15+
`;
16+
17+
return content;
18+
}

Diff for: projectFilesManager.js

+13-5
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 { isTypeScript, isAngular } = require("./projectHelpers");
4+
const { isTypeScript, isAngular, isVue } = require("./projectHelpers");
55

66
function addProjectFiles(projectDir) {
77
const projectTemplates = getProjectTemplates(projectDir);
@@ -34,7 +34,7 @@ function compareProjectFiles(projectDir) {
3434
const newTemplate = fs.readFileSync(newTemplatePath).toString();
3535
if (newTemplate !== currentTemplate) {
3636
const message = `The current project contains a ${path.basename(currentTemplatePath)} file located at ${currentTemplatePath} that differs from the one in the new version of the nativescript-dev-webpack plugin located at ${newTemplatePath}. Some of the plugin features may not work as expected until you manually update the ${path.basename(currentTemplatePath)} file or automatically update it using "./node_modules/.bin/update-ns-webpack --configs" command.`;
37-
console.info(`\x1B[33;1m${message}\x1B[0m` );
37+
console.info(`\x1B[33;1m${message}\x1B[0m`);
3838
}
3939
}
4040
});
@@ -61,9 +61,11 @@ function getProjectTemplates(projectDir) {
6161
const TSCONFIG_TNS_NAME = "tsconfig.tns.json";
6262

6363
let templates;
64-
if (isAngular({projectDir})) {
64+
if (isAngular({ projectDir })) {
6565
templates = getAngularTemplates(WEBPACK_CONFIG_NAME, TSCONFIG_TNS_NAME);
66-
} else if (isTypeScript({projectDir})) {
66+
} else if (isVue({ projectDir })) {
67+
templates = getVueTemplates(WEBPACK_CONFIG_NAME);
68+
} else if (isTypeScript({ projectDir })) {
6769
templates = getTypeScriptTemplates(WEBPACK_CONFIG_NAME, TSCONFIG_TNS_NAME);
6870
} else {
6971
templates = getJavaScriptTemplates(WEBPACK_CONFIG_NAME);
@@ -86,6 +88,12 @@ function getTypeScriptTemplates(webpackConfigName, tsconfigName) {
8688
};
8789
}
8890

91+
function getVueTemplates(webpackConfigName) {
92+
return {
93+
"webpack.vue.js": webpackConfigName
94+
};
95+
}
96+
8997
function getJavaScriptTemplates(webpackConfigName) {
9098
return {
9199
"webpack.javascript.js": webpackConfigName,
@@ -114,4 +122,4 @@ module.exports = {
114122
removeProjectFiles,
115123
forceUpdateProjectFiles,
116124
compareProjectFiles,
117-
};
125+
};

Diff for: projectHelpers.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ const isTypeScript = ({ projectDir, packageJson } = {}) => {
1515
packageJson.dependencies &&
1616
packageJson.dependencies.hasOwnProperty("typescript")
1717
) || (
18-
packageJson.devDependencies &&
19-
packageJson.devDependencies.hasOwnProperty("typescript")
20-
) || isAngular({ packageJson });
18+
packageJson.devDependencies &&
19+
packageJson.devDependencies.hasOwnProperty("typescript")
20+
) || isAngular({ packageJson });
2121
};
2222

2323
const isAngular = ({ projectDir, packageJson } = {}) => {
@@ -27,6 +27,13 @@ const isAngular = ({ projectDir, packageJson } = {}) => {
2727
.some(dependency => /^@angular\b/.test(dependency));
2828
};
2929

30+
const isVue = ({ projectDir, packageJson } = {}) => {
31+
packageJson = packageJson || getPackageJson(projectDir);
32+
33+
return packageJson.dependencies && Object.keys(packageJson.dependencies)
34+
.some(dependency => dependency === "nativescript-vue");
35+
};
36+
3037
const getPackageJson = projectDir => {
3138
const packageJsonPath = getPackageJsonPath(projectDir);
3239
return JSON.parse(readFileSync(packageJsonPath, "utf8"));
@@ -84,7 +91,8 @@ module.exports = {
8491
isAndroid,
8592
isIos,
8693
isAngular,
94+
isVue,
8795
isTypeScript,
8896
writePackageJson,
8997
convertSlashesInPath
90-
};
98+
};

Diff for: templates/webpack.vue.js

+8-16
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ const { relative, resolve, sep } = require("path");
33
const webpack = require("webpack");
44
const CleanWebpackPlugin = require("clean-webpack-plugin");
55
const CopyWebpackPlugin = require("copy-webpack-plugin");
6-
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
76
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
87
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
98

@@ -47,6 +46,10 @@ module.exports = env => {
4746
hmr, // --env.hmr
4847
} = env;
4948

49+
const externals = (env.externals || []).map((e) => { // --env.externals
50+
return new RegExp(e + ".*");
51+
});
52+
5053
const mode = production ? "production" : "development"
5154

5255
const appFullPath = resolve(projectRoot, appPath);
@@ -59,6 +62,7 @@ module.exports = env => {
5962
const config = {
6063
mode: mode,
6164
context: appFullPath,
65+
externals,
6266
watchOptions: {
6367
ignored: [
6468
appResourcesFullPath,
@@ -162,28 +166,19 @@ module.exports = env => {
162166
},
163167
].filter(loader => Boolean(loader)),
164168
},
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-
172169
{
173170
test: /\.css$/,
174171
use: [
175172
'nativescript-dev-webpack/style-hot-loader',
176-
'css-hot-loader',
177-
MiniCssExtractPlugin.loader,
173+
'nativescript-dev-webpack/apply-css-loader.js',
178174
{ loader: "css-loader", options: { minimize: false, url: false } },
179175
],
180176
},
181177
{
182178
test: /\.scss$/,
183179
use: [
184180
'nativescript-dev-webpack/style-hot-loader',
185-
'css-hot-loader',
186-
MiniCssExtractPlugin.loader,
181+
'nativescript-dev-webpack/apply-css-loader.js',
187182
{ loader: "css-loader", options: { minimize: false, url: false } },
188183
"sass-loader",
189184
],
@@ -203,9 +198,6 @@ module.exports = env => {
203198
},
204199
plugins: [
205200
// ... Vue Loader plugin omitted
206-
new MiniCssExtractPlugin({
207-
filename: `app.${platform}.css`,
208-
}),
209201
// make sure to include the plugin!
210202
new VueLoaderPlugin(),
211203
// Define useful constants like TNS_WEBPACK
@@ -271,4 +263,4 @@ module.exports = env => {
271263
}
272264

273265
return config;
274-
};
266+
};

0 commit comments

Comments
 (0)