提交 e847af40 编写于 作者: fxy060608's avatar fxy060608

wip(app): nvue

上级 5f477c82
......@@ -9,6 +9,5 @@ declare namespace NodeJS {
UNI_CLI_CONTEXT: string
UNI_COMPILER_VERSION: string
UNI_HBUILDERX_PLUGINS: string
UNI_NVUE_COMPILER: 'uni-app' | 'weex'
}
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.configResolved = void 0;
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
const asset_1 = require("../plugins/vitejs/plugins/asset");
const css_1 = require("../plugins/vitejs/plugins/css");
const configResolved = (config) => {
const manifestJson = uni_cli_shared_1.parseManifestJsonOnce(process.env.UNI_INPUT_DIR);
if (uni_cli_shared_1.getNVueCompiler(manifestJson) === 'uni-app') {
process.env.UNI_USING_NVUE_COMPILER = 'uni-app';
}
if (uni_cli_shared_1.getNVueStyleCompiler(manifestJson) === 'uni-app') {
process.env.UNI_USING_NVUE_STYLE_COMPILER = 'uni-app';
}
if (process.env.UNI_APP_CODE_SPLITING) {
initCodeSpliting(config);
}
......
import { Plugin, ResolvedConfig } from 'vite'
import {
parseManifestJsonOnce,
getNVueCompiler,
getNVueStyleCompiler,
} from '@dcloudio/uni-cli-shared'
import { assetPlugin } from '../plugins/vitejs/plugins/asset'
import { cssPlugin, cssPostPlugin } from '../plugins/vitejs/plugins/css'
export const configResolved: Plugin['configResolved'] = (config) => {
const manifestJson = parseManifestJsonOnce(process.env.UNI_INPUT_DIR)
if (getNVueCompiler(manifestJson) === 'uni-app') {
process.env.UNI_USING_NVUE_COMPILER = 'uni-app'
}
if (getNVueStyleCompiler(manifestJson) === 'uni-app') {
process.env.UNI_USING_NVUE_STYLE_COMPILER = 'uni-app'
}
if (process.env.UNI_APP_CODE_SPLITING) {
initCodeSpliting(config as ResolvedConfig)
} else {
......
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.initEnv = void 0;
function initEnv(options) {
if (options.styleCompiler === 'uni-app') {
process.env.UNI_NVUE_STYLE_COMPILER = 'uni-app';
}
}
exports.initEnv = initEnv;
......@@ -5,7 +5,8 @@ const optimization_1 = require("./optimization");
const output_1 = require("./output");
const module_1 = require("./module");
const plugins_1 = require("./plugins");
function createConfig(mode) {
const resolve_1 = require("./resolve");
function createConfig(mode, options) {
return {
mode: mode,
devtool: false,
......@@ -16,9 +17,10 @@ function createConfig(mode) {
externals: {
vue: 'Vue',
},
module: module_1.createModule(options),
optimization: optimization_1.createOptimization(),
output: output_1.createOutput(),
module: module_1.createModule(),
resolve: resolve_1.createResolve(),
plugins: plugins_1.createPlugins(),
};
}
......
......@@ -2,9 +2,9 @@
Object.defineProperty(exports, "__esModule", { value: true });
exports.createModule = void 0;
const rules_1 = require("./rules");
function createModule() {
function createModule(options) {
return {
rules: rules_1.createRules(),
rules: rules_1.createRules(options),
};
}
exports.createModule = createModule;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createBabelLoader = void 0;
const loader_1 = require("../../../loader");
const preprocessLoader = {
loader: loader_1.resolveLoader('preprocess'),
options: {
type: ['js'],
},
};
function createBabelLoader() {
return {
test: /\.js$/,
......@@ -11,6 +18,7 @@ function createBabelLoader() {
babelrc: false,
},
},
preprocessLoader,
],
};
}
......
......@@ -6,24 +6,24 @@ function createCssLoaders() {
return [
{
test: /\.css$/,
oneOf: oneOf_1.createOneOf()
oneOf: oneOf_1.createOneOf(),
},
{
test: /\.scss$/,
oneOf: oneOf_1.createOneOf(scssLoader)
oneOf: oneOf_1.createOneOf(scssLoader),
},
{
test: /\.sass$/,
oneOf: oneOf_1.createOneOf(sassLoader)
oneOf: oneOf_1.createOneOf(sassLoader),
},
{
test: /\.less$/,
oneOf: oneOf_1.createOneOf(lessLoader)
oneOf: oneOf_1.createOneOf(lessLoader),
},
{
test: /\.styl(us)?$/,
oneOf: oneOf_1.createOneOf(stylusLoader)
}
oneOf: oneOf_1.createOneOf(stylusLoader),
},
];
}
exports.createCssLoaders = createCssLoaders;
......@@ -33,9 +33,9 @@ const scssLoader = {
sourceMap: false,
additionalData: '@import "@/uni.scss";',
sassOptions: {
outputStyle: 'expanded'
}
}
outputStyle: 'expanded',
},
},
};
const sassLoader = {
loader: require.resolve('sass-loader'),
......@@ -44,20 +44,20 @@ const sassLoader = {
additionalData: '@import "@/uni.sass"',
sassOptions: {
outputStyle: 'expanded',
indentedSyntax: true
}
}
indentedSyntax: true,
},
},
};
const lessLoader = {
loader: require.resolve('less-loader'),
options: {
sourceMap: false
}
sourceMap: false,
},
};
const stylusLoader = {
loader: require.resolve('stylus-loader'),
options: {
sourceMap: false,
preferPathResolver: 'webpack'
}
preferPathResolver: 'webpack',
},
};
......@@ -7,7 +7,12 @@ exports.createOneOf = void 0;
const path_1 = __importDefault(require("path"));
const loader_1 = require("../../../../loader");
const styleLoader = { loader: loader_1.resolveLoader('style') };
const preprocessLoader = { loader: loader_1.resolveLoader('preprocess') };
const preprocessLoader = {
loader: loader_1.resolveLoader('preprocess'),
options: {
type: ['js'],
},
};
const postcssLoader = {
loader: require.resolve('postcss-loader'),
options: {
......@@ -27,11 +32,11 @@ const postcssLoader = {
return path_1.default.resolve(process.env.UNI_INPUT_DIR, id.substr(1));
}
return id;
}
})
]
}
}
},
}),
],
},
},
};
function createOneOf(preLoader) {
const use = [styleLoader, preprocessLoader];
......@@ -42,11 +47,11 @@ function createOneOf(preLoader) {
return [
{
resourceQuery: /\?vue/,
use
use,
},
{
use
}
use,
},
];
}
exports.createOneOf = createOneOf;
......@@ -4,7 +4,7 @@ exports.createRules = void 0;
const babelLoader_1 = require("./babelLoader");
const cssLoader_1 = require("./cssLoader");
const vueLoader_1 = require("./vueLoader");
function createRules() {
return [vueLoader_1.createVueLoader(), babelLoader_1.createBabelLoader(), ...cssLoader_1.createCssLoaders()];
function createRules(options) {
return [vueLoader_1.createVueLoader(options), babelLoader_1.createBabelLoader(), ...cssLoader_1.createCssLoaders()];
}
exports.createRules = createRules;
......@@ -2,9 +2,9 @@
Object.defineProperty(exports, "__esModule", { value: true });
exports.createCompilerOptions = void 0;
const modules_1 = require("./modules");
function createCompilerOptions() {
function createCompilerOptions(options) {
return {
modules: modules_1.createModules(),
modules: modules_1.createModules(options),
};
}
exports.createCompilerOptions = createCompilerOptions;
......@@ -6,10 +6,10 @@ const boolAttr_1 = require("./boolAttr");
const easycom_1 = require("./easycom");
const renderWhole_1 = require("./renderWhole");
const tags_1 = require("./tags");
function createModules() {
function createModules(options) {
// 先处理 easycom
const modules = [easycom_1.createEasycomModule(), renderWhole_1.createRenderWholeModule()];
if (process.env.UNI_NVUE_COMPILER === 'uni-app') {
if (options.compiler === 'uni-app') {
modules.push(tags_1.createTagsModule());
}
modules.push(assetUrl_1.createAssetUrlModule());
......
......@@ -6,7 +6,14 @@ const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
const compilerOptions_1 = require("./compilerOptions");
const utils_1 = require("../../../../../utils");
const easycom_1 = require("./easycom");
function createVueLoader() {
const loader_1 = require("../../../../loader");
const preprocessLoader = {
loader: loader_1.resolveLoader('preprocess'),
options: {
type: ['js', 'html'],
},
};
function createVueLoader(options) {
uni_cli_shared_1.initEasycomsOnce(process.env.UNI_INPUT_DIR, process.env.UNI_PLATFORM);
return {
test: [/\.nvue(\?[^?]+)?$/, /\.vue(\?[^?]+)?$/],
......@@ -16,9 +23,10 @@ function createVueLoader() {
options: {
hotReload: false,
compiler: createCompiler(),
compilerOptions: compilerOptions_1.createCompilerOptions(),
compilerOptions: compilerOptions_1.createCompilerOptions(options),
},
},
preprocessLoader,
],
};
}
......
......@@ -8,6 +8,7 @@ const terser_webpack_plugin_1 = __importDefault(require("terser-webpack-plugin")
function createOptimization() {
return {
nodeEnv: false,
moduleIds: 'deterministic',
minimizer: [
new terser_webpack_plugin_1.default({
terserOptions: {
......
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createBannerPlugin = void 0;
const webpack_1 = require("webpack");
const BannerPlugin_1 = __importDefault(require("../../plugin/BannerPlugin"));
function createBannerPlugin() {
return new webpack_1.BannerPlugin({
return new BannerPlugin_1.default({
banner: '"use weex:vue";',
raw: true,
exclude: 'Vue',
});
}
exports.createBannerPlugin = createBannerPlugin;
......@@ -3,30 +3,28 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolve = void 0;
exports.createResolve = void 0;
const path_1 = __importDefault(require("path"));
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
const inputDir = process.env.UNI_INPUT_DIR;
exports.resolve = {
extensions: ['.js', '.nvue', '.vue', '.json'],
alias: {
'@': inputDir,
'uni-pages': path_1.default.resolve(inputDir, 'pages.json'),
'@dcloudio/uni-stat': require.resolve('@dcloudio/uni-stat'),
'uni-app-style': uni_cli_shared_1.resolveMainPathOnce(inputDir) +
'?' +
JSON.stringify({
type: 'appStyle',
}),
'uni-stat-config': path_1.default.resolve(inputDir, 'pages.json') +
'?' +
JSON.stringify({
type: 'stat',
}),
},
modules: [
'node_modules',
path_1.default.resolve(process.env.UNI_CLI_CONTEXT, 'node_modules'),
path_1.default.resolve(inputDir, 'node_modules'),
],
};
// import { resolveMainPathOnce } from '@dcloudio/uni-cli-shared'
function createResolve() {
const inputDir = process.env.UNI_INPUT_DIR;
return {
extensions: ['.js', '.nvue', '.vue', '.json'],
alias: {
'@': inputDir,
// '@dcloudio/uni-stat': require.resolve('@dcloudio/uni-stat')
// 'uni-app-style':
// resolveMainPathOnce(inputDir) +
// '?' +
// JSON.stringify({
// type: 'appStyle'
// })
},
modules: [
'node_modules',
path_1.default.resolve(process.env.UNI_CLI_CONTEXT, 'node_modules'),
path_1.default.resolve(inputDir, 'node_modules'),
],
};
}
exports.createResolve = createResolve;
......@@ -8,11 +8,13 @@ const webpack_1 = __importDefault(require("webpack"));
const uni_shared_1 = require("@dcloudio/uni-shared");
const config_1 = require("./config");
const alias_1 = require("./alias");
const env_1 = require("../utils/env");
const initModuleAliasOnce = uni_shared_1.once(alias_1.initModuleAlias);
function runWebpack(mode) {
function runWebpack(mode, options) {
env_1.initEnv(options);
initModuleAliasOnce();
return new Promise((resolve, reject) => {
webpack_1.default(config_1.createConfig(mode), (err, stats) => {
webpack_1.default(config_1.createConfig(mode, options), (err, stats) => {
if (err) {
return reject(err.stack || err);
}
......@@ -31,11 +33,11 @@ function runWebpack(mode) {
});
});
}
function runWebpackBuild() {
return runWebpack('production');
function runWebpackBuild(options) {
return runWebpack('production', options);
}
exports.runWebpackBuild = runWebpackBuild;
function runWebpackDev() {
return runWebpack('development');
function runWebpackDev(options) {
return runWebpack('development', options);
}
exports.runWebpackDev = runWebpackDev;
......@@ -23,13 +23,13 @@ function preprocessLoader(content, map) {
function preContent(type, content) {
switch (type) {
case 'js':
return uni_cli_shared_1.preJs(content);
return uni_cli_shared_1.preNVueJs(content);
case 'html':
return uni_cli_shared_1.preHtml(content);
return uni_cli_shared_1.preNVueHtml(content);
case 'css':
return uni_cli_shared_1.preCss(content);
return uni_cli_shared_1.preNVueCss(content);
case 'json':
return uni_cli_shared_1.preJson(content);
return uni_cli_shared_1.preNVueJson(content);
}
return content;
}
......
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const webpack_1 = require("webpack");
const webpack_sources_1 = require("webpack-sources");
class BannerPlugin {
/**
* @param {BannerPluginArgument} options options object
*/
constructor(options) {
this.banner = () => options.banner;
}
/**
* Apply the plugin
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
const banner = this.banner;
compiler.hooks.compilation.tap('BannerPlugin', (compilation) => {
compilation.hooks.processAssets.tap({
name: 'BannerPlugin',
stage: webpack_1.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_TRANSFER,
}, () => {
for (const chunk of compilation.chunks) {
if (!chunk.canBeInitial()) {
continue;
}
for (const file of chunk.files) {
const data = {
chunk,
filename: file,
};
const comment = compilation.getPath(banner, data);
compilation.updateAsset(file, (old) => new webpack_sources_1.ConcatSource(comment, '\n', old));
}
}
});
});
}
}
exports.default = BannerPlugin;
"use strict";
var __importStar = (this && this.__importStar) || function(mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null)
for (var k in mod)
if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", {
value: true
});
const postcss = __importStar(require("postcss"));
// postcss-selector-parser does have typings but it's problematic to work with.
const selectorParser = require('postcss-selector-parser');
exports.default = postcss.plugin('remove-scoped', (options) => (root) => {
root.each(function rewriteSelector(node) {
if (!node.selector) {
// handle media queries
if (node.type === 'atrule') {
if (node.name === 'media' || node.name === 'supports') {
node.each(rewriteSelector);
}
}
return;
}
node.selector = selectorParser((selectors) => {
selectors.each((selector) => {
let node = null;
// find the last child node to insert attribute selector
selector.each((n) => {
// ">>>" combinator
// and /deep/ alias for >>>, since >>> doesn't work in SASS
if (n.type === 'combinator' &&
(n.value === '>>>' || n.value === '/deep/')) {
n.value = ' ';
n.spaces.before = n.spaces.after = '';
return false;
}
// in newer versions of sass, /deep/ support is also dropped, so add a ::v-deep alias
if (n.type === 'pseudo' && n.value === '::v-deep') {
n.value = n.spaces.before = n.spaces.after = '';
return false;
}
if (n.type !== 'pseudo' && n.type !== 'combinator') {
node = n;
exports.default = () => {
return {
postcssPlugin: 'remove-scoped',
Once (root) {
root.each(function rewriteSelector(node) {
if (!node.selector) {
// handle media queries
if (node.type === 'atrule') {
if (node.name === 'media' || node.name === 'supports') {
node.each(rewriteSelector);
}
}
return;
}
node.selector = selectorParser((selectors) => {
selectors.each((selector) => {
let node = null;
// find the last child node to insert attribute selector
selector.each((n) => {
// ">>>" combinator
// and /deep/ alias for >>>, since >>> doesn't work in SASS
if (n.type === 'combinator' &&
(n.value === '>>>' || n.value === '/deep/')) {
n.value = ' ';
n.spaces.before = n.spaces.after = '';
return false;
}
// in newer versions of sass, /deep/ support is also dropped, so add a ::v-deep alias
if (n.type === 'pseudo' && n.value === '::v-deep') {
n.value = n.spaces.before = n.spaces.after = '';
return false;
}
if (n.type !== 'pseudo' && n.type !== 'combinator') {
node = n;
}
});
if (node) {
node.spaces.after = '';
} else {
// For deep selectors & standalone pseudo selectors,
// the attribute selectors are prepended rather than appended.
// So all leading spaces must be eliminated to avoid problems.
selector.first.spaces.before = '';
}
});
}).processSync(node.selector);
});
if (node) {
node.spaces.after = '';
} else {
// For deep selectors & standalone pseudo selectors,
// the attribute selectors are prepended rather than appended.
// So all leading spaces must be eliminated to avoid problems.
selector.first.spaces.before = '';
}
});
}).processSync(node.selector);
});
});
}
}
}
"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const postcss = __importStar(require("postcss"));
// postcss-selector-parser does have typings but it's problematic to work with.
const selectorParser = require('postcss-selector-parser');
exports.default = postcss.plugin('add-id', (options) => (root) => {
const id = options;
const keyframes = Object.create(null);
root.each(function rewriteSelector(node) {
if (!node.selector) {
// handle media queries
if (node.type === 'atrule') {
if (node.name === 'media' || node.name === 'supports') {
node.each(rewriteSelector);
}
else if (/-?keyframes$/.test(node.name)) {
// register keyframes
keyframes[node.params] = node.params = node.params + '-' + id;
}
}
return;
}
node.selector = selectorParser((selectors) => {
selectors.each((selector) => {
let node = null;
// find the last child node to insert attribute selector
selector.each((n) => {
// ">>>" combinator
// and /deep/ alias for >>>, since >>> doesn't work in SASS
if (n.type === 'combinator' &&
(n.value === '>>>' || n.value === '/deep/')) {
n.value = ' ';
n.spaces.before = n.spaces.after = '';
return false;
}
// in newer versions of sass, /deep/ support is also dropped, so add a ::v-deep alias
if (n.type === 'pseudo' && n.value === '::v-deep') {
n.value = n.spaces.before = n.spaces.after = '';
return false;
}
if (n.type !== 'pseudo' && n.type !== 'combinator') {
node = n;
exports.default = (options) => {
return {
postcssPlugin: 'add-id',
Once (root) {
const id = options;
const keyframes = Object.create(null);
root.each(function rewriteSelector(node) {
if (!node.selector) {
// handle media queries
if (node.type === 'atrule') {
if (node.name === 'media' || node.name === 'supports') {
node.each(rewriteSelector);
}
else if (/-?keyframes$/.test(node.name)) {
// register keyframes
keyframes[node.params] = node.params = node.params + '-' + id;
}
}
});
if (node) {
node.spaces.after = '';
}
else {
// For deep selectors & standalone pseudo selectors,
// the attribute selectors are prepended rather than appended.
// So all leading spaces must be eliminated to avoid problems.
selector.first.spaces.before = '';
}
// fixed by xxxxxx (h5,app-plus v3 平台继续使用 attribute,其他平台使用 className)
if(
process.env.UNI_PLATFORM === 'h5' ||
(
process.env.UNI_PLATFORM === 'app-plus' &&
process.env.UNI_USING_V3
)
){
selector.insertAfter(node, selectorParser.attribute({
attribute: id
}));
} else {
selector.insertAfter(node, selectorParser.className({
value: id
}));
return;
}
node.selector = selectorParser((selectors) => {
selectors.each((selector) => {
let node = null;
// find the last child node to insert attribute selector
selector.each((n) => {
// ">>>" combinator
// and /deep/ alias for >>>, since >>> doesn't work in SASS
if (n.type === 'combinator' &&
(n.value === '>>>' || n.value === '/deep/')) {
n.value = ' ';
n.spaces.before = n.spaces.after = '';
return false;
}
// in newer versions of sass, /deep/ support is also dropped, so add a ::v-deep alias
if (n.type === 'pseudo' && n.value === '::v-deep') {
n.value = n.spaces.before = n.spaces.after = '';
return false;
}
if (n.type !== 'pseudo' && n.type !== 'combinator') {
node = n;
}
});
if (node) {
node.spaces.after = '';
}
else {
// For deep selectors & standalone pseudo selectors,
// the attribute selectors are prepended rather than appended.
// So all leading spaces must be eliminated to avoid problems.
selector.first.spaces.before = '';
}
// fixed by xxxxxx (h5,app-plus v3 平台继续使用 attribute,其他平台使用 className)
if(
process.env.UNI_PLATFORM === 'h5' ||
(
process.env.UNI_PLATFORM === 'app-plus' &&
process.env.UNI_USING_V3
)
){
selector.insertAfter(node, selectorParser.attribute({
attribute: id
}));
} else {
selector.insertAfter(node, selectorParser.className({
value: id
}));
}
});
}).processSync(node.selector);
});
}).processSync(node.selector);
});
// If keyframes are found in this <style>, find and rewrite animation names
// in declarations.
// Caveat: this only works for keyframes and animation rules in the same
// <style> element.
if (Object.keys(keyframes).length) {
root.walkDecls(decl => {
// individual animation-name declaration
if (/^(-\w+-)?animation-name$/.test(decl.prop)) {
decl.value = decl.value
.split(',')
.map(v => keyframes[v.trim()] || v.trim())
.join(',');
}
// shorthand
if (/^(-\w+-)?animation$/.test(decl.prop)) {
decl.value = decl.value
.split(',')
.map(v => {
const vals = v.trim().split(/\s+/);
const i = vals.findIndex(val => keyframes[val]);
if (i !== -1) {
vals.splice(i, 1, keyframes[vals[i]]);
return vals.join(' ');
// If keyframes are found in this <style>, find and rewrite animation names
// in declarations.
// Caveat: this only works for keyframes and animation rules in the same
// <style> element.
if (Object.keys(keyframes).length) {
root.walkDecls(decl => {
// individual animation-name declaration
if (/^(-\w+-)?animation-name$/.test(decl.prop)) {
decl.value = decl.value
.split(',')
.map(v => keyframes[v.trim()] || v.trim())
.join(',');
}
else {
return v;
// shorthand
if (/^(-\w+-)?animation$/.test(decl.prop)) {
decl.value = decl.value
.split(',')
.map(v => {
const vals = v.trim().split(/\s+/);
const i = vals.findIndex(val => keyframes[val]);
if (i !== -1) {
vals.splice(i, 1, keyframes[vals[i]]);
return vals.join(' ');
}
else {
return v;
}
})
.join(',');
}
})
.join(',');
});
}
});
}
}
});
}
"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const postcss = __importStar(require("postcss"));
exports.default = postcss.plugin('trim', () => (css) => {
css.walk(({ type, raws }) => {
if (type === 'rule' || type === 'atrule') {
if (raws.before)
raws.before = '\n';
if (raws.after)
raws.after = '\n';
exports.default = () => {
return {
postcssPlugin: 'trim',
Once (css) {
css.walk(({ type, raws }) => {
if (type === 'rule' || type === 'atrule') {
if (raws.before)
raws.before = '\n';
if (raws.after)
raws.after = '\n';
}
});
}
});
});
}
}
\ No newline at end of file
......@@ -35,7 +35,7 @@ function convertLengthShorthand (rule, prop) {
* @param {*} index
*/
function mergeStyle (object, classNames, preClassNames, ruleResult, prop, index) {
if (!process.env.UNI_USING_NVUE_STYLE_COMPILER) {
if (process.env.UNI_NVUE_STYLE_COMPILER !== 'uni-app') {
object[classNames] = object[classNames] || {}
object[classNames][prop] = ruleResult[prop]
return
......@@ -110,7 +110,7 @@ function parse (code, done) {
/* istanbul ignore else */
if (typeof subResult.value === 'number' || typeof subResult.value === 'string') {
if (process.env.UNI_USING_NVUE_STYLE_COMPILER) {
if (process.env.UNI_NVUE_STYLE_COMPILER === 'uni-app') {
var oldValue = ruleResult[camelCasedName]
// 增加 important 权重信息
ruleResult[camelCasedName] = Array.isArray(oldValue) && oldValue[1] > importantWeight ? oldValue : [subResult.value, importantWeight]
......@@ -128,7 +128,7 @@ function parse (code, done) {
rule.selectors.forEach(function (selector) {
selector = selector.replace(/\s*([\+\~\>])\s*/g, '$1').replace(/\s+/, ' ')
// 支持组合选择器
const res = selector.match(process.env.UNI_USING_NVUE_STYLE_COMPILER ? /^((?:(?:\.[A-Za-z0-9_\-]+)+[\+\~\> ])*)((?:\.[A-Za-z0-9_\-\:]+)+)$/ : /^(\.)([A-Za-z0-9_\-:]+)$/)
const res = selector.match(process.env.UNI_NVUE_STYLE_COMPILER === 'uni-app' ? /^((?:(?:\.[A-Za-z0-9_\-]+)+[\+\~\> ])*)((?:\.[A-Za-z0-9_\-\:]+)+)$/ : /^(\.)([A-Za-z0-9_\-:]+)$/)
if (res) {
var preClassNames = res[1]
var classNames = res[2]
......
......@@ -47,10 +47,12 @@
"vue-hot-reload-api": "^2.3.0",
"vue-style-loader": "^4.1.0",
"vue-template-es2015-compiler": "^1.9.0",
"webpack": "^5.45.1"
"webpack": "^5.45.1",
"webpack-sources": "^3.1.2"
},
"devDependencies": {
"@types/terser-webpack-plugin": "^5.0.4",
"@types/webpack-sources": "^2.1.1",
"vue-template-compiler": "^2.6.14"
}
}
interface NVueCompilerOptions {
compiler: 'uni-app' | 'weex'
styleCompiler: 'uni-app' | 'weex'
}
export function initEnv(options: NVueCompilerOptions) {
if (options.styleCompiler === 'uni-app') {
process.env.UNI_NVUE_STYLE_COMPILER = 'uni-app'
}
}
......@@ -3,8 +3,10 @@ import { createOptimization } from './optimization'
import { createOutput } from './output'
import { createModule } from './module'
import { createPlugins } from './plugins'
import { createResolve } from './resolve'
export function createConfig(
mode: 'production' | 'development'
mode: 'production' | 'development',
options: NVueCompilerOptions
): Configuration {
return {
mode: mode,
......@@ -16,9 +18,10 @@ export function createConfig(
externals: {
vue: 'Vue',
},
module: createModule(options),
optimization: createOptimization(),
output: createOutput(),
module: createModule(),
resolve: createResolve(),
plugins: createPlugins(),
}
}
......@@ -2,8 +2,10 @@ import { Configuration } from 'webpack'
import { createRules } from './rules'
export function createModule(): Configuration['module'] {
export function createModule(
options: NVueCompilerOptions
): Configuration['module'] {
return {
rules: createRules(),
rules: createRules(options),
}
}
import { RuleSetRule } from 'webpack'
import { resolveLoader } from '../../../loader'
const preprocessLoader = {
loader: resolveLoader('preprocess'),
options: {
type: ['js'],
},
}
export function createBabelLoader(): RuleSetRule {
return {
test: /\.js$/,
......@@ -10,6 +16,7 @@ export function createBabelLoader(): RuleSetRule {
babelrc: false,
},
},
preprocessLoader,
],
}
}
......@@ -5,24 +5,24 @@ export function createCssLoaders(): RuleSetRule[] {
return [
{
test: /\.css$/,
oneOf: createOneOf()
oneOf: createOneOf(),
},
{
test: /\.scss$/,
oneOf: createOneOf(scssLoader)
oneOf: createOneOf(scssLoader),
},
{
test: /\.sass$/,
oneOf: createOneOf(sassLoader)
oneOf: createOneOf(sassLoader),
},
{
test: /\.less$/,
oneOf: createOneOf(lessLoader)
oneOf: createOneOf(lessLoader),
},
{
test: /\.styl(us)?$/,
oneOf: createOneOf(stylusLoader)
}
oneOf: createOneOf(stylusLoader),
},
]
}
......@@ -32,9 +32,9 @@ const scssLoader = {
sourceMap: false,
additionalData: '@import "@/uni.scss";',
sassOptions: {
outputStyle: 'expanded'
}
}
outputStyle: 'expanded',
},
},
}
const sassLoader = {
......@@ -44,22 +44,22 @@ const sassLoader = {
additionalData: '@import "@/uni.sass"',
sassOptions: {
outputStyle: 'expanded',
indentedSyntax: true
}
}
indentedSyntax: true,
},
},
}
const lessLoader = {
loader: require.resolve('less-loader'),
options: {
sourceMap: false
}
sourceMap: false,
},
}
const stylusLoader = {
loader: require.resolve('stylus-loader'),
options: {
sourceMap: false,
preferPathResolver: 'webpack'
}
preferPathResolver: 'webpack',
},
}
......@@ -2,7 +2,12 @@ import path from 'path'
import { resolveLoader } from '../../../../loader'
const styleLoader = { loader: resolveLoader('style') }
const preprocessLoader = { loader: resolveLoader('preprocess') }
const preprocessLoader = {
loader: resolveLoader('preprocess'),
options: {
type: ['js'],
},
}
const postcssLoader = {
loader: require.resolve('postcss-loader'),
options: {
......@@ -20,11 +25,11 @@ const postcssLoader = {
return path.resolve(process.env.UNI_INPUT_DIR, id.substr(1))
}
return id
}
})
]
}
}
},
}),
],
},
},
}
export function createOneOf(preLoader?: {
loader: string
......@@ -38,10 +43,10 @@ export function createOneOf(preLoader?: {
return [
{
resourceQuery: /\?vue/,
use
use,
},
{
use
}
use,
},
]
}
......@@ -2,6 +2,6 @@ import { RuleSetRule } from 'webpack'
import { createBabelLoader } from './babelLoader'
import { createCssLoaders } from './cssLoader'
import { createVueLoader } from './vueLoader'
export function createRules(): RuleSetRule[] {
return [createVueLoader(), createBabelLoader(), ...createCssLoaders()]
export function createRules(options: NVueCompilerOptions): RuleSetRule[] {
return [createVueLoader(options), createBabelLoader(), ...createCssLoaders()]
}
import { CompilerOptions } from 'vue-template-compiler'
import { createModules } from './modules'
export function createCompilerOptions(): CompilerOptions {
export function createCompilerOptions(
options: NVueCompilerOptions
): CompilerOptions {
return {
modules: createModules(),
modules: createModules(options),
}
}
......@@ -4,10 +4,10 @@ import { createBoolAttrModule } from './boolAttr'
import { createEasycomModule } from './easycom'
import { createRenderWholeModule } from './renderWhole'
import { createTagsModule } from './tags'
export function createModules(): ModuleOptions[] {
export function createModules(options: NVueCompilerOptions): ModuleOptions[] {
// 先处理 easycom
const modules = [createEasycomModule(), createRenderWholeModule()]
if (process.env.UNI_NVUE_COMPILER === 'uni-app') {
if (options.compiler === 'uni-app') {
modules.push(createTagsModule())
}
......
......@@ -6,8 +6,15 @@ import { createCompilerOptions } from './compilerOptions'
import { resolveLib } from '../../../../../utils'
import { generateEasycomCode } from './easycom'
import { VueLoaderOptions } from '../../../../../../lib/vue-loader'
import { resolveLoader } from '../../../../loader'
const preprocessLoader = {
loader: resolveLoader('preprocess'),
options: {
type: ['js', 'html'],
},
}
export function createVueLoader(): RuleSetRule {
export function createVueLoader(options: NVueCompilerOptions): RuleSetRule {
initEasycomsOnce(process.env.UNI_INPUT_DIR, process.env.UNI_PLATFORM)
return {
test: [/\.nvue(\?[^?]+)?$/, /\.vue(\?[^?]+)?$/],
......@@ -17,9 +24,10 @@ export function createVueLoader(): RuleSetRule {
options: {
hotReload: false,
compiler: createCompiler(),
compilerOptions: createCompilerOptions(),
compilerOptions: createCompilerOptions(options),
} as VueLoaderOptions,
},
preprocessLoader,
],
}
}
......
......@@ -4,6 +4,7 @@ import { Configuration } from 'webpack'
export function createOptimization(): Configuration['optimization'] {
return {
nodeEnv: false, // 禁用,由 define 统一设置
moduleIds: 'deterministic',
minimizer: [
new TerserPlugin({
terserOptions: {
......
import { BannerPlugin } from 'webpack'
import BannerPlugin from '../../plugin/BannerPlugin'
export function createBannerPlugin() {
return new BannerPlugin({
banner: '"use weex:vue";',
raw: true,
exclude: 'Vue',
})
}
import path from 'path'
import { Configuration } from 'webpack'
import { resolveMainPathOnce } from '@dcloudio/uni-cli-shared'
const inputDir = process.env.UNI_INPUT_DIR
export const resolve: Configuration['resolve'] = {
extensions: ['.js', '.nvue', '.vue', '.json'],
alias: {
'@': inputDir,
'uni-pages': path.resolve(inputDir, 'pages.json'),
'@dcloudio/uni-stat': require.resolve('@dcloudio/uni-stat'),
'uni-app-style':
resolveMainPathOnce(inputDir) +
'?' +
JSON.stringify({
type: 'appStyle',
}),
'uni-stat-config':
path.resolve(inputDir, 'pages.json') +
'?' +
JSON.stringify({
type: 'stat',
}),
},
modules: [
'node_modules',
path.resolve(process.env.UNI_CLI_CONTEXT, 'node_modules'),
path.resolve(inputDir, 'node_modules'),
],
// import { resolveMainPathOnce } from '@dcloudio/uni-cli-shared'
export function createResolve(): Configuration['resolve'] {
const inputDir = process.env.UNI_INPUT_DIR
return {
extensions: ['.js', '.nvue', '.vue', '.json'],
alias: {
'@': inputDir,
// '@dcloudio/uni-stat': require.resolve('@dcloudio/uni-stat')
// 'uni-app-style':
// resolveMainPathOnce(inputDir) +
// '?' +
// JSON.stringify({
// type: 'appStyle'
// })
},
modules: [
'node_modules',
path.resolve(process.env.UNI_CLI_CONTEXT, 'node_modules'),
path.resolve(inputDir, 'node_modules'),
],
}
}
......@@ -3,13 +3,18 @@ import { once } from '@dcloudio/uni-shared'
import { createConfig } from './config'
import { initModuleAlias } from './alias'
import { initEnv } from '../utils/env'
const initModuleAliasOnce = once(initModuleAlias)
function runWebpack(mode: 'production' | 'development') {
function runWebpack(
mode: 'production' | 'development',
options: NVueCompilerOptions
) {
initEnv(options)
initModuleAliasOnce()
return new Promise((resolve, reject) => {
webpack(createConfig(mode), (err, stats) => {
webpack(createConfig(mode, options), (err, stats) => {
if (err) {
return reject(err.stack || err)
}
......@@ -32,10 +37,10 @@ function runWebpack(mode: 'production' | 'development') {
})
}
export function runWebpackBuild() {
return runWebpack('production')
export function runWebpackBuild(options: NVueCompilerOptions) {
return runWebpack('production', options)
}
export function runWebpackDev() {
return runWebpack('development')
export function runWebpackDev(options: NVueCompilerOptions) {
return runWebpack('development', options)
}
import { LoaderContext } from 'webpack'
import { preJs, preCss, preHtml, preJson } from '@dcloudio/uni-cli-shared'
import {
preNVueJs,
preNVueCss,
preNVueHtml,
preNVueJson,
} from '@dcloudio/uni-cli-shared'
import { normalizePreprocessErrMsg } from '../../utils/preprocess'
const utils = require('loader-utils')
function preprocessLoader(this: LoaderContext<{}>, content: string, map: any) {
......@@ -25,13 +30,13 @@ function preContent(
) {
switch (type) {
case 'js':
return preJs(content)
return preNVueJs(content)
case 'html':
return preHtml(content)
return preNVueHtml(content)
case 'css':
return preCss(content)
return preNVueCss(content)
case 'json':
return preJson(content)
return preNVueJson(content)
}
return content
}
......
import { Compilation, Compiler } from 'webpack'
import { ConcatSource } from 'webpack-sources'
interface BannerPluginOptions {
banner: string
}
export default class BannerPlugin {
private banner: () => string
/**
* @param {BannerPluginArgument} options options object
*/
constructor(options: BannerPluginOptions) {
this.banner = () => options.banner
}
/**
* Apply the plugin
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler: Compiler) {
const banner = this.banner
compiler.hooks.compilation.tap('BannerPlugin', (compilation) => {
compilation.hooks.processAssets.tap(
{
name: 'BannerPlugin',
stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_TRANSFER,
},
() => {
for (const chunk of compilation.chunks) {
if (!chunk.canBeInitial()) {
continue
}
for (const file of chunk.files) {
const data = {
chunk,
filename: file,
}
const comment = compilation.getPath(banner, data)
compilation.updateAsset(
file,
(old) => new ConcatSource(comment, '\n', old as any) as any
)
}
}
}
)
})
}
}
......@@ -2,35 +2,10 @@ import { extend } from '@vue/shared'
import { BuildOptions, ServerOptions } from 'vite'
import { CliOptions } from '.'
import { build, buildSSR } from './build'
import { runNVue } from './nvue'
import { createServer, createSSRServer } from './server'
import { initEnv } from './utils'
async function runNVue(mode: 'prod' | 'dev') {
let hasCliNVue = false
try {
if (require.resolve('@dcloudio/uni-cli-nvue')) {
hasCliNVue = true
}
} catch (e) {}
if (!hasCliNVue) {
return
}
let nvue
try {
nvue = require('@dcloudio/uni-cli-nvue')
} catch (e) {
console.error(e)
}
if (!nvue) {
return
}
if (mode === 'prod') {
await nvue.runWebpackBuild()
} else {
await nvue.runWebpackDev()
}
}
export async function runDev(options: CliOptions & ServerOptions) {
initEnv('dev', options)
try {
......
import {
parseManifestJsonOnce,
getNVueCompiler,
getNVueStyleCompiler,
} from '@dcloudio/uni-cli-shared'
function initNVueCompilerOptions() {
const manifestJson = parseManifestJsonOnce(process.env.UNI_INPUT_DIR)
const nvueCompilerOptions = {
compiler: 'uni-app',
styleCompiler: 'weex',
}
if (getNVueCompiler(manifestJson) === 'uni-app') {
nvueCompilerOptions.compiler = 'uni-app'
}
if (getNVueStyleCompiler(manifestJson) === 'uni-app') {
nvueCompilerOptions.styleCompiler = 'uni-app'
}
return nvueCompilerOptions
}
export async function runNVue(mode: 'prod' | 'dev') {
let hasCliNVue = false
try {
if (require.resolve('@dcloudio/uni-cli-nvue')) {
hasCliNVue = true
}
} catch (e) {}
if (!hasCliNVue) {
return
}
let nvue
try {
nvue = require('@dcloudio/uni-cli-nvue')
} catch (e) {
console.error(e)
}
if (!nvue) {
return
}
const options = initNVueCompilerOptions()
if (mode === 'prod') {
await nvue.runWebpackBuild(options)
} else {
await nvue.runWebpackDev(options)
}
}
......@@ -2423,9 +2423,9 @@
integrity sha512-iiUgKzV9AuaEkZqkOLDIvlQiL6ltuZd9tGcW3gwpnX8JbuiuhFlEGmmFXEXkN50Cvq7Os88IY2v0dkDqXYWVgA==
"@types/node@*":
version "16.4.7"
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.4.7.tgz#f7afa78769d4b477f5092d7c3468e2e8653d779c"
integrity sha512-aDDY54sst8sx47CWT6QQqIZp45yURq4dic0+HCYfYNcY5Ejlb/CLmFnRLfy3wQuYafOeh3lB/DAKaqRKBtcZmA==
version "16.4.8"
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.4.8.tgz#ef4974f47524448428542365db2fe7b638f928e5"
integrity sha512-VL7RZyCpfYEmbyd3/Eq5RNYhZt7yoL1JThZQ3KzimzhLya2Qa86U1ZZmioNWAAjiz99z1ED1xF9NUV2srvfVrA==
"@types/node@10.17.13":
version "10.17.13"
......@@ -2515,6 +2515,11 @@
"@types/mime" "^1"
"@types/node" "*"
"@types/source-list-map@*":
version "0.1.2"
resolved "https://registry.yarnpkg.com/@types/source-list-map/-/source-list-map-0.1.2.tgz#0078836063ffaf17412349bba364087e0ac02ec9"
integrity sha512-K5K+yml8LTo9bWJI/rECfIPrGgxdpeNbj+d53lwN4QjW1MCwlkhUms+gtdzigTeUyBr09+u8BwOIY3MXvHdcsA==
"@types/stack-utils@^2.0.0":
version "2.0.1"
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.1.tgz#20f18294f797f2209b5f65c8e3b5c8e8261d127c"
......@@ -2535,6 +2540,15 @@
terser "^5.3.8"
webpack "^5.1.0"
"@types/webpack-sources@^2.1.1":
version "2.1.1"
resolved "https://registry.yarnpkg.com/@types/webpack-sources/-/webpack-sources-2.1.1.tgz#6af17e3a3ded71eec2b98008d7c12f498a0a4506"
integrity sha512-MjM1R6iuw8XaVbtkCBz0N349cyqBjJHCbQiOeppe3VBeFvxqs74RKHAVt9LkxTnUWc7YLZOEsUfPUnmK6SBPKQ==
dependencies:
"@types/node" "*"
"@types/source-list-map" "*"
source-map "^0.7.3"
"@types/yargs-parser@*":
version "20.2.1"
resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-20.2.1.tgz#3b9ce2489919d9e4fea439b76916abc34b2df129"
......@@ -4319,9 +4333,9 @@ ee-first@1.1.1:
integrity sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=
electron-to-chromium@^1.3.723:
version "1.3.791"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.791.tgz#e38f325ff22470bdcff34409d58c0baf9c2e3e93"
integrity sha512-Tdx7w1fZpeWOOBluK+kXTAKCXyc79K65RB6Zp0+sPSZZhDjXlrxfGlXrlMGVVQUrKCyEZFQs1UBBLNz5IdbF0g==
version "1.3.792"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.792.tgz#791b0d8fcf7411885d086193fb49aaef0c1594ca"
integrity sha512-RM2O2xrNarM7Cs+XF/OE2qX/aBROyOZqqgP+8FXMXSuWuUqCfUUzg7NytQrzZU3aSqk1Qq6zqnVkJsbfMkIatg==
elliptic@^6.5.3:
version "6.5.4"
......@@ -4531,9 +4545,9 @@ eslint-visitor-keys@^2.0.0:
integrity sha512-0rSmRBzXgDzIsD6mGdJgevzgezI534Cer5L/vyMX0kHzT/jiB43jRhd9YUlMGYLQy2zprNmoT8qasCGtY+QaKw==
eslint@^7.17.0:
version "7.31.0"
resolved "https://registry.yarnpkg.com/eslint/-/eslint-7.31.0.tgz#f972b539424bf2604907a970860732c5d99d3aca"
integrity sha512-vafgJpSh2ia8tnTkNUkwxGmnumgckLh5aAbLa1xRmIn9+owi8qBNGKL+B881kNKNTy7FFqTEkpNkUvmw0n6PkA==
version "7.32.0"
resolved "https://registry.yarnpkg.com/eslint/-/eslint-7.32.0.tgz#c6d328a14be3fb08c8d1d21e12c02fdb7a2a812d"
integrity sha512-VHZ8gX+EDfz+97jGcgyGCyRia/dPOd6Xh9yPv8Bl1+SoaIwD+a/vlrOmGRUyOYu7MwUhc7CxqeaDZU13S4+EpA==
dependencies:
"@babel/code-frame" "7.12.11"
"@eslint/eslintrc" "^0.4.3"
......@@ -9978,7 +9992,7 @@ webidl-conversions@^6.1.0:
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-6.1.0.tgz#9111b4d7ea80acd40f5270d666621afa78b69514"
integrity sha512-qBIvFLGiBpLjfwmYAaHPXsn+ho5xZnGvyGvsarywGNc8VyQJUMHJ8OBKGGrPER0okBeMDaan4mNBlgBROxuI8w==
webpack-sources@^3.1.1:
webpack-sources@^3.1.1, webpack-sources@^3.1.2:
version "3.1.2"
resolved "https://registry.yarnpkg.com/webpack-sources/-/webpack-sources-3.1.2.tgz#614c962ac45dfac7f0f482052db8324a6e0dc274"
integrity sha512-//DeuK5SzM6yFRXNOGK+4tX7QB8PghkL8kFBPyqSlN62oJOUkmby8ptV7+IBGH6BkIuIw5Rjd7OvvwZaoiF4ag==
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册